Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++ and timers

This thread is locked; no one can reply to it. rss feed Print
 1   2 
C++ and timers
Steve++
Member #1,816
January 2002

Something I forgot to mention:
Notice in the code that the Allegro timer isn't even used. In this case, I'm synchronizing my timer to the screen refresh rate, using vsync(). You could, however, synchronize the code to the Allegro timer, or any other timer mechanism.
To use the allegro timer, create a timer handler that simply increments a global variable. Don't fall into the trap of thinking you can make the Timer object global and call its tick() method directly. This could make your program/system crash.
Use a global variable that's updated within the timer handler, in conjunction with a loop inside main() (or wherever) that constantly checks the global variable and calls the tick method if needed. Here's a brief, untested skeleton code block:
code:
#include <allegro.h>
// The global timer variable
int global_ticks
// The timer handler
void my_timer() {
global_ticks++;
}
// ...
int main() {
allegro_init();
install_timer();
Timer *t = new Timer();
// Create your TimeListener objects
// ...
global_ticks = 0;
install_int(my_timer, 10);
// Other stuff you might do here
// ...

// Main game loop
while(player->alive) {
while (global_ticks) {
t->tick();
global_ticks--;
}
// This is where you render the scene in the back buffer
// ...
vsync();
// This is where you blit the back buffer to the screen.
}
}
END_OF_MAIN()

[ January 18, 2002: Message edited by: Steve++ ]

Fladimir da Gorf
Member #1,565
October 2001
avatar

A few thoughts....
Though u know u'll want only 5 bitmaps per an object, I'd consider using a vector instead of BITMAP _1, BITMAP _2... So u can add frames later on, when u think it's necessary and it even works automatically ( using myvec.size() instead of plain 5 or 4 ) That's even more C++ to ur proc...
Secondly, I wouldn't use timers at all... think about how the game would work in PCs with different speeds? It would look weird if the main character's bitmap would change 20 times but he has just walked 2 meters, for example. Just use plain frame counters, and define the number of screen refreshes needed to advance to the next bitmap in the vector list. And, u don't even need to think about how many timers u'll have in the same time. ( Think about the gameplay; the player couldn't shoot just if there would be too many bullets around... )
[ January 18, 2002: Message edited by: Garga ]

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

23yrold3yrold
Member #1,134
March 2001
avatar

The keyframe example on my website might be of use. It's similar to Amarillion's way.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

 1   2 


Go to: