Thread: Help with Allegro

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    Help with Allegro

    I'm using this Allegro tutorial, but it's becoming over simplified as it touches on more advanced topics. At the point where I am, the tutorial is trying to explain how to do animations using buffering or page flipping or whatever, and is trying to combine it with the timer stuff they didn't really explain well in the last lesson. I have lots of questions on the code that the tutorial produces, so here is the code:

    Code:
    #include <allegro.h>
    
    //long speed_counter = 0;
    
    //void increment_speed_counter()
    //{
        //speed_counter++;
    //} END_OF_FUNCTION(increment_speed_counter);
    
    int main(int argc, char* argv[])
    {
        allegro_init();
        install_keyboard();
        //install_timer();
        
        //LOCK_VARIABLE(speed_counter);
        //LOCK_FUNCTION(increment_speed_counter);
        //install_int_ex(increment_speed_counter, BPS_TO_TIMER(350));
        
        set_color_depth(16);
        set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
        
        BITMAP* frame_one = load_bitmap("C:\\Documents and Settings\\Owner\\My Documents\\John's Pictures\\Su-27A.bmp", NULL);
        BITMAP* frame_two = load_bitmap("C:\\Documents and Settings\\Owner\\My Documents\\John's Pictures\\Su-27B.bmp", NULL);
        BITMAP* frame_three = load_bitmap("C:\\Documents and Settings\\Owner\\My Documents\\John's Pictures\\Su-27C.bmp", NULL);
        
        BITMAP* buffer;
        buffer = create_bitmap(640, 480);
        
        int my_pic_x = 0;
        int my_pic_y = 0;
        int frame_counter = 0;
        
        while(!key[KEY_ESC])
        {
            //while(speed_counter > 0)
            //{
                if(key[KEY_RIGHT])
                {
                    my_pic_x++;
                }
                else if(key[KEY_LEFT])
                {
                    my_pic_x--;
                }
                else if(key[KEY_UP])
                {
                    my_pic_y--;
                }
                else if(key[KEY_DOWN])
                {
                    my_pic_y++;
                }
                //speed_counter--;
                frame_counter++;
                
                if(frame_counter > 240)
                {
                    frame_counter = 0;
                }
            //}
            
            if(frame_counter < 60)
            {
                draw_sprite(buffer, frame_one, my_pic_x, my_pic_y);
            }
            else if(frame_counter >= 60 && frame_counter < 120)
            {
                draw_sprite(buffer, frame_two, my_pic_x, my_pic_y);
            }
            else
            {
                draw_sprite(buffer, frame_three, my_pic_x, my_pic_y);
            }
            
            blit(buffer, screen, 0, 0, 0, 0, 640, 480);
            clear_bitmap(buffer);
            release_screen();
        }
        
        destroy_bitmap(frame_one);
        destroy_bitmap(frame_two);
        destroy_bitmap(frame_three);
        destroy_bitmap(buffer);
        
        return 0;
    } END_OF_MAIN()
    Okay, the first, and most important question involves the stuff highlighted in blue (and commented out). The program seems to run the same with or without this stuff dealing with timers. Due to the lack of detail in the previous tutorial which covers this stuff, I'm kind of lost as to what exactly the purpose of the commented out code is. However, from what I understand, the variable speed_counter is being incremented the entire program. If this is true, how is it possible that it can ever be decremented and therefore be lower than 0 for the test condition of that commented out while loop? If someone could elaborate about what good this code does for the program and what its exact function is, that would be great. All of that is dealing with the commented out code.

    Next question. The part of the blue highlighted code that is also bolded has me confused as well. I don't understand how the name of a function can be passed to a function. However, I'm thinking that this is a function pointer because I read somewhere that the name of a function represents its address. Is this correct? Also, I removed that piece of code (END_OF_FUNCTION(); ) and it had no effect on the program (of course I did this when the rest was uncommented). Why is this? This happens 2 other times in the code in two other commented out sections.

    Another thing that is bugging me is what would type BITMAP signify if it wasn't declared as a pointer? The tutorial said that type BITMAP is a pointer so why does it require the * in the declaration?

    Note the red highlighted 'things'. I understand that these represent key presses, but what are they? Arrays? Structures? Their syntax has got me stumped.

    The rest of the program I pretty much understand if you leave the commented parts out. Muchas gracias to anyone who can help. I really appreciate it.
    Last edited by homeyg; 03-31-2005 at 09:15 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Here we go

    1. The point of the timer system is for your images and movement to be the same on all computers. The timer system is threaded so while you're decrementing the timer the thread for the timer is incrementing and running.

    2. The end of function and lock function things are really for dos in that sense, what they do is lock the memory for your functions. This is basically what these macros do for you.

    3. You use a pointer to a bitmap because the load_bitmap function returns a pointer to a bitmap.

    4. They are char arrays and the name in the middle is just an enum.
    Last edited by prog-bman; 03-31-2005 at 11:04 PM.
    Woop?

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by prog-bman
    Here we go

    1. The point of the timer system is for your images and movement to be the same on all computers. The timer system is threaded so while you're decrementing the timer the thread for the timer is incrementing and running.
    Alright, I understand that, but how can the thing be incremented and decremented at the same time (so that it can be lower than zero)? How I'm understanding it right now, that test condition could never be reached. If it was both incremented and decremented at the same time wouldn't it cancel out and stay at one number?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I am not exactly sure how the allgero function handles it but it does work. The value you pass as the second argument in install_int_ex determines how often the timer gets called.
    Woop?

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by prog-bman
    I am not exactly sure how the allgero function handles it but it does work. The value you pass as the second argument in install_int_ex determines how often the timer gets called.
    Alright, thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allegro in C for a newb
    By Ideius in forum C Programming
    Replies: 5
    Last Post: 12-29-2005, 04:36 PM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  3. double buffering for allegro
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2002, 02:45 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. Allegro programming in a window
    By Person Man in forum Windows Programming
    Replies: 0
    Last Post: 11-16-2001, 03:23 PM