移除SceneManager
This commit is contained in:
parent
233432fe39
commit
99f9daa627
|
|
@ -42,7 +42,6 @@ e2d::GC::~GC()
|
||||||
|
|
||||||
// 删除所有单例
|
// 删除所有单例
|
||||||
Timer::destroyInstance();
|
Timer::destroyInstance();
|
||||||
SceneManager::destroyInstance();
|
|
||||||
ActionManager::destroyInstance();
|
ActionManager::destroyInstance();
|
||||||
CollisionManager::destroyInstance();
|
CollisionManager::destroyInstance();
|
||||||
}
|
}
|
||||||
|
|
@ -72,7 +71,7 @@ void e2d::GC::clear()
|
||||||
{
|
{
|
||||||
_cleanup = true;
|
_cleanup = true;
|
||||||
|
|
||||||
SceneManager::getInstance()->clear();
|
Game::getInstance()->clearAllScenes();
|
||||||
Timer::getInstance()->clearAllTasks();
|
Timer::getInstance()->clearAllTasks();
|
||||||
ActionManager::getInstance()->clearAll();
|
ActionManager::getInstance()->clearAll();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
#include "..\e2dbase.h"
|
#include "..\e2dbase.h"
|
||||||
|
#include "..\e2dnode.h"
|
||||||
|
#include "..\e2dtransition.h"
|
||||||
#include "..\e2dmanager.h"
|
#include "..\e2dmanager.h"
|
||||||
#include "..\e2dtool.h"
|
#include "..\e2dtool.h"
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
@ -11,6 +13,10 @@ e2d::Game::Game()
|
||||||
, _window(nullptr)
|
, _window(nullptr)
|
||||||
, _input(nullptr)
|
, _input(nullptr)
|
||||||
, _renderer(nullptr)
|
, _renderer(nullptr)
|
||||||
|
, _currScene(nullptr)
|
||||||
|
, _nextScene(nullptr)
|
||||||
|
, _transition(nullptr)
|
||||||
|
, _scenes()
|
||||||
{
|
{
|
||||||
CoInitialize(nullptr);
|
CoInitialize(nullptr);
|
||||||
|
|
||||||
|
|
@ -49,7 +55,7 @@ void e2d::Game::start()
|
||||||
::ShowWindow(hWnd, SW_SHOWNORMAL);
|
::ShowWindow(hWnd, SW_SHOWNORMAL);
|
||||||
::UpdateWindow(hWnd);
|
::UpdateWindow(hWnd);
|
||||||
_window->poll();
|
_window->poll();
|
||||||
SceneManager::getInstance()->update();
|
updateScene();
|
||||||
|
|
||||||
while (!_quit)
|
while (!_quit)
|
||||||
{
|
{
|
||||||
|
|
@ -62,8 +68,8 @@ void e2d::Game::start()
|
||||||
_input->update();
|
_input->update();
|
||||||
Timer::getInstance()->update();
|
Timer::getInstance()->update();
|
||||||
ActionManager::getInstance()->update();
|
ActionManager::getInstance()->update();
|
||||||
SceneManager::getInstance()->update();
|
updateScene();
|
||||||
_renderer->render();
|
drawScene();
|
||||||
_window->poll();
|
_window->poll();
|
||||||
GC::getInstance()->flush();
|
GC::getInstance()->flush();
|
||||||
}
|
}
|
||||||
|
|
@ -123,8 +129,183 @@ void e2d::Game::quit()
|
||||||
_quit = true;
|
_quit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Game::cleanup()
|
void e2d::Game::pushScene(Scene * scene, bool saveCurrentScene)
|
||||||
{
|
{
|
||||||
GC::getInstance()->clear();
|
if (!scene)
|
||||||
Image::clearCache();
|
return;
|
||||||
|
|
||||||
|
// 保存下一场景的指针
|
||||||
|
if (_nextScene) _nextScene->release();
|
||||||
|
_nextScene = scene;
|
||||||
|
_nextScene->retain();
|
||||||
|
|
||||||
|
if (saveCurrentScene && _currScene)
|
||||||
|
{
|
||||||
|
_scenes.push(_currScene);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void e2d::Game::pushScene(Transition * transition, bool saveCurrentScene)
|
||||||
|
{
|
||||||
|
if (!transition)
|
||||||
|
return;
|
||||||
|
|
||||||
|
pushScene(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::Game::popScene()
|
||||||
|
{
|
||||||
|
// 栈为空时,调用返回场景函数失败
|
||||||
|
if (_scenes.size() == 0)
|
||||||
|
{
|
||||||
|
WARN("Scene stack is empty!");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
_nextScene = _scenes.top();
|
||||||
|
_nextScene->release();
|
||||||
|
_scenes.pop();
|
||||||
|
|
||||||
|
return _nextScene;
|
||||||
|
}
|
||||||
|
|
||||||
|
e2d::Scene * e2d::Game::popScene(Transition * transition)
|
||||||
|
{
|
||||||
|
if (!transition)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
auto scene = popScene();
|
||||||
|
if (scene)
|
||||||
|
{
|
||||||
|
if (_transition)
|
||||||
|
{
|
||||||
|
_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::Game::clearAllScenes()
|
||||||
|
{
|
||||||
|
while (!_scenes.empty())
|
||||||
|
{
|
||||||
|
_scenes.top()->release();
|
||||||
|
_scenes.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e2d::Scene * e2d::Game::getCurrentScene()
|
||||||
|
{
|
||||||
|
return _currScene;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::stack<e2d::Scene*>& e2d::Game::getSceneStack()
|
||||||
|
{
|
||||||
|
return _scenes;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool e2d::Game::isTransitioning() const
|
||||||
|
{
|
||||||
|
return _transition != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void e2d::Game::updateScene()
|
||||||
|
{
|
||||||
|
if (_transition)
|
||||||
|
{
|
||||||
|
_transition->_update();
|
||||||
|
|
||||||
|
if (_transition->isDone())
|
||||||
|
{
|
||||||
|
_transition->release();
|
||||||
|
_transition = nullptr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_nextScene)
|
||||||
|
{
|
||||||
|
if (_currScene)
|
||||||
|
{
|
||||||
|
_currScene->onExit();
|
||||||
|
if (_scenes.empty() || _scenes.top() != _currScene)
|
||||||
|
{
|
||||||
|
_currScene->release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_nextScene->onEnter();
|
||||||
|
|
||||||
|
_currScene = _nextScene;
|
||||||
|
_nextScene = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void e2d::Game::drawScene()
|
||||||
|
{
|
||||||
|
// 渲染画面
|
||||||
|
_renderer->beginDraw();
|
||||||
|
{
|
||||||
|
if (_transition)
|
||||||
|
{
|
||||||
|
_transition->_render();
|
||||||
|
}
|
||||||
|
else if (_currScene)
|
||||||
|
{
|
||||||
|
_currScene->visit(_renderer);
|
||||||
|
|
||||||
|
if (_config.isOutlineVisible())
|
||||||
|
{
|
||||||
|
auto brush = _renderer->getSolidColorBrush();
|
||||||
|
brush->SetColor(D2D1::ColorF(D2D1::ColorF::Red, 0.6f));
|
||||||
|
brush->SetOpacity(1.f);
|
||||||
|
_currScene->drawOutline(_renderer);
|
||||||
|
}
|
||||||
|
if (_config.isColliderVisible())
|
||||||
|
{
|
||||||
|
_renderer->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
|
||||||
|
_currScene->drawCollider();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_config.isFpsShow())
|
||||||
|
{
|
||||||
|
_renderer->drawFps();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_renderer->endDraw();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -112,22 +112,16 @@ void e2d::Renderer::init(Window * window)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Renderer::render()
|
void e2d::Renderer::beginDraw()
|
||||||
{
|
{
|
||||||
// 开始渲染
|
// 开始渲染
|
||||||
_renderTarget->BeginDraw();
|
_renderTarget->BeginDraw();
|
||||||
// 使用背景色清空屏幕
|
// 使用背景色清空屏幕
|
||||||
_renderTarget->Clear(_clearColor);
|
_renderTarget->Clear(_clearColor);
|
||||||
|
|
||||||
// äÖȾ³¡¾°
|
|
||||||
SceneManager::getInstance()->render();
|
|
||||||
|
|
||||||
// äÖȾ FPS
|
|
||||||
if (Game::getInstance()->getConfig().isFpsShow())
|
|
||||||
{
|
|
||||||
_renderFps();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void e2d::Renderer::endDraw()
|
||||||
|
{
|
||||||
// 终止渲染
|
// 终止渲染
|
||||||
HRESULT hr = _renderTarget->EndDraw();
|
HRESULT hr = _renderTarget->EndDraw();
|
||||||
|
|
||||||
|
|
@ -150,7 +144,7 @@ void e2d::Renderer::render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void e2d::Renderer::_renderFps()
|
void e2d::Renderer::drawFps()
|
||||||
{
|
{
|
||||||
int duration = (Time::now() - _lastRenderTime).milliseconds();
|
int duration = (Time::now() - _lastRenderTime).milliseconds();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ e2d::Window::Window(const String & title, int width, int height, int iconID)
|
||||||
Rect clientRect = __adjustWindow(_width, _height);
|
Rect clientRect = __adjustWindow(_width, _height);
|
||||||
|
|
||||||
// 创建窗口
|
// 创建窗口
|
||||||
HWND hWnd = ::CreateWindowEx(
|
_hWnd = ::CreateWindowEx(
|
||||||
NULL,
|
NULL,
|
||||||
REGISTER_CLASS,
|
REGISTER_CLASS,
|
||||||
(LPCTSTR)_title,
|
(LPCTSTR)_title,
|
||||||
|
|
@ -65,7 +65,7 @@ e2d::Window::Window(const String & title, int width, int height, int iconID)
|
||||||
this
|
this
|
||||||
);
|
);
|
||||||
|
|
||||||
if (hWnd)
|
if (_hWnd)
|
||||||
{
|
{
|
||||||
// 禁用输入法
|
// 禁用输入法
|
||||||
setTypewritingEnabled(false);
|
setTypewritingEnabled(false);
|
||||||
|
|
@ -77,7 +77,7 @@ e2d::Window::Window(const String & title, int width, int height, int iconID)
|
||||||
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
|
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
|
||||||
}
|
}
|
||||||
// 获取 DPI
|
// 获取 DPI
|
||||||
_dpi = static_cast<float>(::GetDpiForWindow(hWnd));
|
_dpi = static_cast<float>(::GetDpiForWindow(_hWnd));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -409,7 +409,14 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
case WM_MOUSEMOVE:
|
case WM_MOUSEMOVE:
|
||||||
case WM_MOUSEWHEEL:
|
case WM_MOUSEWHEEL:
|
||||||
{
|
{
|
||||||
SceneManager::getInstance()->dispatch(MouseEvent(hWnd, uMsg, wParam, lParam, window->_dpi));
|
auto game = Game::getInstance();
|
||||||
|
if (game->isTransitioning())
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (game->getCurrentScene())
|
||||||
|
{
|
||||||
|
game->getCurrentScene()->dispatch(MouseEvent(hWnd, uMsg, wParam, lParam, window->_dpi), false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
result = 0;
|
result = 0;
|
||||||
hasHandled = true;
|
hasHandled = true;
|
||||||
|
|
@ -419,7 +426,14 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
case WM_KEYDOWN:
|
case WM_KEYDOWN:
|
||||||
case WM_KEYUP:
|
case WM_KEYUP:
|
||||||
{
|
{
|
||||||
SceneManager::getInstance()->dispatch(KeyEvent(hWnd, uMsg, wParam, lParam));
|
auto game = Game::getInstance();
|
||||||
|
if (game->isTransitioning())
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (game->getCurrentScene())
|
||||||
|
{
|
||||||
|
game->getCurrentScene()->dispatch(KeyEvent(hWnd, uMsg, wParam, lParam), false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
result = 0;
|
result = 0;
|
||||||
hasHandled = true;
|
hasHandled = true;
|
||||||
|
|
@ -469,8 +483,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
// 重绘窗口
|
// 重绘窗口
|
||||||
case WM_PAINT:
|
case WM_PAINT:
|
||||||
{
|
{
|
||||||
auto renderer = Game::getInstance()->getRenderer();
|
Game::getInstance()->drawScene();
|
||||||
renderer->render();
|
|
||||||
ValidateRect(hWnd, nullptr);
|
ValidateRect(hWnd, nullptr);
|
||||||
}
|
}
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
@ -480,10 +493,11 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||||
// 窗口关闭消息
|
// 窗口关闭消息
|
||||||
case WM_CLOSE:
|
case WM_CLOSE:
|
||||||
{
|
{
|
||||||
e2d::Scene * pCurrentScene = e2d::SceneManager::getInstance()->getCurrentScene();
|
auto game = Game::getInstance();
|
||||||
if (!pCurrentScene || pCurrentScene->onCloseWindow())
|
auto currScene = game->getCurrentScene();
|
||||||
|
if (!currScene || currScene->onCloseWindow())
|
||||||
{
|
{
|
||||||
e2d::Game::getInstance()->quit();
|
game->quit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
|
||||||
|
|
@ -45,9 +45,10 @@ void e2d::CollisionManager::__removeCollider(Collider * collider)
|
||||||
|
|
||||||
void e2d::CollisionManager::__updateCollider(Collider* collider)
|
void e2d::CollisionManager::__updateCollider(Collider* collider)
|
||||||
{
|
{
|
||||||
if (Game::getInstance()->isPaused() ||
|
auto game = Game::getInstance();
|
||||||
!Game::getInstance()->getConfig().isCollisionEnabled() ||
|
if (game->isPaused() ||
|
||||||
SceneManager::getInstance()->isTransitioning())
|
!game->getConfig().isCollisionEnabled() ||
|
||||||
|
game->isTransitioning())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::vector<Collider*> currColliders;
|
std::vector<Collider*> currColliders;
|
||||||
|
|
|
||||||
|
|
@ -1,230 +0,0 @@
|
||||||
#include "..\e2dmanager.h"
|
|
||||||
#include "..\e2devent.h"
|
|
||||||
#include "..\e2dnode.h"
|
|
||||||
#include "..\e2dtransition.h"
|
|
||||||
|
|
||||||
|
|
||||||
e2d::SceneManager * e2d::SceneManager::_instance = nullptr;
|
|
||||||
|
|
||||||
e2d::SceneManager * e2d::SceneManager::getInstance()
|
|
||||||
{
|
|
||||||
if (!_instance)
|
|
||||||
_instance = new (std::nothrow) SceneManager;
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::SceneManager::destroyInstance()
|
|
||||||
{
|
|
||||||
if (_instance)
|
|
||||||
{
|
|
||||||
delete _instance;
|
|
||||||
_instance = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
e2d::SceneManager::SceneManager()
|
|
||||||
: _currScene(nullptr)
|
|
||||||
, _nextScene(nullptr)
|
|
||||||
, _transition(nullptr)
|
|
||||||
, _scenes()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
e2d::SceneManager::~SceneManager()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::SceneManager::push(Scene * scene, bool saveCurrentScene)
|
|
||||||
{
|
|
||||||
if (!scene)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// 保存下一场景的指针
|
|
||||||
if (_nextScene) _nextScene->release();
|
|
||||||
_nextScene = scene;
|
|
||||||
_nextScene->retain();
|
|
||||||
|
|
||||||
if (saveCurrentScene && _currScene)
|
|
||||||
{
|
|
||||||
_scenes.push(_currScene);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
{
|
|
||||||
// 栈为空时,调用返回场景函数失败
|
|
||||||
if (_scenes.size() == 0)
|
|
||||||
{
|
|
||||||
WARN("Scene stack is empty!");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
_nextScene = _scenes.top();
|
|
||||||
_nextScene->release();
|
|
||||||
_scenes.pop();
|
|
||||||
|
|
||||||
return _nextScene;
|
|
||||||
}
|
|
||||||
|
|
||||||
e2d::Scene * e2d::SceneManager::pop(Transition * transition)
|
|
||||||
{
|
|
||||||
if (!transition)
|
|
||||||
return nullptr;
|
|
||||||
|
|
||||||
auto scene = SceneManager::pop();
|
|
||||||
if (scene)
|
|
||||||
{
|
|
||||||
if (_transition)
|
|
||||||
{
|
|
||||||
_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()
|
|
||||||
{
|
|
||||||
while (!_scenes.empty())
|
|
||||||
{
|
|
||||||
_scenes.top()->release();
|
|
||||||
_scenes.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
e2d::Scene * e2d::SceneManager::getCurrentScene()
|
|
||||||
{
|
|
||||||
return _currScene;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::stack<e2d::Scene*>& e2d::SceneManager::getSceneStack()
|
|
||||||
{
|
|
||||||
return _scenes;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool e2d::SceneManager::isTransitioning()
|
|
||||||
{
|
|
||||||
return _transition != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::SceneManager::update()
|
|
||||||
{
|
|
||||||
if (_transition)
|
|
||||||
{
|
|
||||||
_transition->_update();
|
|
||||||
|
|
||||||
if (_transition->isDone())
|
|
||||||
{
|
|
||||||
_transition->release();
|
|
||||||
_transition = nullptr;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_nextScene)
|
|
||||||
{
|
|
||||||
if (_currScene)
|
|
||||||
{
|
|
||||||
_currScene->onExit();
|
|
||||||
if (_scenes.empty() || _scenes.top() != _currScene)
|
|
||||||
{
|
|
||||||
_currScene->release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_nextScene->onEnter();
|
|
||||||
|
|
||||||
_currScene = _nextScene;
|
|
||||||
_nextScene = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::SceneManager::render()
|
|
||||||
{
|
|
||||||
if (_transition)
|
|
||||||
{
|
|
||||||
_transition->_render();
|
|
||||||
}
|
|
||||||
else if (_currScene)
|
|
||||||
{
|
|
||||||
auto renderer = Game::getInstance()->getRenderer();
|
|
||||||
_currScene->visit(renderer);
|
|
||||||
|
|
||||||
auto& config = Game::getInstance()->getConfig();
|
|
||||||
if (config.isOutlineVisible())
|
|
||||||
{
|
|
||||||
auto brush = renderer->getSolidColorBrush();
|
|
||||||
brush->SetColor(D2D1::ColorF(D2D1::ColorF::Red, 0.6f));
|
|
||||||
brush->SetOpacity(1.f);
|
|
||||||
_currScene->drawOutline(renderer);
|
|
||||||
}
|
|
||||||
if (config.isColliderVisible())
|
|
||||||
{
|
|
||||||
renderer->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
||||||
_currScene->drawCollider();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::SceneManager::dispatch(const MouseEvent & e)
|
|
||||||
{
|
|
||||||
if (_transition != nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (_currScene)
|
|
||||||
{
|
|
||||||
_currScene->dispatch(e, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void e2d::SceneManager::dispatch(const KeyEvent & e)
|
|
||||||
{
|
|
||||||
if (_transition != nullptr)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (_currScene)
|
|
||||||
{
|
|
||||||
_currScene->dispatch(e, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -265,9 +265,6 @@ public:
|
||||||
Color color
|
Color color
|
||||||
);
|
);
|
||||||
|
|
||||||
// 渲染游戏画面
|
|
||||||
void render();
|
|
||||||
|
|
||||||
// 获取文字渲染器
|
// 获取文字渲染器
|
||||||
TextRenderer * getTextRenderer() const { return _textRenderer; }
|
TextRenderer * getTextRenderer() const { return _textRenderer; }
|
||||||
|
|
||||||
|
|
@ -300,9 +297,14 @@ public:
|
||||||
Window * window
|
Window * window
|
||||||
);
|
);
|
||||||
|
|
||||||
private:
|
// 开始渲染
|
||||||
|
void beginDraw();
|
||||||
|
|
||||||
|
// 结束渲染
|
||||||
|
void endDraw();
|
||||||
|
|
||||||
// 渲染 FPS
|
// 渲染 FPS
|
||||||
void _renderFps();
|
void drawFps();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _renderTimes;
|
int _renderTimes;
|
||||||
|
|
@ -322,6 +324,9 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class Scene;
|
||||||
|
class Transition;
|
||||||
|
|
||||||
// 游戏
|
// 游戏
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
|
|
@ -329,6 +334,28 @@ public:
|
||||||
// 获取 Game 实例
|
// 获取 Game 实例
|
||||||
static Game * getInstance();
|
static Game * getInstance();
|
||||||
|
|
||||||
|
// 获取窗体
|
||||||
|
Window * getWindow() const { return _window; }
|
||||||
|
|
||||||
|
// 获取输入设备
|
||||||
|
Input * getInput() const { return _input; }
|
||||||
|
|
||||||
|
// 获取图形设备
|
||||||
|
Renderer * getRenderer() const { return _renderer; }
|
||||||
|
|
||||||
|
// 获取游戏配置
|
||||||
|
const Config& getConfig() const;
|
||||||
|
|
||||||
|
// 设置窗体
|
||||||
|
void setWindow(
|
||||||
|
Window * window
|
||||||
|
);
|
||||||
|
|
||||||
|
// 修改游戏配置
|
||||||
|
void setConfig(
|
||||||
|
const Config& config
|
||||||
|
);
|
||||||
|
|
||||||
// 启动游戏
|
// 启动游戏
|
||||||
void start();
|
void start();
|
||||||
|
|
||||||
|
|
@ -341,33 +368,46 @@ public:
|
||||||
// 结束游戏
|
// 结束游戏
|
||||||
void quit();
|
void quit();
|
||||||
|
|
||||||
// 清理资源
|
|
||||||
void cleanup();
|
|
||||||
|
|
||||||
// 游戏是否暂停
|
// 游戏是否暂停
|
||||||
bool isPaused();
|
bool isPaused();
|
||||||
|
|
||||||
// 修改游戏配置
|
// 场景入栈
|
||||||
void setConfig(
|
void pushScene(
|
||||||
const Config& config
|
Scene * scene, /* 下一个场景的指针 */
|
||||||
|
bool saveCurrentScene = true /* 是否保存当前场景 */
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取游戏配置
|
// 场景入栈
|
||||||
const Config& getConfig() const;
|
void pushScene(
|
||||||
|
Transition * transition, /* 场景动画 */
|
||||||
// 设置窗体
|
bool saveCurrentScene = true /* 是否保存当前场景 */
|
||||||
void setWindow(
|
|
||||||
Window * window
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 获取窗体
|
// 场景出栈
|
||||||
Window * getWindow() const { return _window; }
|
Scene* popScene();
|
||||||
|
|
||||||
// 获取输入设备
|
// 场景出栈
|
||||||
Input * getInput() const { return _input; }
|
Scene* popScene(
|
||||||
|
Transition * transition /* 场景动画 */
|
||||||
|
);
|
||||||
|
|
||||||
// 获取图形设备
|
// 清空保存的所有场景
|
||||||
Renderer * getRenderer() const { return _renderer; }
|
void clearAllScenes();
|
||||||
|
|
||||||
|
// 获取当前场景
|
||||||
|
Scene * getCurrentScene();
|
||||||
|
|
||||||
|
// 获取场景栈
|
||||||
|
const std::stack<Scene*>& getSceneStack();
|
||||||
|
|
||||||
|
// 是否正在进行场景动画
|
||||||
|
bool isTransitioning() const;
|
||||||
|
|
||||||
|
// 更新场景内容
|
||||||
|
void updateScene();
|
||||||
|
|
||||||
|
// 渲染场景画面
|
||||||
|
void drawScene();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Game();
|
Game();
|
||||||
|
|
@ -383,7 +423,10 @@ private:
|
||||||
Window* _window;
|
Window* _window;
|
||||||
Input* _input;
|
Input* _input;
|
||||||
Renderer* _renderer;
|
Renderer* _renderer;
|
||||||
|
Scene* _currScene;
|
||||||
|
Scene* _nextScene;
|
||||||
|
Transition* _transition;
|
||||||
|
std::stack<Scene*> _scenes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,87 +7,7 @@ namespace e2d
|
||||||
|
|
||||||
|
|
||||||
class Node;
|
class Node;
|
||||||
class Scene;
|
|
||||||
class Action;
|
class Action;
|
||||||
class KeyEvent;
|
|
||||||
class MouseEvent;
|
|
||||||
class Transition;
|
|
||||||
|
|
||||||
|
|
||||||
// 场景管理器
|
|
||||||
class SceneManager
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// 获取场景管理器实例
|
|
||||||
static SceneManager * getInstance();
|
|
||||||
|
|
||||||
// 销毁实例
|
|
||||||
static void destroyInstance();
|
|
||||||
|
|
||||||
// 场景入栈
|
|
||||||
void push(
|
|
||||||
Scene * scene, /* 下一个场景的指针 */
|
|
||||||
bool saveCurrentScene = true /* 是否保存当前场景 */
|
|
||||||
);
|
|
||||||
|
|
||||||
// 场景入栈
|
|
||||||
void push(
|
|
||||||
Transition * transition, /* 场景动画 */
|
|
||||||
bool saveCurrentScene = true /* 是否保存当前场景 */
|
|
||||||
);
|
|
||||||
|
|
||||||
// 场景出栈
|
|
||||||
Scene* pop();
|
|
||||||
|
|
||||||
// 场景出栈
|
|
||||||
Scene* pop(
|
|
||||||
Transition * transition /* 场景动画 */
|
|
||||||
);
|
|
||||||
|
|
||||||
// 清空保存的所有场景
|
|
||||||
void clear();
|
|
||||||
|
|
||||||
// 获取当前场景
|
|
||||||
Scene * getCurrentScene();
|
|
||||||
|
|
||||||
// 获取场景栈
|
|
||||||
const std::stack<Scene*>& getSceneStack();
|
|
||||||
|
|
||||||
// 是否正在进行转场动作
|
|
||||||
bool isTransitioning();
|
|
||||||
|
|
||||||
// 更新场景内容
|
|
||||||
void update();
|
|
||||||
|
|
||||||
// 渲染场景画面
|
|
||||||
void render();
|
|
||||||
|
|
||||||
// 分发鼠标消息
|
|
||||||
void dispatch(
|
|
||||||
const MouseEvent& e
|
|
||||||
);
|
|
||||||
|
|
||||||
// 分发按键消息
|
|
||||||
void dispatch(
|
|
||||||
const KeyEvent& e
|
|
||||||
);
|
|
||||||
|
|
||||||
private:
|
|
||||||
SceneManager();
|
|
||||||
|
|
||||||
~SceneManager();
|
|
||||||
|
|
||||||
E2D_DISABLE_COPY(SceneManager);
|
|
||||||
|
|
||||||
private:
|
|
||||||
Scene * _currScene;
|
|
||||||
Scene * _nextScene;
|
|
||||||
Transition * _transition;
|
|
||||||
std::stack<Scene*> _scenes;
|
|
||||||
|
|
||||||
static SceneManager * _instance;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// 动作管理器
|
// 动作管理器
|
||||||
class ActionManager
|
class ActionManager
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,14 @@ namespace e2d
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
class Game;
|
||||||
class Scene;
|
class Scene;
|
||||||
class SceneManager;
|
|
||||||
|
|
||||||
// 场景过渡
|
// 场景过渡
|
||||||
class Transition :
|
class Transition :
|
||||||
public Ref
|
public Ref
|
||||||
{
|
{
|
||||||
friend class SceneManager;
|
friend class Game;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Transition(
|
explicit Transition(
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,6 @@
|
||||||
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
|
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
|
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -160,9 +160,6 @@
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
|
||||||
<Filter>Manager</Filter>
|
<Filter>Manager</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\..\core\Manager\SceneManager.cpp">
|
|
||||||
<Filter>Manager</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp">
|
<ClCompile Include="..\..\core\Node\Button.cpp">
|
||||||
<Filter>Node</Filter>
|
<Filter>Node</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|
|
||||||
|
|
@ -223,7 +223,6 @@
|
||||||
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
|
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
|
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
<ClCompile Include="..\..\core\Node\Menu.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -160,9 +160,6 @@
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp">
|
||||||
<Filter>Manager</Filter>
|
<Filter>Manager</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\..\core\Manager\SceneManager.cpp">
|
|
||||||
<Filter>Manager</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp">
|
<ClCompile Include="..\..\core\Node\Button.cpp">
|
||||||
<Filter>Node</Filter>
|
<Filter>Node</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|
|
||||||
|
|
@ -243,7 +243,6 @@
|
||||||
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
|
<ClCompile Include="..\..\core\Event\MouseEvent.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
|
||||||
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
|
|
||||||
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
<ClCompile Include="..\..\core\Node\Button.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
<ClCompile Include="..\..\core\Node\Scene.cpp" />
|
||||||
<ClCompile Include="..\..\core\Node\ToggleButton.cpp" />
|
<ClCompile Include="..\..\core\Node\ToggleButton.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -69,9 +69,6 @@
|
||||||
<ClCompile Include="..\..\core\Base\Window.cpp">
|
<ClCompile Include="..\..\core\Base\Window.cpp">
|
||||||
<Filter>Base</Filter>
|
<Filter>Base</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="..\..\core\Manager\SceneManager.cpp">
|
|
||||||
<Filter>Manager</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="..\..\core\Base\Renderer.cpp">
|
<ClCompile Include="..\..\core\Base\Renderer.cpp">
|
||||||
<Filter>Base</Filter>
|
<Filter>Base</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue