Cocos2dx Chinese to UTF8 code solution, cocos2dxutf8
Reprinted please indicate the source: curtain roll west wind column (http://blog.csdn.net/ljxfblog)
Cocos2dx supports Chinese display, as long as the Chinese character into a UTF-8 character set can be displayed normally, but in practice there will still be a lot of problems will bother developers.
Generally, there are the following solutions:
1, the code file (. h/. cpp/. lua and so on) file encoding format into the UTF-8 format, which is a good solution for single-host games. However, the text sent from the server is powerless.
2. Use the iconv library for conversion. The interface is relatively simple, and the win32 version can also be used directly. However, on Android, You need to compile and integrate the iconv source code by yourself, which is difficult.
3. Be self-reliant and write your own code for implementation.
This article focuses on the third solution. The second solution is actually good, but it is more difficult, and there will be time to wait.
You need to consider the following issues to write your own UTF-8 interface. It is mainly a cross-platform problem.
1. In win32, it is very simple and easy to implement, because win32API has provided us with related interfaces (WideCharToMultiByte/MultiByteToWideChar, etc.), you just need to process it a little bit. The related code is as follows:
const char* gb23122utf8(const char* gb2312) { int len = MultiByteToWideChar(0, 0, gb2312, -1, NULL, 0); wchar_t* wstr = new wchar_t[len+1]; memset(wstr, 0, len+1); MultiByteToWideChar(0, 0, gb2312, -1, wstr, len); len = WideCharToMultiByte(65001, 0, wstr, -1, NULL, 0, NULL, NULL); char* str = new char[len+1];memset(str, 0, len+1); WideCharToMultiByte(65001, 0, wstr, -1, str, len, NULL, NULL); if(wstr) delete[] wstr; return str;} 2. It is a little troublesome on the Android platform. The first consideration is that the C language has interfaces similar to win32 interfaces (mbstowcs/wcstombs). In this solution, you need to use the setlocale interface. After testing, we found that, this interface is valid in both windows and linux and can be converted to UTF-8 correctly. However, this interface is invalid on Android and always returns NULL. Therefore, mbstowcs/wcstombs cannot be used. Later, I found some information and decided to use the icu library. This library is available on most Android machines, but the version is different, but it can still be correctly transferred. Just use this method temporarily, later, we will use the tall solution. The Code is as follows:
First, you need to find the interface function address in the icu database:
#include <dlfcn.h>void (*ucnv_convert)(const char *, const char *, char * , int32_t , const char *, int32_t,int32_t*) = 0;bool openIcuuc(){void* libFile = dlopen("/system/lib/libicuuc.so", RTLD_LAZY); if (libFile){ucnv_convert = (void (*)(const char *, const char *, char * , int32_t , const char *, int32_t,int32_t*))dlsym(libFile, "ucnv_convert_3_8");int index = 0;char fun_name[64];while (ucnv_convert == NULL){sprintf(fun_name, "ucnv_convert_4%d", index++);ucnv_convert = (void (*)(const char *, const char *, char * , int32_t , const char *, int32_t,int32_t*))dlsym(libFile, fun_name);if (ucnv_convert) return true;if (++index > 11) break;}dlclose(libFile);}return false;} Second, the Conversion Function Code is as follows:
const char* gb23122utf8(const char * gb2312){if (ucnv_convert == NULL){openIcuuc();}if (ucnv_convert){int err_code = 0;int len = strlen(gb2312);char* str = new char[len * 2 + 10]; memset(str, 0, len * 2 + 10);ucnv_convert("utf-8", "gb2312", str, len * 2 + 10, gb2312, len, &err_code);if (err_code == 0){return str;}}char test[256] = "gb23122utf8 error";char* str = new char[30]; strcpy(str, test);return str;}Okay, that's all done. Tests on several Android machines are OK, but the failure on the simulator may be due to a lack of libraries.
Of course, you can expose this interface to lua if necessary.
static int luaA_Strg2u(lua_State *L){const char* gb2312 = luaL_checkstring(L, 1);const char* utf8 = gb23122utf8(gb2312);lua_pushstring(L, utf8);delete [] utf8;return 1;}void registerLuaFunction(lua_State* luaState){lua_register(luaState, "strg2u", luaA_Strg2u);tolua_api4lua_open(luaState);}
Finally, share the encapsulated file with you!
Solution for cocos2dx conversion from Chinese to UTF8 code
Weak question about cocos2dx
Maybe you can see cocos2d code. Which is oc 2dx? C ++, but not all of the system-related layers? For example, java in Android, ios must use oc.
Q: How difficult is java cocos2dx game development and learning curve?
There must be some risks ~ Another two problems were solved. One was that no game was ever done ~ The entire game logic may be faulty ~ The other is that c ++ is unfamiliar. If the project is a little bigger, the memory may be leaked ~ Cocos2d-x must be used to do mobile games ~ In case of leaks, the entire program may crash ~ I am afraid of this