Enable SDL_ttf to support Chinese Characters

Source: Internet
Author: User

By default, you can use TTF_RenderUTF8_Blended () function to render UTF8 text to the video window, but because the local Windows Chinese environment is not unicode encoding but gbk encoding, this involves multi-byte (dubyte) it is a problem to convert to a wide byte. Therefore, you need to use a small function to convert the data to be output to SDL_tff. Note that the function used must be used in windows. h header file

 

char* localeToUTF8(char* src){    char* buf = NULL;    int nRetLen = 0;        wchar_t* unicode_buf = NULL;    // *function: MultiByteToWideChar (Maps a character string to a UTF-16 (wide character) string.)    // - UINT CodePage (Code page to use in performing the conversion. )    //                CP_ACP: The system default Windows ANSI code page.    // - DWORD dwFlags (Flags indicating the conversion type)    //                0:    // - LPCSTR lpMultiByteStr (Pointer to the character string to convert.)    //                src: the word that you want to conver    // - int cbMultiByte (you want to process size of lpMultiByteStr)    //                -1:  the function processes the entire input string, including the terminating null character. when the input string    //                            not contains terminating null character, it will failure.    // - LPWSTR lpWideCharStr (Pointer to a buffer that receives the converted string.)    //                NULL: no receives WideChar.    // - int cchWideChar (size of lpWideCharStr)    //                0: set the paramter for the function returns the required buffer size.    // * return value : because of cchWideChar is 0, so returns the required buffer size of lpWideCharStr    nRetLen = MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0);    //  allocate space for unicode_buf    unicode_buf = (wchar_t*)malloc((nRetLen+1) * sizeof(wchar_t));    // covert the src to utf-8, and store in unicode_buf    MultiByteToWideChar(CP_ACP, 0, src, -1, unicode_buf, nRetLen);    // *function: WideCharToMultiByte (Maps a UTF-16 (wide character) string to a new character string. )    // - UINT CodePage (Code page to use in performing the conversion. )    //                CP_UTF8: With this value set, lpDefaultChar and lpUsedDefaultChar must be set to NULL.    // - DWORD dwFlags (Flags indicating the conversion type. )    //                0 :    // - LPCWSTR lpWideCharStr (Pointer to the Unicode string to convert.)    //                unicode_buf : the word that you want to conver    // - int cchWideChar (you want to process size of lpWideCharStr)    //                -1: the function processes the entire input string, including the terminating null character. when the input string    //                        not contains terminating null character, it will failure.    // - LPSTR lpMultiByteStr (Pointer to a buffer that receives the converted string.)    //                NULL : no receives MultiByteStr.    // - int cbMultiByte (size of lpMultiByteStr)    //                0: set the paramter for the function returns the required buffer size.    // - LPCSTR lpDefaultChar (Pointer to the character to use if a character cannot be represented in the specified code page. )    //                NULL : For the CP_UTF7 and CP_UTF8 settings for CodePage, this parameter must be set to NULL.    // - LPBOOL lpUsedDefaultChar (Pointer to a flag that indicates if the function has used a default character in the conversion.)    //                NULL : For the CP_UTF7 and CP_UTF8 settings for CodePage, this parameter must be set to NULL.    nRetLen = WideCharToMultiByte(CP_UTF8, 0, unicode_buf, -1, NULL, 0, NULL, NULL);    //  allocate space for buf    buf = (char*)malloc(nRetLen+1);    WideCharToMultiByte(CP_UTF8, 0, unicode_buf, -1, buf, nRetLen, NULL, NULL);    // release space of unicode_buf    free(unicode_buf);    return buf;}

In this function, convert the string from multi-byte ASCII encoding to unicode wide byte encoding, and convert the unicode wide byte to UTF8 multi-byte encoding, because SDL_ttf does not support width therefore, unicode serves as a bridge between the two. After the conversion, you can release the unicode wide BYTE variable in the intermediate conversion process. Here, the GBK Chinese character encoding it is actually an extended ASCII code,

 

 

References:

Http://www.cnblogs.com/bombless/archive/2010/11/21/SDL-using-chinese-character.html

Http://www.phpweblog.net/fuyongjie/archive/2009/03/11/6374.html

Http://blog.csdn.net/shellching/article/details/5316442

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.