|
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); |