diff --git a/cocos2dx/draw_nodes/CCDrawNode.cpp b/cocos2dx/draw_nodes/CCDrawNode.cpp index 50a99ad086..ab715e48ac 100644 --- a/cocos2dx/draw_nodes/CCDrawNode.cpp +++ b/cocos2dx/draw_nodes/CCDrawNode.cpp @@ -23,6 +23,8 @@ #include "CCDrawNode.h" #include "shaders/CCShaderCache.h" #include "CCGL.h" +#include "support/CCNotificationCenter.h" +#include "CCEventType.h" NS_CC_BEGIN @@ -118,6 +120,8 @@ DrawNode::~DrawNode() ccGLBindVAO(0); _vao = 0; #endif + + NotificationCenter::getInstance()->removeObserver(this, EVNET_COME_TO_FOREGROUND); } DrawNode* DrawNode::create() @@ -157,18 +161,17 @@ bool DrawNode::init() glGenVertexArrays(1, &_vao); ccGLBindVAO(_vao); #endif - + glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(V2F_C4B_T2F)* _bufferCapacity, _buffer, GL_STREAM_DRAW); - glEnableVertexAttribArray(kVertexAttrib_Position); + ccGLEnableVertexAttribs( kVertexAttribFlag_PosColorTex ); + glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices)); - glEnableVertexAttribArray(kVertexAttrib_Color); glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, colors)); - glEnableVertexAttribArray(kVertexAttrib_TexCoords); glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, texCoords)); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -181,6 +184,12 @@ bool DrawNode::init() _dirty = true; + // Need to listen the event only when not use batchnode, because it will use VBO + NotificationCenter::getInstance()->addObserver(this, + callfuncO_selector(DrawNode::listenBackToForeground), + EVNET_COME_TO_FOREGROUND, + NULL); + return true; } @@ -196,6 +205,7 @@ void DrawNode::render() ccGLBindVAO(_vao); #else ccGLEnableVertexAttribs(kVertexAttribFlag_PosColorTex); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); // vertex glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(V2F_C4B_T2F), (GLvoid *)offsetof(V2F_C4B_T2F, vertices)); @@ -216,11 +226,9 @@ void DrawNode::render() void DrawNode::draw() { + CC_NODE_DRAW_SETUP(); ccGLBlendFunc(_blendFunc.src, _blendFunc.dst); - - getShaderProgram()->use(); - getShaderProgram()->setUniformsForBuiltins(); - + render(); } @@ -440,4 +448,11 @@ void DrawNode::setBlendFunc(const BlendFunc &blendFunc) _blendFunc = blendFunc; } +/** listen the event that coming to foreground on Android + */ +void DrawNode::listenBackToForeground(Object *obj) +{ + init(); +} + NS_CC_END diff --git a/cocos2dx/draw_nodes/CCDrawNode.h b/cocos2dx/draw_nodes/CCDrawNode.h index 3c74e56e5e..70bebf186d 100644 --- a/cocos2dx/draw_nodes/CCDrawNode.h +++ b/cocos2dx/draw_nodes/CCDrawNode.h @@ -79,6 +79,10 @@ public: DrawNode(); + /** listen the event that coming to foreground on Android + */ + void listenBackToForeground(Object *obj); + private: void ensureCapacity(unsigned int count); void render(); diff --git a/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp b/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp index 9503ab8d63..1a9345401f 100644 --- a/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp +++ b/cocos2dx/particle_nodes/CCParticleSystemQuad.cpp @@ -483,16 +483,14 @@ void ParticleSystemQuad::setupVBOandVAO() glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _totalParticles, _quads, GL_DYNAMIC_DRAW); + ccGLEnableVertexAttribs(kVertexAttribFlag_PosColorTex); // vertices - glEnableVertexAttribArray(kVertexAttrib_Position); glVertexAttribPointer(kVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices)); // colors - glEnableVertexAttribArray(kVertexAttrib_Color); glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors)); // tex coords - glEnableVertexAttribArray(kVertexAttrib_TexCoords); glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); diff --git a/cocos2dx/shaders/CCGLProgram.cpp b/cocos2dx/shaders/CCGLProgram.cpp index a525687e31..1a44724e29 100644 --- a/cocos2dx/shaders/CCGLProgram.cpp +++ b/cocos2dx/shaders/CCGLProgram.cpp @@ -241,7 +241,7 @@ bool GLProgram::link() _vertShader = _fragShader = 0; -#if DEBUG +#if COCOS2D_DEBUG glGetProgramiv(_program, GL_LINK_STATUS, &status); if (status == GL_FALSE) diff --git a/cocos2dx/shaders/ccGLStateCache.cpp b/cocos2dx/shaders/ccGLStateCache.cpp index 74a83097a7..549719f652 100644 --- a/cocos2dx/shaders/ccGLStateCache.cpp +++ b/cocos2dx/shaders/ccGLStateCache.cpp @@ -76,7 +76,11 @@ void ccGLInvalidateStateCache( void ) s_eBlendingSource = -1; s_eBlendingDest = -1; s_eGLServerState = 0; +#if CC_TEXTURE_ATLAS_USE_VAO + s_uVAO = 0; #endif + +#endif // CC_ENABLE_GL_STATE_CACHE } void ccGLDeleteProgram( GLuint program ) diff --git a/cocos2dx/textures/CCTextureAtlas.cpp b/cocos2dx/textures/CCTextureAtlas.cpp index 222532a96a..59e3486a12 100644 --- a/cocos2dx/textures/CCTextureAtlas.cpp +++ b/cocos2dx/textures/CCTextureAtlas.cpp @@ -255,16 +255,14 @@ void TextureAtlas::setupVBOandVAO() glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _capacity, _quads, GL_DYNAMIC_DRAW); + ccGLEnableVertexAttribs(kVertexAttribFlag_PosColorTex); // vertices - glEnableVertexAttribArray(kVertexAttrib_Position); glVertexAttribPointer(kVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, vertices)); // colors - glEnableVertexAttribArray(kVertexAttrib_Color); glVertexAttribPointer(kVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, colors)); // tex coords - glEnableVertexAttribArray(kVertexAttrib_TexCoords); glVertexAttribPointer(kVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); diff --git a/plugin/samples/HelloAnalytics-JS/proj.android/jni/hellocpp/main.cpp b/plugin/samples/HelloAnalytics-JS/proj.android/jni/hellocpp/main.cpp index d602be8273..26a98e22fe 100644 --- a/plugin/samples/HelloAnalytics-JS/proj.android/jni/hellocpp/main.cpp +++ b/plugin/samples/HelloAnalytics-JS/proj.android/jni/hellocpp/main.cpp @@ -34,10 +34,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/plugin/samples/HelloIAP-JS/proj.android/jni/hellocpp/main.cpp b/plugin/samples/HelloIAP-JS/proj.android/jni/hellocpp/main.cpp index d602be8273..26a98e22fe 100644 --- a/plugin/samples/HelloIAP-JS/proj.android/jni/hellocpp/main.cpp +++ b/plugin/samples/HelloIAP-JS/proj.android/jni/hellocpp/main.cpp @@ -34,10 +34,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/plugin/samples/HelloPlugins/proj.android/jni/hellocpp/main.cpp b/plugin/samples/HelloPlugins/proj.android/jni/hellocpp/main.cpp index 8c0ced20a8..5a68d26107 100644 --- a/plugin/samples/HelloPlugins/proj.android/jni/hellocpp/main.cpp +++ b/plugin/samples/HelloPlugins/proj.android/jni/hellocpp/main.cpp @@ -33,10 +33,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Cpp/AssetsManagerTest/proj.android/jni/hellocpp/main.cpp b/samples/Cpp/AssetsManagerTest/proj.android/jni/hellocpp/main.cpp index 4c4ff0ded1..ac6f58ba07 100644 --- a/samples/Cpp/AssetsManagerTest/proj.android/jni/hellocpp/main.cpp +++ b/samples/Cpp/AssetsManagerTest/proj.android/jni/hellocpp/main.cpp @@ -30,10 +30,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Cpp/HelloCpp/proj.android/jni/hellocpp/main.cpp b/samples/Cpp/HelloCpp/proj.android/jni/hellocpp/main.cpp index 47292fa901..7d495c10a1 100644 --- a/samples/Cpp/HelloCpp/proj.android/jni/hellocpp/main.cpp +++ b/samples/Cpp/HelloCpp/proj.android/jni/hellocpp/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Cpp/SimpleGame/proj.android/jni/hellocpp/main.cpp b/samples/Cpp/SimpleGame/proj.android/jni/hellocpp/main.cpp index a48824a8ac..e8639bcc2d 100644 --- a/samples/Cpp/SimpleGame/proj.android/jni/hellocpp/main.cpp +++ b/samples/Cpp/SimpleGame/proj.android/jni/hellocpp/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Cpp/TestCpp/proj.android/jni/testcpp/main.cpp b/samples/Cpp/TestCpp/proj.android/jni/testcpp/main.cpp index 112914f998..e503aab97c 100644 --- a/samples/Cpp/TestCpp/proj.android/jni/testcpp/main.cpp +++ b/samples/Cpp/TestCpp/proj.android/jni/testcpp/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Javascript/CocosDragonJS/proj.android/jni/cocosdragonjs/main.cpp b/samples/Javascript/CocosDragonJS/proj.android/jni/cocosdragonjs/main.cpp index 61ccd4a230..1f5171ccb1 100644 --- a/samples/Javascript/CocosDragonJS/proj.android/jni/cocosdragonjs/main.cpp +++ b/samples/Javascript/CocosDragonJS/proj.android/jni/cocosdragonjs/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Javascript/CrystalCraze/proj.android/jni/crystalcraze/main.cpp b/samples/Javascript/CrystalCraze/proj.android/jni/crystalcraze/main.cpp index 61ccd4a230..1f5171ccb1 100644 --- a/samples/Javascript/CrystalCraze/proj.android/jni/crystalcraze/main.cpp +++ b/samples/Javascript/CrystalCraze/proj.android/jni/crystalcraze/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Javascript/MoonWarriors/proj.android/jni/moonwarriors/main.cpp b/samples/Javascript/MoonWarriors/proj.android/jni/moonwarriors/main.cpp index 61ccd4a230..1f5171ccb1 100644 --- a/samples/Javascript/MoonWarriors/proj.android/jni/moonwarriors/main.cpp +++ b/samples/Javascript/MoonWarriors/proj.android/jni/moonwarriors/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Javascript/TestJavascript/proj.android/jni/testjavascript/main.cpp b/samples/Javascript/TestJavascript/proj.android/jni/testjavascript/main.cpp index 61ccd4a230..1f5171ccb1 100644 --- a/samples/Javascript/TestJavascript/proj.android/jni/testjavascript/main.cpp +++ b/samples/Javascript/TestJavascript/proj.android/jni/testjavascript/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Javascript/WatermelonWithMe/proj.android/jni/watermelonwithme/main.cpp b/samples/Javascript/WatermelonWithMe/proj.android/jni/watermelonwithme/main.cpp index 61ccd4a230..1f5171ccb1 100644 --- a/samples/Javascript/WatermelonWithMe/proj.android/jni/watermelonwithme/main.cpp +++ b/samples/Javascript/WatermelonWithMe/proj.android/jni/watermelonwithme/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Lua/HelloLua/proj.android/jni/hellolua/main.cpp b/samples/Lua/HelloLua/proj.android/jni/hellolua/main.cpp index 3abc83ff7c..51516c9603 100644 --- a/samples/Lua/HelloLua/proj.android/jni/hellolua/main.cpp +++ b/samples/Lua/HelloLua/proj.android/jni/hellolua/main.cpp @@ -31,10 +31,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/samples/Lua/TestLua/proj.android/jni/testlua/main.cpp b/samples/Lua/TestLua/proj.android/jni/testlua/main.cpp index 54c7a4b8c8..e503aab97c 100644 --- a/samples/Lua/TestLua/proj.android/jni/testlua/main.cpp +++ b/samples/Lua/TestLua/proj.android/jni/testlua/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/template/multi-platform-cpp/proj.android/jni/hellocpp/main.cpp b/template/multi-platform-cpp/proj.android/jni/hellocpp/main.cpp index 112914f998..e503aab97c 100644 --- a/template/multi-platform-cpp/proj.android/jni/hellocpp/main.cpp +++ b/template/multi-platform-cpp/proj.android/jni/hellocpp/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/template/multi-platform-js/proj.android/jni/hellojavascript/main.cpp b/template/multi-platform-js/proj.android/jni/hellojavascript/main.cpp index 61ccd4a230..1f5171ccb1 100644 --- a/template/multi-platform-js/proj.android/jni/hellojavascript/main.cpp +++ b/template/multi-platform-js/proj.android/jni/hellojavascript/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues(); diff --git a/template/multi-platform-lua/proj.android/jni/hellolua/main.cpp b/template/multi-platform-lua/proj.android/jni/hellolua/main.cpp index 54c7a4b8c8..e503aab97c 100644 --- a/template/multi-platform-lua/proj.android/jni/hellolua/main.cpp +++ b/template/multi-platform-lua/proj.android/jni/hellolua/main.cpp @@ -32,10 +32,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi } else { - ccDrawInit(); ccGLInvalidateStateCache(); - ShaderCache::getInstance()->reloadDefaultShaders(); + ccDrawInit(); TextureCache::reloadAllTextures(); NotificationCenter::getInstance()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); Director::getInstance()->setGLDefaultValues();