Hello everyone,
I have a piece of code that I need some help with. I'm actually suspecting this is a Lite-C bug, but I'm posting here first to be sure.
The code crashes at a certain point. If I debug the code, it seems the code magically jumps to a totaly unrelated function. No function call, nothing. It just jumps to the middle of some other function.
When I remove the do..while loop in this function, the code works fine.
Try for yourself, please. Just copy this in a new file and run. The debugger will be activated right away. Then it will do 20 str_cpy's in a loop, and suddenly jump to the other function.
The code may be a bit large, but you don't have to read or understand it. Just confirm that it does not follow the normal path of execution.
Code:
#include <acknex.h>
#define NUM_LINES_IN_CONSOLE 20
FONT* arialFont = "Arial#16";
int numLinesUsed = 0;
TEXT* console =
{
layer = 1;
pos_x = 10;
pos_y = 10;
font = arialFont;
strings = 20;//NUM_LINES_IN_CONSOLE; // Syntax error: Number syntax...??
}
/*
- Name: initConsole
- Desc: initializes the console. Right now, it just makes it visible.
- Params: -
*/
void initConsole()
{
console->flags |= VISIBLE;
if(screen_color.red == 0) screen_color.red = 1;
}
/*
- Name: consoleScrollMessage
- Desc: prints a new message in the console. If the console is full, older messages will
be scrolled up.
- Params: STRING* message - the message to be printed
*/
void consoleScrollMessage(STRING* message)
{
// Check if the console is full:
if(numLinesUsed >= NUM_LINES_IN_CONSOLE)
{
// It is full, move older messages up 1 string:
int i = 0;
for(i = 0; i + 1 < NUM_LINES_IN_CONSOLE; i++)
{
// Copy next string to current string:
str_cpy((console.pstring)[i], (console.pstring)[i + 1]);
}
}
else
{
// The console wasn't full so increment numLinesUsed:
numLinesUsed++;
}
// Finally, print the message (prepended with "> "):
str_cpy((console.pstring)[numLinesUsed - 1], "> ");
str_cat((console.pstring)[numLinesUsed - 1], message);
}
void consoleInkey(STRING* str)
{
consoleScrollMessage("");
str_cpy((console.pstring)[numLinesUsed - 1], " ");
// COMMENT THIS DO WHILE LOOP OUT AND IT WILL WORK:
do {
inkey((console.pstring)[numLinesUsed - 1]);
}
while(result != 13);
str_cpy(str, (console.pstring)[numLinesUsed - 1]);
}
#define CONSOLE_INKEY(str) consoleInkey(str); wait_for(consoleInkey)
/*
- Name: consoleClear
- Desc: clears all lines in the console.
- Params: -
*/
void consoleClear()
{
int i;
for(i = 0; i < NUM_LINES_IN_CONSOLE; i++)
{
str_cpy((console.pstring)[i], "");
}
}
void main()
{
// Print numbers 0 - 20 in the console:
initConsole();
wait(1);
var i = 0;
STRING* strtmp = str_create(" ");
for(i = 0; i < 20; i++)
{
consoleScrollMessage(str_for_num(strtmp, i));
}
// Debugging starts here:
i++;//!
consoleScrollMessage(str_for_num(strtmp, i));
}