Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » FPS counter (Allegro 5)

Credits go to J-Gamer for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
FPS counter (Allegro 5)
verthex
Member #11,340
September 2009
avatar

If the above code doesn't work properly for you, try this out for a test :

The above code did work but I got .5 frames per second on average.

The code after also worked after I made some changes but it just exits after printing the fps.

while (!exit)
   {
    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,0), 16, 32,0,"FPS: %f",(float)fps);
    //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(event_queue))
      {exit = true;}
   }

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Okay, change the is_event_queue_empty line to this :

if (!al_is_event_queue_empty()) {
   ALLEGRO_EVENT ev;
   al_wait_for_event(&ev);
   if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
      break;
   }
}

And see if it still runs slowly for you.

Other ideas :
1) Go back to the regular code, and check the bitmap flags of the bitmap you create and make sure it is a video bitmap :

bitmap = al_create_bitmap(screen_width , screen_height);
if (!bitmap) {
   printf("failed to create bitmap.\n");
   return 1;
}
if (!(al_get_bitmap_flags(bitmap) & ALLEGRO_VIDEO_BITMAP)) {
   printf("Not a video bitmap!\n");
}

2) Link to the debugging Allegro library and then run your program. Then post the allegro.log file it produces. The devs may be able to see something of why it's so slow in it.

verthex
Member #11,340
September 2009
avatar

1) Go back to the regular code, and check the bitmap flags of the bitmap you create and make sure it is a video bitmap :

Tried it and I didn't get any text.

#SelectExpand
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.0f; 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 al_set_new_display_flags(ALLEGRO_OPENGL); 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 if (!(al_get_bitmap_flags(bitmap) & ALLEGRO_VIDEO_BITMAP)) 77 { 78 printf("Not a video bitmap!\n"); 79 } 80 81 al_register_event_source(event_queue, al_get_keyboard_event_source()); 82 al_register_event_source(event_queue, al_get_display_event_source(display)); 83 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 84 85 al_set_target_bitmap(bitmap); 86 al_clear_to_color(al_map_rgb(0,0,0)); 87 al_set_target_bitmap(al_get_backbuffer(display)); 88 89 al_flip_display(); 90 al_start_timer(timer); 91 92 al_init_font_addon(); 93 if (!al_init_ttf_addon()) { 94 fprintf(stderr , "failed to initialize the ttf addon.\n"); 95 return -1; 96 } 97 ttf_font = al_load_ttf_font("times.ttf", 16, 0); 98 if (!ttf_font) { 99 fprintf(stderr , "failed to load font.\n"); 100 return -1; 101 } 102 103 104 bool redraw = true; 105 106 double old_time = al_get_time(); 107 108 while(!exit) 109 { 110 double new_time = al_get_time(); 111 double fps = 1.0/(new_time - old_time); 112 old_time = new_time; 113 114 if (redraw) 115 { 116 int r = rand()%255; 117 int g = rand()%255; 118 int b = rand()%255; 119 int a = rand()%128 + 64; 120 int x1 = rand()%screen_width; 121 int x2 = rand()%screen_width; 122 int y1 = rand()%screen_height; 123 int y2 = rand()%screen_height; 124 125 al_set_target_bitmap(bitmap); 126 ALLEGRO_COLOR color = al_map_rgba(r*a/255,g*a/255,b*a/255,a); 127 al_draw_line(x1,y1,x2,y2,color,20.0); 128 al_set_target_backbuffer(display); 129 al_clear_to_color(al_map_rgb(0,0,0)); 130 al_draw_bitmap(bitmap,0,0,0); 131 al_draw_filled_rectangle(16.0f , 32.0f , 156.0f , 52.0f , al_map_rgb(0,0,0)); 132 al_draw_textf(ttf_font,al_map_rgb(255,255,0), 16, 32,0,"FPS: %f",(float)fps); 133 al_flip_display(); 134 redraw = true; 135 } 136 while (1) 137 { 138 ALLEGRO_EVENT ev; 139 al_wait_for_event(event_queue , &ev); 140 if (ev.type == ALLEGRO_EVENT_TIMER) 141 { 142 redraw = true; 143 } 144 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 145 { 146 exit = true; 147 break; 148 } 149 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 150 { 151 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 152 { 153 exit = true; 154 break; 155 } 156 } 157 if (al_is_event_queue_empty(event_queue)) 158 { 159 break; 160 }// this way all events are used 161 } 162 } 163 164 al_destroy_timer(timer); 165 al_destroy_display(display); 166 al_destroy_event_queue(event_queue); 167 168 return 0; 169}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Before you try that (messing with your dlls) , try this :

al_set_new_display_flags(ALLEGRO_OPENGL);
display = al_create_display(screen_width , screen_height);
// As before ........

If you're having trouble with Direct3D and DirectDraw, I would suggest downloading and installing the latest video drivers for your system, from your computer manufacturer if they offer them, or from ati.amd.com or from www.nvidia.com depending on what kind of graphics card you have.

verthex
Member #11,340
September 2009
avatar

Before you try that (messing with your dlls) , try this :

That fixes it! Thanks.

I prefer OpenGL anyways even when I use Irrlicht I always pick that as my driver. But now my FPS counter is changing so fast I can't even read it.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

 1   2 


Go to: