Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » al_draw_text() and special characters

This thread is locked; no one can reply to it. rss feed Print
al_draw_text() and special characters
DrFail
Member #14,578
September 2012

Hey, i'm currently working on some sort of game-engine for a school project.
But i have some troubles with the al_draw_text() function. The problem is, that i can't print special characters (especially ä,ö and ü), which are used often in german.
I already tried stuff like char(129) or "\x81" but it didn't work.
I also found the function al_draw_ustr(), but i never did anything with unicode and i'm not sure how it works.
Is there a way to print these characters?

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'm far from expert, but I'd think al_ustr_new_from_buffer() would work. You'd have to free the utf string yourself to avoid memory leaks.

They all watch too much MSNBC... they get ideas.

DrFail
Member #14,578
September 2012

#SelectExpand
1string text = "äöüÄÖÜ"; 2ALLEGRO_USTR* uText = al_ustr_new(text.c_str()); 3al_draw_ustr(myFont, myColor, 42, 42, 0, uText); 4al_ustr_free(uText);

This would do the same as al_draw_text(), but print the special characters?

SiegeLord
Member #7,827
October 2006
avatar

al_draw_text can print special characters just fine (e.g. this is demonstrated in the ex_tff). There are two possible issues which might break it in your case.

1. Your source file must be encoded using UTF-8
2. Your font must have glyphs for those special characters

Now... you say that you tried explicit unicode characters... so perhaps it's the second issue?

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

DrFail
Member #14,578
September 2012

When I use
al_draw_text(myFont, myColor, 42, 42, 0, "test_ä_end");
the output is just "test_". The rest is missing.
But I'm pretty sure that the font (arial.ttf) has those letters.

Maybe it's the first case. I'm using Dev-C++ 5.3, but i don't know if it uses UTF-8.

Is there a way to find out?
Or any other possible solution?

SiegeLord
Member #7,827
October 2006
avatar

DrFail said:

Is there a way to find out?
Or any other possible solution?

Nobody uses DevCpp, so there's no way to find out. The workabout that always works is to load the strings from a separate file which is UTF-8 encoded (you can be sure of that by using an editor that explicitly allows the option). This is in fact what ex_ttf does for MSVC compatibility.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

DrFail
Member #14,578
September 2012

I just messed around a little and luckily found a solution :)
With the escape sequence "\u" it's possible to print the letters.

#SelectExpand
1string test = "test \u00E4\u00C4\u00F6\u00D6\u00FC\u00DC end"; 2al_draw_text(font, color, 42, 42, 0, test.c_str());

This does exactly what i wanted.

SiegeLord said:

... load the strings from a separate file which is UTF-8 encoded.

Good you mentioned that.
This makes it possible to get text from files with with special characters and print it on the screen, without extra code.

Thanks for the help!

Go to: