Merge pull request #9699 from super626/fixandroid

fix background/foreground switch
This commit is contained in:
minggo 2014-12-30 17:07:38 +08:00
commit d517831e01
2 changed files with 108 additions and 10 deletions

View File

@ -295,6 +295,27 @@ Sprite3DUVAnimationTest::Sprite3DUVAnimationTest()
//the callback function update cylinder's texcoord //the callback function update cylinder's texcoord
schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate)); schedule(schedule_selector(Sprite3DUVAnimationTest::cylinderUpdate));
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
auto glProgram = _state->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames("Sprite3DTest/cylinder.vert", "Sprite3DTest/cylinder.frag");
glProgram->link();
glProgram->updateUniforms();
}
);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
Sprite3DUVAnimationTest::~Sprite3DUVAnimationTest()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
} }
std::string Sprite3DUVAnimationTest::title() const std::string Sprite3DUVAnimationTest::title() const
@ -358,15 +379,15 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
// use custom shader // use custom shader
auto shader =GLProgram::createWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag"); auto shader =GLProgram::createWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag");
auto state = GLProgramState::create(shader); _state = GLProgramState::create(shader);
_plane->setGLProgramState(state); _plane->setGLProgramState(_state);
//pass mesh's attribute to shader //pass mesh's attribute to shader
long offset = 0; long offset = 0;
auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount(); auto attributeCount = _plane->getMesh()->getMeshVertexAttribCount();
for (auto i = 0; i < attributeCount; i++) { for (auto i = 0; i < attributeCount; i++) {
auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i); auto meshattribute = _plane->getMesh()->getMeshVertexAttribute(i);
state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size, meshattribute.size,
meshattribute.type, meshattribute.type,
GL_FALSE, GL_FALSE,
@ -374,7 +395,7 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
(GLvoid*)offset); (GLvoid*)offset);
offset += meshattribute.attribSizeBytes; offset += meshattribute.attribSizeBytes;
} }
state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform()); _state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform());
//create shadow texture //create shadow texture
auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png"); auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png");
@ -384,7 +405,7 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
tRepeatParams.wrapS = GL_CLAMP_TO_EDGE; tRepeatParams.wrapS = GL_CLAMP_TO_EDGE;
tRepeatParams.wrapT = GL_CLAMP_TO_EDGE; tRepeatParams.wrapT = GL_CLAMP_TO_EDGE;
shadowTexture->setTexParameters(tRepeatParams); shadowTexture->setTexParameters(tRepeatParams);
state->setUniformTexture("u_shadowTexture",shadowTexture); _state->setUniformTexture("u_shadowTexture",shadowTexture);
layer->addChild(_plane); layer->addChild(_plane);
//create the orc //create the orc
@ -399,6 +420,39 @@ Sprite3DFakeShadowTest::Sprite3DFakeShadowTest()
layer->setCameraMask(2); layer->setCameraMask(2);
schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f); schedule(CC_SCHEDULE_SELECTOR(Sprite3DFakeShadowTest::updateCamera), 0.0f);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
auto glProgram = _state->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames("Sprite3DTest/simple_shadow.vert","Sprite3DTest/simple_shadow.frag");
glProgram->link();
glProgram->updateUniforms();
_state->setUniformMat4("u_model_matrix",_plane->getNodeToWorldTransform());
//create shadow texture
auto shadowTexture = Director::getInstance()->getTextureCache()->addImage("Sprite3DTest/shadowCircle.png");
Texture2D::TexParams tRepeatParams;//set texture parameters
tRepeatParams.magFilter = GL_LINEAR;
tRepeatParams.minFilter = GL_LINEAR;
tRepeatParams.wrapS = GL_CLAMP_TO_EDGE;
tRepeatParams.wrapT = GL_CLAMP_TO_EDGE;
shadowTexture->setTexParameters(tRepeatParams);
_state->setUniformTexture("u_shadowTexture",shadowTexture);
}
);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
Sprite3DFakeShadowTest::~Sprite3DFakeShadowTest()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
} }
std::string Sprite3DFakeShadowTest::title() const std::string Sprite3DFakeShadowTest::title() const
@ -573,8 +627,8 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest()
auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b"); auto teapot = Sprite3D::create("Sprite3DTest/teapot.c3b");
//create and set our custom shader //create and set our custom shader
auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag"); auto shader =GLProgram::createWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag");
auto state = GLProgramState::create(shader); _state = GLProgramState::create(shader);
teapot->setGLProgramState(state); teapot->setGLProgramState(_state);
teapot->setPosition3D(Vec3(0,-5,-20)); teapot->setPosition3D(Vec3(0,-5,-20));
teapot->setRotation3D(Vec3(-90,180,0)); teapot->setRotation3D(Vec3(-90,180,0));
auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0)); auto rotate_action = RotateBy::create(1.5,Vec3(0,30,0));
@ -584,7 +638,7 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest()
auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount(); auto attributeCount = teapot->getMesh()->getMeshVertexAttribCount();
for (auto i = 0; i < attributeCount; i++) { for (auto i = 0; i < attributeCount; i++) {
auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i); auto meshattribute = teapot->getMesh()->getMeshVertexAttribute(i);
state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib], _state->setVertexAttribPointer(s_attributeNames[meshattribute.vertexAttrib],
meshattribute.size, meshattribute.size,
meshattribute.type, meshattribute.type,
GL_FALSE, GL_FALSE,
@ -595,6 +649,27 @@ Sprite3DBasicToonShaderTest::Sprite3DBasicToonShaderTest()
addChild(teapot); addChild(teapot);
addChild(_camera); addChild(_camera);
setCameraMask(2); setCameraMask(2);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
[this](EventCustom*)
{
auto glProgram = _state->getGLProgram();
glProgram->reset();
glProgram->initWithFilenames("Sprite3DTest/toon.vert","Sprite3DTest/toon.frag");
glProgram->link();
glProgram->updateUniforms();
}
);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
Sprite3DBasicToonShaderTest::~Sprite3DBasicToonShaderTest()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
} }
std::string Sprite3DBasicToonShaderTest::title() const std::string Sprite3DBasicToonShaderTest::title() const
@ -639,7 +714,10 @@ Sprite3DLightMapTest::Sprite3DLightMapTest()
listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DLightMapTest::onTouchesMoved, this); listener->onTouchesMoved = CC_CALLBACK_2(Sprite3DLightMapTest::onTouchesMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
} }
Sprite3DLightMapTest::~Sprite3DLightMapTest()
{
}
std::string Sprite3DLightMapTest::title() const std::string Sprite3DLightMapTest::title() const
{ {
return "light map test"; return "light map test";

View File

@ -71,6 +71,7 @@ class Sprite3DUVAnimationTest : public Sprite3DTestDemo
public: public:
CREATE_FUNC(Sprite3DUVAnimationTest); CREATE_FUNC(Sprite3DUVAnimationTest);
Sprite3DUVAnimationTest(); Sprite3DUVAnimationTest();
virtual ~Sprite3DUVAnimationTest();
virtual std::string title() const override; virtual std::string title() const override;
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
@ -80,6 +81,10 @@ protected:
float _cylinder_texture_offset; float _cylinder_texture_offset;
float _shining_duraion; float _shining_duraion;
GLProgramState * _state; GLProgramState * _state;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
EventListenerCustom* _backToForegroundListener;
#endif
}; };
class Sprite3DFakeShadowTest : public Sprite3DTestDemo class Sprite3DFakeShadowTest : public Sprite3DTestDemo
@ -94,6 +99,7 @@ public:
}; };
CREATE_FUNC(Sprite3DFakeShadowTest); CREATE_FUNC(Sprite3DFakeShadowTest);
Sprite3DFakeShadowTest(); Sprite3DFakeShadowTest();
virtual ~Sprite3DFakeShadowTest();
virtual std::string title() const override; virtual std::string title() const override;
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
void Move(cocos2d::Ref* sender,int value); void Move(cocos2d::Ref* sender,int value);
@ -110,6 +116,11 @@ private:
unsigned int _curState; unsigned int _curState;
cocos2d::Sprite3D * _plane; cocos2d::Sprite3D * _plane;
cocos2d::Sprite3D * _orc; cocos2d::Sprite3D * _orc;
GLProgramState * _state;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
EventListenerCustom* _backToForegroundListener;
#endif
}; };
class Sprite3DLightMapTest : public Sprite3DTestDemo class Sprite3DLightMapTest : public Sprite3DTestDemo
@ -117,6 +128,7 @@ class Sprite3DLightMapTest : public Sprite3DTestDemo
public: public:
CREATE_FUNC(Sprite3DLightMapTest); CREATE_FUNC(Sprite3DLightMapTest);
Sprite3DLightMapTest(); Sprite3DLightMapTest();
virtual ~Sprite3DLightMapTest();
virtual std::string title() const override; virtual std::string title() const override;
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event); void onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event);
@ -129,9 +141,17 @@ class Sprite3DBasicToonShaderTest : public Sprite3DTestDemo
public: public:
CREATE_FUNC(Sprite3DBasicToonShaderTest); CREATE_FUNC(Sprite3DBasicToonShaderTest);
Sprite3DBasicToonShaderTest(); Sprite3DBasicToonShaderTest();
virtual ~Sprite3DBasicToonShaderTest();
virtual std::string title() const override; virtual std::string title() const override;
virtual std::string subtitle() const override; virtual std::string subtitle() const override;
protected:
GLProgramState * _state;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
EventListenerCustom* _backToForegroundListener;
#endif
}; };
class EffectSprite3D; class EffectSprite3D;