The first thing to make clear is that COCOS2DX is in support of Chinese. Because its encoded format is UTF-8 (no signature)-This is just the conclusion I found online, not from the official, so I can't be sure, but it should be correct.
So, why in VS2012 environment, COCOS2DX can't display Chinese correctly?
This is because the encoding format is inconsistent. the default encoding format for the Chinese version of VS2012 is gb2312-80 (I'm not sure what the English version is, in short, it's not UTF-8). In the GB2312-80 encoding format, one Chinese occupies 2 bytes, while in the UTF-8 format, one Chinese is 3 bytes. This is bound to appear garbled.
Find out the reason, the way is very clear. Now I have found two simple ways: 1. Change the encoding format of the source file; 2. Make the Cocos2d-x program read Chinese characters from UTF-8 (unsigned) formatted text. Here's how:
1. Change the encoding format of the source file
The VS2012 Chinese version provides a way to directly modify the default encoding format of the source file, select the Advanced save option, file, and then select Encode as Unicode (UTF-8 unsigned). After that, the source file is encoded in the UTF-8 format, which naturally enables cocos2d-x to parse correctly. And the experiment proves that using Cocos run-p Android to run on Android, also can display Chinese correctly.
2. Reading Chinese characters from a file
Online access to information, most recommend this method. You can use Fileutils's singleton object to read text files, list files. Its getvaluevectorfromfile, Getvaluemapfromfile, Getstringfromfile and other methods can easily extract the text content. Just set the encoding format of the file you want to read to UTF-8 (no signature) to get the Chinese correctly.
TIPS: How do I get a single Chinese character from an English string?
Like traversing a normal string, it is sometimes necessary to traverse a Chinese string. It is not possible to use the normal traversal method, this is because a Chinese occupies three bytes, read should be prepared four bytes to hold it, the last byte is ' s ', so that the correct read to a Chinese character. The code is as follows:
// BUF read it to you. Char buf[4] = {0" hello "3); log ( " One Chinese char:%s ", buf);
Log will output: you
Solve the problem that COCOS2DX does not support Chinese in the VS2012 Chinese edition environment