Bitmap Font doesn't draw any character after try to write a not defined char

I was using al_draw_scaled to draw this string that has a 'á' NON ASCII Character, this char was not defined on my bitmap font.

1footer_trademark = "Mega Man X is á registered trademark of Capcom Co., Ltd.";

I wanted to draw the other characters instead got this 'error' :o

Edgar Reynaldo

Undefined behavior is undefined...

So, don't do that?

Peter Hull

What function are you using? al_draw_scaled ??
Please can you post some more code?

1void Text::draw() 2{ 3 ALLEGRO_TRANSFORM al_transform; 4 al_identity_transform(&al_transform); 5 al_scale_transform(&al_transform, _V_scale_x*bitmap_scale_x, _V_scale_y*bitmap_scale_y); 6 al_translate_transform(&al_transform,_V_pos_x,_V_pos_y); 7 al_use_transform(&al_transform); 8 al_draw_text(*_V_font,_V_color,0,0,_V_flag,_V_text.c_str()); 9 al_identity_transform(&al_transform); 10 al_use_transform(&al_transform); 11}

I'm using the function al_draw_text to draw the text _V_text with my font _V_font that was my bitmap font.

The problem is that i wanted to draw the text that the following joystick functions return me, and this values (for example in spanish) return me non-ascii characters like 'Botón 1'


Edgar Reynaldo

So use a bitmap font with those characters in it. ;)

Peter Hull

I tracked through the code a bit and it seems that Allegro uses only the ANSI variants of the APIs (I am assuming you're using Windows?) so it probably is returning the names with characters from whatever code page you are using. (presumably 1252 Ansi Latin 1)
So, as Edgar says, if you want ó to appear you need a glyph for it in your bitmap font, but
you also seem to be saying that it doesn't print out any subsequent characters once it's hit an invalid character (looking at the megaman screenshot). If so, this might be a bug; Allegro should print a 'fallback' character whenever it doesn't have a glyph but then carry on with the rest of the string.
Can you post your font bitmap ?


If the encoding is UTF16 then á will be encoded as \xe1\x00 and the "\x00" byte will terminate the string.

Peter Hull

If the encoding is UTF-16 then all ascii characters will be encoded as <something>\x00 and the \x00 byte will terminate the string.

Edgar Reynaldo
Peter Hull

Alright but has anyone go a bitmap suitable for feeding to al_grab_font_from_bitmap?


There is my Font that i was using to draw my text, may the 'fallback' character has a code on ASCII to set up with al_grab_font_from_bitmap. Or maybe this is the problem of an unicode that has the null character \x00.

I wanted to show the name of the button of the joystick that a user want to use in my game to configure the game controls, but imagine for example in spanish the button name return "Botón 5" with ó on another encoding, that allegro cannot find the ó and print at least something like "Botn 5", to doesnt break that controls configuration is working!

Peter Hull

I see it too. I will investigate.

Edgar Reynaldo

You could write a program to read in a ttf file, and output a bitmap font. In theory it should be simple enough.

Peter Hull

What happens is:
1. Get the control name via al_get_joystick_button_name or whatever
2. (assuming you're on windows) Allegro uses the Windows ANSI API (not the unicode one) and returns a string using the active code page (assuming it's windows-1252)
3. This string may contain characters in the range 128-255 - for example ó which is 0xD3
4. Calling al_draw_text (other text functions end up in the same place anyway)
5. Calls al_ref_cstr on the text to create a UTF-8 string
6. Calls al_draw_ustr on the UTF-8 string
7. Calls font->vtable->render which is color_render in font.c
8. Uses al_ustr_get_next to step through the string and renders each glyph.

Unfortunately al_ref_cstr does not 'create a UTF-8 string', it just references the data in a C string as if it were UTF-8. So, when al_ustr_get_next comes to the 0xD3, it sees this as an invalid UTF-8 code, returning '-2', and this in turn causes color_render to stop. Hence no more characters are printed.

To solve this, you would need to use the Win32 API to convert the windows-1252 strings to genuine UTF-8, or Allegro would have to be modified, either:

  • Use unicode APIs for the joystick

  • Cause color_render to skip invalid UTF-8 sequences (printing the 404 character)

In any case the docs for al_draw_text and friends should be a bit more specific on the input - they take UTF-8 and not just any C-string.

If anyone has any more thoughts please post them - might be worth filing an issue on github for this.

Example program:

1#include <allegro5/allegro.h> 2#include <allegro5/allegro_font.h> 3#include <allegro5/allegro_image.h> 4 5int ranges[] = { 6 0x0020, 0x007F, /* ASCII */ 7}; 8const char* GOOD="Boton GOOD"; 9const char* BAD="Bot\323n BAD"; 10 11int main(void) { 12 al_init(); 13 al_init_font_addon(); 14 al_init_image_addon(); 15 ALLEGRO_DISPLAY* display = al_create_display(640,480); 16 ALLEGRO_BITMAP* mmx = al_load_bitmap("mmx12_font_f1.png"); 17 ALLEGRO_FONT* font = al_grab_font_from_bitmap(mmx, 1, ranges); 18 al_draw_text(font, al_map_rgb(0x80, 0xff, 0x80), 5.0f, 5.0f, 0, GOOD); 19 al_draw_text(font, al_map_rgb(0xff, 0x80, 0x80), 5.0f, 50.0f, 0, BAD); 20 al_flip_display(); 21 al_rest(10); 22 return 0; 23}


Thread #617697. Printed from