Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [al_draw_text] really slow

This thread is locked; no one can reply to it. rss feed Print
[al_draw_text] really slow
nicolas_l
Member #13,340
August 2011

Hi, i'm having problems with displaying text with Allegro 5. Whenever i display a text in screen, the framerate drops from ~60 to ~12.

Here is an example i made where this is happening:

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_ttf.h> 5 6const int FPS = 60; 7int main() 8{ 9 al_init(); 10 11 al_init_font_addon(); 12 al_init_ttf_addon(); 13 al_install_keyboard(); 14 15 ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue(); 16 ALLEGRO_TIMER* timer = al_create_timer(1.0 / FPS); 17 al_register_event_source(event_queue, al_get_keyboard_event_source()); 18 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 19 20 ALLEGRO_DISPLAY *display = display = al_create_display(640,480); 21 ALLEGRO_FONT *font = al_load_ttf_font("ARIAL.ttf",20,0 ); 22 23 ALLEGRO_BITMAP* text_img = al_create_bitmap(al_get_text_width(font,"HelloWorld!"), al_get_font_ascent(font)); 24 al_set_target_bitmap(text_img); 25 al_clear_to_color(al_map_rgb(0,0,0)); 26 al_draw_text(font,al_map_rgb(255,255,255), 0,0,0,"HelloWorld!"); 27 al_set_target_bitmap(al_get_backbuffer(display)); 28 29 char is_pressed_space = false; 30 char is_pressed_enter = false; 31 32 int TT = time(NULL); 33 int fps=0, fps_temp=0; 34 35 al_clear_to_color(al_map_rgb(0,0,0)); 36 37 al_flip_display(); 38 al_start_timer(timer); 39 40 int redraw = false; 41 while(1){ 42 43 44 ALLEGRO_EVENT ev; 45 al_wait_for_event(event_queue, &ev); 46 if(ev.type == ALLEGRO_EVENT_TIMER) { 47 redraw = true; 48 } 49 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 50 { 51 if (ev.keyboard.keycode==ALLEGRO_KEY_SPACE) 52 is_pressed_space=true; 53 if (ev.keyboard.keycode==ALLEGRO_KEY_ENTER) 54 is_pressed_enter=true; 55 } 56 if(ev.type == ALLEGRO_EVENT_KEY_UP) 57 { 58 if (ev.keyboard.keycode==ALLEGRO_KEY_SPACE) 59 is_pressed_space=false; 60 if (ev.keyboard.keycode==ALLEGRO_KEY_ENTER) 61 is_pressed_enter=false; 62 if (ev.keyboard.keycode==ALLEGRO_KEY_ESCAPE) 63 break; 64 } 65 66 if(redraw && al_is_event_queue_empty(event_queue)) 67 { 68 redraw = false; 69 70 al_clear_to_color(al_map_rgb(0,0,0)); 71 if (is_pressed_space) 72 al_draw_textf(font,al_map_rgb(255,255,255), 100,100,0,"FPS: %d",fps); 73 if (is_pressed_enter) 74 al_draw_bitmap(text_img, 200, 200, 0); 75 al_flip_display(); 76 77 fps_temp++; 78 int tt = time(NULL); 79 if (tt != TT) 80 { 81 TT=tt; 82 fps = fps_temp; 83 fps_temp=0; 84 printf("%d\n",fps); 85 } 86 } 87 88 89 90 } 91 al_destroy_display(display); 92 return 0; 93}

When nothing is being shown the fps is 60, and it doesn't change when i show the bitmap, but when i show the text it is 12-13.

I'm linking with liballegro-5.0.4-monolith-md-debug.a, but i've also tried with the other options, but no change.
I'm using a slow netbook (1.4Ghz), but i've never experienced anything like this with Allegro4

Matthew Leverton
Supreme Loser
January 1999
avatar

What video card does it have? Attaching the allegro.log file may help.

If you draw this text beforehand (to cache things), does it speed it up: "FPS: 0123456789"?

nicolas_l
Member #13,340
August 2011

it has a integrated Intel GMA500 (it's not a good card, and it sucks in openGL (could that be the cause?))

drawing the text before doesn't change anything

I've attached allegro.log

Matthew Leverton
Supreme Loser
January 1999
avatar

It's using Direct3D at the moment. You can try OpenGL:

#include <allegro5/allegro_opengl.h>

//...

al_set_new_display_flags(ALLEGRO_OPENGL);
al_create_display( ... );

nicolas_l
Member #13,340
August 2011

Ok. But with OpenGL the program won't start (but that is becouse the computer, i've had problems with other openGL games (advice: never buy an netbook with gma500)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

nicolas_l said:

   ALLEGRO_DISPLAY *display = display = al_create_display(640,480);

The compiler says that line is undefined - one too many 'display's there.

I compiled your program against Allegro 5.1 SVN and had no problems at all using consola.ttf. It all ran at 60 fps. This was on a 2.1Ghz dual core Turion 64 laptop with integrated ATI Radeon X1270 though.

Oh, and please fix your code formatting next time - it's really hard to read.

Go to: