您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx3.0 中文支持显示

2015-04-10 11:09 267 查看
转自:http://www.58player.com/article-84994-1.html

#ifndef _SUPPORT_TOOL_H_
02
#define
_SUPPORT_TOOL_H_
03
//////////////////////////////////////////////////////////////////////////
04
#include
"cocos2d.h"
05
 
06
 
07
//////////////////////////////////////////////////////////////////////////
08
 
09
#define
A2U(varString) wSupportTool::AStrToUTF8(varString).c_str() //用于显示中文(窄字符)
10
#define
W2U(varString) wSupportTool::WStrToUTF8(varString).c_str() //用于显示中文(宽字符)
11
 
12
//////////////////////////////////////////////////////////////////////////
13
 
14
class
wSupportTool
15
{
16
public
:
17
//string
to UTF8
18
static
std::string
AStrToUTF8(
const
string&
src)
19
{
20
std::string
curLocale=
setlocale
(LC_ALL,NULL);
21
setlocale
(LC_ALL,
""
);
22
int
len=src.length()+1;
23
wchar_t
*str
=
new
wchar_t
[len];
24
int
total=
mbstowcs
(str,src.c_str(),len);
25
str[total]=0;
26
std::wstring
wstr=str;
27
delete
[]
str;
28
setlocale
(LC_ALL,
curLocale.c_str());
29
return
WStrToUTF8(wstr);
30
}
31
 
32
//wstring
to UTF8
33
static
std::string
WStrToUTF8(
const
wstring&
src)
34
{
35
 
36
std::string
dest;
37
for
(
size_t
i
= 0; i < src.size();i++){
38
wchar_t
w
= src[i];
39
if
(w
<= 0x7f)
40
dest.push_back((
char
)w);
41
else
if
(w
<= 0x7ff){
42
dest.push_back(0xc0
| ((w >> 6)& 0x1f));
43
dest.push_back(0x80|
(w & 0x3f));
44
}
45
else
if
(w
<= 0xffff){
46
dest.push_back(0xe0
| ((w >> 12)& 0x0f));
47
dest.push_back(0x80|
((w >> 6) & 0x3f));
48
dest.push_back(0x80|
(w & 0x3f));
49
}
50
/*else
if (sizeof(wchar_t) > 2 && w <= 0x10ffff){
51
dest.push_back(0xf0
| ((w >> 18)& 0x07));//wchar_t 4-bytes situation
52
dest.push_back(0x80|
((w >> 12) & 0x3f));
53
dest.push_back(0x80|
((w >> 6) & 0x3f));
54
dest.push_back(0x80|
(w & 0x3f));
55
}*/
56
else
57
dest.push_back(
'?'
);
58
}
59
return
dest;
60
}
61
 
62
};
63
 
64
#endif
65
加入这个类后,只要在使用中文的时候,加上A2U或W2U宏就好了
66
[cpp]
view plaincopy
67
//
68
LabelTTF::create(A2U(
"这是一个Label!"
),
"Arial"
,
14);
69
log
(A2U(
"这是一条log!"
));
70
//
71
string
Astr=A2U(
"欢迎来到这个世界!"
);
72
log
(Astr.c_str());
73
LabelTTF::create(Astr,
"Arial"
,
14);
74
 
75
//
76
LabelTTF::create(W2U(L
"这是一个Label!"
),
"Arial"
,
14);
77
log
(W2U(L
"这是一条log!"
));
78
//
79
string
Wstr=W2U(L
"欢迎来到这个世界!"
);
80
log
(Wstr.c_str());
81
LabelTTF::create(Wstr,
"Arial"
,
14);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: