![]() |
|
How would I implement a FPS counter w/ Allegro 5 events? |
DocHoliday
Member #12,498
January 2011
|
in Allegro 4 a FPS counter could be implemented as in http://wiki.allegro.cc/index.php?title=Timers: 1#include <allegro.h>
2
3volatile int ticks = 0;
4void ticker()
5{
6 ticks++;
7}
8END_OF_FUNCTION(ticker)
9
10//a new set of timing variables to keep the game time
11volatile int game_time = 0;
12void game_time_ticker()
13{
14 game_time++;
15}
16END_OF_FUNCTION(game_time_ticker)
17
18const int updates_per_second = 60;
19
20int main()
21{
22 allegro_init();
23 install_timer();
24
25 LOCK_VARIABLE(ticks);
26 LOCK_FUNCTION(ticker);
27 install_int_ex(ticker, BPS_TO_TIMER(updates_per_second));
28
29 LOCK_VARIABLE(game_time);
30 LOCK_FUNCTION(game_time_ticker);
31 install_int_ex(game_time_ticker, BPS_TO_TIMER(10));//i.e. game time is in tenths of seconds
32
33 bool quit = false;
34
35 int fps = 0;
36 int frames_done = 0;
37 int old_time = 0;
38
39 while(!quit)
40 {
41 while(ticks == 0)
42 {
43 rest(1);
44 }
45
46 while(ticks > 0)
47 {
48 int old_ticks = ticks;
49
50 DoLogic();
51
52 ticks--;
53 if(old_ticks <= ticks)
54 break;
55 }
56
57 if(game_time - old_time >= 10)//i.e. a second has passed since we last measured the frame rate
58 {
59 fps = frames_done;
60 //fps now holds the the number of frames done in the last second
61 //you can now output it using textout_ex et al.
62
63 //reset for the next second
64 frames_done = 0;
65 old_time = game_time;
66 }
67
68 DrawEverything();
69 frames_done++;//we drew a frame!
70 }
71 return 0;
72}
73END_OF_MAIN()
In Allegro 5 a simple mouse input program could look like the following (from http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Input): 1#include <stdio.h>
2#include <allegro5/allegro.h>
3
4const float FPS = 60;
5const int SCREEN_W = 640;
6const int SCREEN_H = 480;
7const int BOUNCER_SIZE = 32;
8
9int main(int argc, char **argv)
10{
11 ALLEGRO_DISPLAY *display = NULL;
12 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
13 ALLEGRO_TIMER *timer = NULL;
14 ALLEGRO_BITMAP *bouncer = NULL;
15 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
16 float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
17 bool redraw = true;
18
19 if(!al_init()) {
20 fprintf(stderr, "failed to initialize allegro!\n");
21 return -1;
22 }
23
24 if(!al_install_mouse()) {
25 fprintf(stderr, "failed to initialize the mouse!\n");
26 return -1;
27 }
28
29 timer = al_create_timer(1.0 / FPS);
30 if(!timer) {
31 fprintf(stderr, "failed to create timer!\n");
32 return -1;
33 }
34
35 display = al_create_display(SCREEN_W, SCREEN_H);
36 if(!display) {
37 fprintf(stderr, "failed to create display!\n");
38 al_destroy_timer(timer);
39 return -1;
40 }
41
42 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
43 if(!bouncer) {
44 fprintf(stderr, "failed to create bouncer bitmap!\n");
45 al_destroy_display(display);
46 al_destroy_timer(timer);
47 return -1;
48 }
49
50 al_set_target_bitmap(bouncer);
51
52 al_clear_to_color(al_map_rgb(255, 0, 255));
53
54 al_set_target_bitmap(al_get_backbuffer(display));
55
56 event_queue = al_create_event_queue();
57 if(!event_queue) {
58 fprintf(stderr, "failed to create event_queue!\n");
59 al_destroy_bitmap(bouncer);
60 al_destroy_display(display);
61 al_destroy_timer(timer);
62 return -1;
63 }
64
65 al_register_event_source(event_queue, al_get_display_event_source(display));
66
67 al_register_event_source(event_queue, al_get_timer_event_source(timer));
68
69 al_register_event_source(event_queue, al_get_mouse_event_source());
70
71 al_clear_to_color(al_map_rgb(0,0,0));
72
73 al_flip_display();
74
75 al_start_timer(timer);
76
77 while(1)
78 {
79 ALLEGRO_EVENT ev;
80 al_wait_for_event(event_queue, &ev);
81
82 if(ev.type == ALLEGRO_EVENT_TIMER) {
83 redraw = true;
84 }
85 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
86 break;
87 }
88 else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES ||
89 ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) {
90
91 bouncer_x = ev.mouse.x;
92 bouncer_y = ev.mouse.y;
93 }
94 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
95 break;
96 }
97
98 if(redraw && al_is_event_queue_empty(event_queue)) {
99 redraw = false;
100
101 al_clear_to_color(al_map_rgb(0,0,0));
102
103 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
104
105 al_flip_display();
106 }
107 }
108
109 al_destroy_bitmap(bouncer);
110 al_destroy_timer(timer);
111 al_destroy_display(display);
112 al_destroy_event_queue(event_queue);
113
114 return 0;
115}
How could I efficiently measure the FPS and display this to the screen in Allegro 5? |
SiegeLord
Member #7,827
October 2006
![]() |
Same approach will work. Allegro 5 comes with a much better way to measure current time (game time in that example), al_get_time, so using a separate game timer is not necessary. So the code would roughly be: 1double fps = 0;
2int frames_done = 0;
3double old_time = al_get_time();
4
5while(1)
6{
7 ...
8
9 if(redraw && al_is_event_queue_empty(event_queue)) {
10 ...
11
12 double game_time = al_get_time();
13 if(game_time - old_time >= 1.0) {
14 fps = frames_done / (game_time - old_time);
15
16 frames_done = 0;
17 old_time = game_time;
18 }
19
20 frames_done++;
21 }
22}
There are probably 100 other ways to do it, though... I think pretty much every example that comes with A5 that displays FPS uses a different method of calculating it. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
DocHoliday
Member #12,498
January 2011
|
That was perfect. |
|