diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index d827f670ba..3ef99242f0 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -43,6 +43,7 @@ FontAtlas::FontAtlas(Font &theFont) , _fontAscender(0) , _toForegroundListener(nullptr) , _toBackgroundListener(nullptr) +, _antialiasEnabled(true) { _font->retain(); @@ -189,8 +190,8 @@ void FontAtlas::listenToForeground(EventCustom *event) { auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; - _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, - pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); + _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, + pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); } } #endif @@ -204,7 +205,7 @@ void FontAtlas::addLetterDefinition(const FontLetterDefinition &letterDefinition bool FontAtlas::getLetterDefinitionForChar(unsigned short letteCharUTF16, FontLetterDefinition &outDefinition) { auto outIterator = _fontLetterDefinitions.find(letteCharUTF16); - + if (outIterator != _fontLetterDefinitions.end()) { outDefinition = (*outIterator).second; @@ -274,6 +275,14 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) memset(_currentPageData, 0, _currentPageDataSize); _currentPage++; auto tex = new Texture2D; + if (_antialiasEnabled) + { + tex->setAntiAliasTexParameters(); + } + else + { + tex->setAliasTexParameters(); + } tex->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) ); addTexture(tex,_currentPage); @@ -349,4 +358,28 @@ const Font * FontAtlas::getFont() const return _font; } +void FontAtlas::setAliasTexParameters() +{ + if (_antialiasEnabled) + { + _antialiasEnabled = false; + for (const auto & tex : _atlasTextures) + { + tex.second->setAliasTexParameters(); + } + } +} + +void FontAtlas::setAntiAliasTexParameters() +{ + if (! _antialiasEnabled) + { + _antialiasEnabled = true; + for (const auto & tex : _atlasTextures) + { + tex.second->setAntiAliasTexParameters(); + } + } +} + NS_CC_END diff --git a/cocos/2d/CCFontAtlas.h b/cocos/2d/CCFontAtlas.h index a10fe9eb9f..134f360ea3 100644 --- a/cocos/2d/CCFontAtlas.h +++ b/cocos/2d/CCFontAtlas.h @@ -98,6 +98,18 @@ public: */ void purgeTexturesAtlas(); + /** sets font texture parameters: + - GL_TEXTURE_MIN_FILTER = GL_LINEAR + - GL_TEXTURE_MAG_FILTER = GL_LINEAR + */ + void setAntiAliasTexParameters(); + + /** sets font texture parameters: + - GL_TEXTURE_MIN_FILTER = GL_NEAREST + - GL_TEXTURE_MAG_FILTER = GL_NEAREST + */ + void setAliasTexParameters(); + private: void relaseTextures(); @@ -118,6 +130,7 @@ private: int _fontAscender; EventListenerCustom* _toBackgroundListener; EventListenerCustom* _toForegroundListener; + bool _antialiasEnabled; }; diff --git a/cocos/2d/CCLabel.h b/cocos/2d/CCLabel.h index dcdc6b88f4..01f133cfe0 100644 --- a/cocos/2d/CCLabel.h +++ b/cocos/2d/CCLabel.h @@ -243,6 +243,7 @@ public: virtual const Size& getContentSize() const override; + FontAtlas* getFontAtlas() { return _fontAtlas; } /** Listen "come to background" message It only has effect on Android. */ diff --git a/cocos/2d/CCTexture2D.cpp b/cocos/2d/CCTexture2D.cpp index ca4498c6aa..e8235d614a 100644 --- a/cocos/2d/CCTexture2D.cpp +++ b/cocos/2d/CCTexture2D.cpp @@ -432,6 +432,7 @@ Texture2D::Texture2D() , _hasPremultipliedAlpha(false) , _hasMipmaps(false) , _shaderProgram(nullptr) +, _antialiasEnabled(true) { } @@ -621,16 +622,29 @@ bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat if (mipmapsNum == 1) { - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST); }else { - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _antialiasEnabled ? GL_LINEAR_MIPMAP_NEAREST : GL_NEAREST_MIPMAP_NEAREST); } - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _antialiasEnabled ? GL_LINEAR : GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); +#if CC_ENABLE_CACHE_TEXTURE_DATA + if (_antialiasEnabled) + { + TexParams texParams = {(GLuint)(_hasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR),GL_LINEAR,GL_NONE,GL_NONE}; + VolatileTextureMgr::setTexParameters(this, texParams); + } + else + { + TexParams texParams = {(GLuint)(_hasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST),GL_NEAREST,GL_NONE,GL_NONE}; + VolatileTextureMgr::setTexParameters(this, texParams); + } +#endif + CHECK_GL_ERROR_DEBUG(); // clean possible GL error // Specify OpenGL texture image @@ -1252,6 +1266,18 @@ void Texture2D::setTexParameters(const TexParams &texParams) void Texture2D::setAliasTexParameters() { + if (! _antialiasEnabled) + { + return; + } + + _antialiasEnabled = false; + + if (_name == 0) + { + return; + } + GL::bindTexture2D( _name ); if( ! _hasMipmaps ) @@ -1272,6 +1298,18 @@ void Texture2D::setAliasTexParameters() void Texture2D::setAntiAliasTexParameters() { + if ( _antialiasEnabled ) + { + return; + } + + _antialiasEnabled = true; + + if (_name == 0) + { + return; + } + GL::bindTexture2D( _name ); if( ! _hasMipmaps ) diff --git a/cocos/2d/CCTexture2D.h b/cocos/2d/CCTexture2D.h index 0adb50f4d5..eca4de9f8f 100644 --- a/cocos/2d/CCTexture2D.h +++ b/cocos/2d/CCTexture2D.h @@ -436,6 +436,8 @@ protected: GLProgram* _shaderProgram; static const PixelFormatInfoMap _pixelFormatInfoTables; + + bool _antialiasEnabled; };