Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Clocking tutorial - fps test program

This thread is locked; no one can reply to it. rss feed Print
Clocking tutorial - fps test program
TurboJudas
Member #7,039
March 2006
avatar

the Clocking Tutorial by Cage clocking.html puzzled me quite a bit at first, so I had to make a test program.

You can download the test exe:s and source code here:

http://www.cc.puv.fi/~e0400536/clocktest.rar

My 1,8Ghz computer draws the screen 450fps fullscreen and 220fps windowed, using 98% cpu.when adding sleep(1); after the drawing is finished the fps drops to 63 - 64 fps, using 0 - 2% cpu. I find it somewhat puzzling that if I start eg. midinotate or realplayer the fps rises to about 115.

My friend tested the clocktests on his 700Mhz pc, both the sleep(1) and nosleep fullscreen tests ran at about 75fps, and the windowed tests at about 40fps.

conclusion:
It seems to me it´s working fine. the logic is updated 60 times / second, as the block falls exactly 60 pixels every second. (the lines are drawn with 60 pixel spaces and the y position of the smily-block is incremented 1 pixel per logic loop) the fps depends on your computer and windowed/fullscreen mode.

here is the source code:

1//Small program by Sampo (TurboJudas) testing Cages clocking tutorial
2 
3#include <allegro.h>
4 
5BITMAP *smile;
6BITMAP *buffer;
7BITMAP *back;
8 
9typedef struct {
10 int posx, posy;
11}spryte_t;
12 
13 
14spryte_t Ssmile = {
15 290, 0,
16};
17 
18int fps = 0;
19int fps_dec = 0;
20int fps_cent = 0;
21int fps_screen = 0;
22int fps_dec_screen = 0;
23int fps_cent_screen = 0;
24 
25volatile int logic_counter;
26volatile int fps_counter;
27 
28 
29 
30update_game_logic()
31{
32 if(fps_counter == 60)
33 {
34 fps_screen = fps;
35 fps_dec_screen = fps_dec;
36 fps_cent_screen = fps_cent;
37 fps_counter = 0;
38 fps = 0;
39 fps_dec = 0;
40 fps_cent = 0;
41 }
42
43 Ssmile.posy ++;
44
45 if(Ssmile.posy == 480) Ssmile.posy = -60;
46
47
48}
49 
50 
51 
52void update_game_screen()
53{
54
55 acquire_screen();
56
57 stretch_sprite( buffer, back, 0, 0, SCREEN_W, SCREEN_H);
58
59 line( buffer, 0, 60, 640, 60, makecol( 255, 255, 255));
60 line( buffer, 0, 120, 640, 120, makecol( 255, 255, 255));
61 line( buffer, 0, 180, 640, 180, makecol( 255, 255, 255));
62 line( buffer, 0, 240, 640, 240, makecol( 255, 255, 255));
63 line( buffer, 0, 300, 640, 300, makecol( 255, 255, 255));
64 line( buffer, 0, 360, 640, 360, makecol( 255, 255, 255));
65 line( buffer, 0, 420, 640, 420, makecol( 255, 255, 255));
66 line( buffer, 0, 480, 640, 480, makecol( 255, 255, 255));
67
68 textprintf_ex(buffer, font, 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0), "FPS %d", fps_cent_screen);
69 textprintf_ex(buffer, font, 590, 10, makecol(0, 255, 0), makecol( 0, 0, 0), "%d", fps_dec_screen);
70 textprintf_ex(buffer, font, 599, 10, makecol(0, 255, 0), makecol( 0, 0, 0), "%d", fps_screen);
71 
72 draw_sprite( buffer, smile, Ssmile.posx, Ssmile.posy);
73
74 release_screen();
75
76 blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
77
78 clear(buffer);
79
80 fps ++;
81
82 if(fps == 10)
83 {
84 fps_dec ++;
85 fps = 0;
86 }
87 if(fps_dec == 10)
88 {
89 fps_cent ++;
90 fps_dec = 0;
91 }
92
93}
94 
95 
96void update_logic_counter()
97{
98 logic_counter++;
99 fps_counter ++;
100}
101END_OF_FUNCTION(update_logic_counter);
102 
103int main(){
104
105 allegro_init();
106 install_keyboard();
107 install_timer();
108 set_color_depth(16);
109 set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
110
111 buffer = create_bitmap(SCREEN_W,SCREEN_H);
112 smile = load_bitmap( "smile.bmp", NULL);
113 back = load_bitmap( "back.bmp", NULL);
114 clear(buffer);
115
116 Ssmile.posx = 290;
117 Ssmile.posy = 0;
118
119 LOCK_VARIABLE(logic_counter);
120 LOCK_VARIABLE(fps_counter);
121 LOCK_FUNCTION(update_logic_counter);
122 install_int_ex(update_logic_counter, BPS_TO_TIMER(60));
123
124 while ( !key[KEY_ESC] )
125 {
126 while(logic_counter)
127 {
128 update_game_logic();
129 logic_counter--;
130 }
131
132 update_game_screen();
133
134 sleep(1);
135 //if(fps_dec >= 6) sleep(1);
136 //if(fps_cent >= 1) sleep(1);
137
138 }
139
140 return 0;
141
142}
143END_OF_MAIN();

hazul
Member #4,338
February 2004
avatar

1switch(fps_cent_screen)
2 {
3 case 0:
4 textout_ex( buffer, font, "FPS 0", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
5 break;
6 case 1:
7 textout_ex( buffer, font, "FPS 1", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
8 break;
9 case 2:
10 textout_ex( buffer, font, "FPS 2", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
11 break;
12 case 3:
13 textout_ex( buffer, font, "FPS 3", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
14 break;
15 case 4:
16 textout_ex( buffer, font, "FPS 4", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
17 break;
18 case 5:
19 textout_ex( buffer, font, "FPS 5", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
20 break;
21 case 6:
22 textout_ex( buffer, font, "FPS 6", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
23 break;
24 case 7:
25 textout_ex( buffer, font, "FPS 7", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
26 break;
27 case 8:
28 textout_ex( buffer, font, "FPS 8", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
29 break;
30 case 9:
31 textout_ex( buffer, font, "FPS 9", 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0) );
32 break;
33
34 }

is the same as
textprintf_ex(buffer, font, 550, 10, makecol(0, 255, 0), makecol( 0, 0, 0), "FPS %d", fps_cent_screen);

* * * * *
"Multiplayer is actually the best way of not programming a good AI" -ReyBrujo

TurboJudas
Member #7,039
March 2006
avatar

Thanks, I knew there would be a better way for that :P I´ll change it right away.

Machinae Supremacy rules btw! :) and I just love the sounds of the sid chip!

Birdeeoh
Member #6,862
February 2006
avatar

My 1,8Ghz computer draws the screen 450fps fullscreen and 220fps windowed, using 98% cpu.when adding sleep(1); after the drawing is finished the fps drops to 63 - 64 fps, using 0 - 2% cpu. I find it somewhat puzzling that if I start eg. midinotate or realplayer the fps rises to about 115.

The problem with sleep() is it's very low res, low accuracy. Sleep(1) actually means "sleep AT LEAST 1 millisecond". On Windows it's often 5, 10, even 15 ms.

In my exp when I have a 60hz refresh rate and I insert a Sleep(1), the app usually wakes right around 60hz - sometimes dead on, sometimes up to 65 like you experienced. This means, at the very least, the OS driven refresh rate will force your program awake that often.

It does make sense to me that starting another program which does something involing graphical timing will actually speed your program up as it causes more interupts which become opporunities for the OS to reschedule your process. :)

Makes me wonder if there'd be a reliable way to do a Sleep(1) but have a second thread which causes some regular event to force the OS to wake your process... that way we could get rid of the pesky 100% CPU usage but also get faster than-refresh-rate timing... hmmmmmm

[url http://developer.berlios.de/projects/openlayer/]OpenLayer[/url is an OpenGL accelerated 2D library for fast and easy graphics development under Allegro

gnolam
Member #2,030
March 2002
avatar

Well, there is always TimeBeginPeriod()...

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

miran
Member #2,407
June 2002

Quote:

Well, there is always TimeBeginPeriod() [msdn.microsoft.com]...

Always? ???

--
sig used to be here

A J
Member #3,025
December 2002
avatar

use TimeBeginPeriod() to set the resolution of Sleep() to 1.
remember to use TimeEndPeriod() at your shutdown too ;)

___________________________
The more you talk, the more AJ is right. - ML

gnolam
Member #2,030
March 2002
avatar

Miran said:

Always? ???

It's a set expression.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

HoHo
Member #4,534
April 2004
avatar

Quote:

Well, there is always TimeBeginPeriod()

It should ba always under Windows. Under *nix you have gettimeofday that is much better for timing :P

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

gnolam
Member #2,030
March 2002
avatar

The post I was replying to was only talking about Windows...

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Go to: