(turn) Let Sdl_ttf render Chinese characters in SDL engineering

Source: Internet
Author: User

In my own opinion, many of these are misunderstandings. Here's my point of view, based on my use of SDL under Windows.

As a cross-platform library, SDL has a unique place in terms of character. That is, its runtime supports a character encoding of UTF8, rather than a variety of native character encodings common in Windows. For example, the Chinese version of Windows using the codepage 936, also known as GBK, is actually based on the GB2312 character set of EUC-CN encoding, based on the expansion of a subset of CJK in the Unicode character set produced by the development of a character hardware encoding method.

For example, sdl_wm_setcaption this function, it takes a UTF8 string to set the caption of the window.

For some reason, I did not find a description of SDL's functions using UTF8 encoding in the SDL documentation. This may be one of the reasons for the misunderstanding of SDL's Chinese compatibility.

In any case, this design makes sense because it is not only compatible with platforms that use different local character sets, but also avoids the ucs2\ucs4 contention that causes widespread portability problems.

With regard to the rendering of Chinese characters, a common SDL extension is SDL_TTF, which can support the rendering of TrueType fonts, which is undoubtedly very appealing.

One problem with the Windows version of the Sdl_ttf runtime is that it does not follow the SDL's habit of UTF8, but instead provides 3 similar functions to render the text.

That is, the three functions of Ttf_renderutf8_solid,ttf_rendertext_solid and Ttf_renderunicode_solid.

Where ttf_rendertext_solid can render ASCII strings correctly, and Ttf_renderutf8_solid and ttf_renderunicode_solid are used to render UTF8 encoded and UCS2 encoded strings, respectively.

When using SDL in Windows, if you need to render Chinese characters, you need to convert the local character set to the UTF8 character set.

Of course, it's more convenient if you convert from the Unicode character set to the UTF8 character set. In addition, when using Unicode characters in the MINGW compilation environment, it is important to remember to pass the parameter "-FINPUT-CHARSET=GBK" to the GCC compiler, otherwise it will prompt an illegal byte sequence.

In local character encoding (here we focus on the codepage 936 native character encoding used in Chinese windows), UTF8 character encoding and Unicode character encoding are 3 ways to convert from one to the other, you can use both WideCharToMultiByte and MultiByteToWideChar functions in Windows. Of course you can also use the C run-time library's wcstombs and MBSTOWCS, which are 2 more portable (which might be useful if you need to make a transition in a system other than Windows, but this is useless under Linux because it uses UTF8 encoding directly).

It's good to format strings with WideCharToMultiByte and MultiByteToWideChar in Windows, but if you use Wcstombs and Mbstowcs, It is important to note that Windows does not support the UTF8 format here (see the MSDN article about setlocale: http://msdn.microsoft.com/zh-cn/library/ x99tb11d.aspx), which is when we need ourselves to complete the conversion from Unicode to UTF8.

This question can refer to 2 articles:

Http://www.cppblog.com/jacky2019/archive/2007/03/08/19431.html

Http://blog.sina.com.cn/s/blog_473f16d001000406.html

To demonstrate a small program, I use a local character encoding instead of a Unicode string, and then convert it at run time, which translates directly from local encoding to UTF8 encoding.

This not only uses the Sdl_ttf, but also requires an JAS. ttf file, which needs to be copied in the Windows\Fonts folder of the system partition, the original file name is Msyh.ttf, copied to the folder where the program is located, and renamed Ya Black. Ttf. The main demonstration here is to use UTF strings correctly using files with Chinese in the file name.

Code1 #include <sdl/sdl.h>
2 #include <sdl/sdl_ttf.h>
3 #include <windows.h>
4 Char *localetoutf8 (char *src) {
5 static char *buf = NULL;
6 if (BUF) {
7 free (BUF);
8 buf = NULL;
9}
Ten wchar_t *unicode_buf;
one int nretlen = MultiByteToWideChar (cp_acp,0,src,-1,null,0);
Unicode_buf = (wchar_t*) malloc ((nretlen+1) *sizeof (wchar_t));
MultiByteToWideChar (Cp_acp,0,src,-1,unicode_buf,nretlen);
Nretlen = WideCharToMultiByte (cp_utf8,0,unicode_buf,-1,null,0,null,null);
BUF = (char*) malloc (nretlen+1);
WideCharToMultiByte (Cp_utf8,0,unicode_buf,-1,buf,nretlen,null,null);
(UNICODE_BUF);
return buf;
19}
20
int main (int argc,char *argv[]) {
Sdl_surface *screen = Sdl_setvideomode (640,480,32,sdl_swsurface);
Sdl_wm_setcaption (localeToUTF8 ("rendering Chinese characters in SDL!") "), NULL);
Ttf_init ();
Ttf_font *font = Ttf_openfont (localeToUTF8 ("Ya hei. TTF"), 28);
Sdl_color TextColor = {255, 255, 255};
if (!font) {
The MessageBox (0,0, "No", 0);
return-1;
30}
Sdl_surface *text = NULL;
Text = Ttf_renderutf8_solid (Font,localetoutf8 ("Chinese! "), TextColor);
Sdl_blitsurface (Text,null,screen,null);
Sdl_flip (screen);
Sdl_event Event;
(Sdl_pollevent (&event), Event.type! = sdl_quit);
PNS return 0;
38}
39

Here's a short, many function that explains the conversion from the local character set to the UTF8.

1 code--UTF8 to local character set2#include <stdio.h>3#include <stdlib.h>4#include <locale.h>5#include <string.h>6  Char*utf8tolocal (Char*src) {7wchar_t wbuf[ -];8     Static Charbuf[ -];9     Char*uchar = (Char*) wbuf;Ten     Char*PSRC =src; One     //initialize data to facilitate the generation of ASCII characters in Unicode Amemset (WBUF,0,sizeofwbuf); -      while(*psrc!=' /'){ -         if(*psrc <0){//if it is not an ASCII character theuchar[1] = ((psrc[0] &0x0f) <<4) + ((psrc[1] >>2) &0x0f); -uchar[0] = ((psrc[1] &0x03) <<6) + (psrc[2] &0x3f); -PSRC + =3;//for non-ASCII characters, the UTF8 encoding occupies 3 bytes -Uchar + =2;//wide characters in Windows are 2 bytes and should be changed to 4 under Linux +          } -         Else{ +uchar[0] = psrc[0];//for ASCII characters, you can copy directly APSRC + =1; atUchar + =2; -         } -     } -SetLocale (Lc_all,"");//The default character set is "C", where the local character set is changed -     //in the CP936 system, the above call is equivalent to setlocale (Lc_all, ". 936"); -Wcstombs (Buf,wbuf, -); in     //once the setlocale function is called correctly, we can safely use the wcstombs -     //it converts the character encoding format for us from Unicode to local character set to      returnbuf; + } -  the  intMain () * { $     Char*text =NULL;Panax NotoginsengText = Utf8tolocal ("\xe6\xb1\x89\xe5\xad\x97"); -printf"%s\n", text); the     return 0; +}

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <locale.h>
4 #include <string.h>
5 Char *utf8tolocal (char *src) {
6 wchar_t wbuf[100];
7 static char buf[100];
8 Char *uchar = (char*) wbuf;
9 char *psrc = src;
10//Initialize data to facilitate the generation of ASCII characters in Unicode
memset (wbuf,0,sizeof wbuf);
(*psrc!= ') {
if (*psrc < 0) {//If not ASCII characters
UCHAR[1] = ((psrc[0] & 0x0f) <<4) + ((Psrc[1] >>2) & 0x0f);
Uchar[0] = ((psrc[1] & 0x03) <<6) + (Psrc[2] & 0x3f);
PSRC + = 3;//for non-ASCII characters, UTF8 encoded in 3 bytes
Uchar + = 2;//windows with a width character of 2 bytes, under Linux should be changed to 4
18}
else{
Uchar[0] = psrc[0];//for ASCII characters, you can copy directly
PSRC + = 1;
Uchar + = 2;
23}
24}
SetLocale (Lc_all, "");//The default character set is "C", where the local character set is changed
26//In CP936 system, the above sentence is equivalent to setlocale (Lc_all, ". 936");
Wcstombs (buf,wbuf,100);
28//After correctly calling the setlocale function, we can safely use the wcstombs
29//It will convert the character encoding format for us, from Unicode to local character set
return buf;
31}
32
int main ()
34 {
char *text = NULL;
* Text = utf8tolocal ("\xe6\xb1\x89\xe5\xad\x97");
Notoginseng printf ("%s\n", text);
return 0;
39}
40

Here "\xe6\xb1\x89\xe5\xad\x97" is a string of UTF8 encoded, the content is "Chinese character".

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

(turn) Let Sdl_ttf render Chinese characters in SDL engineering

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.