Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Are timers the best solution

This thread is locked; no one can reply to it. rss feed Print
Are timers the best solution
AceBlkwell
Member #13,038
July 2011
avatar

All,

I’m using Allegro 4. I’ve been experimenting with the timer example that came with it. I’ve been altering it so I can understand how timers work. On a simple timer test, I changed the program to allow me to stop the timer at a certain number. Example the program starts printing 1-2-3-4-5-6 etc while (!keypressed()). I try to stop it at 4 by pressing a key. When I do this the program stops. In order to try again, I have to rerun it. I started working on a loop to run over and over. As I understand it, I would have to remove_int() to zero the timer each time through. Which would mean reinstalling install_int() the next loop through. This seems overly complicated and a potential memory leak. In any case, it got me to wondering if there wasn’t another command to do what I’m looking to do.

Is there a command to reset the timers to zero without removing them? Or is there a command that would allow a person to stop a cycle while trying to hit a specific number / target? Similar to a slot machine. The wheel spins, you hit a button and it stops. The goal is to stop on Cherry.

Thanks
Ace

Audric
Member #907
January 2001

A short-term answer is this :
When a keypress is detected, toggle a boolean on and off : paused = !paused;
Then the entire piece of code inside the timer can be surrounded by if (!paused) { counter++; ... }

However, more generally, you will probably want to implement a complete "game loop" system. It will give you much more choices on what you want to do :
- on user input
- as time passes.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Allegro 4 doesn't let you start and stop timers without re-installing the interrupt.

Audric provides a nice solution to 'pause' the timer.

Ideally you just let the timer run and then check the tick count delta value to see how much time has passed. You can 'reset' the counter at any time by setting it to zero.

AceBlkwell
Member #13,038
July 2011
avatar

Thanks guys. It never occurred to me the variables could be set to a given value. I thought once a variable was associated with a timer, that locked them in to that value only. For the simple goofing around program that I’m learning with, setting to zero manually will work fine. Or as Audric mentioned I could have assigned the timer variable value to another variable and tracked the difference between.

The only concern I have at this point is the timer runs regardless. I was testing a theory. The timer was counting (printing) 1-2-3-4-5 etc. I added a rest(5000) to the mix and the timer variable skipped a number (on screen) . What I can do for that I guess is grab the value before the rest(5000) and reassign once out. Again for the nonsense program I’m toying with, there isn’t any damage one way or the other. Later , however, if I use timers to keep the flow of a game’s action consistent , I’m worried the speed variation of different computers would have negative results on the game. IE I wouldn’t want to find out my game was only good on the computer it was created on.

In any case. Thanks again both of you.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Timers are helpful when trying to achieve a consistent frame rate. They will be more accurate than trying to time the loop with rest alone.

The idea is to lock the timer tick to the monitor's refresh rate, and then display your output when you get a tick. This will keep your game running at the same rate on different computers.

There are several timing methods you can use depending on your desired outcome.

You can use fixed delta timing for your logic or dynamic delta timing. The second can suffer from lag, the first can suffer from starvation.

You'll need a plan to drop visual frames when the processing gets too heavy, or to slow down gracefully.

Look up interpolation timing routines, as they seem to be the best of both worlds. Basically you run like normal, but interpolate positions based on time passed.

Go to: