|
This thread is locked; no one can reply to it. |
1
2
|
FPS counter (Allegro 5) |
verthex
Member #11,340
September 2009
|
The following code works except I want a FPS counter to display the actual working rate somewhere on the screen or gui. I'm also wondering if I could improve my gameloop in terms of the keyboard exit on escape or anything else (if possible). 1
2#include <stdio.h>
3#include <allegro5/allegro.h>
4#include <allegro5/allegro_primitives.h>
5
6const float FPS = 120;
7
8int main(int argc, char **argv)
9{
10 ALLEGRO_DISPLAY *display = NULL;
11 ALLEGRO_BITMAP *bitmap = NULL;
12 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
13 ALLEGRO_TIMER *timer = NULL;
14
15 bool exit = false;
16
17 if(!al_init())
18 {
19 fprintf(stderr, "failed to initialize allegro!\n");
20 return -1;
21 }
22
23 timer = al_create_timer(1.0 / FPS);
24 if(!timer)
25 {
26 fprintf(stderr, "failed to create timer!\n");
27 return -1;
28 }
29
30 int screen_width = 640;
31 int screen_height = 480;
32
33 display = al_create_display(screen_width, screen_height);
34
35 if(!display)
36 {
37 fprintf(stderr, "failed to create display!\n");
38 al_destroy_timer(timer);
39 return -1;
40 }
41
42 if (!al_init_primitives_addon())
43 {
44 fprintf(stderr, "failed to initiialize primitives addon!\n");
45 return -1;
46 }
47
48 if(!al_install_keyboard())
49 {
50 fprintf(stderr, "failed to initialize the keyboard!\n");
51 return -1;
52 }
53
54 event_queue = al_create_event_queue();
55
56 if(!event_queue)
57 {
58 fprintf(stderr, "failed to create event_queue!\n");
59 al_destroy_display(display);
60 al_destroy_timer(timer);
61 return -1;
62 }
63
64 al_register_event_source(event_queue, al_get_keyboard_event_source());
65 al_register_event_source(event_queue, al_get_display_event_source(display));
66 al_register_event_source(event_queue, al_get_timer_event_source(timer));
67
68 al_clear_to_color(al_map_rgb(0,0,0));
69
70 al_flip_display();
71 al_start_timer(timer);
72
73 bool redraw = true;
74
75 while(1)
76 {
77 ALLEGRO_EVENT ev;
78 al_wait_for_event(event_queue, &ev);
79
80 if (redraw)
81 {
82 int r = rand()%255;
83 int g = rand()%255;
84 int b = rand()%255;
85 int x1 = rand()%screen_width;
86 int x2 = rand()%screen_width;
87 int y1 = rand()%screen_height;
88 int y2 = rand()%screen_height;
89
90 ALLEGRO_COLOR color = al_map_rgb(r, g, b);
91 al_draw_line(x1,y1, x2,y2, color, 1.0);
92
93 al_flip_display();
94 redraw = false;
95 }
96 if(ev.type == ALLEGRO_EVENT_TIMER)
97 {
98 redraw = true;
99 }
100 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
101 {
102 break;
103 }
104 if(al_is_event_queue_empty(event_queue))
105 {
106 al_wait_for_event(event_queue , &ev);
107 if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
108 {
109 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
110 {
111 exit = true;
112 break;
113 }
114 }
115 }
116 }
117
118 al_destroy_timer(timer);
119 al_destroy_display(display);
120 al_destroy_event_queue(event_queue);
121
122 return 0;
123}
|
J-Gamer
Member #12,491
January 2011
|
I noticed a few things in here:
As for the FPS, there is a function that returns the current time in the order of milliseconds: al_get_time. You could do something like this: double old_time = al_get_time(); while(!exit) { //game loop logic here double new_time = al_get_time(); double delta = new_time - old_time; double fps = 1/(delta*1000); old_time = new_time; } Hope this helps ^^ " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
verthex
Member #11,340
September 2009
|
J-Gamer said: You should also change your check for ALLEGRO_EVENT_DISPLAY_CLOSE to exit = true; Do you mean I should do this else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { exit = true; break; } or this? else if(ev.type == (exit = true)) { break; } Also how do I display the fps once its calculated? thanks. 1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include <allegro5/allegro_primitives.h>
4
5const float FPS = 120;
6
7int main(int argc, char **argv)
8{
9 ALLEGRO_DISPLAY *display = NULL;
10 ALLEGRO_BITMAP *bitmap = NULL;
11 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
12 ALLEGRO_TIMER *timer = NULL;
13
14 bool exit = false;
15
16 if(!al_init())
17 {
18 fprintf(stderr, "failed to initialize allegro!\n");
19 return -1;
20 }
21
22 timer = al_create_timer(1.0 / FPS);
23 if(!timer)
24 {
25 fprintf(stderr, "failed to create timer!\n");
26 return -1;
27 }
28
29 int screen_width = 640;
30 int screen_height = 480;
31
32 display = al_create_display(screen_width, screen_height);
33
34 if(!display)
35 {
36 fprintf(stderr, "failed to create display!\n");
37 al_destroy_timer(timer);
38 return -1;
39 }
40
41 if (!al_init_primitives_addon())
42 {
43 fprintf(stderr, "failed to initiialize primitives addon!\n");
44 return -1;
45 }
46
47 if(!al_install_keyboard())
48 {
49 fprintf(stderr, "failed to initialize the keyboard!\n");
50 return -1;
51 }
52
53 event_queue = al_create_event_queue();
54
55 if(!event_queue)
56 {
57 fprintf(stderr, "failed to create event_queue!\n");
58 al_destroy_display(display);
59 al_destroy_timer(timer);
60 return -1;
61 }
62
63 al_register_event_source(event_queue, al_get_keyboard_event_source());
64 al_register_event_source(event_queue, al_get_display_event_source(display));
65 al_register_event_source(event_queue, al_get_timer_event_source(timer));
66
67 al_clear_to_color(al_map_rgb(0,0,0));
68
69 al_flip_display();
70 al_start_timer(timer);
71
72 bool redraw = true;
73
74 double old_time = al_get_time();
75
76 while(!exit)
77 {
78 double new_time = al_get_time();
79 double delta = new_time - old_time;
80 double fps = 1/(delta*1000);
81 old_time = new_time;
82
83 ALLEGRO_EVENT ev;
84 al_wait_for_event(event_queue, &ev);
85
86 if (redraw)
87 {
88 int r = rand()%255;
89 int g = rand()%255;
90 int b = rand()%255;
91 int x1 = rand()%screen_width;
92 int x2 = rand()%screen_width;
93 int y1 = rand()%screen_height;
94 int y2 = rand()%screen_height;
95
96 ALLEGRO_COLOR color = al_map_rgb(r, g, b);
97 al_draw_line(x1,y1, x2,y2, color, 1.0);
98
99 al_flip_display();
100 redraw = false;
101 }
102 if(ev.type == ALLEGRO_EVENT_TIMER)
103 {
104 redraw = true;
105 }
106 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
107 {
108 exit = true;
109 break;
110 }
111 if(al_is_event_queue_empty(event_queue))
112 {
113 if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
114 {
115 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
116 {
117 exit = true;
118 break;
119 }
120 }
121 }
122 }
123
124 al_destroy_timer(timer);
125 al_destroy_display(display);
126 al_destroy_event_queue(event_queue);
127
128 return 0;
129}
|
J-Gamer
Member #12,491
January 2011
|
verthex said: Do you mean I should do this No... more something like this: Don't use break in your main loop. Having multiple exit points makes your code less readable. As for drawing the FPS: http://www.allegro.cc/manual/5/al_draw_textf " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
verthex
Member #11,340
September 2009
|
I checked out the font page in the manual and made more changes below but I'm not sure if theres a quick and easy way to convert the fps variable which is double to a const char* (line 90). Any ideas? 1
2#include <stdio.h>
3#include <allegro5/allegro.h>
4#include <allegro5/allegro_primitives.h>
5#include <allegro5/allegro_font.h>
6#include <allegro5/allegro_ttf.h>
7
8const float FPS = 120;
9
10int main(int argc, char **argv)
11{
12 ALLEGRO_DISPLAY *display = NULL;
13 ALLEGRO_BITMAP *bitmap = NULL;
14 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
15 ALLEGRO_TIMER *timer = NULL;
16 ALLEGRO_FONT *ttf_font;
17
18 bool exit = false;
19
20 if(!al_init())
21 {
22 fprintf(stderr, "failed to initialize allegro!\n");
23 return -1;
24 }
25
26 timer = al_create_timer(1.0 / FPS);
27 if(!timer)
28 {
29 fprintf(stderr, "failed to create timer!\n");
30 return -1;
31 }
32
33 int screen_width = 640;
34 int screen_height = 480;
35
36 display = al_create_display(screen_width, screen_height);
37
38 if(!display)
39 {
40 fprintf(stderr, "failed to create display!\n");
41 al_destroy_timer(timer);
42 return -1;
43 }
44
45 if (!al_init_primitives_addon())
46 {
47 fprintf(stderr, "failed to initiialize primitives addon!\n");
48 return -1;
49 }
50
51 if(!al_install_keyboard())
52 {
53 fprintf(stderr, "failed to initialize the keyboard!\n");
54 return -1;
55 }
56
57 event_queue = al_create_event_queue();
58
59 if(!event_queue)
60 {
61 fprintf(stderr, "failed to create event_queue!\n");
62 al_destroy_display(display);
63 al_destroy_timer(timer);
64 return -1;
65 }
66
67 al_register_event_source(event_queue, al_get_keyboard_event_source());
68 al_register_event_source(event_queue, al_get_display_event_source(display));
69 al_register_event_source(event_queue, al_get_timer_event_source(timer));
70
71 al_clear_to_color(al_map_rgb(0,0,0));
72
73 al_flip_display();
74 al_start_timer(timer);
75
76 al_init_ttf_addon();
77 ttf_font = al_load_ttf_font("TIMES.TTF", 16, 0);
78
79 bool redraw = true;
80
81 double old_time = al_get_time();
82
83 while(!exit)
84 {
85 double new_time = al_get_time();
86 double delta = new_time - old_time;
87 double fps = 1/(delta*1000);
88 old_time = new_time;
89
90 al_draw_text(ttf_font, al_map_rgb(255,255,0), 16, 32, 0, fps);//fps needs to be const char*
91
92 ALLEGRO_EVENT ev;
93 al_wait_for_event(event_queue, &ev);
94
95 if (redraw)
96 {
97 int r = rand()%255;
98 int g = rand()%255;
99 int b = rand()%255;
100 int x1 = rand()%screen_width;
101 int x2 = rand()%screen_width;
102 int y1 = rand()%screen_height;
103 int y2 = rand()%screen_height;
104
105 ALLEGRO_COLOR color = al_map_rgb(r, g, b);
106 al_draw_line(x1,y1, x2,y2, color, 1.0);
107
108 al_flip_display();
109 redraw = false;
110 }
111 if(ev.type == ALLEGRO_EVENT_TIMER)
112 {
113 redraw = true;
114 }
115 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
116 {
117 exit = true;
118 }
119 if(al_is_event_queue_empty(event_queue))
120 {
121 if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
122 {
123 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
124 {
125 exit = true;
126 }
127 }
128 }
129 }
130
131 al_destroy_timer(timer);
132 al_destroy_display(display);
133 al_destroy_event_queue(event_queue);
134
135 return 0;
136}
|
J-Gamer
Member #12,491
January 2011
|
al_draw_textf(font,text_color,x,y,FLAG,"FPS: %d",fps); " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
verthex
Member #11,340
September 2009
|
J-Gamer said: al_draw_textf(font,text_color,x,y,FLAG,"FPS: %d",fps); That did draw the fps but now the program seems to endlessly draw text over the same location until its a yellow rectangle plus I cant seem to exit the program, ever. Pressing ESC or trying to close the window wont stop it either, I used process explorer to kill the program. 1
2#include <stdio.h>
3#include <allegro5/allegro.h>
4#include <allegro5/allegro_primitives.h>
5#include <allegro5/allegro_font.h>
6#include <allegro5/allegro_ttf.h>
7
8const float FPS = 120;
9
10int main(int argc, char **argv)
11{
12 ALLEGRO_DISPLAY *display = NULL;
13 ALLEGRO_BITMAP *bitmap = NULL;
14 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
15 ALLEGRO_TIMER *timer = NULL;
16 ALLEGRO_FONT *ttf_font;
17
18 bool exit = false;
19
20 if(!al_init())
21 {
22 fprintf(stderr, "failed to initialize allegro!\n");
23 return -1;
24 }
25
26 timer = al_create_timer(1.0 / FPS);
27 if(!timer)
28 {
29 fprintf(stderr, "failed to create timer!\n");
30 return -1;
31 }
32
33 int screen_width = 640;
34 int screen_height = 480;
35
36 display = al_create_display(screen_width, screen_height);
37
38 if(!display)
39 {
40 fprintf(stderr, "failed to create display!\n");
41 al_destroy_timer(timer);
42 return -1;
43 }
44
45 if (!al_init_primitives_addon())
46 {
47 fprintf(stderr, "failed to initiialize primitives addon!\n");
48 return -1;
49 }
50
51 if(!al_install_keyboard())
52 {
53 fprintf(stderr, "failed to initialize the keyboard!\n");
54 return -1;
55 }
56
57 event_queue = al_create_event_queue();
58
59 if(!event_queue)
60 {
61 fprintf(stderr, "failed to create event_queue!\n");
62 al_destroy_display(display);
63 al_destroy_timer(timer);
64 return -1;
65 }
66
67 al_register_event_source(event_queue, al_get_keyboard_event_source());
68 al_register_event_source(event_queue, al_get_display_event_source(display));
69 al_register_event_source(event_queue, al_get_timer_event_source(timer));
70
71 al_clear_to_color(al_map_rgb(0,0,0));
72
73 al_flip_display();
74 al_start_timer(timer);
75
76 al_init_ttf_addon();
77 ttf_font = al_load_ttf_font("TIMES.TTF", 16, 0);
78
79 bool redraw = true;
80
81 double old_time = al_get_time();
82
83 while(!exit)
84 {
85 double new_time = al_get_time();
86 double delta = new_time - old_time;
87 double fps = 1/(delta*1000);
88 old_time = new_time;
89
90 al_draw_textf(ttf_font,al_map_rgb(255,255,0), 16, 32,0,"FPS: %d",fps);
91
92 ALLEGRO_EVENT ev;
93 al_wait_for_event(event_queue, &ev);
94
95 if (redraw)
96 {
97 int r = rand()%255;
98 int g = rand()%255;
99 int b = rand()%255;
100 int x1 = rand()%screen_width;
101 int x2 = rand()%screen_width;
102 int y1 = rand()%screen_height;
103 int y2 = rand()%screen_height;
104
105 ALLEGRO_COLOR color = al_map_rgb(r, g, b);
106 al_draw_line(x1,y1, x2,y2, color, 1.0);
107
108 al_flip_display();
109 redraw = false;
110 }
111 if(ev.type == ALLEGRO_EVENT_TIMER)
112 {
113 redraw = true;
114 }
115 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
116 {
117 exit = true;
118 break;
119 }
120 if(al_is_event_queue_empty(event_queue))
121 {
122 if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
123 {
124 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
125 {
126 exit = true;
127 break;
128 }
129 }
130 }
131 }
132
133 al_destroy_timer(timer);
134 al_destroy_display(display);
135 al_destroy_event_queue(event_queue);
136
137 return 0;
138}
|
J-Gamer
Member #12,491
January 2011
|
As for the text: draw it in your drawing code, not the main loop. " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
verthex
Member #11,340
September 2009
|
J-Gamer said: should create a bitmap with the screen's dimensions and draw the lines to that one. Does al_flip_display() change the ALLEGRO_DISPLAY and the ALLEGRO_BITMAP buffers to which the program is drawing to?
|
Trent Gamblin
Member #261
April 2000
|
It doesn't change the target bitmap (which is an Allegro concept), but it does swap buffers at a lower level. If you al_flip_display twice in a row you shouldn't be surprised if two different images are flipped onto the visible screen. But if you're following j-gamer's suggestion then you will have to change the target bitmap yourself. Something like this: al_set_target_bitmap(buffer); al_draw_line(...); al_set_target_backbuffer(display); al_draw_bitmap(buffer, 0, 0, 0); al_flip_display(); Since you have two targets, the buffer, and then later the display, you have to set the target bitmap twice.
|
verthex
Member #11,340
September 2009
|
What about, al_draw_textf(ttf_font,al_map_rgb(255,255,0),16,32,0,"FPS: %d",fps); Would writing it to the display during every event timer update and clearing with, al_clear_to_color(al_map_rgb(0,0,0)) work?
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Yes, if you clear the backbuffer and then draw your text and line onto it then it will look normal, but you will lose any lines you have drawn so far. In the latest code you posted you check if the event queue is empty, and if it is, then you check the event type. That's not what you want to do. Check the type of each event as you receive one and deal with it accordingly. Check all the events in the queue at once, using al_is_event_queue_empty to see if you have checked them all. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
verthex
Member #11,340
September 2009
|
Edgar Reynaldo said: Yes, if you clear the backbuffer and then draw your text and line onto it then it will look normal, but you will lose any lines you have drawn so far. Right, thats why I wanted to draw the lines to the buffer and never clear it and draw the text to the display and clear it (with every timer update). Am I wrong, would it work? Quote: In the latest code you posted you check if the event queue is empty, and if it is, then you check the event type. That's not what you want to do. Check the type of each event as you receive one and deal with it accordingly. Check all the events in the queue at once, using al_is_event_queue_empty to see if you have checked them all. Do you mean this 1
2if(al_is_event_queue_empty(event_queue))
3{
4 if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
5 {
6 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
7 {
8 exit = true;
9 break;
10 }
11 }
12 else if(ev.type == ALLEGRO_EVENT_TIMER)
13 {
14 redraw = true;
15 }
16 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
17 {
18 exit = true;
19 break;
20 }
21
22}
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
verthex said: Right, thats why I wanted to draw the lines to the buffer and never clear it and draw the text to the display and clear it (with every timer update). Am I wrong, would it work? Sure, that should work. 1) Draw lines to buffer verthex said: Do you mean this Yes. If there are no events in the queue, why would you check them? As it was, you were only checking certain events if they were last in the queue. Go back to what I was using in the other thread : 1while (!quit) {
2 if (redraw) {
3 Redraw();
4 }
5 while (1) {
6 ALLEGRO_EVENT ev;
7 al_wait_for_event(event_queue , &ev);
8 if (ev.type == ALLEGRO_EVENT_TIMER) {
9 redraw = true;
10 } else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
11 quit = true;
12 break;
13 } else if (ev.type == /*...*/) {
14
15 }
16 if (al_is_event_queue_empty(event_queue)) {break;}// this way all events are used
17 }
18}
This way it will only redraw when you get a timer event. If you want some other behaviour, let me know. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
verthex
Member #11,340
September 2009
|
Edgar Reynaldo said:
1) Draw lines to buffer Wont the lines in the buffer get deleted with every update of the backbuffer (step 2)?
|
Thomas Fjellstrom
Member #476
June 2000
|
No. You're clearing the backbuffer of the display, not the buffer. -- |
verthex
Member #11,340
September 2009
|
I tried this and now it crashes. Am I supposed to fiddle around with what Trent said every time I redraw? 1
2
3#include <stdio.h>
4#include <allegro5/allegro.h>
5#include <allegro5/allegro_primitives.h>
6#include <allegro5/allegro_font.h>
7#include <allegro5/allegro_ttf.h>
8
9const float FPS = 120;
10
11int main(int argc, char **argv)
12{
13 ALLEGRO_DISPLAY *display = NULL;
14 ALLEGRO_BITMAP *bitmap = NULL;
15 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
16 ALLEGRO_TIMER *timer = NULL;
17 ALLEGRO_FONT *ttf_font;
18
19 bool exit = false;
20
21 if(!al_init())
22 {
23 fprintf(stderr, "failed to initialize allegro!\n");
24 return -1;
25 }
26
27 timer = al_create_timer(1.0 / FPS);
28 if(!timer)
29 {
30 fprintf(stderr, "failed to create timer!\n");
31 return -1;
32 }
33
34 int screen_width = 640;
35 int screen_height = 480;
36
37 display = al_create_display(screen_width, screen_height);
38
39 if(!display)
40 {
41 fprintf(stderr, "failed to create display!\n");
42 al_destroy_timer(timer);
43 return -1;
44 }
45
46 if (!al_init_primitives_addon())
47 {
48 fprintf(stderr, "failed to initiialize primitives addon!\n");
49 return -1;
50 }
51
52 if(!al_install_keyboard())
53 {
54 fprintf(stderr, "failed to initialize the keyboard!\n");
55 return -1;
56 }
57
58 event_queue = al_create_event_queue();
59
60 if(!event_queue)
61 {
62 fprintf(stderr, "failed to create event_queue!\n");
63 al_destroy_display(display);
64 al_destroy_timer(timer);
65 return -1;
66 }
67
68 bitmap = al_create_bitmap(screen_width, screen_height);
69 if(!bitmap)
70 {
71 fprintf(stderr, "failed to create bouncer bitmap!\n");
72 al_destroy_display(display);
73 al_destroy_timer(timer);
74 return -1;
75 }
76
77 al_register_event_source(event_queue, al_get_keyboard_event_source());
78 al_register_event_source(event_queue, al_get_display_event_source(display));
79 al_register_event_source(event_queue, al_get_timer_event_source(timer));
80
81 al_set_target_bitmap(bitmap);
82 al_clear_to_color(al_map_rgb(0,0,0));
83 al_set_target_bitmap(al_get_backbuffer(display));
84
85 al_flip_display();
86 al_start_timer(timer);
87
88 al_init_ttf_addon();
89 ttf_font = al_load_ttf_font("TIMES.TTF", 16, 0);
90
91 bool redraw = true;
92
93 double old_time = al_get_time();
94
95 while(!exit)
96 {
97 double new_time = al_get_time();
98 double delta = new_time - old_time;
99 double fps = 1/(delta*1000);
100 old_time = new_time;
101
102 ALLEGRO_EVENT ev;
103 al_wait_for_event(event_queue, &ev);
104
105 if (redraw)
106 {
107 int r = rand()%255;
108 int g = rand()%255;
109 int b = rand()%255;
110 int x1 = rand()%screen_width;
111 int x2 = rand()%screen_width;
112 int y1 = rand()%screen_height;
113 int y2 = rand()%screen_height;
114
115 al_set_target_bitmap(bitmap);
116 ALLEGRO_COLOR color = al_map_rgb(r,g,b);
117 al_draw_line(x1,y1,x2,y2,color,1.0);
118 al_set_target_backbuffer(display);
119 al_draw_textf(ttf_font,al_map_rgb(255,255,0), 16, 32,0,"FPS: %d",fps);
120 al_draw_bitmap(bitmap,0,0,0);
121 al_flip_display();
122 redraw = false;
123 }
124 while (1)
125 {
126 ALLEGRO_EVENT ev;
127 al_wait_for_event(event_queue , &ev);
128 if (ev.type == ALLEGRO_EVENT_TIMER)
129 {
130 redraw = true;
131 }
132 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
133 {
134 exit = true;
135 break;
136 }
137 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
138 {
139 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
140 {
141 exit = true;
142 break;
143 }
144 }
145 if (al_is_event_queue_empty(event_queue))
146 {
147 break;
148 }// this way all events are used
149 }
150 }
151
152 al_destroy_timer(timer);
153 al_destroy_display(display);
154 al_destroy_event_queue(event_queue);
155
156 return 0;
157}
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
1) You forgot to initialize the font addon. Initialize it before you initialize the ttf addon. This is why it is crashing. 2) %lf is the correct printf format specifier for a double, not %d, that is for signed integers. Note : After messing with al_draw_textf for a while, it does not recognize %lf as a valid specifier, even though printf does. Use %f and cast fps to a float. 3) You're waiting for events twice, get rid of lines 102 and 103. 4) You're drawing the buffer on top of the FPS counter, so you'll never see it. Swap lines 119 and 120. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
J-Gamer
Member #12,491
January 2011
|
EDIT: beaten ._. " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
Edgar Reynaldo
Major Reynaldo
May 2007
|
I forgot : 5) al_get_time() returns the number of seconds passed since allegro was initialized, not milliseconds. So the correct formula is My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
J-Gamer
Member #12,491
January 2011
|
mis-read the manual " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
verthex
Member #11,340
September 2009
|
I made the changes like you said but it still crashed. I commented out the line where the text is printed and that worked except the frame rate slowed to 1 line per second. 1
2#include <stdio.h>
3#include <allegro5/allegro.h>
4#include <allegro5/allegro_primitives.h>
5#include <allegro5/allegro_font.h>
6#include <allegro5/allegro_ttf.h>
7
8const float FPS = 120;
9
10int main(int argc, char **argv)
11{
12 ALLEGRO_DISPLAY *display = NULL;
13 ALLEGRO_BITMAP *bitmap = NULL;
14 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
15 ALLEGRO_TIMER *timer = NULL;
16 ALLEGRO_FONT *ttf_font;
17
18 bool exit = false;
19
20 if(!al_init())
21 {
22 fprintf(stderr, "failed to initialize allegro!\n");
23 return -1;
24 }
25
26 timer = al_create_timer(1.0 / FPS);
27 if(!timer)
28 {
29 fprintf(stderr, "failed to create timer!\n");
30 return -1;
31 }
32
33 int screen_width = 640;
34 int screen_height = 480;
35
36 display = al_create_display(screen_width, screen_height);
37
38 if(!display)
39 {
40 fprintf(stderr, "failed to create display!\n");
41 al_destroy_timer(timer);
42 return -1;
43 }
44
45 if (!al_init_primitives_addon())
46 {
47 fprintf(stderr, "failed to initiialize primitives addon!\n");
48 return -1;
49 }
50
51 if(!al_install_keyboard())
52 {
53 fprintf(stderr, "failed to initialize the keyboard!\n");
54 return -1;
55 }
56
57 event_queue = al_create_event_queue();
58
59 if(!event_queue)
60 {
61 fprintf(stderr, "failed to create event_queue!\n");
62 al_destroy_display(display);
63 al_destroy_timer(timer);
64 return -1;
65 }
66
67 bitmap = al_create_bitmap(screen_width, screen_height);
68 if(!bitmap)
69 {
70 fprintf(stderr, "failed to create bouncer bitmap!\n");
71 al_destroy_display(display);
72 al_destroy_timer(timer);
73 return -1;
74 }
75
76 al_register_event_source(event_queue, al_get_keyboard_event_source());
77 al_register_event_source(event_queue, al_get_display_event_source(display));
78 al_register_event_source(event_queue, al_get_timer_event_source(timer));
79
80 al_set_target_bitmap(bitmap);
81 al_clear_to_color(al_map_rgb(0,0,0));
82 al_set_target_bitmap(al_get_backbuffer(display));
83
84 al_flip_display();
85 al_start_timer(timer);
86
87 al_init_font_addon();
88 al_init_ttf_addon();
89 ttf_font = al_load_ttf_font("TIMES.TTF", 16, 0);
90
91 bool redraw = true;
92
93 double old_time = al_get_time();
94
95 while(!exit)
96 {
97 double new_time = al_get_time();
98 double fps = 1/(new_time - old_time);
99 old_time = new_time;
100
101 if (redraw)
102 {
103 int r = rand()%255;
104 int g = rand()%255;
105 int b = rand()%255;
106 int x1 = rand()%screen_width;
107 int x2 = rand()%screen_width;
108 int y1 = rand()%screen_height;
109 int y2 = rand()%screen_height;
110
111 al_set_target_bitmap(bitmap);
112 ALLEGRO_COLOR color = al_map_rgb(r,g,b);
113 al_draw_line(x1,y1,x2,y2,color,1.0);
114 al_set_target_backbuffer(display);
115 al_draw_bitmap(bitmap,0,0,0);
116 // al_draw_textf(ttf_font,al_map_rgb(255,255,0), 16, 32,0,"FPS: %f", (float)fps);
117 al_flip_display();
118 redraw = false;
119 }
120 while (1)
121 {
122 ALLEGRO_EVENT ev;
123 al_wait_for_event(event_queue , &ev);
124 if (ev.type == ALLEGRO_EVENT_TIMER)
125 {
126 redraw = true;
127 }
128 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
129 {
130 exit = true;
131 break;
132 }
133 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
134 {
135 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
136 {
137 exit = true;
138 break;
139 }
140 }
141 if (al_is_event_queue_empty(event_queue))
142 {
143 break;
144 }// this way all events are used
145 }
146 }
147
148 al_destroy_timer(timer);
149 al_destroy_display(display);
150 al_destroy_event_queue(event_queue);
151
152 return 0;
153}
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
al_init_ttf_addon may fail. It returns bool so check the return value. Your font may not be loading properly either. Make sure you are running your program from the directory that contains it or else it will be looking for your font in a different folder. As for why you're only getting 1 line per second, I can't explain that. When I fixed your code and ran it, it worked at 120 frames per second, just like your fps is set to run at. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
verthex
Member #11,340
September 2009
|
Edgar Reynaldo said: Your font may not be loading properly either. I double checked it and it was in the wrong directory after all but it still doesn't work and I get 1 frame a second with no changes to the last code I posted in this thread. Quote: When I fixed your code and ran it, it worked at 120 frames per second, just like your fps is set to run at. It would be helpful if you could post that one. Does it have the text printing to the screen or is that the version before that change?
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
verthex said: It would be helpful if you could post that one. Does it have the text printing to the screen or is that the version before that change? Text output is included, and working properly. I double checked, and the following code runs at 120 fps for me (I used a different font, as I don't have your font handy) : 1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include <allegro5/allegro_primitives.h>
4#include <allegro5/allegro_font.h>
5#include <allegro5/allegro_ttf.h>
6
7//#include <allegro5/allegro_direct3d.h>
8
9const float FPS = 120.0f;
10
11int main(int argc, char **argv)
12{
13 ALLEGRO_DISPLAY *display = NULL;
14 ALLEGRO_BITMAP *bitmap = NULL;
15 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
16 ALLEGRO_TIMER *timer = NULL;
17 ALLEGRO_FONT *ttf_font;
18
19 bool exit = false;
20
21 if(!al_init())
22 {
23 fprintf(stderr, "failed to initialize allegro!\n");
24 return -1;
25 }
26
27 timer = al_create_timer(1.0 / FPS);
28 if(!timer)
29 {
30 fprintf(stderr, "failed to create timer!\n");
31 return -1;
32 }
33
34 int screen_width = 640;
35 int screen_height = 480;
36
37// al_set_new_display_flags(ALLEGRO_DIRECT3D);
38
39 display = al_create_display(screen_width, screen_height);
40
41 if(!display)
42 {
43 fprintf(stderr, "failed to create display!\n");
44 al_destroy_timer(timer);
45 return -1;
46 }
47
48 if (!al_init_primitives_addon())
49 {
50 fprintf(stderr, "failed to initiialize primitives addon!\n");
51 return -1;
52 }
53
54 if(!al_install_keyboard())
55 {
56 fprintf(stderr, "failed to initialize the keyboard!\n");
57 return -1;
58 }
59
60 event_queue = al_create_event_queue();
61
62 if(!event_queue)
63 {
64 fprintf(stderr, "failed to create event_queue!\n");
65 al_destroy_display(display);
66 al_destroy_timer(timer);
67 return -1;
68 }
69
70 bitmap = al_create_bitmap(screen_width, screen_height);
71 if(!bitmap)
72 {
73 fprintf(stderr, "failed to create bouncer bitmap!\n");
74 al_destroy_display(display);
75 al_destroy_timer(timer);
76 return -1;
77 }
78
79 al_register_event_source(event_queue, al_get_keyboard_event_source());
80 al_register_event_source(event_queue, al_get_display_event_source(display));
81 al_register_event_source(event_queue, al_get_timer_event_source(timer));
82
83 al_set_target_bitmap(bitmap);
84 al_clear_to_color(al_map_rgb(0,0,0));
85 al_set_target_bitmap(al_get_backbuffer(display));
86
87 al_flip_display();
88 al_start_timer(timer);
89
90 al_init_font_addon();
91 if (!al_init_ttf_addon()) {
92 fprintf(stderr , "failed to initialize the ttf addon.\n");
93 return -1;
94 }
95 ttf_font = al_load_ttf_font("verdana.ttf", 16, 0);
96 if (!ttf_font) {
97 fprintf(stderr , "failed to load font.\n");
98 return -1;
99 }
100
101
102 bool redraw = true;
103
104 double old_time = al_get_time();
105
106
107 while(!exit)
108 {
109 double new_time = al_get_time();
110 double fps = 1.0/(new_time - old_time);
111 old_time = new_time;
112
113 if (redraw)
114 {
115 int r = rand()%255;
116 int g = rand()%255;
117 int b = rand()%255;
118 int a = rand()%128 + 64;
119 int x1 = rand()%screen_width;
120 int x2 = rand()%screen_width;
121 int y1 = rand()%screen_height;
122 int y2 = rand()%screen_height;
123
124 al_set_target_bitmap(bitmap);
125 ALLEGRO_COLOR color = al_map_rgba(r*a/255,g*a/255,b*a/255,a);
126 al_draw_line(x1,y1,x2,y2,color,20.0);
127 al_set_target_backbuffer(display);
128 al_clear_to_color(al_map_rgb(0,0,0));
129 al_draw_bitmap(bitmap,0,0,0);
130 al_draw_filled_rectangle(16.0f , 32.0f , 156.0f , 52.0f , al_map_rgb(0,0,0));
131 al_draw_textf(ttf_font,al_map_rgb(255,255,0), 16, 32,0,"FPS: %f",(float)fps);
132 al_flip_display();
133 redraw = false;
134 }
135 while (1)
136 {
137 ALLEGRO_EVENT ev;
138 al_wait_for_event(event_queue , &ev);
139 if (ev.type == ALLEGRO_EVENT_TIMER)
140 {
141 redraw = true;
142 }
143 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
144 {
145 exit = true;
146 break;
147 }
148 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
149 {
150 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
151 {
152 exit = true;
153 break;
154 }
155 }
156 if (al_is_event_queue_empty(event_queue))
157 {
158 break;
159 }// this way all events are used
160 }
161 }
162
163 al_destroy_timer(timer);
164 al_destroy_display(display);
165 al_destroy_event_queue(event_queue);
166
167 return 0;
168}
If the above code doesn't work properly for you, try this out for a test : while (!quit) { al_clear_to_color(al_map_rgb(0,0,0)); double new_time = al_get_time(); float fps = 1.0f/(new_time - old_time); old_time = new_time; al_draw_textf(ttf_font , al_map_rgb(255,255,255) , 0.0 , 0.0 , "FPS : %f" , fps); al_flip_display(); if (!al_is_event_queue_empty()) {quit = true;} }
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
1
2
|