关于Cocos2d-x中文乱码问题的解决

  1.首先,复制下面的代码,创建一个icov,h的头文件,并放在项目目录下

  #include "stdlib.h"

  #include "string.h"

  #ifdef WIN32

  //调用时使用这个宏

  #define UTEXT(str) GBKToUTF8(str)

  #else

  #define UTEXT(str) str

  #endif

  #ifdef WIN32

  #include "..\cocos2d-x-3.10\external\win32-specific\icon\include\iconv.h"

  static char g_GBKConvUTF8Buf[5000] = { 0 };

  const char* GBKToUTF8(const char *strChar)

  {

  iconv_t iconvH;

  iconvH = iconv_open("utf-8", "gb2312");

  if (iconvH == 0)

  {

  return NULL;

  }

  size_t strLength = strlen(strChar);

  size_t outLength = strLength << 2;

  size_t copyLength = outLength;

  memset(g_GBKConvUTF8Buf, 0, 5000);

  char* outbuf = (char*)malloc(outLength);

  char* pBuff = outbuf;

  memset(outbuf, 0, outLength);

  if (-1 == iconv(iconvH, &strChar, &strLength, &outbuf, &outLength))

  {

  iconv_close(iconvH);

  return NULL;

  }

  memcpy(g_GBKConvUTF8Buf, pBuff, copyLength);

  free(pBuff);

  iconv_close(iconvH);

  return g_GBKConvUTF8Buf;

  }

  #endif

  2.等到要写中文的时候,先#include "icov.h",然后在有字符串的地方用宏进行强制的转换

  UTEXT("菜鸟在线")

  例子

  displayValueLabel = Label::createWithSystemFont(UTEXT("菜鸟在线"), "Marker Felt", 32);

  3.运行就可以显示正常的中文了

  另外再推荐给大家一个不错的学习网站,很多问题我都是看的这里http://www.newbieol.com/


你可能感兴趣的:(return,中文,项目,include)