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.
This is the file says that im trying to read from:
Code:
type:1
{
anim:1
{
subanim:1
{
x:63
y:8
w:30
h:40
}
subanim:2
{
x:95
y:11
w:33
h:37
}
delay:500
}
}
And this is the code (a function) to read the file
Code:
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] == ':')
{
if (entityInfo.at(entityNum).type == animString[i + 5] - '0')
{
type = entityInfo.at(entityNum).type;
stage = 1;
}
}
}
else if (stage == 1)
{
if (animString[i] == '{')
{
stage = 2;
}
}
else if (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';
}
}
}
}
many thanks, Super Stinger