From 624e27497a062345668a983ae8815040f24dc094 Mon Sep 17 00:00:00 2001 From: RongHong Date: Tue, 6 Sep 2011 09:51:35 +0800 Subject: [PATCH 1/3] fixed #696 Some android machines may crash at CCRenderTexture::end(bool bIsTOCasheTexture) --- cocos2dx/include/CCRenderTexture.h | 4 +- cocos2dx/misc_nodes/CCRenderTexture.cpp | 54 ++++++++++++------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/cocos2dx/include/CCRenderTexture.h b/cocos2dx/include/CCRenderTexture.h index bf8d51bcca..347acc6809 100644 --- a/cocos2dx/include/CCRenderTexture.h +++ b/cocos2dx/include/CCRenderTexture.h @@ -80,7 +80,7 @@ public: #if CC_ENABLE_CACHE_TEXTTURE_DATA /** ends grabbing for android */ - void end(bool bIsTOCasheTexture = true); + void end(bool bIsTOCacheTexture = true); #else /** ends grabbing */ void end(); @@ -119,7 +119,7 @@ protected: GLuint m_uFBO; GLint m_nOldFBO; CCTexture2D *m_pTexture; - GLubyte *m_pTextureDataBuffer; + CCImage *m_pUITextureImage; GLenum m_ePixelFormat; }; diff --git a/cocos2dx/misc_nodes/CCRenderTexture.cpp b/cocos2dx/misc_nodes/CCRenderTexture.cpp index d154d0964f..f5b0b2a536 100644 --- a/cocos2dx/misc_nodes/CCRenderTexture.cpp +++ b/cocos2dx/misc_nodes/CCRenderTexture.cpp @@ -42,7 +42,7 @@ CCRenderTexture::CCRenderTexture() , m_nOldFBO(0) , m_pTexture(0) , m_ePixelFormat(kCCTexture2DPixelFormat_RGBA8888) -, m_pTextureDataBuffer(NULL) +, m_pUITextureImage(NULL) { } @@ -51,12 +51,7 @@ CCRenderTexture::~CCRenderTexture() removeAllChildrenWithCleanup(true); ccglDeleteFramebuffers(1, &m_uFBO); - if (NULL != m_pTextureDataBuffer) - { - delete []m_pTextureDataBuffer; - m_pTextureDataBuffer = NULL; - } - + CC_SAFE_DELETE(m_pUITextureImage); } CCSprite * CCRenderTexture::getSprite() @@ -210,31 +205,33 @@ void CCRenderTexture::beginWithClear(float r, float g, float b, float a) } #if CC_ENABLE_CACHE_TEXTTURE_DATA - void CCRenderTexture::end(bool bIsTOCasheTexture) + void CCRenderTexture::end(bool bIsTOCacheTexture) { - if (bIsTOCasheTexture) - { - if (NULL != m_pTextureDataBuffer) - { - delete []m_pTextureDataBuffer; - m_pTextureDataBuffer = NULL; - } - - // to get the rendered texture data - const CCSize& s = m_pTexture->getContentSizeInPixels(); - int tx = (int)s.width; - int ty = (int)s.height; - m_pTextureDataBuffer = new GLubyte[tx * ty * 4]; - glReadPixels(0,0,tx,ty,GL_RGBA,GL_UNSIGNED_BYTE, m_pTextureDataBuffer); - VolatileTexture::addDataTexture(m_pTexture, m_pTextureDataBuffer, kTexture2DPixelFormat_RGBA8888, s); - } - ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_nOldFBO); // Restore the original matrix and viewport glPopMatrix(); CCSize size = CCDirector::sharedDirector()->getDisplaySizeInPixels(); // glViewport(0, 0, (GLsizei)size.width, (GLsizei)size.height); CCDirector::sharedDirector()->getOpenGLView()->setViewPortInPoints(0, 0, size.width, size.height); + + if (bIsTOCacheTexture) + { + CC_SAFE_DELETE(m_pUITextureImage); + + // to get the rendered texture data + const CCSize& s = m_pTexture->getContentSizeInPixels(); + int tx = (int)s.width; + int ty = (int)s.height; + m_pUITextureImage = new CCImage; + if (true == getUIImageFromBuffer(m_pUITextureImage, 0, 0, tx, ty)) + { + VolatileTexture::addDataTexture(m_pTexture, m_pUITextureImage->getData(), kTexture2DPixelFormat_RGBA8888, s); + } + else + { + CCLOG("Cache rendertexture failed!"); + } + } } #else void CCRenderTexture::end() @@ -355,8 +352,11 @@ bool CCRenderTexture::getUIImageFromBuffer(CCImage *pImage, int x, int y, int nW this->begin(); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0,0,nReadBufferWidth,nReadBufferHeight,GL_RGBA,GL_UNSIGNED_BYTE, pTempData); - this->end(); - +#if CC_ENABLE_CACHE_TEXTTURE_DATA + this->end(false); +#else + this->end(); +#endif // to get the actual texture data // #640 the image read from rendertexture is upseted for (int i = 0; i < nSavedBufferHeight; ++i) From fea3da3156485f078368846ea0905be73b011fa9 Mon Sep 17 00:00:00 2001 From: RongHong Date: Tue, 6 Sep 2011 11:33:48 +0800 Subject: [PATCH 2/3] fixed #696 Some android machines may crash at CCRenderTexture::end(bool bIsTOCasheTexture) --- cocos2dx/include/CCRenderTexture.h | 8 +-- cocos2dx/misc_nodes/CCRenderTexture.cpp | 68 +++++++++++-------------- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/cocos2dx/include/CCRenderTexture.h b/cocos2dx/include/CCRenderTexture.h index 347acc6809..f912510d97 100644 --- a/cocos2dx/include/CCRenderTexture.h +++ b/cocos2dx/include/CCRenderTexture.h @@ -78,13 +78,9 @@ public: /** end is key word of lua, use other name to export to lua. */ inline void endToLua(){ end();}; -#if CC_ENABLE_CACHE_TEXTTURE_DATA - /** ends grabbing for android */ + /** ends grabbing*/ + // para bIsTOCacheTexture the parameter is only used for android to cache the texture void end(bool bIsTOCacheTexture = true); -#else - /** ends grabbing */ - void end(); -#endif /** clears the texture with a color */ void clear(float r, float g, float b, float a); diff --git a/cocos2dx/misc_nodes/CCRenderTexture.cpp b/cocos2dx/misc_nodes/CCRenderTexture.cpp index f5b0b2a536..75fec26a66 100644 --- a/cocos2dx/misc_nodes/CCRenderTexture.cpp +++ b/cocos2dx/misc_nodes/CCRenderTexture.cpp @@ -204,46 +204,36 @@ void CCRenderTexture::beginWithClear(float r, float g, float b, float a) glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); } -#if CC_ENABLE_CACHE_TEXTTURE_DATA - void CCRenderTexture::end(bool bIsTOCacheTexture) - { - ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_nOldFBO); - // Restore the original matrix and viewport - glPopMatrix(); - CCSize size = CCDirector::sharedDirector()->getDisplaySizeInPixels(); - // glViewport(0, 0, (GLsizei)size.width, (GLsizei)size.height); - CCDirector::sharedDirector()->getOpenGLView()->setViewPortInPoints(0, 0, size.width, size.height); - - if (bIsTOCacheTexture) - { - CC_SAFE_DELETE(m_pUITextureImage); - - // to get the rendered texture data - const CCSize& s = m_pTexture->getContentSizeInPixels(); - int tx = (int)s.width; - int ty = (int)s.height; - m_pUITextureImage = new CCImage; - if (true == getUIImageFromBuffer(m_pUITextureImage, 0, 0, tx, ty)) - { - VolatileTexture::addDataTexture(m_pTexture, m_pUITextureImage->getData(), kTexture2DPixelFormat_RGBA8888, s); - } - else - { - CCLOG("Cache rendertexture failed!"); - } - } - } -#else - void CCRenderTexture::end() - { - ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_nOldFBO); - // Restore the original matrix and viewport - glPopMatrix(); - CCSize size = CCDirector::sharedDirector()->getDisplaySizeInPixels(); +void CCRenderTexture::end(bool bIsTOCacheTexture) +{ + ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_nOldFBO); + // Restore the original matrix and viewport + glPopMatrix(); + CCSize size = CCDirector::sharedDirector()->getDisplaySizeInPixels(); // glViewport(0, 0, (GLsizei)size.width, (GLsizei)size.height); - CCDirector::sharedDirector()->getOpenGLView()->setViewPortInPoints(0, 0, size.width, size.height); - } -#endif + CCDirector::sharedDirector()->getOpenGLView()->setViewPortInPoints(0, 0, size.width, size.height); + +#if CC_ENABLE_CACHE_TEXTTURE_DATA + if (bIsTOCacheTexture) + { + CC_SAFE_DELETE(m_pUITextureImage); + + // to get the rendered texture data + const CCSize& s = m_pTexture->getContentSizeInPixels(); + int tx = (int)s.width; + int ty = (int)s.height; + m_pUITextureImage = new CCImage; + if (true == getUIImageFromBuffer(m_pUITextureImage, 0, 0, tx, ty)) + { + VolatileTexture::addDataTexture(m_pTexture, m_pUITextureImage->getData(), kTexture2DPixelFormat_RGBA8888, s); + } + else + { + CCLOG("Cache rendertexture failed!"); + } + } +#endif +} void CCRenderTexture::clear(float r, float g, float b, float a) { From 53227073a73e3284e550862a2fe03c24e18e2ae3 Mon Sep 17 00:00:00 2001 From: RongHong Date: Tue, 6 Sep 2011 11:43:09 +0800 Subject: [PATCH 3/3] fixed #696 to modify some end() usilization --- cocos2dx/misc_nodes/CCRenderTexture.cpp | 5 +---- tests/tests/RenderTextureTest/RenderTextureTest.cpp | 4 ---- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/cocos2dx/misc_nodes/CCRenderTexture.cpp b/cocos2dx/misc_nodes/CCRenderTexture.cpp index 75fec26a66..8ecdc913e5 100644 --- a/cocos2dx/misc_nodes/CCRenderTexture.cpp +++ b/cocos2dx/misc_nodes/CCRenderTexture.cpp @@ -342,11 +342,8 @@ bool CCRenderTexture::getUIImageFromBuffer(CCImage *pImage, int x, int y, int nW this->begin(); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0,0,nReadBufferWidth,nReadBufferHeight,GL_RGBA,GL_UNSIGNED_BYTE, pTempData); -#if CC_ENABLE_CACHE_TEXTTURE_DATA this->end(false); -#else - this->end(); -#endif + // to get the actual texture data // #640 the image read from rendertexture is upseted for (int i = 0; i < nSavedBufferHeight; ++i) diff --git a/tests/tests/RenderTextureTest/RenderTextureTest.cpp b/tests/tests/RenderTextureTest/RenderTextureTest.cpp index edb0d9f7e4..1393f4f645 100644 --- a/tests/tests/RenderTextureTest/RenderTextureTest.cpp +++ b/tests/tests/RenderTextureTest/RenderTextureTest.cpp @@ -196,11 +196,7 @@ void RenderTextureTest::ccTouchesMoved(CCSet* touches, CCEvent* event) } } // finish drawing and return context back to the screen -#if CC_ENABLE_CACHE_TEXTTURE_DATA m_target->end(false); -#else - m_target->end(); -#endif } void RenderTextureTest::ccTouchesEnded(CCSet* touches, CCEvent* event)