CCLabelTTF with special characters

Hi!

When I use special characters in CCLabelTTF, it shows question marks (?).
An example:

CCLabelTTF* pLabel = CCLabelTTF::labelWithString(“Olá multidão!”, “Arial”, 24);

It doesn’t show ‘á’ and ‘ã’.
I found a way to show as I want: changing CCImage.cpp, in BitmapDC drawImage method the line

nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);

to

nLen = MultiByteToWideChar(CP_ACP, 0, pszText, nLen, pwszBuffer, nBufLen);

but it is that the best solution?

Thanks in advance,

Joao Ribeiro

PS: Sorry my english, it is far away from perfection :slight_smile:

Did you use 2.0 ? I have tested this issue with the latest codes on github gles20 branch. And it works fine. As i know, 1.0 branch does have this bug, but it has been fixed at gles20 branch. Have a try, thanks. :slight_smile:

I used the last stable version in the cocos2d-x site, “cocos2d-2.0-rc0a-x-2.0 @ May 31 2012”, that is 2.0.
I also cloned the gles20 branch, modifying the “Hello World” text of the CCLabelTTF to “Olá mundo! obrigatório pontuação mínima”, but the result was the same.
As attachment, there is a printscreen.
I’m using VS2008.

Thanks for the reply!

Is there anybody that can answer this thread?

Plz upload your demo project. :slight_smile:

Ok. Like I said, I only changed HelloWorld.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::node();

    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::node();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::itemWithNormalImage(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Olá mundo! obrigatório pontuação mínima", "Arial", 24);
    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 50) );

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(size.width/2, size.height/2) );

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);

    return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

The characters ‘á’, ‘ó’, ‘ç’, ‘ã’ and ‘í’ appear like ‘?’.
It is the win32 solution for VS2008.

Thanks for reply! :slight_smile:

Just upload an attachment, I want to know whether you use UTF-8 format.

You should make sure that the format of HelloWorldScene.cpp is UTF-8.

It wasn’t UTF-8, but now it is and the problem continues.
When I debug, I check that in CCImage.cpp (libcocos2d/platform/win32/CCImage.cpp), in BitmapDC drawImage method, the function

nLen = MultiByteToWideChar(CP_UTF8, 0, pszText, nLen, pwszBuffer, nBufLen);

receives pszText=“Olá mundo! obrigatório pontuação mínima” and sets the pwszBuffer value as “Ol? mundo! obrigat?rio pontua??o m?nima”.
The pwszBuffer is written on the screen… So, the original text was modified in CCImage.cpp. :frowning:

Hi, I think vs2008 have a ugly bug if you write utf-8 string in .cpp source directly.
So in my opinion, you should write your utf-8 string to one plist. Then use :

    CCDictionary* pDict = CCDictionary::createWithContentsOfFile("strings.xml");
    CCLabelTTF* pLabel = CCLabelTTF::create(((CCString*)pDict->objectForKey("special"))->getCString(), "Arial", 24);

Does it work now? :slight_smile:

Thanks, that solved my issue! And without changing cocos2d-x code! :smiley:
That’s what I was searching.
Note that if it is a bug of vs2008, than vs2010 has this bug too, because I tested it in vs2010 and it showed the string with ‘?’ too. :slight_smile:

Yes, vs2010 also has this bug.
João Ribeiro wrote:

Thanks, that solved my issue! And without changing cocos2d-x code! :smiley:
That’s what I was searching.
Note that if it is a bug of vs2008, than vs2010 has this bug too, because I tested it in vs2010 and it showed the string with ‘?’ too. :slight_smile: