add: Scene::Update

This commit is contained in:
Nomango 2018-10-03 22:43:21 +08:00
parent 422c7cbdc7
commit 318103b148
5 changed files with 23 additions and 5 deletions

View File

@ -223,6 +223,10 @@ namespace e2d
// 结束
void Quit();
// 关闭窗口时触发
// 返回值:返回 false 将阻止窗口关闭
virtual bool OnExit() { return true; }
// 修改窗体大小
void SetSize(
int width, /* 窗体宽度 */

View File

@ -152,9 +152,8 @@ namespace e2d
// 退出场景
virtual void OnExit() {}
// 关闭窗口
// 说明:返回 false 将阻止窗口关闭
virtual bool OnCloseWindow() { return true; }
// 更新场景
virtual void Update() {}
// 设置根节点
void SetRoot(

View File

@ -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

View File

@ -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<int>(width * 96.f / dpi_x);
game->height_ = static_cast<int>(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();
}

View File

@ -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);