diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index 2879ac72de..24b56bd448 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -14,15 +14,15 @@ NS_CC_BEGIN FontAtlas::FontAtlas(Font &theFont) : -_font(theFont), +_font(&theFont), _currentPageData(nullptr) { - _font.retain(); + _font->retain(); - FontFreeType* fontTTf = dynamic_cast(&_font); + FontFreeType* fontTTf = dynamic_cast(_font); if (fontTTf && fontTTf->isDynamicGlyphCollection()) { - _currentPageLineHeight = _font.getFontMaxHeight(); + _currentPageLineHeight = _font->getFontMaxHeight(); _commonLineHeight = _currentPageLineHeight * 0.8f; Texture2D * tex = new Texture2D; _currentPage = 0; @@ -34,16 +34,16 @@ _currentPageData(nullptr) _currentPageData = new unsigned char[_currentPageDataSize]; memset(_currentPageData, 0, _currentPageDataSize); addTexture(*tex,0); + tex->release(); } } FontAtlas::~FontAtlas() { - _font.release(); + _font->release(); relaseTextures(); - if(_currentPageData) - delete []_currentPageData; + delete []_currentPageData; } void FontAtlas::relaseTextures() @@ -79,7 +79,7 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) if(_currentPageData == nullptr) return false; - FontFreeType* fontTTf = (FontFreeType*)&_font; + FontFreeType* fontTTf = (FontFreeType*)_font; std::vector fontDefs; int length = cc_wcslen(utf16String); @@ -100,7 +100,6 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) if (!fontTTf->getBBOXFotChar(utf16String[i], tempRect)) { - //log("Warning: Cannot find definition for glyph: %c in font:%s", utf16String[i], _fontName.c_str()); tempDef.validDefinition = false; tempDef.letteCharUTF16 = utf16String[i]; tempDef.commonLineHeight = 0; @@ -150,7 +149,9 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) return false; memset(_currentPageData, 0, _currentPageDataSize); _currentPage++; - addTexture(*(new Texture2D),_currentPage); + Texture2D* tex = new Texture2D; + addTexture(*tex,_currentPage); + tex->release(); } } renderCharAt(fontDefs[i].letteCharUTF16,_currentPageOrigX,_currentPageOrigY,_currentPageData,1024); @@ -182,7 +183,7 @@ bool FontAtlas::renderCharAt(unsigned short int charToRender, int posX, int posY int sourceHeight = 0; // get the glyph's bitmap - sourceBitmap = _font.getGlyphBitmap(charToRender, sourceWidth, sourceHeight); + sourceBitmap = _font->getGlyphBitmap(charToRender, sourceWidth, sourceHeight); if (!sourceBitmap) return false; @@ -234,7 +235,7 @@ void FontAtlas::setCommonLineHeight(float newHeight) _commonLineHeight = newHeight; } -Font & FontAtlas::getFont() const +const Font * FontAtlas::getFont() const { return _font; } diff --git a/cocos/2d/CCFontAtlas.h b/cocos/2d/CCFontAtlas.h index b1b6eaa9b4..a388a5b987 100644 --- a/cocos/2d/CCFontAtlas.h +++ b/cocos/2d/CCFontAtlas.h @@ -70,7 +70,7 @@ public: void setCommonLineHeight(float newHeight); Texture2D & getTexture(int slot); - Font & getFont() const; + const Font * getFont() const; private: bool renderCharAt(unsigned short int charToRender, int posX, int posY, unsigned char *destMemory, int destSize); @@ -79,7 +79,7 @@ private: std::map _atlasTextures; std::map _fontLetterDefinitions; float _commonLineHeight; - Font & _font; + Font * _font; // Dynamic GlyphCollection related stuff int _currentPage; diff --git a/cocos/2d/CCFontAtlasFactory.cpp b/cocos/2d/CCFontAtlasFactory.cpp index 2b8e0b4ca4..aeb0b6154e 100644 --- a/cocos/2d/CCFontAtlasFactory.cpp +++ b/cocos/2d/CCFontAtlasFactory.cpp @@ -35,7 +35,9 @@ FontAtlas * FontAtlasFactory::createAtlasFromFNT(const char* fntFilePath) return atlas; } else + { return nullptr; + } } NS_CC_END diff --git a/cocos/2d/CCFontDefinition.cpp b/cocos/2d/CCFontDefinition.cpp index 7852d56d84..938204f3b0 100644 --- a/cocos/2d/CCFontDefinition.cpp +++ b/cocos/2d/CCFontDefinition.cpp @@ -49,6 +49,7 @@ FontDefinitionTTF* FontDefinitionTTF::create(Font *font, int textureSize) if (ret->initDefinition(font, glyph, textureSize)) { + ret->autorelease(); return ret; } else diff --git a/cocos/2d/CCFontFNT.cpp b/cocos/2d/CCFontFNT.cpp index cd1e586e62..7a1ca6f200 100644 --- a/cocos/2d/CCFontFNT.cpp +++ b/cocos/2d/CCFontFNT.cpp @@ -38,11 +38,7 @@ FontFNT * FontFNT::create(const char* fntFilePath) FontFNT::~FontFNT() { - //_configuration release when execute LabelBMFont::purgeCachedData(); - /*if (_configuration) - { - _configuration->release(); - }*/ + } Size * FontFNT::getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const diff --git a/cocos/2d/CCFontFreeType.cpp b/cocos/2d/CCFontFreeType.cpp index 3fbac58b5b..0e4e49d96f 100644 --- a/cocos/2d/CCFontFreeType.cpp +++ b/cocos/2d/CCFontFreeType.cpp @@ -88,8 +88,8 @@ FT_Library FontFreeType::getFTLibrary() } FontFreeType::FontFreeType(bool dynamicGlyphCollection) - : _letterPadding(5) - ,_ttfData(nullptr), + : _letterPadding(5), + _ttfData(nullptr), _dynamicGlyphCollection(dynamicGlyphCollection), _fontRef(nullptr) { @@ -130,8 +130,6 @@ bool FontFreeType::createFontObject(const std::string &fontName, int fontSize) FontFreeType::~FontFreeType() { - // release the font - // TO DO if (_fontRef) { FT_Done_Face(_fontRef); @@ -160,8 +158,6 @@ FontAtlas * FontFreeType::createFontAtlas() FontAtlas *atlas = def->createFontAtlas(); - // release the font definition, we don't need it anymore - def->release(); return atlas; } } diff --git a/cocos/2d/CCFontFreeType.h b/cocos/2d/CCFontFreeType.h index cdf3d1a1ac..a305b2253e 100644 --- a/cocos/2d/CCFontFreeType.h +++ b/cocos/2d/CCFontFreeType.h @@ -39,7 +39,9 @@ class CC_DLL FontFreeType : public Font public: static FontFreeType * create(const std::string &fontName, int fontSize, GlyphCollection glyphs, const char *customGlyphs); - + + static void shutdownFreeType(); + virtual FontAtlas * createFontAtlas() override; virtual Size * getAdvancesForTextUTF16(unsigned short *text, int &outNumLetters) const override; virtual GlyphDef * getGlyphDefintionsForText(const char *text, int &outNumGlyphs, bool UTF16text = false) const override; @@ -49,8 +51,7 @@ public: bool getBBOXFotChar(unsigned short theChar, Rect &outRect) const; - inline bool isDynamicGlyphCollection() { return _dynamicGlyphCollection;} - static void shutdownFreeType(); + inline bool isDynamicGlyphCollection() { return _dynamicGlyphCollection;} protected: diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index a7ed8e2978..ce928825bc 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -112,12 +112,9 @@ Label::Label(FontAtlas *atlas, TextHAlignment alignment) Label::~Label() { - if (_currentUTF16String) - delete [] _currentUTF16String; - if (_originalUTF16String) - delete [] _originalUTF16String; - if (_advances) - delete [] _advances; + delete [] _currentUTF16String; + delete [] _originalUTF16String; + delete [] _advances; if (_fontAtlas) FontAtlasCache::releaseFontAtlas(_fontAtlas); @@ -254,8 +251,7 @@ void Label::alignText() int strLen = cc_wcslen(_currentUTF16String); if (_children && _children->count() != 0) { - Object* child; - CCARRAY_FOREACH(_children, child) + for (auto child: *_children) { Node* pNode = static_cast( child ); if (pNode) @@ -275,7 +271,7 @@ void Label::alignText() { if (_lettersInfo[ctr].def.validDefinition) { - child = (Sprite*)this->getChildByTag(ctr); + child = static_cast( this->getChildByTag(ctr) ); if (child) { uvRect.size.height = _lettersInfo[ctr].def.height; @@ -302,10 +298,8 @@ bool Label::computeAdvancesForString(unsigned short int *stringToRender) _advances = 0; } - Font &theFont = _fontAtlas->getFont(); - int letterCount = 0; - _advances = theFont.getAdvancesForTextUTF16(stringToRender, letterCount); + _advances = _fontAtlas->getFont()->getAdvancesForTextUTF16(stringToRender, letterCount); if(!_advances) return false; @@ -519,7 +513,7 @@ int Label::getAdvanceForChar(unsigned short c, int hintPositionInString) const Rect Label::getRectForChar(unsigned short c) const { - return _fontAtlas->getFont().getRectForChar(c); + return _fontAtlas->getFont()->getRectForChar(c); } // string related stuff @@ -649,31 +643,27 @@ void Label::updateDisplayedOpacity(GLubyte parentOpacity) { _displayedOpacity = _realOpacity * parentOpacity/255.0; - Object* pObj; - CCARRAY_FOREACH(_children, pObj) + for (auto child: *_children) { - Sprite *item = static_cast( pObj ); + Sprite *item = static_cast( child ); item->updateDisplayedOpacity(_displayedOpacity); } - //if (_cascadeOpacityEnabled) + V3F_C4B_T2F_Quad *quads = _textureAtlas->getQuads(); + int count = _textureAtlas->getTotalQuads(); + Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity ); + if (_isOpacityModifyRGB) { - V3F_C4B_T2F_Quad *quads = _textureAtlas->getQuads(); - int count = _textureAtlas->getTotalQuads(); - Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity ); - if (_isOpacityModifyRGB) - { - color4.r *= _displayedOpacity/255.0f; - color4.g *= _displayedOpacity/255.0f; - color4.b *= _displayedOpacity/255.0f; - } - for (int index=0; indexupdateQuad(&quads[index], index); - } + color4.r *= _displayedOpacity/255.0f; + color4.g *= _displayedOpacity/255.0f; + color4.b *= _displayedOpacity/255.0f; + } + for (int index = 0; index < count; ++index) + { + quads[index].bl.colors = color4; + quads[index].br.colors = color4; + quads[index].tl.colors = color4; + quads[index].tr.colors = color4; + _textureAtlas->updateQuad(&quads[index], index); } } diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index e9e56e31ca..b59850632b 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -82,9 +82,9 @@ public: virtual unsigned char getDisplayedOpacity() const override; // CCLabelTextFormat protocol implementation - virtual std::vector *getLettersInfo(){ return &_lettersInfo; }; - virtual bool recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex); - virtual bool recordPlaceholderInfo(int spriteIndex); + virtual std::vector *getLettersInfo() override { return &_lettersInfo; }; + virtual bool recordLetterInfo(const cocos2d::Point& point,unsigned short int theChar, int spriteIndex) override; + virtual bool recordPlaceholderInfo(int spriteIndex) override; virtual float getLetterPosXLeft( int index ) const override; virtual float getLetterPosXRight( int index ) const override; diff --git a/cocos/2d/CCLabelTextFormatter.cpp b/cocos/2d/CCLabelTextFormatter.cpp index dc3bc82020..87dd3cf7cd 100644 --- a/cocos/2d/CCLabelTextFormatter.cpp +++ b/cocos/2d/CCLabelTextFormatter.cpp @@ -71,8 +71,6 @@ bool LabelTextFormatter::multilineText(LabelTextFormatProtocol *theLabel) } skip += justSkipped; tIndex = j + skip; - //if (!info->visible) - // continue; if (i >= stringLength) break;