Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: display Chinese characters ???  (Read 5883 times)

0 Members and 1 Guest are viewing this topic.

qxtianlong

  • Newbie
  • *
  • Posts: 3
    • View Profile
display Chinese characters ???
« on: November 07, 2009, 04:28:56 am »
logo.h
Code: [Select]

sf::Font            mFont1;
sf::Font            mFont2;
sf::String          mText1;
sf::String          mText2;

logo.cpp

Code: [Select]


void Logo::Init()
{
// load background Image
if (!mBgImage.LoadFromFile("Data\\gfx\\secondlife.png"))
{
// Error...
cout << "[!] Wasn't able to load the secondlife.png File" << endl;
}

// link background image to sprite
mBgSprite.SetImage(mBgImage);

mBgSprite.SetPosition((WINWIDTH-mBgImage.GetWidth())/2,(WINHEIGHT-mBgImage.GetHeight())/2);

if(!mFont1.LoadFromFile("Data\\fonts\\simkai.ttf",30,L"        SecondLife Present"))
{
cout << "[!] Wasn't able to load the simkai.ttf File" << endl;
}

if(!mFont2.LoadFromFile("Data\\fonts\\simkai.ttf",30,L"第二人生游戏开发俱乐部发行"))
{
cout << "[!] Wasn't able to load the simkai.ttf File" << endl;
}

int start = (WINHEIGHT - mBgImage.GetHeight()) / 2 + mBgImage.GetHeight() + 1;

mText1.SetFont(mFont1);
mText1.SetText(L"        SecondLife Present");
mText1.SetSize(18);
mText1.SetPosition(WINHEIGHT/2,start+16);
mText1.SetColor(sf::Color(0,255,0));

mText2.SetFont(mFont2);
mText2.SetText(L"第二人生游戏开发俱乐部发行");
mText2.SetSize(18);
mText2.SetPosition(WINHEIGHT/2,start+48);
mText2.SetColor(sf::Color(0,255,0));


mClock.Reset();
}


simkai.ttf is chinese font, mText1 and mText2 is chinese characters.

Code: [Select]

void Logo::Draw(sf::RenderWindow &Window)
{
Window.Clear();

Window.Draw(mBgSprite);

Window.Draw(mText1);
Window.Draw(mText2);

if( i < 20 )
i = 0;
else
i -= 10;

Window.Draw(sf::Shape::Rectangle(0, 0, WINWIDTH, WINHEIGHT, sf::Color(0,0,0,i)));


Window.Display();
}



The above code is correct, if not correct please give me an example.

My English is not good[/code]

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
display Chinese characters ???
« Reply #1 on: November 07, 2009, 04:54:22 am »
Soo, what is your problem?

Don't define variables in headers, it's not safe nor sane.
I use the latest build of SFML2

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
display Chinese characters ???
« Reply #2 on: November 07, 2009, 11:32:57 am »
Quote from: "OniLink10"
Don't define variables in headers, it's not safe nor sane.

Class members... ?
Want to play movies in your SFML application? Check out sfeMovie!

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
display Chinese characters ???
« Reply #3 on: November 07, 2009, 08:45:31 pm »
Quote from: "Ceylo"
Quote from: "OniLink10"
Don't define variables in headers, it's not safe nor sane.

Class members... ?
That would work, but he is not using class members, he is just defining the variables.
I use the latest build of SFML2

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
display Chinese characters ???
« Reply #4 on: November 07, 2009, 08:48:22 pm »
You don't know whether he has shown the whole header file content.
Want to play movies in your SFML application? Check out sfeMovie!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
display Chinese characters ???
« Reply #5 on: November 07, 2009, 11:02:43 pm »
And since the variables begin with a "m" and the .cpp file shows member functions, it's highly probable that qxtianlong speaks about a class.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

fantasy86

  • Newbie
  • *
  • Posts: 2
    • View Profile
Unicode & Method to display Chinese Text
« Reply #6 on: December 25, 2009, 03:08:12 pm »
( I only test with Windows and VC)
The Unicode module of SFML can't deal with multibyte ANSI string properly(at least Chinese), and after some tests, here's my conclusion:

1. On Windows using VC compiler, the simplist way to display chinese is using 'L' with string literal, like:
Code: [Select]

    if(!zhfont.LoadFromFile("simkai.ttf", 30, sf::Unicode::Text(L"ok你好hi"))) return -1;
    sf::String msg(sf::Unicode::Text(L"ok你好hi"), zhfont, 18);

Because VC compiler convert the L"xx" string to wide string(const wchar_t [], UTF-16),  and then SFML handles correctly. It works, but is not portable.
2. We may save the text in UTF-8 file, and load it. Pass the text as "const unsigned char *" or "const Uint8 *" type to sf::Unicode::Text, like:
Code: [Select]

    std::string s;
    std::ifstream fin("msg");
    std::getline(fin, s);
    fin.close();
    // now use sf::Unicode::Text((const unsigned char *)s.c_str())

3. The reason lies in the function Unicode::UTF32ToANSI and Unicode::ANSIToUTF32 in "include\SFML\System\Unicode.inl". The C++ facet code seems not work, and the mbtowc() code can't deal with multibyte ansi string correctly, so I modified it:
Code: [Select]

#pragma warning(disable: 4996)
////////////////////////////////////////////////////////////
/// Generic function to convert an UTF-32 characters range
/// to an ANSI characters range, using the given locale
////////////////////////////////////////////////////////////
template <typename In, typename Out>
inline Out WCSToANSI(In Begin, In End, Out Output, char Replacement)
{
//wctomb(NULL, 0);
char bytes[6];
while (Begin < End)
{
int n=wctomb(bytes, static_cast<wchar_t>(*Begin++));
if ( n >= 0) {
for(int i=0; i < n; ++i) { *Output++ = bytes[i]; }
} else if (Replacement) {
*Output++ = Replacement;
}
}

    return Output;
}
template <typename In, typename Out>
inline Out Unicode::UTF32ToANSI(In Begin, In End, Out Output, char Replacement)
{
switch(sizeof(wchar_t)) {
case 4: WCSToANSI(Begin, End, Output, Replacement); break;
case 2: {
Unicode::UTF16String s;
s.reserve((End-Begin)*2+1);
Unicode::UTF32ToUTF16(Begin, End, std::back_inserter(s), Replacement);
WCSToANSI(s.begin(), s.end(), Output, Replacement);
break;
}
default: break;
}
return Output;
}

////////////////////////////////////////////////////////////
/// Generic function to convert an ANSI characters range
/// to an UTF-32 characters range, using the given locale
////////////////////////////////////////////////////////////
template <typename In, typename Out>
inline Out ANSIToWCS(In Begin, In End, Out Output)
{

//mbtowc(NULL, NULL, 6);
wchar_t wc;
char *bytes = new char[End - Begin];
for(int i=0; i< End-Begin; ++i) { bytes[i] = *(Begin+i); }

const char *p = bytes;
while (p < bytes+(End-Begin))
{
int n = mbtowc(&wc, p, MB_CUR_MAX);
if(n>0) {
p += n;
*Output++ = (wc);
}
}
delete[] bytes;

    return Output;
}

template <typename In, typename Out>
inline Out Unicode::ANSIToUTF32(In Begin, In End, Out Output)
{
switch(sizeof(wchar_t)) {
case 4: ANSIToWCS(Begin, End, Output); break;
case 2: {
Unicode::UTF16String s;
s.reserve(End-Begin+1);
ANSIToWCS(Begin, End, std::back_inserter(s));
Unicode::UTF16ToUTF32(s.begin(), s.end(), Output, 0);
break;
}
default: break;
}
return Output;
}


NOTE: I removed the ‘Locale’ param, you may add it to fast test
NOTE:  be sure to call
Code: [Select]
setlocale(LC_ALL, "");at the start of main().
 
Not tested much, just work on my machine, hope this can help somebody :)