I try to get familiar with timers and so on. But I sadly understand not very much (at the moment I read this Tutorial: http://wiki.allegro.cc/index.php?title=Timers Perhaps someone knows a better one?)
Now to my question. In the documentation I read this:
BPS_TO_TIMER(bps) - give the number of ticks each second
Okay, I so i tried:
cout << BPS_TO_TIMER(1) << endl; //result: 1193181
Then I tried:
cout << BPS_TO_TIMER(2) << endl; //result: 596590 WHY?!!!
If there are 1193181 ticks in one second, then there must be 1193181*2 = 2386362 ticks in two seconds. Or not?
Please help me with these f****** timers. I'm so frustraded.
Karl
BPS_TO_TIMER deals with beats per second, not seconds. BPS_TO_TIMER(2) gives you 2 beats per second, not one beat every 2 seconds. The wiki is worded poorly, "give the number of ticks each second" refers to what you pass in to the macro, not what it gives back to you. If you want every 2 seconds, use SECS_TO_TIMER(2) (or SECONDS_TO_TIMER, whatever it's named).
I've edited that section on the wiki to try to make it a little clearer, and combined with the code in the next section, it should be pretty obvious.
Does someone understand this code: (from the tutorial)
#include <allegro.h>
volatile int ticks = 0;
void ticker()
{
ticks++;
}
END_OF_FUNCTION(ticker)
const int updates_per_second = 60;
int main()
{
allegro_init();
install_timer();
//initialize the timer
LOCK_VARIABLE(ticks);
LOCK_FUNCTION(ticker);
install_int_ex(ticker, BPS_TO_TIMER(updates_per_second));
bool quit = false;
while(!quit)
{
while(ticks == 0)
{
rest(100 / updates_per_second);//rest until a full tick has passed
}
while(ticks > 0)
{
int old_ticks = ticks;
DoLogic();
ticks--;
if(old_ticks <= ticks)//logic is taking too long: abort and draw a frame
break;
}
DrawEverything();
}
return 0;
}
END_OF_MAIN()
Can you explain it to me? Why "const int updates_per_second = 60;"? Why the value 60? What does the programmer expect with the line:
install_int_ex(ticker, BPS_TO_TIMER(updates_per_second));
He writes the variable "updates_per_second" (60) for the second argument. Why? How many seconds are 60 Ticks? It think the result will be less than one second, it makes no sense or rather I do not understand it.
Why has allegro not any easy function who calls another function in X time.
For example:
void func(....)
{
load_bitmap....
}
timer(60, func); //ever 60 Millisecs call func(...)
That would be much easier.
I hope anybody is familiar with the topic and can explain me timer routines a bit. Or at least the source code I posted.
Karl
PS: Please excuse my worse english.
Can you explain it to me? Why "const int updates_per_second = 60;"? Why the value 60? What does the programmer expect with the line:
install_int_ex(ticker, BPS_TO_TIMER(updates_per_second));
He writes the variable "updates_per_second" (60) for the second argument. Why? How many seconds are 60 Ticks? It think the result will be less than one second, it makes no sense or rather I do not understand it.
The BPS_TO_TIMER() macro is used to convert between beats per second (bps) and hardware clock time. Whatever value you give it, that's how many ticks you will get each second. So if you go install_int_ex(ticker, BPS_TO_TIMER(60));, 60 ticks will take one second. One tick will take 1/60th of a second.
If you want to tick every 60 msec, you do this: install_int_ex(ticker, MSEC_TO_TIMER(60)).
edit: Also, if you're going to post code here, please put it inside <code></code> tags.
install_int_ex(ticker, BPS_TO_TIMER(BPS_TO_TIMER(60));, 60 ticks will take one second. One tick will take 1/60th of a second.
If you want to tick every 60 msec, you do this: install_int_ex(ticker, BPS_TO_TIMER(MSEC_TO_TIMER(60)).
I don't think those nested macros will do what you think they will do. Fisrt you convert 60 msec to "timer", then pass in that to BPS to timer, so you'll end up with an insane timer value.
Oops, that was bad copy pasta. I fixed it.
So this code here:
install_int_ex(ticker, BPS_TO_TIMER(60)) 60 ticks will take one second
and this code here:
install_int_ex(ticker, MSEC_TO_TIMER(60)) 60 ticks will take one millisecond (a little bit fast or?)
Is taht right?
Karl
No.
install_int_ex(ticker, BPS_TO_TIMER(60)); // This timer will fire 60 times per second install_int_ex(ticker, MSEC_TO_TIMER(60)); // This timer will fire with 60 ms between each tick
I have still one question to the code:
Why isn't here a 1000 instead of 100?
rest(100 / updates_per_second);
I thought the timer ticks 60 times a second and 1000 milliseconds are 1 second or?
Kalr
Because it's meant to be 0.1 of the time interval represented by 1 tick. It probably should just say rest(1) or rest(0).
At this point we want to wait the time for one tick. We have 60 ticks (updates_per_second) per second. -> 1000 / 60 = 17 Thus one tick every 17 milliseconds.
Due to that 100/60 does not really make sense?
At this point we want to wait the time for one tick.
No. We want to wait until the current tick has passed, not wait for the entire time of one tick. In other words, if we're only halfway through the current tick, we only want to rest for half a tick. Because we don't know exactly what position in the tick we are, we rest for fractions of a tick (in this case, as SiegeLord said, 1/10th or 0.1 of a tick) until the current tick has ended.