Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » strange timer behaviour

This thread is locked; no one can reply to it. rss feed Print
strange timer behaviour
Frank Drebin
Member #2,987
December 2002
avatar

ok i got the default game loop
speed_counter is incremented 100 times per second.

while (!key[KEY_ESC])
{
   while (speed_counter>0)
   {
      game();
      speed_counter--;
   }
   draw_game();
   //while (speed_counter==0) rest(1);
}

this way i get 100+ FPS.
if i uncomment the rest(1)-line my FPS-counter stays at 64.
so shouldn't i get here FPS around 100, too?

Jonatan Hedborg
Member #4,886
July 2004
avatar

Are you using vsync or tripple-buffering?

Frank Drebin
Member #2,987
December 2002
avatar

no just double buffering.
the strange thing is that i uncommented this line once before and then it worked fine...
this was with an older compiler but this shouldn't matter!

Kitty Cat
Member #2,815
October 2002
avatar

Resting isn't accurate. Resting for 1ms will cause you to rest closer to 10ms (giving you 100 FPS max). Then add in the time to do the logic and render each frame, and possible background tasks in the system, and you'll get less than 100 FPS. What's your speed if you use rest(0) instead of rest(1)?

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Frank Drebin
Member #2,987
December 2002
avatar

for rest(0) it's the same thing.
and i know that the timer isn't that accurate but when i did this before the FPS were around 100 (+- 5) but not constant 64...

Arthur Kalliokoski
Second in Command
February 2005
avatar

Maybe something has turned on your vid card vsync. Just a week ago my favorite old DOS editor could use the extended keys again for no reason that I could see.

"I hate computers, they're so nasty and complex. I could just pinch them" Marvin the Martian

They all watch too much MSNBC... they get ideas.

Frank Drebin
Member #2,987
December 2002
avatar

in this case is there any way to disable vsync or do i have to reintstall the older version of mingw?

Kitty Cat
Member #2,815
October 2002
avatar

I doubt its your compiler. There should be an option in your video card settings to force vsync on or off, or leave it application defined.

I doubt it's a vsync issue though. What version of Allegro do you have?

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Goalie Ca
Member #2,579
July 2002
avatar

Calling rest(1) forces the OS to put the process on the queue and hence reschedule it. 10ms time slices isn't too uncommon. Quick guess, you using windows?

ps: This is why its not a "real-time" os.

-------------
Bah weep granah weep nini bong!

Frank Drebin
Member #2,987
December 2002
avatar

yes its in windows (and linux) and newest allegro
even if rest(1) would delay more than 10 ms there would be only a few frames less but not that much

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

even if rest(1) would delay more than 10 ms

Tasks/Threads get 10ms slices, its more than possible to loose 100ms after doing your slice, it all depends on the system load, and the priorities all threads are running at.

edit, You CANNOT depend on the scheduler doing anything in a non real time OS.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Frank Drebin
Member #2,987
December 2002
avatar

ok i reinstalled the older verision of the compiler with no success.
i said my timer runs 100 times per second and with rest(1) i get 64FPS.
i have tested timers that runs 50 and 40 times per second and here i get the 50 and 40+-1 FPS.
so whats the difference betwenn a 50 and a 100 BPS timer ???

BrknPhoenix
Member #7,304
June 2006

edit: Eh nevermind, not entirely sure what you're getting at

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

so whats the difference betwenn a 50 and a 100 BPS timer

About twice as many tics (meaning more logic calls, and more attempted frames drawn). If your system is just barely keeping up at 100FPS with no resting, it can lose several frames if you try to rest any.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Frank Drebin
Member #2,987
December 2002
avatar

i know that 100 is twice as fast as a 50 BPS timer...
i meant what makes the difference in the FPS?

and without rest(1) i get around 150 FPS so this shouldn't be the problem.

[edit]
even this code doesn't work - test it yourself !

1#include <allegro.h>
2 
3volatile int speed_counter=0;
4volatile int fps=0;
5volatile int frames=0;
6BITMAP* bbuffer;
7 
8void increment_speed_counter()
9{
10 speed_counter++;
11}
12END_OF_FUNCTION(increment_speed_counter);
13 
14void update_fps()
15{
16 fps=frames;
17 frames=0;
18}
19END_OF_FUNCTION(update_fps);
20 
21void init()
22{
23 allegro_init();
24 install_keyboard();
25 set_color_depth(16);
26 set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,640,480,0,0);
27 install_timer();
28 LOCK_VARIABLE(speed_counter);
29 LOCK_VARIABLE(fps);
30 LOCK_VARIABLE(frames);
31 LOCK_FUNCTION(increment_speed_counter);
32 LOCK_FUNCTION(update_fps);
33 install_int_ex(increment_speed_counter,BPS_TO_TIMER(100));
34 install_int_ex(update_fps,BPS_TO_TIMER(1));
35}
36 
37int main()
38{
39 init();
40 
41 bbuffer=create_bitmap(640,480);
42
43 while (!key[KEY_ESC])
44 {
45 while (speed_counter>0)
46 {
47 speed_counter--;
48 }
49 clear_bitmap(bbuffer);
50 textprintf_ex(bbuffer,font,0,0,makecol(255,255,255),-1,"FPS: %i",fps);
51 blit(bbuffer,screen,0,0,0,0,640,480);
52 frames++;
53 while (speed_counter==0) rest(1);
54 }
55 
56 destroy_bitmap(bbuffer);
57
58 return 0;
59}
60END_OF_MAIN()

Kitty Cat
Member #2,815
October 2002
avatar

Both rest(0) and rest(1) give me 55~60 FPS with that. Granted though, X isn't the best at getting the fastest speed.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Frank Drebin
Member #2,987
December 2002
avatar

Go to: