The file created by visual stdio is automatically converted from gb2312 encoding to utf8 encoding.

Source: Internet
Author: User

Cocos2d-x is a good mobile game framework, but because of the use of visual stdio2008 in Chinese Windows System Default created class files, including. h and. cpp, its file encoding is gb2312. When these files are compiled on cygwin with ndk, although the compilation is no problem, but the Chinese will appear garbled during reading, this is a lack of cocos2d-x, for example, when cclabelttf displays Chinese characters, garbled characters may occur, and the spriteframebyname method of ccspriteframe cannot input Chinese characters. This is indeed a headache.

There are two solutions: one is to save the source code file as utf8 format, the other is to transcode when using Chinese characters, if you use the second method, for cross-platform, generally, the iconv library is used for encoding and conversion. But because iconv is based on lgpl protocol, cocos2d-x has been in the latest cocos2d-1.0.1-x-0.12.0 release iconv static library removed, the use of in fact does not recommend a large number of frequent calls to iconv encoding conversion, after all, the efficiency is reduced.

The second method is to convert the source code file encoding to utf8 encoding. The manual method is to have an advanced save option in the File menu of visual stdio, set the encoding to Unicode (utf8 with signature) and save it. However, this method is stupid and purely physical. I have studied how to modify the Class Wizard of visual stdio and want to change the file created by the Class Wizard to utf8 format. If you create a. h and. cpp file, you can use
NEWC ++ file in Visual Studio 9.0/VC/vcprojectitems. CPP and hfile. H is saved as utf8, so that each time a new one is created. H and. CPP will all be in utf8 format. However, generally, class files are created using the Class Wizard, so I wrote a small program that can convert the source code to the utf8 format. The Code is as follows:

// Changefileencoding. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include" changefileencoding. H "# include <string> # ifdef _ debug # define new debug_new # endif // unique application object cwinapp theapp; using namespace STD; void recursivefile (cstring strfiletype ); void convertgbtoutf8 (cstring strwritepath, const char * gb2312); int _ tmain (INT argc, tchar * argv [], tchar * envp []) {int nretcode = 0; // initialize the MFC and display the error if (! Afxwininit (: getmodulehandle (null), null,: getcommandline (), 0) {// todo: change the error code to meet your needs _ tprintf (_ T ("error: MFC initialization failed \ n"); nretcode = 1 ;} else {/* For (INT I = 0; I <argc; I ++) {MessageBox (null, argv [I], l "Arglist contents", mb_ OK );} * /// declare a cfilefind class variable to search // accept a parameter as the source code file's root directory tchar * lpszdirname = argv [1]; cstring strfiletype; strfiletype. format (_ T ("% s \\*. * "), lpszdirname); // recursive. h file and. CPP file. If it is not UTF-8 encoded Convert to utf8 encoding recursivefile (strfiletype);} return nretcode;} void recursivefile (cstring strfiletype) {cfilefind finder; bool isfinded = finder. findfile (strfiletype); // find the first file while (isfinded) {isfinded = finder. findnextfile (); // recursively searches for other files if (! Finder. isdots () // if not ". "directory {cstring strfoundfile = finder. getfilepath (); If (finder. isdirectory () // if it is a directory, it recursively calls {cstring strnextfiletype; strnextfiletype. format (_ T ("% s \\*. * "), strfoundfile); recursivefile (strnextfiletype);} else {// if it is a header file or CPP file if (strfoundfile. right (4) = _ T (". CPP ") | strfoundfile. right (2) = _ T (". H ") {cfile filereader (strfoundfile, cfile: moderead); byte head [3]; filereader. read (Head, 3 ); // determine whether the BOM file header is included. If (head [0] = 0xef & head [1] = 0xbb & head [2] = 0xbf) {filereader. close (); continue;} filereader. seektobegin (); int buflength = 256; char * Buf = new char [buflength]; zeromemory (BUF, buflength); int nreadlength; STD: String strcontent; while (nreadlength = filereader. read (BUF, buflength) {strcontent. append (BUF, nreadlength); zeromemory (BUF, nreadlength);} Delete Buf; filereader. close (); convertgbtoutf8 (strfoundfile, strcontent. c_str () ;}}} finder. close ();} void convertgbtoutf8 (cstring strwritepath, const char * gb2312) {cfile FP; FP. open (strwritepath, cfile: modecreate | cfile: modewrite | cfile: typebinary, null); int Len = multibytetowidechar (cp_acp, 0, gb2312,-1, null, 0); wchar_t * wstr = new wchar_t [Len + 1]; memset (wstr, 0, Len + 1); multibytetowidechar (cp_acp, 0, gb2312,-1, wstr, len); Len = widechartomultibyte (cp_utf8, 0, wstr,-1, null, 0, null); char * STR = new char [Len + 1]; memset (STR, 0, Len + 1); Len = widechartomultibyte (cp_utf8, 0, wstr,-1, STR, Len, null, null); If (wstr) delete [] wstr; STR [Len] = '\ n'; const unsigned char arybom [] = {0xef, 0xbb, 0xbf}; FP. write (arybom, sizeof (arybom); FP. write (STR, Len); Delete [] STR; FP. close ();}

After changefileencoding.exeis generated, place changefileencoding.exe in the root directory of the main program project of the cocos2d-x. For example, the directory tree of the project is as follows:

Project

---------- \ Cocos2dx

---------- \ Cocosdenshion

---------- \ Box2d

---------- \ Mainapplication

In the mainapplication main program project, place changefileencoding.exe in this directory, set the project properties of mainapplication, and enter changefileencoding.exe./classes in "generate event" -- "pre-generated event" -- "command line. In this way, although the added file is gb2312 encoding, it is automatically converted to utf8 encoding before compilation.

If you need source code, leave your email.

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.