Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_draw_text() Performance Issues

This thread is locked; no one can reply to it. rss feed Print
al_draw_text() Performance Issues
Cherubrad
Member #15,544
March 2014

Hey! I'm running into some major performance issues with Allegro 5 when using the al_draw_text() function.

I know my problem is arising because I'm forced to use:

#SelectExpand
1al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); 2//load fonts 3al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);

Otherwise my text will be missing characters. Currently, scrolling text melts my CPU.

Does anyone have any ideas how I could work through this hurdle? As far as I'm aware my openGL and Allegro are both up to date.

#SelectExpand
1 2int main() 3{ 4 5 struct Data { 6 ALLEGRO_FONT *f1, *f2; 7 ALLEGRO_DISPLAY *display; 8 ALLEGRO_EVENT_QUEUE *queue; 9 ALLEGRO_COLOR white; 10 11 12 } data; 13 14 //Initializing Allegro 15 al_init(); 16 al_init_font_addon(); 17 al_init_ttf_addon(); 18 al_install_keyboard(); 19 20 21 data.white = al_map_rgb(255,255,255); 22 23 24 const char *font_file = "arial.ttf"; 25 26 //Setting OPENGL 27 al_set_new_display_flags(ALLEGRO_OPENGL); 28 29 //Creating my display 30 data.display = al_create_display(1024, 576); 31 32 //PROBLEM ARISES HERE: 33 //Text wont draw on video 34 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); 35 data.f1 = al_load_ttf_font(font_file, 25, 0); 36 data.f2 = al_load_ttf_font(font_file, 15, 0); 37 al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); 38 39 40 //creates the queue 41 data.queue = al_create_event_queue(); 42 43 bool done = false; 44 int y = 600; 45 int moveSpeed = 5; 46 47 ALLEGRO_KEYBOARD_STATE keyState; 48 ALLEGRO_EVENT event; 49 ALLEGRO_TIMER *timer = al_create_timer(1.0 / 24); 50 al_register_event_source(data.queue, al_get_timer_event_source(timer)); 51 al_register_event_source(data.queue, al_get_display_event_source(data.display)); 52 al_start_timer(timer); 53 54 while(!done) 55 { 56 al_wait_for_event(data.queue, &event); 57 if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 58 { 59 done = true; 60 } 61 if(event.type == ALLEGRO_EVENT_TIMER) 62 { 63 al_get_keyboard_state(&keyState); 64 if(al_key_down(&keyState, ALLEGRO_KEY_ESCAPE)) 65 { 66 done = true; 67 } 68 al_clear_to_color(al_map_rgb(0, 0, 0)); 69 //Loop to draw same line of text 10 times 70 for(int i =0; i < 10; i++) 71 al_draw_text(data.f1, data.white, 512, y+(30*i), ALLEGRO_ALIGN_CENTER, "CREDDSDADSADSADSADAITS"); 72 al_flip_display(); 73 //I'm just moving it 1 pixel per update 74 y--; 75 } 76 if(y <= 0) 77 break; 78 79 80 81 } 82 83 84 85 al_destroy_font(data.f1); 86 al_destroy_font(data.f2); 87 88 al_destroy_display(data.display); 89 al_destroy_event_queue(data.queue); 90 91 return 0; 92 93}

beoran
Member #12,636
March 2011

Which version of Allegro are you using? This might be a bug, so please try the latest possible version of allegro 5 and see if the error persists. Also, what do the Allegro example programs do? Do they work correctly?

#00JMP00
Member #14,740
November 2012

Hi!

I don't know exactly what you are doing, but it seems as if there was a logic bug in your textroutine;

#SelectExpand
1 270 for(int i =0; i < 10; i++) 3 71 al_draw_text(data.f1, data.white, 512, y+(30*i), ALLEGRO_ALIGN_CENTER, "CREDDSDADSADSADSADAITS"); 4 72 al_flip_display(); 5 73 //I'm just moving it 1 pixel per update 6 74 y--; 7 75 }

The text y-position is incremented you the for loop y+(30*i), but decremented at the same time. Is this by accident or should it be like that???

Pho75_
Member #12,377
November 2010

Try using video bitmaps and pre-cache JUST the font glyphs that you need before your game loop.

font = al_load_ttf_font(font_file, 25, 0);
al_draw_text(font, al_map_rgba(0,0,0,0), 0, 0, 0, "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");

I've run into font-caching problems with Allegro5 in the past:
http://www.allegro.cc/forums/thread/611175

Cherubrad
Member #15,544
March 2014

@beoran It writes multiple lines of the same string as text, moving it upwards at ~24 pixels per second. Think credits at the end of a movie. It only serves as a test.

@#00JMP00 It's a simple loop to draw multiple lines of the same text (10 in this example) that are right below each other. The y-- simply moves the entire body of text -24 pixels per second (assuming my CPU didn't melt from drawing text...). I've run a more optimal version keep track of time vs movement, but it also runs into the same issue after a handful of text lines are being displayed.

@Pho75_
Thank you, I'll give that a try. Just one question, if my game runs / calls multiple different loops (for different event related displays) will that affect the cache? They're all sharing the same queue / timer.

Pho75_
Member #12,377
November 2010

Cherubrad said:

Thank you, I'll give that a try. Just one question, if my game runs / calls multiple different loops (for different event related displays) will that affect the cache? They're all sharing the same queue / timer.

I'm not sure I understand your question.
By "caching" I meant that Allegro allocates bitmaps for the font glyphs
and renders the characters on-demand when you call al_draw_text.
So do it in the beginning right after you load the font, so it's not
happening during the loop where you render your graphics to screen.

If you're asking if you can use a video bitmap or memory bitmap
in multiple display windows, the answer is yes.
It has no relation to whether displays and/or timers share an event queue or not.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Bitmaps in A5 are connected to a display, and are generally compatible with that display only - you have to make the selected display active before loading any fonts or images you want to use on that display. Read more here about how to make a display active, with al_set_target_bitmap and you can check with al_get_current_display().

Go to: