Fixed label may become white/black block after resume from background on android

This commit is contained in:
Wenhai Lin 2015-01-21 21:58:18 +08:00
parent 72fb059aeb
commit 342c582124
3 changed files with 31 additions and 65 deletions

View File

@ -36,7 +36,8 @@ NS_CC_BEGIN
const int FontAtlas::CacheTextureWidth = 512; const int FontAtlas::CacheTextureWidth = 512;
const int FontAtlas::CacheTextureHeight = 512; const int FontAtlas::CacheTextureHeight = 512;
const char* FontAtlas::EVENT_PURGE_TEXTURES = "__cc_FontAtlasPurgeTextures"; const char* FontAtlas::CMD_PURGE_FONTATLAS = "__cc_PURGE_FONTATLAS";
const char* FontAtlas::CMD_RESET_FONTATLAS = "__cc_RESET_FONTATLAS";
FontAtlas::FontAtlas(Font &theFont) FontAtlas::FontAtlas(Font &theFont)
: _font(&theFont) : _font(&theFont)
@ -44,7 +45,6 @@ FontAtlas::FontAtlas(Font &theFont)
, _fontAscender(0) , _fontAscender(0)
, _rendererRecreatedListener(nullptr) , _rendererRecreatedListener(nullptr)
, _antialiasEnabled(true) , _antialiasEnabled(true)
, _rendererRecreate(false)
{ {
_font->retain(); _font->retain();
@ -122,25 +122,11 @@ void FontAtlas::purgeTexturesAtlas()
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font); FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
if (fontTTf && _atlasTextures.size() > 1) if (fontTTf && _atlasTextures.size() > 1)
{ {
for( auto &item: _atlasTextures) _font->retain();
{
if (item.first != 0)
{
item.second->release();
}
}
auto temp = _atlasTextures[0];
_atlasTextures.clear();
_atlasTextures[0] = temp;
_fontLetterDefinitions.clear();
memset(_currentPageData,0,_currentPageDataSize);
_currentPage = 0;
_currentPageOrigX = 0;
_currentPageOrigY = 0;
auto eventDispatcher = Director::getInstance()->getEventDispatcher(); auto eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->dispatchCustomEvent(EVENT_PURGE_TEXTURES,this); eventDispatcher->dispatchCustomEvent(CMD_PURGE_FONTATLAS,this);
eventDispatcher->dispatchCustomEvent(CMD_RESET_FONTATLAS,this);
_font->release();
} }
} }
@ -149,27 +135,11 @@ void FontAtlas::listenRendererRecreated(EventCustom *event)
FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font); FontFreeType* fontTTf = dynamic_cast<FontFreeType*>(_font);
if (fontTTf) if (fontTTf)
{ {
for( auto &item: _atlasTextures) _font->retain();
{
if (item.first != 0)
{
item.second->release();
}
}
auto temp = _atlasTextures[0];
_atlasTextures.clear();
_atlasTextures[0] = temp;
_fontLetterDefinitions.clear();
memset(_currentPageData,0,_currentPageDataSize);
_currentPage = 0;
_currentPageOrigX = 0;
_currentPageOrigY = 0;
_rendererRecreate = true;
auto eventDispatcher = Director::getInstance()->getEventDispatcher(); auto eventDispatcher = Director::getInstance()->getEventDispatcher();
eventDispatcher->dispatchCustomEvent(EVENT_PURGE_TEXTURES,this); eventDispatcher->dispatchCustomEvent(CMD_PURGE_FONTATLAS,this);
_rendererRecreate = false; eventDispatcher->dispatchCustomEvent(CMD_RESET_FONTATLAS,this);
_font->release();
} }
} }
@ -309,25 +279,17 @@ bool FontAtlas::prepareLetterDefinitions(const std::u16string& utf16String)
if(existNewLetter) if(existNewLetter)
{ {
if (_rendererRecreate) unsigned char *data = nullptr;
if(pixelFormat == Texture2D::PixelFormat::AI88)
{ {
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, data = _currentPageData + CacheTextureWidth * (int)startY * 2;
pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); }
}
else else
{ {
unsigned char *data = nullptr; data = _currentPageData + CacheTextureWidth * (int)startY;
if(pixelFormat == Texture2D::PixelFormat::AI88)
{
data = _currentPageData + CacheTextureWidth * (int)startY * 2;
}
else
{
data = _currentPageData + CacheTextureWidth * (int)startY;
}
_atlasTextures[_currentPage]->updateWithData(data, 0, startY,
CacheTextureWidth, _currentPageOrigY - startY + _commonLineHeight);
} }
_atlasTextures[_currentPage]->updateWithData(data, 0, startY,
CacheTextureWidth, _currentPageOrigY - startY + _commonLineHeight);
} }
return true; return true;
} }

View File

@ -61,7 +61,8 @@ class CC_DLL FontAtlas : public Ref
public: public:
static const int CacheTextureWidth; static const int CacheTextureWidth;
static const int CacheTextureHeight; static const int CacheTextureHeight;
static const char* EVENT_PURGE_TEXTURES; static const char* CMD_PURGE_FONTATLAS;
static const char* CMD_RESET_FONTATLAS;
/** /**
* @js ctor * @js ctor
*/ */
@ -125,7 +126,6 @@ protected:
int _fontAscender; int _fontAscender;
EventListenerCustom* _rendererRecreatedListener; EventListenerCustom* _rendererRecreatedListener;
bool _antialiasEnabled; bool _antialiasEnabled;
bool _rendererRecreate;
}; };

View File

@ -36,7 +36,6 @@
#include "base/CCEventListenerCustom.h" #include "base/CCEventListenerCustom.h"
#include "base/CCEventDispatcher.h" #include "base/CCEventDispatcher.h"
#include "base/CCEventCustom.h" #include "base/CCEventCustom.h"
#include "deprecated/CCString.h" #include "deprecated/CCString.h"
NS_CC_BEGIN NS_CC_BEGIN
@ -263,24 +262,29 @@ Label::Label(FontAtlas *atlas /* = nullptr */, TextHAlignment hAlignment /* = Te
setAnchorPoint(Vec2::ANCHOR_MIDDLE); setAnchorPoint(Vec2::ANCHOR_MIDDLE);
reset(); reset();
auto purgeTextureListener = EventListenerCustom::create(FontAtlas::EVENT_PURGE_TEXTURES, [this](EventCustom* event){ auto purgeTextureListener = EventListenerCustom::create(FontAtlas::CMD_PURGE_FONTATLAS, [this](EventCustom* event){
if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas) if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas)
{ {
Node::removeAllChildrenWithCleanup(true); Node::removeAllChildrenWithCleanup(true);
_batchNodes.clear(); _batchNodes.clear();
_batchNodes.push_back(this); _batchNodes.push_back(this);
if (_contentDirty) if (_fontAtlas)
{ {
updateContent(); FontAtlasCache::releaseFontAtlas(_fontAtlas);
}
else
{
alignText();
} }
} }
}); });
_eventDispatcher->addEventListenerWithSceneGraphPriority(purgeTextureListener, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(purgeTextureListener, this);
auto resetTextureListener = EventListenerCustom::create(FontAtlas::CMD_RESET_FONTATLAS, [this](EventCustom* event){
if (_fontAtlas && _currentLabelType == LabelType::TTF && event->getUserData() == _fontAtlas)
{
_fontAtlas = nullptr;
this->setTTFConfig(_fontConfig);
}
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(resetTextureListener, this);
} }
Label::~Label() Label::~Label()