diff --git a/kiwano/base/Component.h b/kiwano/base/Component.h index c57e7473..b76efe7c 100644 --- a/kiwano/base/Component.h +++ b/kiwano/base/Component.h @@ -28,7 +28,16 @@ namespace kiwano { public: virtual void SetupComponent(Application*) = 0; - virtual void DestroyComponent() = 0; + + virtual void BeforeUpdate() {} + virtual void OnUpdate(float dt) {} + virtual void AfterUpdate() {} + + virtual void BeforeRender() {} + virtual void OnRender() {} + virtual void AfterRender() {} + + virtual void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {} }; } diff --git a/kiwano/base/Input.cpp b/kiwano/base/Input.cpp index b6fac0be..d9caf178 100644 --- a/kiwano/base/Input.cpp +++ b/kiwano/base/Input.cpp @@ -20,7 +20,7 @@ #include "input.h" #include "logs.h" -#include +#include // GET_X_LPARAM, GET_Y_LPARAM namespace kiwano { @@ -67,6 +67,41 @@ namespace kiwano mouse_pos_y_ = y; } + void Input::HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) + { + switch (msg) + { + case WM_LBUTTONUP: + case WM_LBUTTONDOWN: + //case WM_LBUTTONDBLCLK: + case WM_MBUTTONUP: + case WM_MBUTTONDOWN: + //case WM_MBUTTONDBLCLK: + case WM_RBUTTONUP: + case WM_RBUTTONDOWN: + //case WM_RBUTTONDBLCLK: + case WM_MOUSEMOVE: + case WM_MOUSEWHEEL: + { + if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { UpdateKey(VK_LBUTTON, (msg == WM_LBUTTONDOWN) ? true : false); } + else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { UpdateKey(VK_RBUTTON, (msg == WM_RBUTTONDOWN) ? true : false); } + else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { UpdateKey(VK_MBUTTON, (msg == WM_MBUTTONDOWN) ? true : false); } + else if (msg == WM_MOUSEMOVE) { UpdateMousePos(static_cast(GET_X_LPARAM(lparam)), static_cast(GET_Y_LPARAM(lparam))); } + + break; + } + + case WM_KEYDOWN: + case WM_SYSKEYDOWN: + case WM_KEYUP: + case WM_SYSKEYUP: + { + bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; + UpdateKey((int)wparam, down); + } + } + } + bool Input::IsDown(int key_or_btn) { KGE_ASSERT(key_or_btn >= 0 && key_or_btn < KEY_NUM); diff --git a/kiwano/base/Input.h b/kiwano/base/Input.h index 3e753844..23b42ef7 100644 --- a/kiwano/base/Input.h +++ b/kiwano/base/Input.h @@ -69,6 +69,8 @@ namespace kiwano void UpdateMousePos(float, float); + void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override; + protected: Input(); diff --git a/kiwano/platform/Application.cpp b/kiwano/platform/Application.cpp index a6da25bd..78e2c4bb 100644 --- a/kiwano/platform/Application.cpp +++ b/kiwano/platform/Application.cpp @@ -244,6 +244,13 @@ namespace kiwano const auto dt = (now - last) * time_scale_; last = now; + // Before update + for (Component* c : components_) + { + c->BeforeUpdate(); + } + + // Updating if (transition_) { transition_->Update(dt); @@ -296,15 +303,22 @@ namespace kiwano if (debug_node_) debug_node_->Update(dt); - Input::Instance().Update(); + // After update + for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) + { + (*rit)->AfterUpdate(); + } } void Application::Render() { - ThrowIfFailed( - Renderer::Instance().BeginDraw() - ); + // Before render + for (Component* c : components_) + { + c->BeforeRender(); + } + // Rendering if (transition_) { transition_->Render(); @@ -319,9 +333,11 @@ namespace kiwano if (debug_node_) debug_node_->Render(); - ThrowIfFailed( - Renderer::Instance().EndDraw() - ); + // After render + for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) + { + (*rit)->AfterRender(); + } } void Application::PreformInMainThread(Closure function) @@ -332,12 +348,16 @@ namespace kiwano LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { - Application * app = reinterpret_cast( - static_cast(::GetWindowLongPtrW(hwnd, GWLP_USERDATA)) - ); + Application * app = reinterpret_cast(static_cast(::GetWindowLongPtrW(hwnd, GWLP_USERDATA))); if (!app) return ::DefWindowProcW(hwnd, msg, wparam, lparam); + + // Handle Message + for (Component* c : app->components_) + { + c->HandleMessage(hwnd, msg, wparam, lparam); + } switch (msg) { @@ -356,11 +376,9 @@ namespace kiwano case WM_KEYUP: case WM_SYSKEYUP: { - bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; - Input::Instance().UpdateKey((int)wparam, down); - if (!app->transition_ && app->curr_scene_) { + bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; Event evt(down ? Event::KeyDown : Event::KeyUp); evt.key.code = static_cast(wparam); evt.key.count = static_cast(lparam & 0xFF); @@ -395,10 +413,6 @@ namespace kiwano case WM_MOUSEMOVE: case WM_MOUSEWHEEL: { - if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { Input::Instance().UpdateKey(VK_LBUTTON, (msg == WM_LBUTTONDOWN) ? true : false); } - else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { Input::Instance().UpdateKey(VK_RBUTTON, (msg == WM_RBUTTONDOWN) ? true : false); } - else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { Input::Instance().UpdateKey(VK_MBUTTON, (msg == WM_MBUTTONDOWN) ? true : false); } - if (!app->transition_ && app->curr_scene_) { Event evt; @@ -419,24 +433,11 @@ namespace kiwano app->curr_scene_->Dispatch(evt); } - - if (msg == WM_MOUSEMOVE) - { - Input::Instance().UpdateMousePos( - static_cast(GET_X_LPARAM(lparam)), - static_cast(GET_Y_LPARAM(lparam)) - ); - } } break; case WM_SIZE: { - UINT width = LOWORD(lparam); - UINT height = HIWORD(lparam); - - Renderer::Instance().Resize(width, height); - if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam) { KGE_LOG(L"Window minimized"); @@ -448,8 +449,8 @@ namespace kiwano if (app->curr_scene_) { Event evt(Event::WindowResized); - evt.win.width = static_cast(width); - evt.win.height = static_cast(height); + evt.win.width = LOWORD(lparam); + evt.win.height = HIWORD(lparam); app->curr_scene_->Dispatch(evt); } diff --git a/kiwano/renderer/render.cpp b/kiwano/renderer/render.cpp index aae78c66..d1478213 100644 --- a/kiwano/renderer/render.cpp +++ b/kiwano/renderer/render.cpp @@ -100,6 +100,35 @@ namespace kiwano d3d_res_.Reset(); } + void Renderer::BeforeRender() + { + ThrowIfFailed( + BeginDraw() + ); + } + + void Renderer::AfterRender() + { + ThrowIfFailed( + EndDraw() + ); + } + + void Renderer::HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) + { + switch (msg) + { + case WM_SIZE: + { + UINT width = LOWORD(lparam); + UINT height = HIWORD(lparam); + + Resize(width, height); + break; + } + } + } + HRESULT Renderer::CreateDeviceResources() { HRESULT hr = S_OK; diff --git a/kiwano/renderer/render.h b/kiwano/renderer/render.h index e90f6a02..6f4127fc 100644 --- a/kiwano/renderer/render.h +++ b/kiwano/renderer/render.h @@ -51,10 +51,6 @@ namespace kiwano KGE_DECLARE_SINGLETON(Renderer); public: - HRESULT BeginDraw(); - - HRESULT EndDraw(); - HRESULT CreateLayer( ComPtr& layer ); @@ -142,6 +138,19 @@ namespace kiwano UINT height ); + public: + void SetupComponent(Application*) override; + + void DestroyComponent() override; + + void BeforeRender() override; + + void AfterRender() override; + + void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) override; + + void SetCollectingStatus(bool collecting); + public: struct Status { @@ -150,12 +159,6 @@ namespace kiwano int primitives; }; - void SetupComponent(Application*) override; - - void DestroyComponent() override; - - void SetCollectingStatus(bool collecting); - inline HWND GetTargetWindow() const { return hwnd_; } inline Status const& GetStatus() const { return status_; } @@ -179,6 +182,10 @@ namespace kiwano HRESULT HandleDeviceLost(); + HRESULT BeginDraw(); + + HRESULT EndDraw(); + private: unsigned long ref_count_;