diff --git a/core/Manager/SceneManager.cpp b/core/Manager/SceneManager.cpp index dfe38125..2f9420ff 100644 --- a/core/Manager/SceneManager.cpp +++ b/core/Manager/SceneManager.cpp @@ -42,14 +42,6 @@ void e2d::SceneManager::push(Scene * scene, bool saveCurrentScene) if (_nextScene) _nextScene->release(); _nextScene = scene; _nextScene->retain(); - - // 初始化场景切换动画 - if (_transition && !_transition->init(_currScene, _nextScene)) - { - WARN("Transition initialize failed!"); - _transition->release(); - _transition = nullptr; - } if (saveCurrentScene && _currScene) { @@ -57,6 +49,30 @@ void e2d::SceneManager::push(Scene * scene, bool saveCurrentScene) } } +void e2d::SceneManager::push(Transition * transition, bool saveCurrentScene) +{ + if (!transition) + return; + + SceneManager::push(transition->_inScene, saveCurrentScene); + + if (_transition) + { + _transition->_stop(); + _transition->release(); + } + _transition = transition; + _transition->retain(); + + // 初始化场景切换动画 + if (!_transition->_init(_currScene)) + { + WARN("Transition initialize failed!"); + _transition->release(); + _transition = nullptr; + } +} + e2d::Scene* e2d::SceneManager::pop() { // 栈为空时,调用返回场景函数失败 @@ -67,31 +83,41 @@ e2d::Scene* e2d::SceneManager::pop() } _nextScene = _scenes.top(); + _nextScene->release(); _scenes.pop(); - // 初始化场景切换动画 - if (_transition && !_transition->init(_currScene, _nextScene)) - { - WARN("Transition initialize failed!"); - _transition->release(); - _transition = nullptr; - } - return _nextScene; } -void e2d::SceneManager::setTransition(Transition * transition) +e2d::Scene * e2d::SceneManager::pop(Transition * transition) { - if (transition) + if (!transition) + return nullptr; + + auto scene = SceneManager::pop(); + if (scene) { if (_transition) { - _transition->stop(); + _transition->_stop(); _transition->release(); } _transition = transition; _transition->retain(); + + _transition->_inScene = scene; + _transition->_inScene->retain(); + + // 初始化场景切换动画 + if (!_transition->_init(_currScene)) + { + WARN("Transition initialize failed!"); + _transition->release(); + _transition = nullptr; + } } + + return scene; } void e2d::SceneManager::clear() @@ -122,7 +148,7 @@ void e2d::SceneManager::update() { if (_transition) { - _transition->update(); + _transition->_update(); if (_transition->isDone()) { @@ -157,7 +183,7 @@ void e2d::SceneManager::render() { if (_transition) { - _transition->render(); + _transition->_render(); } else if (_currScene) { diff --git a/core/Transition/BoxTransition.cpp b/core/Transition/BoxTransition.cpp index 641e6a1d..61c06c05 100644 --- a/core/Transition/BoxTransition.cpp +++ b/core/Transition/BoxTransition.cpp @@ -1,14 +1,14 @@ #include "..\e2dtransition.h" #include "..\e2dnode.h" -e2d::BoxTransition::BoxTransition(float duration) - : Transition(duration) +e2d::BoxTransition::BoxTransition(Scene* scene, float duration) + : Transition(scene, duration) { } -bool e2d::BoxTransition::init(Scene * prev, Scene * next) +bool e2d::BoxTransition::_init(Scene * prev) { - if (Transition::init(prev, next)) + if (Transition::_init(prev)) { _inLayerParam.opacity = 0; return true; @@ -16,9 +16,9 @@ bool e2d::BoxTransition::init(Scene * prev, Scene * next) return false; } -void e2d::BoxTransition::update() +void e2d::BoxTransition::_update() { - Transition::update(); + Transition::_update(); if (_delta <= 0.5) { @@ -41,11 +41,7 @@ void e2d::BoxTransition::update() ); if (_delta >= 1) { - this->stop(); + this->_stop(); } } } - -void e2d::BoxTransition::reset() -{ -} diff --git a/core/Transition/EmergeTransition.cpp b/core/Transition/EmergeTransition.cpp index c79d8f5a..9aabb5aa 100644 --- a/core/Transition/EmergeTransition.cpp +++ b/core/Transition/EmergeTransition.cpp @@ -1,14 +1,14 @@ #include "..\e2dtransition.h" #include "..\e2dnode.h" -e2d::EmergeTransition::EmergeTransition(float duration) - : Transition(duration) +e2d::EmergeTransition::EmergeTransition(Scene* scene, float duration) + : Transition(scene, duration) { } -bool e2d::EmergeTransition::init(Scene * prev, Scene * next) +bool e2d::EmergeTransition::_init(Scene * prev) { - if (Transition::init(prev, next)) + if (Transition::_init(prev)) { _outLayerParam.opacity = 1; _inLayerParam.opacity = 0; @@ -17,19 +17,15 @@ bool e2d::EmergeTransition::init(Scene * prev, Scene * next) return false; } -void e2d::EmergeTransition::update() +void e2d::EmergeTransition::_update() { - Transition::update(); + Transition::_update(); _outLayerParam.opacity = 1 - _delta; _inLayerParam.opacity = _delta; if (_delta >= 1) { - this->stop(); + this->_stop(); } } - -void e2d::EmergeTransition::reset() -{ -} diff --git a/core/Transition/FadeTransition.cpp b/core/Transition/FadeTransition.cpp index 459e0b9f..23edb392 100644 --- a/core/Transition/FadeTransition.cpp +++ b/core/Transition/FadeTransition.cpp @@ -1,14 +1,14 @@ #include "..\e2dtransition.h" #include "..\e2dnode.h" -e2d::FadeTransition::FadeTransition(float duration) - : Transition(duration) +e2d::FadeTransition::FadeTransition(Scene* scene, float duration) + : Transition(scene, duration) { } -bool e2d::FadeTransition::init(Scene * prev, Scene * next) +bool e2d::FadeTransition::_init(Scene * prev) { - if (Transition::init(prev, next)) + if (Transition::_init(prev)) { _outLayerParam.opacity = 1; _inLayerParam.opacity = 0; @@ -17,9 +17,9 @@ bool e2d::FadeTransition::init(Scene * prev, Scene * next) return false; } -void e2d::FadeTransition::update() +void e2d::FadeTransition::_update() { - Transition::update(); + Transition::_update(); if (_delta < 0.5) { @@ -32,11 +32,7 @@ void e2d::FadeTransition::update() _inLayerParam.opacity = (_delta - 0.5f) * 2; if (_delta >= 1) { - this->stop(); + this->_stop(); } } } - -void e2d::FadeTransition::reset() -{ -} diff --git a/core/Transition/MoveTransition.cpp b/core/Transition/MoveTransition.cpp index 3f90f106..b5dae0e1 100644 --- a/core/Transition/MoveTransition.cpp +++ b/core/Transition/MoveTransition.cpp @@ -1,15 +1,15 @@ #include "..\e2dtransition.h" #include "..\e2dnode.h" -e2d::MoveTransition::MoveTransition(float duration, Direction direction) - : Transition(duration) +e2d::MoveTransition::MoveTransition(Scene* scene, float duration, Direction direction) + : Transition(scene, duration) , _direction(direction) { } -bool e2d::MoveTransition::init(Scene * prev, Scene * next) +bool e2d::MoveTransition::_init(Scene * prev) { - if (Transition::init(prev, next)) + if (Transition::_init(prev)) { float width = _windowSize.width; float height = _windowSize.height; @@ -41,9 +41,9 @@ bool e2d::MoveTransition::init(Scene * prev, Scene * next) return false; } -void e2d::MoveTransition::update() +void e2d::MoveTransition::_update() { - Transition::update(); + Transition::_update(); if (_outScene) { @@ -56,11 +56,11 @@ void e2d::MoveTransition::update() if (_delta >= 1) { - this->stop(); + this->_stop(); } } -void e2d::MoveTransition::reset() +void e2d::MoveTransition::_reset() { if (_outScene) _outScene->setPos(0, 0); _inScene->setPos(0, 0); diff --git a/core/Transition/Transition.cpp b/core/Transition/Transition.cpp index 92a97161..f92a3437 100644 --- a/core/Transition/Transition.cpp +++ b/core/Transition/Transition.cpp @@ -2,18 +2,20 @@ #include "..\e2dtransition.h" #include "..\e2dnode.h" -e2d::Transition::Transition(float duration) +e2d::Transition::Transition(Scene* scene, float duration) : _end(false) , _started() , _delta(0) , _outScene(nullptr) - , _inScene(nullptr) + , _inScene(scene) , _outLayer(nullptr) , _inLayer(nullptr) , _outLayerParam() , _inLayerParam() { _duration = std::max(duration, 0.f); + if (_inScene) + _inScene->retain(); } e2d::Transition::~Transition() @@ -29,13 +31,23 @@ bool e2d::Transition::isDone() return _end; } -bool e2d::Transition::init(Scene * prev, Scene * next) +bool e2d::Transition::_init(Scene * prev) { - auto renderer = Renderer::getInstance(); - // 创建图层 - HRESULT hr = renderer->getRenderTarget()->CreateLayer(&_inLayer); + _started = Time::now(); + _outScene = prev; - if (SUCCEEDED(hr)) + if (_outScene) + _outScene->retain(); + + // 创建图层 + HRESULT hr = S_OK; + auto renderer = Renderer::getInstance(); + if (_inScene) + { + hr = renderer->getRenderTarget()->CreateLayer(&_inLayer); + } + + if (SUCCEEDED(hr) && _outScene) { hr = renderer->getRenderTarget()->CreateLayer(&_outLayer); } @@ -45,12 +57,6 @@ bool e2d::Transition::init(Scene * prev, Scene * next) return false; } - _started = Time::now(); - _outScene = prev; - _inScene = next; - if (_outScene) _outScene->retain(); - if (_inScene) _inScene->retain(); - _windowSize = Window::getInstance()->getSize(); _outLayerParam = _inLayerParam = D2D1::LayerParameters( D2D1::InfiniteRect(), @@ -65,7 +71,7 @@ bool e2d::Transition::init(Scene * prev, Scene * next) return true; } -void e2d::Transition::update() +void e2d::Transition::_update() { if (_duration == 0) { @@ -78,7 +84,7 @@ void e2d::Transition::update() } } -void e2d::Transition::render() +void e2d::Transition::_render() { auto pRT = Renderer::getInstance()->getRenderTarget(); @@ -121,8 +127,8 @@ void e2d::Transition::render() } } -void e2d::Transition::stop() +void e2d::Transition::_stop() { _end = true; - reset(); + _reset(); } diff --git a/core/e2dmanager.h b/core/e2dmanager.h index 32a2a7ce..75b31374 100644 --- a/core/e2dmanager.h +++ b/core/e2dmanager.h @@ -24,16 +24,22 @@ public: // 场景入栈 void push( - Scene * scene, /* 下一个场景的指针 */ - bool saveCurrentScene = true /* 是否保存当前场景 */ + Scene * scene, /* 下一个场景的指针 */ + bool saveCurrentScene = true /* 是否保存当前场景 */ + ); + + // 场景入栈 + void push( + Transition * transition, /* 场景动画 */ + bool saveCurrentScene = true /* 是否保存当前场景 */ ); // 场景出栈 Scene* pop(); - // 设置场景切换动作 - void setTransition( - Transition * transition /* 场景切换动作 */ + // 场景出栈 + Scene* pop( + Transition * transition /* 场景动画 */ ); // 清空保存的所有场景 diff --git a/core/e2dnode.h b/core/e2dnode.h index 19a7e79b..494cc461 100644 --- a/core/e2dnode.h +++ b/core/e2dnode.h @@ -5,7 +5,7 @@ namespace e2d { -class Layer; +class Scene; class Action; class Transition; class CollisionManager; @@ -14,7 +14,6 @@ class Node : public Ref { friend class Scene; - friend class Layer; friend class Collider; friend class Transition; friend class CollisionManager; diff --git a/core/e2dtransition.h b/core/e2dtransition.h index c6f6aee4..50fa4208 100644 --- a/core/e2dtransition.h +++ b/core/e2dtransition.h @@ -6,36 +6,42 @@ namespace e2d class Scene; +class SceneManager; // 场景过渡 class Transition : public Ref { + friend class SceneManager; + public: - explicit Transition(float duration); + explicit Transition( + Scene* scene, + float duration + ); virtual ~Transition(); // 场景过渡动画是否结束 bool isDone(); +protected: // 初始化场景过渡动画 - virtual bool init( - Scene * prev, - Scene * next + virtual bool _init( + Scene * prev ); // 更新场景过渡动画 - virtual void update(); + virtual void _update(); // 渲染场景过渡动画 - virtual void render(); + virtual void _render(); // 停止场景过渡动画 - virtual void stop(); + virtual void _stop(); // 重置场景过渡动画 - virtual void reset() = 0; + virtual void _reset() { }; protected: bool _end; @@ -58,18 +64,17 @@ class FadeTransition : { public: explicit FadeTransition( - float duration /* 动画持续时长 */ + Scene* scene, /* 切换的场景 */ + float duration /* 动画持续时长 */ ); +protected: // 更新动画 - virtual void update() override; + virtual void _update() override; - virtual bool init( - Scene * prev, - Scene * next + virtual bool _init( + Scene * prev ) override; - - virtual void reset() override; }; @@ -79,18 +84,16 @@ class EmergeTransition : { public: explicit EmergeTransition( - float duration /* 浮现动画持续时长 */ + Scene* scene, /* 切换的场景 */ + float duration /* 浮现动画持续时长 */ ); - // 更新动画 - virtual void update() override; +protected: + virtual void _update() override; - virtual bool init( - Scene * prev, - Scene * next + virtual bool _init( + Scene * prev ) override; - - virtual void reset() override; }; @@ -100,18 +103,16 @@ class BoxTransition : { public: explicit BoxTransition( - float duration /* 动画持续时长 */ + Scene* scene, /* 切换的场景 */ + float duration /* 动画持续时长 */ ); - // 更新动画 - virtual void update() override; +protected: + virtual void _update() override; - virtual bool init( - Scene * prev, - Scene * next + virtual bool _init( + Scene * prev ) override; - - virtual void reset() override; }; @@ -121,19 +122,19 @@ class MoveTransition : { public: explicit MoveTransition( - float moveDuration, /* 场景移动动画持续时长 */ + Scene* scene, /* 切换的场景 */ + float moveDuration, /* 场景移动动画持续时长 */ Direction direction = Direction::Left /* 场景移动方向 */ ); - // 更新动画 - virtual void update() override; +protected: + virtual void _update() override; - virtual bool init( - Scene * prev, - Scene * next + virtual bool _init( + Scene * prev ) override; - virtual void reset() override; + virtual void _reset() override; protected: Direction _direction;