Pixelate:Issue 11/Timers Using Allegro

From Allegro Wiki

Jump to: navigation, search
Timers Using Allegro
Original author: Bobby Ferris
some mail
Website: http://rafsoft.cjb.net
zip: Countdown.cpp
Image:rsoftpresents.gif
The Many different types of timers in games, and their uses

Hello, everyone! I am Bobby Ferris, and this is my first Pixelate Article. (well, the first article of my choice of topic, I did the SH2003 reviews too) Today, I am going to describe all of the different types of timers that you use with Allegro games, and their uses. I will also show you how to implement them. The code I will show you will be in C++, but is fairly easy to port to C. Ok, without further ado, I will start explaining! The code fragements aren't guarenteed to work, but should. I take no responsibility for the code and it's effects, either =)

Ready to learn about timers?

Ok, everyone, the first and maybe most important use is to make sure that your game runs at the same speed on all computers. This is important, because if you design your game on a 366MHz computer, like I do, you don't want it running at light-speed on a 1 or 2 GHz computer! And you don't want it to run real slow on a 133 MHz computer. So, to do this, you would have to right a basic timer function like this:

 #include
 
 volatile int count=0;
 int other_count=0;
 
 void timer_handle()
 {
         count++; //Make it short and sweet, just increment a variable
  }END_OF_FUNCTION(timer_handle); //End of Function
 
 int main()
 {
         allegro_init();
         install_timer();
         LOCK_VARIABLE(counter);
         LOCK_FUNCTION(timer_handle);
         install_int_ex(timer_handle, BPS_TO_TIMER(30)); //make the function be called 30 times per second
         while()
         {
                 do
                 {
                         //Logic, to be updated until we catch up to the timer, update drawing buffers, etc.
                         other_count++;
                 }while(other_count < count)
                 /*now, draw. we don't want the logic to slow down too much, so on slower systems, the logic runs
                    at full speed (or very close to it) and the drawing may get choppy -- copy buffers to screen.*/
         }
         allegro_exit();
 }
 

So, now we can keep the game going at a somewhat constant speed. Next, I will show you how to create a FPS timer.

FPS Timer

Now, you want a FPS, or Frames per Second timer. You can edit the last example and merge the code, but for sake of simplicity, I will just show you how to write only a FPS timer.

Basically, all you have to do is create a FPS variable. If you want an average FPS, add an average FPS variable, and if you want a last FPS display, just create a last FPS variable. You just re-average each second, and do other work with fps. For example, to have all of the above options, you would do something similar to this:

 #include
 volatile int fps=0;
 volatile int avg_fps=0;
 volatile int last_fps=0;
 
 void fps_timer()
 {
         last_fps=fps;
         fps=0;
         avg_fps=(avg_fps*last_fps)/2;
 }
 
 int main()
 {
         allegro_init();
         install_timer();
 
         install_timer_ex(fps_timer,SECS_TO_TIMER(1));
 
         while(!key[KEY_ESC])
         {
                 //drawing code//
 
                 //draw here//
                 update_screen();
                 fps++;
                 //textout fps, avgfps, lastfps, etc//
                 textmode(makecol(0,0,0));
                 textprintf(buffer,font,0,0,makecol(0,0,0),"FPS: %i  Average FPS: %i", last_fps, avg_fps);
                 draw_screen();
         }
 }
 
 

And you thought it was hard to make a FPS timer. Well, anyway, now we go to countdown timer class!

Count Down Timer

Now, you want a time limit in your game? Wow, you are greedy. But lucky for you, I am going to show you how! Basically, as you can guess, we will have to install a timer hook, but this time we decrement a value instead of increment or reset!

 /**
 RAFSoft Timer Article:
 Example Program: Countdown.cpp
 **/
 
 #include
 #include
 
 volatile int time_left=0;
 
 static void timer_timer()
 {
         time_left--;
 }
 
 int main()
 {
         allegro_init();
         install_timer();
 
         time_left = 5;//5 seconds
         install_int_ex(timer_timer, SECS_TO_TIMER(1));
         while(time_left != 0)cout << "Seconds Left: " << time_left << "\n";
         remove_int(timer_timer); //stop the madness!
 
         cout << "We are there!";
 
 }END_OF_MAIN();
 
 

See, it isn't too hard for a countdown, either! Well, you probably get the hang of it by now. In this section, I introduced a new function: remove_int(); The conversation I had with a DOS window over countdown.cpp is shown here:


E:\WINDOWS\Desktop\Pixelate Article\Examples>countdown
Seconds Left: 5
Seconds Left: 4
Seconds Left: 3
Seconds Left: 2
Seconds Left: 1
We are there!

In the next section, I will explain some other parts of the Allegro library that use timers, that you may not have noticed.

Timers are used everywhere

Wow, you survived. Here is that list I just promised you of other timer_using parts of Allegro:

- GUI
- Mouse
- vsync();
...

So, now you are a timer expert. I haven't decided what my next article will be about, but I will think of something. Assuming that this article is accepted, maybe i will do something along the lines of GUI customization, or something. Well, everyone, peace out, and have fun! All comments, suggestions, comments, complaints, and more can be sent to me at webmaster@rafsoft.zzn.com! Thanks to Shawn and the Allegro Community for creating a great library, Pixelate for creating a great online magazine, my parents and relatives for giving me money last Christmas so I could buy my laptop to use for development, and everyone else that I forgot. Talk to y'all later!

PS: The example countdown.cpp can be downloaded [countdown.cpp here]

Personal tools