I'm thinking about caching static text which are getting drawn on screen. I.e. just draw such text into a bitmap one time and never again call al_draw_text for it, unless something changes.
But I ran into two problems.
1) How to determine size of caching bitmap and string position inside it? For now, I do this:
al_get_text_dimensions( font, text, &x, &y, &w, &h );
ALLEGRO_BITMAP *bmp = al_create_bitmap( w, h );
ScopedTarget t( bmp );
al_draw_text( font, color, -x, -y, ALLEGRO_ALIGN_LEFT, text );
Then, I draw a cached/non-cached text to compare the results:
And the cached text ends up is off by TWO pixels to the left! So, obviously, I'm doing something wrong.
2) Even worse things happen, when I want to draw RIGHT or CENTER-aligned text. The cached bitmap stays the same, of course (it shouldn't be affected by alignment, should it?). But how to draw it, so the result would be the same? For now, I do this:
al_draw_text( font, color, x, y, ALLEGRO_ALIGN_RIGHT, text );
al_draw_bitmap( bmp, x - al_get_bitmap_width(bmp), y, 0 );
al_draw_text( font, color, x, y, ALLEGRO_ALIGN_CENTER, text );
al_draw_bitmap( bmp, x - al_get_bitmap_width(bmp)/2,0f, y, 0 );
But in both cases, cached text is off by a different amount of pixels, so I think I'm missing something here too.
Can anyone help me with this? I think I'm missing something very simple, or either very complex