CCLabelTTF fail on Windows build

Hi, I’m new to Cocos2d-x although a very experienced game programmer. I’ve been messing around with the HelloWorld example and cannot get a custom .ttf font to work on Windows.

All I’ve done is change the “Hello World” line to:

CCLabelTTF* pLabel = CCLabelTTF::create(“Hello World”, “Abberancy.ttf”, 24);

I then copied the Abberancy.ttf font file into the root Resources folder for HelloCpp. Thinking it is a path issue, I copied the .ttf into /fonts and /ipadhd and some other subdirectories just to be sure.

No dice. It always displays the Arial font. The TestFont example DOES work when I run it, but I have as of yet been unable to determine why it works there but fails for HelloWorld. I’m not seeing any errors or warnings anywhere, but maybe I’m not looking in the right place. Is there a .log file somewhere I can look at?

Any ideas appreciated, thanks!

I “fixed” the issue by commenting out the frame rate counter and then running the project. Once it was run without the counter, when I put it back, it works fine! wtf???

That is some seriously finnicky stuff right there. Does anyone know where it might save font state on disk? I’d like to reproduce this behavior and try and fix it.

I fixed the problem. There’s a bug in CCImage.cpp in setFont() - if the previously rendered font was NOT different than the default font, then it wouldn’t bother to create a new font. Hence the finicky behavior. Here’s the fix:

Change this code block:

@

// delete old font
if (m_hFont != hDefFont)
{
DeleteObject(m_hFont);
// release old font register
if (m_curFontPath.size() > 0)
{
wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath);
if (pwszBuffer)
{
if(RemoveFontResource(pwszBuffer))
{
SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0);
}
delete [] pwszBuffer;
pwszBuffer = NULL;
}
}
if (fontPath.size() > 0)
m_curFontPath = fontPath;
else
m_curFontPath.clear();
// register temp font
if (m_curFontPath.size() > 0)
{
wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath);
if (pwszBuffer)
{
if(AddFontResource(pwszBuffer))
{
SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0);
}
delete [] pwszBuffer;
pwszBuffer = NULL;
}
}
}

To this:

// delete old font
if (m_hFont != hDefFont)
{
DeleteObject(m_hFont);
// release old font register
if (m_curFontPath.size() > 0)
{
wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath);
if (pwszBuffer)
{
if(RemoveFontResource(pwszBuffer))
{
SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0);
}
delete [] pwszBuffer;
pwszBuffer = NULL;
}
}
}

if (fontPath.size() > 0)
m_curFontPath = fontPath;
else
m_curFontPath.clear();

// register temp font
if (m_curFontPath.size() > 0)
{
wchar_t * pwszBuffer = utf8ToUtf16(m_curFontPath);
if (pwszBuffer)
{
if(AddFontResource(pwszBuffer))
{
SendMessage( m_hWnd, WM_FONTCHANGE, 0, 0);
}
delete [] pwszBuffer;
pwszBuffer = NULL;
}
}

@

Sorry about the brackets being all over the place, it’s the best I could do with this forum. Be careful when implementing, it’s essentially just a change in bracket placement.

Thanks! I’ve just spent 2hours tracking this issue…
Its shocking that it still hasn’t been fixed, I’m using version 2.20

It looks as if all development is on v3. I can’t find where to push a pull request with a fix…

I added a pull request to git-hub, with a fix for v3: https://github.com/cocos2d/cocos2d-x/pull/5264