Jump to content
  • Advertisement
Sign in to follow this  
yurian

Using C++ and Allegro, animation not working

This topic is 3538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using C++ and Allegro, first test now is not working. Program is supposed to play an animation after the title (gamestatus = STARTANIM;) Not working, but I cannot find out the reason. Please help me :)
#include <allegro.h>
#include <cstdlib>
#include <time.h>

//Prototype///////////////////////////////////////
void resource_error();
//Timer///////////////////////////////////////////
volatile long int logicplus = 1;
void logictime()
{
    logicplus ++;
}
END_OF_FUNCTION(logictime);

volatile long int animplus = 1;
void animtime()
{
    animplus ++;
}
END_OF_FUNCTION(animtime);

//Main Function////////////////////////////////////////////////////////////////////
int main(int argc, char*argv[])
{
    //Big Variables///////////////////////////////
    bool exitgame = false; // Main game loop runs as long as this variable is false

    #define TITLE 0
    #define STARTANIM 5
    #define STARTMENU 10
    #define OPTIONS 14
    #define INSTRUCTIONS 15
    #define PLAYGAME 20
    #define CUTSCENE 25
    #define RESULTLEVEL 30
    #define RESULTGAME 35
    #define CREDITS 40
    int gamestatus = TITLE;

    //Minor Variables/////////////////////////////
    int animloopcount = 0;
    //Initialize//////////////////////////////////
    allegro_init();
    install_keyboard();
    install_timer();

    install_int_ex(logictime,MSEC_TO_TIMER(200));
    LOCK_VARIABLE(logicplus);
    LOCK_FUNCTION(logictime);
    install_int_ex(animtime,MSEC_TO_TIMER(200));
    LOCK_VARIABLE(animplus);
    LOCK_FUNCTION(animtime);

    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);

    //Resources///////////////////////////////////
    BITMAP*buffer = create_bitmap(640,480);
    if(buffer == NULL)
    {
        resource_error();
    }

    #define pressanyb 0
    #define pressanyw 1
    #define startan1 2
    #define startan2 3
    #define startan3 4
    #define titlebit 5

    DATAFILE *data = NULL;
    data = load_datafile("data.dat");
    if(data == NULL)
    {
        resource_error();
    }

    //Game Loop////////////////////////////////////

    while(!exitgame)
    {
        //Senses
        draw_sprite(screen,buffer,0,0);

        if(gamestatus == TITLE)
        {
            draw_sprite(buffer,(BITMAP*)data[titlebit].dat,0,0);
            if(animplus <= 3){draw_sprite(buffer,(BITMAP*)data[pressanyb].dat,320 - 150,450 - 50);}
            if(animplus >= 4){draw_sprite(buffer,(BITMAP*)data[pressanyw].dat,320 - 150,450 - 50);}
            if(animplus >= 7){animplus = 0;}
        }
        if(gamestatus == STARTANIM)
        {
            if(animloopcount < 20)
            {
                if(animplus == 1){draw_sprite(buffer,(BITMAP*)data[startan1].dat,0,0);}
                if(animplus == 2){draw_sprite(buffer,(BITMAP*)data[startan2].dat,0,0);}
                if(animplus == 3){draw_sprite(buffer,(BITMAP*)data[startan3].dat,0,0);}
                if(animplus >= 3){animplus = 1; draw_sprite(buffer,(BITMAP*)data[startan1].dat,0,0);}
                animloopcount ++;
            }
        }
        if(gamestatus == STARTMENU)
        {

        }

        //Logic
        if(logicplus >= 1)
        {
            if(key[KEY_ESC])
            {
                rest(250);
                if(gamestatus == TITLE){exitgame = true;}
                if(gamestatus == STARTANIM){gamestatus = STARTMENU;}
                if(gamestatus == STARTMENU){exitgame = true;}
            }
            if(gamestatus == TITLE)
            {
                if(key[KEY_SPACE] || key[KEY_ENTER])
                {
                    rest(250);
                    gamestatus = STARTANIM;
                    animloopcount = 0;
                    animplus = 0;
                }
            }
            if(gamestatus == STARTANIM)
            {
                if(key[KEY_SPACE] || key[KEY_ENTER])
                {
                    rest(250);
                    animloopcount = 30;
                    if(animloopcount > 20){gamestatus = STARTMENU;}
                }
            }
            if(gamestatus == STARTMENU)
            {
                exitgame = true;
            }

            logicplus = 0;
        }
    }

    //Cleanup//////////////////////////////////////

    clear_keybuf();

    destroy_bitmap(buffer);
    unload_datafile(data);

    return 0;
}
END_OF_MAIN();

//Lesser Functions//////////////////////////////////////////////////////////////
void resource_error()
{
    set_gfx_mode(GFX_TEXT,0,0,0,0);
    allegro_message("Failed loading resources, check for missing files");
    exit(EXIT_FAILURE);
}



What does happen- Press ESC, Space, or Enter button to continue in the title works perfectly, but freezes onto the title picture after pressing. Gamestatus variable is on STARTANIM for sure because I can press ENTER to close as expected. But then I do not know why the game does not show bitmaps then.

Share this post


Link to post
Share on other sites
Advertisement
You should try stepping through the code one line at a time so that you can see if the lines that draw the images are actually being run. If they are then you know it is a problem with how they are being drawn or the variables themselves. If the code if not being run then you have to look at the the animloopcount is.
So, if you haven't already, just step through the code and keep an eye on the variables.

Good Luck,
Eagle11

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!