diff --git a/src/kiwano/base/Event.hpp b/src/kiwano/base/Event.hpp index 014a2278..154699c0 100644 --- a/src/kiwano/base/Event.hpp +++ b/src/kiwano/base/Event.hpp @@ -23,6 +23,8 @@ namespace kiwano { + // 事件类型 + typedef int EventType; // 鼠标事件 struct MouseEvent @@ -36,7 +38,7 @@ namespace kiwano { struct // Events::MouseDown | Events::MouseUp | Events::MouseClick { - int button; + MouseButton::Value button; }; struct // Events::MouseWheel @@ -45,7 +47,7 @@ namespace kiwano }; }; - static bool Check(int type); + static bool Check(EventType type); }; // 键盘事件 @@ -56,7 +58,7 @@ namespace kiwano { struct // Events::KeyDown | Events::KeyUp { - int code; // enum KeyCode + KeyCode::Value code; }; struct // Events::Char @@ -65,7 +67,7 @@ namespace kiwano }; }; - static bool Check(int type); + static bool Check(EventType type); }; // 窗口事件 @@ -96,7 +98,7 @@ namespace kiwano }; }; - static bool Check(int type); + static bool Check(EventType type); }; // 自定义事件 @@ -110,7 +112,7 @@ namespace kiwano // 事件 struct KGE_API Event { - enum Type : int + enum Type : EventType { First, @@ -155,23 +157,23 @@ namespace kiwano CustomEvent custom; }; - Event(int type = Type::First) : type(type), target(nullptr) {} + Event(EventType type = Type::First) : type(type), target(nullptr) {} }; // Check-functions - inline bool MouseEvent::Check(int type) + inline bool MouseEvent::Check(EventType type) { return type > Event::MouseFirst && type < Event::MouseLast; } - inline bool KeyboardEvent::Check(int type) + inline bool KeyboardEvent::Check(EventType type) { return type > Event::KeyFirst && type < Event::KeyLast; } - inline bool WindowEvent::Check(int type) + inline bool WindowEvent::Check(EventType type) { return type > Event::WindowFirst && type < Event::WindowLast; } diff --git a/src/kiwano/base/Window.cpp b/src/kiwano/base/Window.cpp index 57dd465b..93514036 100644 --- a/src/kiwano/base/Window.cpp +++ b/src/kiwano/base/Window.cpp @@ -196,6 +196,17 @@ namespace kiwano } } + void Window::PollEvents() + { + static MSG msg = {}; + + if (::GetMessageW(&msg, nullptr, 0, 0)) + { + ::TranslateMessage(&msg); + ::DispatchMessageW(&msg); + } + } + String Window::GetTitle() const { if (handle_) diff --git a/src/kiwano/base/Window.h b/src/kiwano/base/Window.h index b3189693..02244d3f 100644 --- a/src/kiwano/base/Window.h +++ b/src/kiwano/base/Window.h @@ -49,12 +49,12 @@ namespace kiwano bool fullscreen; // 全屏模式 WindowConfig( - String const& title = L"Kiwano Game", - std::uint32_t width = 640, - std::uint32_t height = 480, - std::uint32_t icon = 0, - bool resizable = false, - bool fullscreen = false + String const& title = L"Kiwano Game", + std::uint32_t width = 640, + std::uint32_t height = 480, + std::uint32_t icon = 0, + bool resizable = false, + bool fullscreen = false ); }; @@ -98,6 +98,8 @@ namespace kiwano void Prepare(); + void PollEvents(); + HWND GetHandle() const; DWORD GetWindowStyle() const; @@ -117,8 +119,8 @@ namespace kiwano bool resizable_; bool is_fullscreen_; HWND handle_; - int width_; - int height_; + int width_; + int height_; WCHAR* device_name_; CursorType mouse_cursor_; }; diff --git a/src/kiwano/platform/Application.cpp b/src/kiwano/platform/Application.cpp index b231b853..9095b186 100644 --- a/src/kiwano/platform/Application.cpp +++ b/src/kiwano/platform/Application.cpp @@ -116,19 +116,12 @@ namespace kiwano { KGE_ASSERT(inited_ && "Calling Application::Run before Application::Init"); - HWND hwnd = Window::GetInstance()->GetHandle(); + end_ = false; - if (hwnd) + Window::GetInstance()->Prepare(); + while (!end_) { - end_ = false; - Window::GetInstance()->Prepare(); - - MSG msg = {}; - while (::GetMessageW(&msg, nullptr, 0, 0) && !end_) - { - ::TranslateMessage(&msg); - ::DispatchMessageW(&msg); - } + Window::GetInstance()->PollEvents(); } } @@ -351,14 +344,14 @@ namespace kiwano evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); - if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; } - else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; } - else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; } - else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; } + if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; } + else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; } + else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; } + else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; } - if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; } - else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; } - else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; } + if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; } + else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; } + else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; } app->DispatchEvent(evt); } @@ -444,6 +437,8 @@ namespace kiwano if (!app->OnClosing()) { + Event evt(Event::WindowClosed); + app->DispatchEvent(evt); return 0; } } @@ -453,8 +448,7 @@ namespace kiwano { KGE_LOG(L"Window was destroyed"); - Event evt(Event::WindowClosed); - app->DispatchEvent(evt); + app->Quit(); app->OnDestroy(); ::PostQuitMessage(0);