Hey, Just joined the forum! hope to find much knowledge!
Anyway, Im writing a c++ game using the SDL graphics library.
Ive got gravity, a character, a map loaded (from a textfile), etc. But I need to animate the character and i think its inefficient to put all the info of the x's, y's, w's and h's of all the characters movements in the code itself. I decided to put it all in a text file and 'parse' it, and this is where im having trouble, just cant get it to read the characters properly.
Am i taking the right approach, and if i am, how would i 'parse' the text file, ive tried many times myself.
void animateEntity(int animation, int entityNum , vector<liveEntity> entityInfo)
{
//Temp variable for storage
int type = 0;
//Different Stages
int stage = 0;
//used for EOF
int c;
//For loops and other things
int i = 0;
//load some vars
entityInfo.at(entityNum).animState = animation;
entityInfo.at(entityNum).subAnimState++;
//load the file
FILE *animFile;
animFile = fopen("animations.txt", "r");
while((c = fgetc(animFile)) != EOF)
{
i++;
}
fclose(animFile);
//create string/char based on i
char animString[i];
i = 0; //Reset i for reuse
//put the chars into the array
animFile = fopen("animations.txt", "r");
while((c = fgetc(animFile)) != EOF)
{
animString[i] = c;
}
fclose(animFile);
for (i = 0; i < sizeof(animString); i++)
{
if (stage == 0)
{
if (animString[i] == 't' && animString[i + 1] == 'y' && animString[i + 2] == 'p' && animString[i + 3] == 'e' && animString[i + 4] == ':') //Trying to read the word 'type:'
{
if (entityInfo.at(entityNum).type == animString[i + 5] - '0')
{
type = entityInfo.at(entityNum).type;
stage = 1;
}
}
}
elseif (stage == 1)
{
if (animString[i] == '{')
{
stage = 2;
}
}
elseif (stage == 2)
{
if (animString[i] == 'a' && animString[i + 1] == 'n' && animString[i + 2] == 'i' && animString[i + 3] == 'm' && animString[i + 4] == ':')
{
entityInfo.at(entityNum).subAnimState = animString[i + 5] - '0';
}
}
}
}
this is my own attempt at reading code by creating this function(did not work :( ),
and for mik,
what i mean by putting directly in code, is create a large array (however many dimensions) and store all the x's, y's, widths and heights of all my character movements on my character sheet (bitmap) in that array, because everytime the bitmap would change, i would have to recompile,etc. Instead I thought i could read from a text file.
EDIT: sorry, this is the file im trying to read. This just represents a differents spirtes in a bitmap spreadsheet.