From a13e8ae859899276339bea057362d9e82bed2c2f Mon Sep 17 00:00:00 2001 From: RongHong Date: Fri, 3 Jun 2011 15:22:50 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BF=AE=E6=AD=A3gles=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E8=BF=87=E4=BD=8E=E6=97=B6=E4=BC=9A=E5=B4=A9=E6=BA=83=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=202.=E4=BF=AE=E6=AD=A3unscheduleAllSelectors?= =?UTF-8?q?=EF=BC=88=EF=BC=89=E4=B8=AD=E9=81=8D=E5=8E=86=E5=93=88=E5=B8=8C?= =?UTF-8?q?=E8=A1=A8=E5=AF=BC=E8=87=B4=E5=B4=A9=E6=BA=83=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cocos2dx/CCScheduler.cpp | 8 +++- cocos2dx/cocos2d.cpp | 22 +++++++++++ cocos2dx/effects/CCGrabber.cpp | 37 +++++++++++++++++++ cocos2dx/effects/CCGrabber.h | 3 ++ cocos2dx/include/cocos2d.h | 11 ++++++ .../CCTransition.cpp | 6 +++ .../CCTransitionRadial.cpp | 6 +++ cocos2dx/misc_nodes/CCRenderTexture.cpp | 9 +++++ .../RenderTextureTest/RenderTextureTest.cpp | 25 ++++++++++++- .../tests/TransitionsTest/TransitionsTest.cpp | 35 ++++++++++++++++-- 10 files changed, 156 insertions(+), 6 deletions(-) diff --git a/cocos2dx/CCScheduler.cpp b/cocos2dx/CCScheduler.cpp index 8d7772a771..09d4437e0e 100644 --- a/cocos2dx/CCScheduler.cpp +++ b/cocos2dx/CCScheduler.cpp @@ -436,11 +436,15 @@ void CCScheduler::unscheduleUpdateForTarget(const SelectorProtocol *pTarget) void CCScheduler::unscheduleAllSelectors(void) { // Custom Selectors - tHashSelectorEntry *pElement; + tHashSelectorEntry *pElement = NULL; + tHashSelectorEntry *pNextElement = NULL; for (pElement = m_pHashForSelectors; pElement != NULL;) { + // pElement may be removed in unscheduleAllSelectorsForTarget + pNextElement = (tHashSelectorEntry *)pElement->hh.next; unscheduleAllSelectorsForTarget(pElement->target); - pElement = (tHashSelectorEntry *)pElement->hh.next; + + pElement = pNextElement; } // Updates selectors diff --git a/cocos2dx/cocos2d.cpp b/cocos2dx/cocos2d.cpp index 7402434f14..2acf81ec17 100644 --- a/cocos2dx/cocos2d.cpp +++ b/cocos2dx/cocos2d.cpp @@ -30,4 +30,26 @@ const char* cocos2dVersion() { return "cocos2d v0.99.5"; } + +eGLESVersion getGlesVersion() +{ + std::string strVersion((char *)glGetString(GL_VERSION)); + + if (strVersion.find("1.0") != -1) + { + return GLES_VER_1_0; + } + else if (strVersion.find("1.1") != -1) + { + return GLES_VER_1_1; + } + else if (strVersion.find("2.0") != -1) + { + return GLES_VER_2_0; + } + + return GLES_VER_INVALID; +} + + }//namespace cocos2d diff --git a/cocos2dx/effects/CCGrabber.cpp b/cocos2dx/effects/CCGrabber.cpp index e511f19491..0cc2122d45 100644 --- a/cocos2dx/effects/CCGrabber.cpp +++ b/cocos2dx/effects/CCGrabber.cpp @@ -33,12 +33,28 @@ namespace cocos2d : m_fbo(0) , m_oldFBO(0) { + m_eGlesVersion = getGlesVersion(); + + // If the gles version is lower than GLES_VER_1_0, + // all the functions in CCGrabber return directly. + if (m_eGlesVersion <= GLES_VER_1_0) + { + return ; + } + // generate FBO ccglGenFramebuffers(1, &m_fbo); } void CCGrabber::grab(cocos2d::CCTexture2D *pTexture) { + // If the gles version is lower than GLES_VER_1_0, + // all the functions in CCGrabber return directly. + if (m_eGlesVersion <= GLES_VER_1_0) + { + return ; + } + glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &m_oldFBO); // bind @@ -60,6 +76,13 @@ namespace cocos2d void CCGrabber::beforeRender(cocos2d::CCTexture2D *pTexture) { + // If the gles version is lower than GLES_VER_1_0, + // all the functions in CCGrabber return directly. + if (m_eGlesVersion <= GLES_VER_1_0) + { + return ; + } + glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &m_oldFBO); ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_fbo); @@ -78,12 +101,26 @@ namespace cocos2d void CCGrabber::afterRender(cocos2d::CCTexture2D *pTexture) { + // If the gles version is lower than GLES_VER_1_0, + // all the functions in CCGrabber return directly. + if (m_eGlesVersion <= GLES_VER_1_0) + { + return ; + } + ccglBindFramebuffer(CC_GL_FRAMEBUFFER, m_oldFBO); glColorMask(true, true, true, true); // #631 } CCGrabber::~CCGrabber() { + // If the gles version is lower than GLES_VER_1_0, + // all the functions in CCGrabber return directly. + if (m_eGlesVersion <= GLES_VER_1_0) + { + return ; + } + CCLOGINFO("cocos2d: deallocing %p", this); ccglDeleteFramebuffers(1, &m_fbo); } diff --git a/cocos2dx/effects/CCGrabber.h b/cocos2dx/effects/CCGrabber.h index 75e2f902a4..af320725e8 100644 --- a/cocos2dx/effects/CCGrabber.h +++ b/cocos2dx/effects/CCGrabber.h @@ -25,6 +25,7 @@ THE SOFTWARE. #ifndef __EFFECTS_CCGRABBER_H__ #define __EFFECTS_CCGRABBER_H__ +#include "cocos2d.h" #include "CCObject.h" #include "CCGL.h" @@ -46,6 +47,8 @@ namespace cocos2d protected: GLuint m_fbo; GLint m_oldFBO; + eGLESVersion m_eGlesVersion; + }; } // end of namespace cocos2d diff --git a/cocos2dx/include/cocos2d.h b/cocos2dx/include/cocos2d.h index 300ff4a890..3cba35e515 100644 --- a/cocos2dx/include/cocos2d.h +++ b/cocos2dx/include/cocos2d.h @@ -120,6 +120,17 @@ THE SOFTWARE. namespace cocos2d { const char* cocos2dVersion(); + +enum eGLESVersion +{ + GLES_VER_INVALID, + GLES_VER_1_0, + GLES_VER_1_1, + GLES_VER_2_0 +}; + +eGLESVersion CC_DLL getGlesVersion(); + }//namespace cocos2d #endif // __COCOS2D_H__ diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCTransition.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCTransition.cpp index 8e4554d514..d935207edf 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCTransition.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCTransition.cpp @@ -1114,6 +1114,12 @@ void CCTransitionCrossFade::onEnter() // create the first render texture for inScene CCRenderTexture* inTexture = CCRenderTexture::renderTextureWithWidthAndHeight((int)size.width, (int)size.height); + + if (NULL == inTexture) + { + return; + } + inTexture->getSprite()->setAnchorPoint( ccp(0.5f,0.5f) ); inTexture->setPosition( ccp(size.width/2, size.height/2) ); inTexture->setAnchorPoint( ccp(0.5f,0.5f) ); diff --git a/cocos2dx/layers_scenes_transitions_nodes/CCTransitionRadial.cpp b/cocos2dx/layers_scenes_transitions_nodes/CCTransitionRadial.cpp index 92bd0580eb..3cd592c229 100644 --- a/cocos2dx/layers_scenes_transitions_nodes/CCTransitionRadial.cpp +++ b/cocos2dx/layers_scenes_transitions_nodes/CCTransitionRadial.cpp @@ -62,6 +62,12 @@ void CCTransitionRadialCCW::onEnter() // create the second render texture for outScene CCRenderTexture *outTexture = CCRenderTexture::renderTextureWithWidthAndHeight((int)size.width, (int)size.height); + + if (NULL == outTexture) + { + return; + } + outTexture->getSprite()->setAnchorPoint(ccp(0.5f,0.5f)); outTexture->setPosition(ccp(size.width/2, size.height/2)); outTexture->setAnchorPoint(ccp(0.5f,0.5f)); diff --git a/cocos2dx/misc_nodes/CCRenderTexture.cpp b/cocos2dx/misc_nodes/CCRenderTexture.cpp index 08a3732f42..906a00e357 100644 --- a/cocos2dx/misc_nodes/CCRenderTexture.cpp +++ b/cocos2dx/misc_nodes/CCRenderTexture.cpp @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ +#include "cocos2d.h" #include "CCRenderTexture.h" #include "CCDirector.h" #include "platform/platform.h" @@ -84,8 +85,16 @@ CCRenderTexture * CCRenderTexture::renderTextureWithWidthAndHeight(int w, int h) CC_SAFE_DELETE(pRet) return NULL; } + bool CCRenderTexture::initWithWidthAndHeight(int w, int h, CCTexture2DPixelFormat eFormat) { + // If the gles version is lower than GLES_VER_1_0, + // some extended gles functions can't be implemented, so return false directly. + if (getGlesVersion() <= GLES_VER_1_0) + { + return false; + } + bool bRet = false; do { diff --git a/tests/tests/RenderTextureTest/RenderTextureTest.cpp b/tests/tests/RenderTextureTest/RenderTextureTest.cpp index 26b8411132..64738e2784 100644 --- a/tests/tests/RenderTextureTest/RenderTextureTest.cpp +++ b/tests/tests/RenderTextureTest/RenderTextureTest.cpp @@ -1,3 +1,4 @@ +#include "cocos2d.h" #include "RenderTextureTest.h" static int sceneIdx = -1; @@ -115,11 +116,24 @@ std::string RenderTextureTestDemo::subtitle() } RenderTextureTest::RenderTextureTest() +: m_brush(NULL) { + if (cocos2d::getGlesVersion() <= GLES_VER_1_0) + { + CCMessageBox("The Opengl ES version is lower than 1.1, and the test may not run correctly.", "Cocos2d-x Hint"); + return; + } + CCSize s = CCDirector::sharedDirector()->getWinSize(); // create a render texture, this is what we're going to draw into m_target = CCRenderTexture::renderTextureWithWidthAndHeight(s.width, s.height); + + if (NULL == m_target) + { + return; + } + m_target->setPosition(ccp(s.width/2, s.height/2)); // note that the render texture is a cocosnode, and contains a sprite of it's texture for convience, @@ -138,7 +152,11 @@ RenderTextureTest::RenderTextureTest() RenderTextureTest::~RenderTextureTest() { - m_brush->release(); + if (NULL != m_brush) + { + m_brush->release(); + m_brush = NULL; + } } void RenderTextureTest::ccTouchesMoved(CCSet* touches, CCEvent* event) @@ -203,6 +221,11 @@ RenderTextureIssue937::RenderTextureIssue937() /* A2 & B2 setup */ CCRenderTexture *rend = CCRenderTexture::renderTextureWithWidthAndHeight(32, 64); + if (NULL == rend) + { + return; + } + // It's possible to modify the RenderTexture blending function by // [[rend sprite] setBlendFunc:(ccBlendFunc) {GL_ONE, GL_ONE_MINUS_SRC_ALPHA}]; diff --git a/tests/tests/TransitionsTest/TransitionsTest.cpp b/tests/tests/TransitionsTest/TransitionsTest.cpp index 8a8b57fa03..16bbc889d0 100644 --- a/tests/tests/TransitionsTest/TransitionsTest.cpp +++ b/tests/tests/TransitionsTest/TransitionsTest.cpp @@ -212,10 +212,39 @@ CCTransitionScene* createTransition(int nIndex, ccTime t, CCScene* s) case 22: return CCTransitionSlideInR::transitionWithDuration(t, s); case 23: return CCTransitionSlideInT::transitionWithDuration(t, s); case 24: return CCTransitionSlideInB::transitionWithDuration(t, s); + case 25: + { + if (getGlesVersion() <= GLES_VER_1_0) + { + CCMessageBox("The Opengl ES version is lower than 1.1, and TransitionCrossFade may not run correctly, it is ignored.", "Cocos2d-x Hint"); + return NULL; + } + + return CCTransitionCrossFade::transitionWithDuration(t,s); + } + break; + case 26: + { + if (getGlesVersion() <= GLES_VER_1_0) + { + CCMessageBox("The Opengl ES version is lower than 1.1, and TransitionRadialCCW may not run correctly, it is ignored.", "Cocos2d-x Hint"); + return NULL; + } - case 25: return CCTransitionCrossFade::transitionWithDuration(t,s); - case 26: return CCTransitionRadialCCW::transitionWithDuration(t,s); - case 27: return CCTransitionRadialCW::transitionWithDuration(t,s); + return CCTransitionRadialCCW::transitionWithDuration(t,s); + } + break; + case 27: + { + if (getGlesVersion() <= GLES_VER_1_0) + { + CCMessageBox("The Opengl ES version is lower than 1.1, and TransitionRadialCW may not run correctly, it is ignored.", "Cocos2d-x Hint"); + return NULL; + } + + return CCTransitionRadialCW::transitionWithDuration(t,s); + } + break; case 28: return PageTransitionForward::transitionWithDuration(t, s); case 29: return PageTransitionBackward::transitionWithDuration(t, s); case 30: return CCTransitionFadeTR::transitionWithDuration(t, s);