Caching texts
Max Savenkov

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:

int x=0,y=0,w=0,h=0;
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:

al_draw_text( font, color, x, y, ALLEGRO_ALIGN_LEFT, text );
al_draw_bitmap( bmp, x, y, 0 );

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 :)

Dizzy Egg

In point 1) you draw the text using -x and -y, so if y was 1 you would draw the text at -1 on the bitmap and then at 1 on the display?

Gideon Weems

I think that's it, too. Take the minuses out of your coordinates for al_draw_text and see what happens.

Also, you don't get any compiler warnings about the implicit cast to float? It's been too long...

Max Savenkov
Dizzy Egg said:

In point 1) you draw the text using -x and -y, so if y was 1 you would draw the text at -1 on the bitmap and then at 1 on the display?

That helps with point 1), thanks! And I looked at the source at solved 2). I just have to use al_get_text_length instead of bbw return parameter of al_al_get_text_dimensions to adjust X of al_draw_bitmap, because they differ quite a lot.

Thread #610883. Printed from Allegro.cc