Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » UTF-16 to UTF-8 without allocation

This thread is locked; no one can reply to it. rss feed Print
UTF-16 to UTF-8 without allocation
torhu
Member #2,727
September 2002
avatar

I'm looking into doing a Unicode build of Allegro 5.1 on Windows. To support both ANSI and UNICODE builds, I'm thinking about adding this function:

#ifdef UNICODE
   char *_al_win_strconv(char *dest, const TCHAR *src, size_t destsize);
#else
   #define _al_win_strconv _al_sane_strncpy
#endif

In unicode builds it would convert from utf-16 to utf-8. Does Allegro have internal functions that I can base it on? Or does anyone have this and would be willing to contribute it to Allegro? I'm lazy today :P

Elias
Member #358
May 2000

You probably could use al_ustr_new_from_utf16 followed by al_ustr_to_buffer and al_ustr_free.

--
"Either help out or stop whining" - Evert

billyquith
Member #13,534
September 2011

Allegro is UTF-8 internally, so it is probably best to keep all text as UTF-8, especially if you only want to display non-ASCII characters on the screen (i.e. you don't need to use any other Windows unicode functions). You don't need a Unicode build of your game to display non-ASCII.

The Windows "Unicode" compile mode is to change from narrow/multi-byte to wide (UTF-16) chars, and is there for historical reasons (UCS-2). It is a misleading name; both modes support Unicode. They should be called "multi-byte" and "wide".

See: http://www.utf8everywhere.org

I changed (a fork of) GWEN over to using UTF-8 everywhere, which simplifies using text, and makes Allegro 5 text rendering more efficient (i.e. nothing needs doing). It only needs to be converted to wide for Windows, on the fly, which has a very low overhead. Before it was doing it the other way round, everything was wide and converted to narrow on the fly, but UTF-8 everywhere is a better plan.

If you'd like the code to "widen" and "narrow" the unicode strings, it is here: https://github.com/billyquith/GWEN/blob/gwork/gwen/src/Utility.cpp

I'm using the C++11 locale API. There are other alternatives.

My work is in the "gwork" branch. If you are using Allegro I'd suggest using this branch/fork of GWEN as it has better Allegro support. ;D

torhu
Member #2,727
September 2002
avatar

Elias said:

You probably could use al_ustr_new_from_utf16 followed by al_ustr_to_buffer and al_ustr_free.

I basically made a version of al_ustr_new_from_utf16 that takes a buffer and writes the output into that.

Allegro is UTF-8 internally, so it is probably best to keep all text as UTF-8, especially if you only want to display non-ASCII characters on the screen (i.e. you don't need to use any other Windows unicode functions). You don't need a Unicode build of your game to display non-ASCII.

This is about the Windows-specific part of the library, not a game...

Peter Wang
Member #23
April 2000

Bit late, but see also src/win/wunicode.c (though they allocate).

torhu
Member #2,727
September 2002
avatar

Yeah, I put my new function into that file.

Seems like I will be too busy with my studies to get much done the next few months, though :(

Go to: