|
|
| timer |
|
chin jiunn gai
Member #6,753
January 2006
|
how can i implement a count timer from 10 to 0 second ? |
|
waldermort
Member #6,259
October 2005
|
Why don't you try to use the allegro timer routines: volatile int counter; void my_timer_handler() { counter++; } END_OF_FUNCTION(my_timer_handler) and in your initialisation code you should lock the memory: LOCK_VARIABLE(counter); LOCK_FUNCTION(my_timer_handler); then you should install_timer(); install_int_ex(my_timer_handler, SECS_TO_TIMER(1)); then wherever you want your timer function if (counter > 1) { // this will be done every second counter--; } Remember when you are not using the above, the timer will constantly increase which can cause some problems. call remove_int() after the loop and then install_int_ex() again next time you want it, or within the function place if (counter > 1) counter = 1;
|
|
Ceagon Xylas
Member #5,495
February 2005
|
Hey its the first person I've seen explain the timer useage well. Thanks waldermort. I've asked about this a bunch and no one's ever really told me the details I wanted =] |
|
waldermort
Member #6,259
October 2005
|
Welcome Correct timer usage can be confusing as hell at first, but play around with them long enough and they become as easy is 'int x'. To others who wish to use a timer in their main loop (mainly used to regulate game speed) then break your code into 2 parts. The first for doing all the calculating and thinking 'logic' and the other for drawing all your bitmaps. Then in the main loop your code should look like this: install_timer(); install_int_ex(my_timer_handler,MSEC_TO_TIMER(80)); while (counter > 0) { // do all your logic code here counter--; } // do all your drawing here the counter will increase to 1 after every 80 miliseconds causing it to do the logic, eventually decreasing it back to 0, resulting in the bitmaps being drawn. Changing the comparison of counter can bring extra benefits like 'only do once' etc. On another note, while loops will eat away at the CPU and can cause other programs to act slowly or stop responding, I recomend inserting 'wait(1)' inside the loop which will give some of the CPU speed back to other programs. |
|
Tobias Dammers
Member #2,604
August 2002
|
And I recommend not touching allegro timers at all, unless you absolutely have to. If you're on windows, use QueryPerformanceCounter, on linux, use gettimeofday. Both are very accurate, less resource-hungry and even easier to use than allegro timers. Only downside is that they are platform-specific, so if you plan on distributing for several platforms, you need a wrapper. --- |
|
|