From bfa7991a9449bc7cf7c19a35f8ab55e07a2a47f0 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 18 Oct 2012 10:15:46 +0800 Subject: [PATCH 1/3] fixed #1466:restore shader when comming to background --- .../TestCpp/Classes/ShaderTest/ShaderTest.cpp | 18 ++++++++++++++++++ .../TestCpp/Classes/ShaderTest/ShaderTest.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp b/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp index ef476077fa..0e8e42e723 100644 --- a/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp +++ b/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp @@ -150,6 +150,11 @@ ShaderNode::ShaderNode() { } +ShaderNode::~ShaderNode() +{ + CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND); +} + ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag) { ShaderNode *node = new ShaderNode(); @@ -161,6 +166,10 @@ ShaderNode* ShaderNode::shaderNodeWithVertex(const char *vert, const char *frag) bool ShaderNode::initWithVertex(const char *vert, const char *frag) { + CCNotificationCenter::sharedNotificationCenter()->addObserver(this, + callfuncO_selector(ShaderNode::listenBackToForeground), + EVNET_COME_TO_FOREGROUND, + NULL); loadShaderVertex(vert, frag); @@ -171,10 +180,19 @@ bool ShaderNode::initWithVertex(const char *vert, const char *frag) setContentSize(CCSizeMake(SIZE_X, SIZE_Y)); setAnchorPoint(ccp(0.5f, 0.5f)); + + m_vertFileName = vert; + m_fragFileName = frag; return true; } +void ShaderNode::listenBackToForeground(CCObject *obj) +{ + this->setShaderProgram(NULL); + loadShaderVertex(m_vertFileName.c_str(), m_fragFileName.c_str()); +} + void ShaderNode::loadShaderVertex(const char *vert, const char *frag) { CCGLProgram *shader = new CCGLProgram(); diff --git a/samples/TestCpp/Classes/ShaderTest/ShaderTest.h b/samples/TestCpp/Classes/ShaderTest/ShaderTest.h index 22bfdea063..d6cdb0eab0 100644 --- a/samples/TestCpp/Classes/ShaderTest/ShaderTest.h +++ b/samples/TestCpp/Classes/ShaderTest/ShaderTest.h @@ -114,9 +114,11 @@ class ShaderNode : public CCNode { public: ShaderNode(); + ~ShaderNode(); bool initWithVertex(const char *vert, const char *frag); void loadShaderVertex(const char *vert, const char *frag); + void listenBackToForeground(CCObject *obj); virtual void update(float dt); virtual void setPosition(const CCPoint &newPosition); @@ -130,6 +132,8 @@ private: ccVertex2F m_resolution; float m_time; GLuint m_uniformCenter, m_uniformResolution, m_uniformTime; + std::string m_vertFileName; + std::string m_fragFileName; }; class ShaderTestScene : public TestScene From b6426f01ff6eeff6e8bea5090b43ee82594d7b5c Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 18 Oct 2012 10:39:56 +0800 Subject: [PATCH 2/3] fixed #1466:restore shader when comming to background --- .../TestCpp/Classes/ShaderTest/ShaderTest.cpp | 79 ++++++++++++------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp b/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp index 0e8e42e723..0b7350f0fd 100644 --- a/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp +++ b/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp @@ -456,9 +456,12 @@ std::string ShaderPlasma::subtitle() class SpriteBlur : public CCSprite { public: + ~SpriteBlur(); void setBlurSize(float f); bool initWithTexture(CCTexture2D* texture, const CCRect& rect); void draw(); + void initProgram(); + void listenBackToForeground(CCObject *obj); static SpriteBlur* create(const char *pszFileName); @@ -469,6 +472,11 @@ public: GLuint subLocation; }; +SpriteBlur::~SpriteBlur() +{ + CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EVNET_COME_TO_FOREGROUND); +} + SpriteBlur* SpriteBlur::create(const char *pszFileName) { SpriteBlur* pRet = new SpriteBlur(); @@ -484,48 +492,65 @@ SpriteBlur* SpriteBlur::create(const char *pszFileName) return pRet; } +void SpriteBlur::listenBackToForeground(CCObject *obj) +{ + setShaderProgram(NULL); + initProgram(); +} + bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect) { if( CCSprite::initWithTexture(texture, rect) ) { + CCNotificationCenter::sharedNotificationCenter()->addObserver(this, + callfuncO_selector(ShaderNode::listenBackToForeground), + EVNET_COME_TO_FOREGROUND, + NULL); + CCSize s = getTexture()->getContentSizeInPixels(); blur_ = ccp(1/s.width, 1/s.height); sub_[0] = sub_[1] = sub_[2] = sub_[3] = 0; - GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile( - CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString(); - CCGLProgram* pProgram = new CCGLProgram(); - pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); - setShaderProgram(pProgram); - pProgram->release(); - - CHECK_GL_ERROR_DEBUG(); - - getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); - getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color); - getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); - - CHECK_GL_ERROR_DEBUG(); - - getShaderProgram()->link(); - - CHECK_GL_ERROR_DEBUG(); - - getShaderProgram()->updateUniforms(); - - CHECK_GL_ERROR_DEBUG(); - - subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract"); - blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize"); - - CHECK_GL_ERROR_DEBUG(); + this->initProgram(); + return true; } return false; } +void SpriteBlur::initProgram() +{ + GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile( + CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString(); + CCGLProgram* pProgram = new CCGLProgram(); + pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); + setShaderProgram(pProgram); + pProgram->release(); + + CHECK_GL_ERROR_DEBUG(); + + getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position); + getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color); + getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); + + CHECK_GL_ERROR_DEBUG(); + + getShaderProgram()->link(); + + CHECK_GL_ERROR_DEBUG(); + + getShaderProgram()->updateUniforms(); + + CHECK_GL_ERROR_DEBUG(); + + subLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "substract"); + blurLocation = glGetUniformLocation( getShaderProgram()->getProgram(), "blurSize"); + + CHECK_GL_ERROR_DEBUG(); +} + void SpriteBlur::draw() { ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex ); From 501ceb21ebe2fe3f792ddb3f4b01157a9969984c Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 18 Oct 2012 11:00:44 +0800 Subject: [PATCH 3/3] fixed #1466:restore shader when comming to background --- samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp b/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp index 0b7350f0fd..565fecf34f 100644 --- a/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp +++ b/samples/TestCpp/Classes/ShaderTest/ShaderTest.cpp @@ -503,7 +503,7 @@ bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect) if( CCSprite::initWithTexture(texture, rect) ) { CCNotificationCenter::sharedNotificationCenter()->addObserver(this, - callfuncO_selector(ShaderNode::listenBackToForeground), + callfuncO_selector(SpriteBlur::listenBackToForeground), EVNET_COME_TO_FOREGROUND, NULL); @@ -523,7 +523,7 @@ bool SpriteBlur::initWithTexture(CCTexture2D* texture, const CCRect& rect) void SpriteBlur::initProgram() { GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile( - CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString(); + CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Shaders/example_Blur.fsh"))->getCString(); CCGLProgram* pProgram = new CCGLProgram(); pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, fragSource); setShaderProgram(pProgram);