From b73e97e40397602701dec244d80036f2d5a85505 Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Mon, 29 Aug 2016 16:33:52 +0800 Subject: [PATCH] add enableAntiAlias for Native for fireball/issues/2836 --- native/cocos/platform/CCGLView.cpp | 27 ++++++++++++++++++++++++ native/cocos/platform/CCGLView.h | 15 +++++++++++++ native/cocos/renderer/CCTexture2D.cpp | 1 + native/cocos/renderer/CCTextureCache.cpp | 9 ++++++++ native/cocos/renderer/CCTextureCache.h | 8 +++++++ 5 files changed, 60 insertions(+) diff --git a/native/cocos/platform/CCGLView.cpp b/native/cocos/platform/CCGLView.cpp index 993cc80ce9..9bb022f45b 100644 --- a/native/cocos/platform/CCGLView.cpp +++ b/native/cocos/platform/CCGLView.cpp @@ -101,6 +101,7 @@ GLContextAttrs GLView::getGLContextAttrs() GLView::GLView() : _scaleX(1.0f) , _scaleY(1.0f) +, _antiAliasEnabled(true) , _resolutionPolicy(ResolutionPolicy::UNKNOWN) { } @@ -457,5 +458,31 @@ float GLView::getScaleY() const return _scaleY; } +bool GLView::isAntiAliasEnabled() const +{ + return _antiAliasEnabled; +} + +void GLView::enableAntiAlias(bool enabled) +{ + if (_antiAliasEnabled == enabled) { + return; + } + _antiAliasEnabled = enabled; + + cocos2d::Vector list = Director::getInstance()->getTextureCache()->getAllTextures(); + for (size_t i = 0; i < list.size(); i++) { + Texture2D* tex = list.at(i); + if (tex) { + if (enabled) { + tex->setAntiAliasTexParameters(); + } + else { + tex->setAliasTexParameters(); + } + } + } +} + NS_CC_END diff --git a/native/cocos/platform/CCGLView.h b/native/cocos/platform/CCGLView.h index bccd5fa294..e43325b686 100644 --- a/native/cocos/platform/CCGLView.h +++ b/native/cocos/platform/CCGLView.h @@ -371,6 +371,19 @@ public: */ ResolutionPolicy getResolutionPolicy() const { return _resolutionPolicy; } + /** Returns whether the current enable on anti-alias. + * + * @return The antiAlias enabled. + */ + bool isAntiAliasEnabled() const; + + /** Whether to Enable on anti-alias. + * + * @param enabled The Enable or not anti-alias. + */ + void enableAntiAlias(bool enabled); + + #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) virtual HWND getWin32Window() = 0; #endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */ @@ -392,6 +405,8 @@ protected: Rect _viewPortRect; // the view name std::string _viewName; + // anti alias state + bool _antiAliasEnabled; float _scaleX; float _scaleY; diff --git a/native/cocos/renderer/CCTexture2D.cpp b/native/cocos/renderer/CCTexture2D.cpp index e36f6244c7..d726a45b3c 100644 --- a/native/cocos/renderer/CCTexture2D.cpp +++ b/native/cocos/renderer/CCTexture2D.cpp @@ -446,6 +446,7 @@ Texture2D::Texture2D() , _ninePatchInfo(nullptr) { s_allGLTexture2D.insert(this); + _antialiasEnabled = Director::getInstance()->getOpenGLView()->isAntiAliasEnabled(); } Texture2D::~Texture2D() diff --git a/native/cocos/renderer/CCTextureCache.cpp b/native/cocos/renderer/CCTextureCache.cpp index 24ca7a1a55..9ccb84976c 100644 --- a/native/cocos/renderer/CCTextureCache.cpp +++ b/native/cocos/renderer/CCTextureCache.cpp @@ -456,6 +456,15 @@ bool TextureCache::reloadTexture(const std::string& fileName) return ret; } +cocos2d::Vector TextureCache::getAllTextures() const +{ + cocos2d::Vector result; + for( auto it=_textures.begin(); it!=_textures.end(); ++it ) { + result.pushBack(it->second); + } + return result; +} + // TextureCache - Remove void TextureCache::removeAllTextures() diff --git a/native/cocos/renderer/CCTextureCache.h b/native/cocos/renderer/CCTextureCache.h index aee08409b1..a74a16a501 100644 --- a/native/cocos/renderer/CCTextureCache.h +++ b/native/cocos/renderer/CCTextureCache.h @@ -38,6 +38,7 @@ THE SOFTWARE. #include #include "base/CCRef.h" +#include "base/CCVector.h" #include "renderer/CCTexture2D.h" #include "platform/CCImage.h" @@ -151,6 +152,13 @@ public: */ bool reloadTexture(const std::string& fileName); + /** + * get all textures. + * + * @return all textures. + */ + cocos2d::Vector getAllTextures() const; + /** Purges the dictionary of loaded textures. * Call this method if you receive the "Memory Warning". * In the short term: it will free some resources preventing your app from being killed.