From 318103b148da34f7ad6fe85daad991f2f7e87021 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 3 Oct 2018 22:43:21 +0800 Subject: [PATCH] add: Scene::Update --- core/e2dmodule.h | 4 ++++ core/e2dobject.h | 5 ++--- core/events/MouseEvent.cpp | 3 +++ core/modules/Game.cpp | 15 +++++++++++++-- core/modules/Input.cpp | 1 + 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/core/e2dmodule.h b/core/e2dmodule.h index c5ab79c5..aec975ab 100644 --- a/core/e2dmodule.h +++ b/core/e2dmodule.h @@ -223,6 +223,10 @@ namespace e2d // 结束 void Quit(); + // 关闭窗口时触发 + // 返回值:返回 false 将阻止窗口关闭 + virtual bool OnExit() { return true; } + // 修改窗体大小 void SetSize( int width, /* 窗体宽度 */ diff --git a/core/e2dobject.h b/core/e2dobject.h index ea75a03c..6b7fc234 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -152,9 +152,8 @@ namespace e2d // 退出场景 virtual void OnExit() {} - // 关闭窗口 - // 说明:返回 false 将阻止窗口关闭 - virtual bool OnCloseWindow() { return true; } + // 更新场景 + virtual void Update() {} // 设置根节点 void SetRoot( diff --git a/core/events/MouseEvent.cpp b/core/events/MouseEvent.cpp index ed49a8f9..8189626b 100644 --- a/core/events/MouseEvent.cpp +++ b/core/events/MouseEvent.cpp @@ -32,6 +32,7 @@ float e2d::MouseEvent::GetX() const { HDC hdc = ::GetDC(0); int dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX); + ::ReleaseDC(0, hdc); return ((float)(short)LOWORD(l_param_)) * 96.f / dpi_x; } @@ -39,6 +40,7 @@ float e2d::MouseEvent::GetY() const { HDC hdc = ::GetDC(0); int dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY); + ::ReleaseDC(0, hdc); return ((float)(short)HIWORD(l_param_)) * 96.f / dpi_y; } @@ -47,6 +49,7 @@ e2d::Point e2d::MouseEvent::GetPosition() const HDC hdc = ::GetDC(0); int dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX); int dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY); + ::ReleaseDC(0, hdc); return Point( ((float)(short)LOWORD(l_param_)) * 96.f / dpi_x, ((float)(short)HIWORD(l_param_)) * 96.f / dpi_y diff --git a/core/modules/Game.cpp b/core/modules/Game.cpp index a6b2a69a..d6523d8e 100644 --- a/core/modules/Game.cpp +++ b/core/modules/Game.cpp @@ -209,6 +209,16 @@ void e2d::Game::UpdateScene() curr_scene_ = next_scene_; next_scene_ = nullptr; } + + if (curr_scene_) + { + curr_scene_->Update(); + } + + if (next_scene_) + { + next_scene_->Update(); + } } void e2d::Game::DrawScene() @@ -357,6 +367,7 @@ e2d::Rect e2d::Game::Locate(int width, int height) HDC hdc = ::GetDC(0); int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); + ::ReleaseDC(0, hdc); RECT rect = { 0, 0, LONG(ceil(width * dpi_x / 96.f)), LONG(ceil(height * dpi_y / 96.f)) }; // 计算合适的窗口大小 @@ -543,6 +554,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) HDC hdc = ::GetDC(0); int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); + ::ReleaseDC(0, hdc); game->width_ = static_cast(width * 96.f / dpi_x); game->height_ = static_cast(height * 96.f / dpi_y); } @@ -588,8 +600,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) // 窗口关闭消息 case WM_CLOSE: { - auto currScene = game->GetCurrentScene(); - if (!currScene || currScene->OnCloseWindow()) + if (game->OnExit()) { game->Quit(); } diff --git a/core/modules/Input.cpp b/core/modules/Input.cpp index b70d17a7..6c2ace4e 100644 --- a/core/modules/Input.cpp +++ b/core/modules/Input.cpp @@ -150,6 +150,7 @@ e2d::Point e2d::Input::GetMousePos() HDC hdc = ::GetDC(0); int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); + ::ReleaseDC(0, hdc); POINT mousePos; ::GetCursorPos(&mousePos);