From 0689e1b0e94eeb5198c9492e2c21d082d5b1b790 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 17 Oct 2017 23:50:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E6=97=B6=E9=95=BF=E5=87=BD=E6=95=B0=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E7=AA=97=E5=8F=A3=E5=9C=A8=E6=BF=80=E6=B4=BB=E3=80=81?= =?UTF-8?q?=E9=9D=9E=E6=BF=80=E6=B4=BB=E3=80=81=E5=85=B3=E9=97=AD=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=89=A7=E8=A1=8C=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easy2D/Base/EApp.cpp | 70 ++++++++++++++++++++++++++--------- Easy2D/Base/EScene.cpp | 13 +++++++ Easy2D/Easy2D.vcxproj | 1 + Easy2D/Easy2D.vcxproj.filters | 3 ++ Easy2D/Node/EText.cpp | 1 + Easy2D/ebase.h | 31 +++++++++++++--- 6 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 Easy2D/Node/EText.cpp diff --git a/Easy2D/Base/EApp.cpp b/Easy2D/Base/EApp.cpp index 18695bbc..9a1e560f 100644 --- a/Easy2D/Base/EApp.cpp +++ b/Easy2D/Base/EApp.cpp @@ -16,6 +16,8 @@ using namespace std::chrono; e2d::EApp * s_pInstance = nullptr; // 场景栈 std::stack s_SceneStack; +// 游戏开始时间 +static steady_clock::time_point s_tStart; e2d::EApp::EApp() : m_bRunning(false) @@ -195,6 +197,8 @@ void e2d::EApp::run() UpdateWindow(GetHWnd()); // 运行游戏 m_bRunning = true; + // 记录开始时间 + s_tStart = steady_clock::now(); MSG msg; @@ -222,7 +226,15 @@ void e2d::EApp::setFPS(UINT32 fps) nAnimationInterval = 1000 / fps; } -bool e2d::EApp::onExit() +void e2d::EApp::onActivate() +{ +} + +void e2d::EApp::onInactive() +{ +} + +bool e2d::EApp::onCloseWindow() { return true; } @@ -242,15 +254,18 @@ void e2d::EApp::_mainLoop() // 判断间隔时间是否足够 if (nInterval >= nAnimationInterval) { - // 记录当前时间 - tLast = GetNow(); - // 游戏控制流程 - _onControl(); - // 刷新游戏画面 - if (!_onRender()) + if (!m_bPaused) { - MessageBox(GetHWnd(), L"Game Render Failed!", L"Error", MB_OK); - this->quit(); + // 记录当前时间 + tLast = GetNow(); + // 游戏控制流程 + _onControl(); + // 刷新游戏画面 + if (!_onRender()) + { + MessageBox(GetHWnd(), L"Game Render Failed!", L"Error", MB_OK); + this->quit(); + } } } else @@ -436,7 +451,12 @@ HWND e2d::EApp::getHWnd() return GetHWnd(); } -void e2d::EApp::closeWindow() +LONGLONG e2d::EApp::getTotalDurationFromStart() +{ + return GetInterval(s_tStart); +} + +void e2d::EApp::hideWindow() { ShowWindow(GetHWnd(), SW_HIDE); } @@ -460,11 +480,6 @@ void e2d::EApp::free() } // 删除图片缓存 ESprite::clearCache(); - // 删除所有定时器、监听器和动画 - ETimerManager::clearAllTimers(); - EMsgManager::clearAllKeyboardListeners(); - EMsgManager::clearAllMouseListeners(); - //ActionManager::clearAllActions(); // 删除所有对象 EObjectManager::clearAllObjects(); } @@ -481,7 +496,7 @@ void e2d::EApp::end() void e2d::EApp::_enterNextScene() { - // 执行当前场景的 onExit 函数 + // 执行当前场景的 onCloseWindow 函数 if (m_pCurrentScene) { m_pCurrentScene->onExit(); @@ -650,10 +665,31 @@ LRESULT e2d::EApp::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam wasHandled = true; break; + // 窗口激活消息 + case WM_ACTIVATE: + { + if (LOWORD(wParam) == WA_INACTIVE) + { + pEApp->getCurrentScene()->onInactive(); + pEApp->onInactive(); + pEApp->m_bPaused = true; + } + else + { + pEApp->onActivate(); + pEApp->getCurrentScene()->onActivate(); + pEApp->m_bPaused = false; + } + } + result = 1; + wasHandled = true; + break; + // 窗口关闭消息 case WM_CLOSE: { - if (pEApp->onExit()) + if (pEApp->getCurrentScene()->onCloseWindow() && + pEApp->onCloseWindow()) { DestroyWindow(hWnd); } diff --git a/Easy2D/Base/EScene.cpp b/Easy2D/Base/EScene.cpp index aa36dafe..535398a9 100644 --- a/Easy2D/Base/EScene.cpp +++ b/Easy2D/Base/EScene.cpp @@ -26,6 +26,19 @@ void e2d::EScene::onExit() { } +void e2d::EScene::onActivate() +{ +} + +void e2d::EScene::onInactive() +{ +} + +bool e2d::EScene::onCloseWindow() +{ + return true; +} + void e2d::EScene::_onRender() { m_Root->_callOn(); diff --git a/Easy2D/Easy2D.vcxproj b/Easy2D/Easy2D.vcxproj index 192e15f9..4b270f11 100644 --- a/Easy2D/Easy2D.vcxproj +++ b/Easy2D/Easy2D.vcxproj @@ -208,6 +208,7 @@ + diff --git a/Easy2D/Easy2D.vcxproj.filters b/Easy2D/Easy2D.vcxproj.filters index 9f37157b..a7c22c1f 100644 --- a/Easy2D/Easy2D.vcxproj.filters +++ b/Easy2D/Easy2D.vcxproj.filters @@ -78,6 +78,9 @@ Msg\Listener + + Node + diff --git a/Easy2D/Node/EText.cpp b/Easy2D/Node/EText.cpp new file mode 100644 index 00000000..6de60dab --- /dev/null +++ b/Easy2D/Node/EText.cpp @@ -0,0 +1 @@ +#include "..\enodes.h" \ No newline at end of file diff --git a/Easy2D/ebase.h b/Easy2D/ebase.h index 288119ee..6107702f 100644 --- a/Easy2D/ebase.h +++ b/Easy2D/ebase.h @@ -46,8 +46,14 @@ public: UINT32 fps ); - // 退出程序时的执行程序 - virtual bool onExit(); + // 重写这个函数,它将在窗口激活时执行 + virtual void onActivate(); + + // 重写这个函数,它将在窗口非激活时执行 + virtual void onInactive(); + + // 重写这个函数,它将在关闭窗口时执行 + virtual bool onCloseWindow(); // 释放所有内存资源 void free(); @@ -95,6 +101,12 @@ public: // 获取当前场景 static EScene * getCurrentScene(); + // 获取窗口句柄 + static HWND getHWnd(); + + // 获取从游戏开始到当前经过的毫秒数 + static LONGLONG getTotalDurationFromStart(); + // 获取 AppName static EString getAppName(); @@ -113,11 +125,8 @@ public: bool value ); - // 获取窗口句柄 - static HWND getHWnd(); - // 隐藏窗口 - static void closeWindow(); + static void hideWindow(); // 显示窗口 static void showWindow(); @@ -166,6 +175,7 @@ protected: protected: bool m_bRunning; + bool m_bPaused; EString m_sTitle; EString m_sAppName; EColor m_ClearColor; @@ -191,6 +201,15 @@ public: // 重写这个函数,它将在离开这个场景时自动执行 virtual void onExit(); + // 重写这个函数,它将在窗口激活时执行 + virtual void onActivate(); + + // 重写这个函数,它将在窗口非激活时执行 + virtual void onInactive(); + + // 重写这个函数,它将在关闭窗口时执行 + virtual bool onCloseWindow(); + // 添加子节点到场景 void add( ENode * child,