Hi, I'm come from Zhengzhou City Henan Province China.I have one big problem about how to display the Chinese in allegro program. I have read the allegro manual . It is said that allegro is supported the TTF and UTF-8. I think use the TTF can display chinese, however it is can't display any chinese words? Who can help me? I am sorry my English is so poor, I hope you can understand my problem! below is my code and I use the VC10(VS2010).thx everyone!
Make sure you save your source file as UTF-8
Make sure your font supports those characters
Please put your code in <code></code> tags.
That said, make sure your files are saved in UTF-8, not any ASCII code page or other versions of unicode. (and what Matthew just said...)
One other thing, you can use al_rest instead of Sleep.
Matthew Leverton:
Make sure you save your source file as UTF-8
Make sure your font supports those characters
______
I have to convert that source file to UTF8,but still can not display Chinese characters.
What other ways can dispaly Chinese character?
If it still isn't showing up, make sure the font you loaded has the exact characters you're trying to show.
In Visual Studio, go to File -> Advanced Save Options. Make sure the encoding is set to UTF8.
It's suspiciously incredible how accurate the Google translator is when translating that file. It's like you have translated from English to Chinese.
I think it's more that Google's translation looks at the context. Then when it knows the text is about programming, it uses a matching dictionary.
The following code works fine for me on Linux.
I can see the Chinese red text.
I compile it with gcc like this:
gcc forum_2.c -I /usr/local/include -L /usr/local/lib -lallegro_color -lallegro_font -lallegro_ttf -lallegro -o forum2
I have to put the font in the same place as the forum2 executable.
SIMYOU.TTF
What do you see? Please check if you are using utf-8, and if your font file is OK.
Visual Studio is probably messing with your source file. I had this problem when I still used it(Since then I moved to Code::Blocks, and lived happilly ever after)
This is counter-intuitive, but try saving your source file as ANSI encoding(whithout converting it, so chinese characters will appear as gilberish), and compiling again with VC. Worked for me.
EDIT: Unfortunately, at the time, this method is the only way I got my language to work properly using MSVC. Check this thread for more info.
......
I have tried to have done following things:
1.Put the "SIMYOU.TTF" file in the same place as the executable.
2.I'm sure this font file to support chinese.
3.In Visual Studio, go to File -> Advanced Save Options. Make sure the encoding is set to UTF8.
4.I deleted this source file and rewrite a source file.
However,it can't dispaly chinese yet,only display english character.
I write in chinese and english a string,but only display endlish part.
ustr =al_ustr_new("hello你好!");
Your best options are to move your strings into an external file, or use gcc.
The problem is that MSVC converts string literals from the source file encoding (UTF-8) into the local encoding (e.g. GB2312). There are ways to trick it not to do that, but it is not reliable.
Thank you very much for your help
I have solved this problem.
I hope allegro's next version support this function..
eg:
void al_draw_text(const ALLEGRO_FONT *font,
ALLEGRO_COLOR color, float x, float y, int flags,
wchar_t const *text)
Just a note, but unless you absolutely need to, there's no reason to use WinMain. Allegro lets you use a normal main() function, even on windows. Using WinMain makes your program less portable.
I hope allegro's next version support this function..
eg:
void al_draw_text(const ALLEGRO_FONT *font,
ALLEGRO_COLOR color, float x, float y, int flags,
wchar_t const *text)
It's not hard to make:
al_draw_text(const ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, flags, uint16_t const *text) { ALLEGRO_USTR * ustr = al_ustr_new_from_utf16(text); assert(ustr); al_draw_text(font, color, x, y, flags, ustr); al_ustr_free(ustr); }
Untested code, of course
http://alleg.sourceforge.net/a5docs/5.0.8/utf8.html#utf-16-conversion
Also, never use wchar_t. While wchar_t is UTF16 with MSCV, in general it's not.
It's not hard to make:
al_draw_text(const ALLEGRO_FONT *font, ALLEGRO_COLOR color,
float x, float y, flags, uint16_t const *text)
{
ALLEGRO_USTR * ustr = al_ustr_new_from_utf16(text);
assert(ustr);
al_draw_text(font, color, x, y, flags, ustr);
al_ustr_free(ustr);
}
Untested code, of course
http://alleg.sourceforge.net/a5docs/5.0.8/utf8.html#utf-16-conversion
Also, never use wchar_t. While wchar_t is UTF16 with MSCV, in general it's not.
Thanks!
It is correct way that use al_ustr_new_from_utf16(...) .;)
I enjoyed the lively atmosphere of this forum and helpfulness you.
I'm hard studying english now.