Using a timer in allegro

This is a discussion on Using a timer in allegro within the Game Programming forums, part of the General Programming Boards category; I have started trying to learn Allegro, so i'm quite new to it, and it is really my first experience ...

  1. #1
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35

    Question Using a timer in allegro

    I have started trying to learn Allegro, so i'm quite new to it, and it is really my first experience with an API.

    What i want to do is similar to this (pseudo-code) :
    Code:
    while (!exit)
    {
       if (NextUpdate < CurrentTime)
       {
          UpdateScreen();
          UpdatePlayer();
          UpdateEnemies();
          GetInput();
          Blah();
    
          NextUpdate = CurrentTime + 33.333; //milliseconds
       }
    }
    I've read the allegro docs, and the stuff about using the timer, but i still have no clue as to how i would do this.

    Help/code examples would be great, thanks

  2. #2
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Well, I'm assuming you only want the code for using a timer so here goes...

    This program is going to show you how to draw to the screen and when to draw to the screen next by using a timer.

    Code:
    #include <allegro.h>
    
    void setup();
    void redraw();
    void update_counter();
    
    int counter = 0;
    
    int x, y;
    
    BITMAP *bmp;
    BITMAP *buffer;
    
    int main()
     {
     setup();
    
     redraw();
    
     counter = 0; //setting the counter to 0 because it has been running
     while(!keypressed())   /*this loop is SUPPOSED TO run until a key is pressed
                              however there is constant updating which is preventing
                              this from happening so the program won't terminate
                              until you a special windows key command, such as 
                              ctrl+alt+del or alt+tab*/
     { 
      if(counter == 180)  //this is going to happen every 3 seconds
      {
      redraw();
      counter = 0;
      }
     }
     
    // allegro_exit();
     return 0;
    }
    END_OF_MAIN();
    
    void setup()
     {
      allegro_init();
      install_timer();  //this must be called before any other timer functions are called
      install_int_ex( update_counter, BPS_TO_TIMER(60)); //this is the "actual" timer function
    
     set_color_depth(32);
     set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 800, 600, 0, 0);
    
     bmp = load_bitmap("name_of_bmp.bmp", NULL);
     buffer = create_bitmap(SCREEN_W, SCREEN_H);
    
     }
    
    void update_counter()
     {
      counter++;
     }
    
    void redraw()
     {
    
     clear_bitmap(buffer);  //clearing the buffer to not leave any trails
     blit(bmp, buffer, 0, 0, x, y, SCREEN_W, SCREEN_H);  //blitting the image to the buffer
     blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);  //blitting the buffer to the screen
    
     if(x < SCREEN_W - 100)
      {
      x += 10;
      }
     if(y < SCREEN_H - 100)
      {
      y += 5;
      }
    
     }
    The program is going to draw your bitmap to the screen then every 3 seconds it is going to redraw the image to the screen and after every redraw it is going to move the image over 10 pixels to the right and 5 pixels down until each of them reach their limit

    Hope that helps you out and now I have another tutorial to add to my site.

    EDIT: had a few things wrong w/ the program so I fixed them
    Last edited by TechWins; 09-12-2002 at 04:56 PM.

  3. #3
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    oh and this link will probably be able to help you out.

  4. #4
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    declare counter as a volatile int, not just a regular int, and use LOCK_FUNCTION() and LOCK_VARIABLE() on the counter and the counter function

    Example:
    Code:
    // int counter = 0;  Replace this line with the next one
    volatile int counter = 0;
    and after you call install_timer(); put this in
    Code:
    LOCK_VARIABLE(counter);
    LOCK_FUNCTION(update_counter);
    This is just personal preference, but I like to use install_int() instead of install_int_ex() and pass 1000 as the second argument. But, like I said, just personal preference. You can use it any way you like.

  5. #5
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    I can't remember why right now, but I know that I don't like to include the LOCK_FUNCTION and LOCK_VARIABLE for a certain reason.

    n/m I remember now

    If you do LOCK_VARIABLE then you can't ever redeclare the value of int. So if you were to LOCK_VARIABLE(counter) then my example program wouldn't work. So in some instances you don't want to LOCK_VARIABLE.

    Regardless, what exactly is the benefit of LOCK_VARIABLE, and the same goes with LOCK_FUNCTION?

  6. #6
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    it's for cross platfrom stuff. On some sytems it could crash the computer if you don't lock em' and declare the counter volatile.

  7. #7
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    oh, and you can change the value of int, because I do with my FPS counters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allegro timer
    By ichijoji in forum Game Programming
    Replies: 5
    Last Post: 12-07-2004, 06:11 PM
  2. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  3. allegro timer in a class
    By MadHatter in forum Game Programming
    Replies: 7
    Last Post: 12-06-2002, 09:06 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21