Timers - BPS
KarlKoch

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

BAF

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).

LennyLen

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.

KarlKoch

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.

LennyLen
KarlKoch said:

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.

Thomas Fjellstrom
LennyLen said:

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.

LennyLen

Oops, that was bad copy pasta. I fixed it.

KarlKoch

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

gnolam

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

KarlKoch

I have still one question to the code:

#SelectExpand
1LOCK_FUNCTION(ticker); 2 install_int_ex(ticker, BPS_TO_TIMER(updates_per_second)); 3 4 bool quit = false; 5 6 while(!quit) 7 { 8 while(ticks == 0) 9 { 10 rest(100 / updates_per_second);//rest until a full tick has passed 11 } 12 13 while(ticks > 0) 14 { 15 int old_ticks = ticks; 16 17 DoLogic(); 18 19 ticks--; 20 if(old_ticks <= ticks)//logic is taking too long: abort and draw a frame 21 break; 22 } 23 24 DrawEverything(); 25 }

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

SiegeLord

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).

KarlKoch

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?

LennyLen
KarlKoch said:

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.

Thread #603483. Printed from Allegro.cc