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