I have a file like this
EntityName Jaws
{
Animation WALK
{
NumberOfFrames 9
DirectionOfSprite L
DirectionGenerate LR
FPS 9
}
Animation IDLE
{
NumberOfFrames 6
DirectionOfSprite L
DirectionGenerate LR
FPS 9
}
.......
.......
}
How do I parse this file in this struct
struct AnimationData
{
string animationName;
int noOfFrames;
eAnimationDataDirection direction;
int FPS;
};
struct EntityAnimationData
{
string entityName;
vector<AnimationData> animationData;
string getSpriteResourceName(AnimationData animationData, int frameNumber);
};
I want to store this data into the struct.
How do I go about to get a clean solution?
I’ve read the basic reading of a file.
This is what I’ve tried
EntityAnimationData parseAnimationData(const char* filename)
{
EntityAnimationData data;
ifstream file;
file.open(filename);
char output[128];
string op;
if (file.is_open())
{
while (!file.eof())
{
file >> output;
if(strcmp(parameter,AD_ENTITY_NAME.c_str())==0)
{
file >> output;
data.entityName = output;
cout<<data.entityName<<endl;
do
{
file >> output;
cout<<output<<endl;
}while(strcmp(output,"}")!=0);
}
}
}
file.close();
return data;
}
If this is for a game you’re making, I would consider changing the input file format details. If you use an existing file format, you could use a library to dramatically simplify parsing.
For example, this same data could easily be stored as XML, which would allow you to use an XML parser (such as TinyXML-2) to read the data, and probably make creation of the file simpler, as well. It also would make it simpler to add more robust handling of formatting issues, such as extra whitespace, etc.