Thread: introduction/Approach to parsing/Game development

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Question introduction/Approach to parsing/Game development

    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
    Last edited by Super_Stinger; 02-23-2012 at 02:16 AM. Reason: Code stuffed up

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > animString[i] = c;
    Perhaps put all the chars in the same place?

    Or perhaps spread through the array
    animString[i++] = c;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Edit:

    Rather than inventing your own format and parser, consider using something like http://en.wikipedia.org/wiki/XML

    You don't have to spend so much effort inventing the file format, or getting all picky parsing individual characters. There are many libraries available to do the grunt work, and give you the semantic content of the file right away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Thanks!

    Thanks for that, ill look it up

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I note that your current file format very loosely resembles JSON. You could very well just turn it into JSON then use a JSON library like JSON Spirit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incremental development approach
    By Jasper in forum Tech Board
    Replies: 2
    Last Post: 08-27-2009, 11:37 PM
  2. Game Development
    By Judas in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-16-2007, 10:41 AM
  3. software development approach...
    By dkt in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-16-2001, 09:15 PM
  4. OOT vs Structured Approach in open source software development
    By dkt in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-12-2001, 09:30 PM
  5. development of my game
    By agerealm in forum Game Programming
    Replies: 4
    Last Post: 10-05-2001, 12:40 PM

Tags for this Thread