This commit is contained in:
Nomango 2019-10-12 08:54:10 +08:00
parent 3f9817a20d
commit 63cb724347
4 changed files with 47 additions and 38 deletions

View File

@ -23,6 +23,8 @@
namespace kiwano namespace kiwano
{ {
// ʼþÀàÐÍ
typedef int EventType;
// 鼠标事件 // 鼠标事件
struct MouseEvent struct MouseEvent
@ -36,7 +38,7 @@ namespace kiwano
{ {
struct // Events::MouseDown | Events::MouseUp | Events::MouseClick struct // Events::MouseDown | Events::MouseUp | Events::MouseClick
{ {
int button; MouseButton::Value button;
}; };
struct // Events::MouseWheel 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 struct // Events::KeyDown | Events::KeyUp
{ {
int code; // enum KeyCode KeyCode::Value code;
}; };
struct // Events::Char 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 struct KGE_API Event
{ {
enum Type : int enum Type : EventType
{ {
First, First,
@ -155,23 +157,23 @@ namespace kiwano
CustomEvent custom; CustomEvent custom;
}; };
Event(int type = Type::First) : type(type), target(nullptr) {} Event(EventType type = Type::First) : type(type), target(nullptr) {}
}; };
// Check-functions // Check-functions
inline bool MouseEvent::Check(int type) inline bool MouseEvent::Check(EventType type)
{ {
return type > Event::MouseFirst && type < Event::MouseLast; 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; 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; return type > Event::WindowFirst && type < Event::WindowLast;
} }

View File

@ -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 String Window::GetTitle() const
{ {
if (handle_) if (handle_)

View File

@ -49,12 +49,12 @@ namespace kiwano
bool fullscreen; // 全屏模式 bool fullscreen; // 全屏模式
WindowConfig( WindowConfig(
String const& title = L"Kiwano Game", String const& title = L"Kiwano Game",
std::uint32_t width = 640, std::uint32_t width = 640,
std::uint32_t height = 480, std::uint32_t height = 480,
std::uint32_t icon = 0, std::uint32_t icon = 0,
bool resizable = false, bool resizable = false,
bool fullscreen = false bool fullscreen = false
); );
}; };
@ -98,6 +98,8 @@ namespace kiwano
void Prepare(); void Prepare();
void PollEvents();
HWND GetHandle() const; HWND GetHandle() const;
DWORD GetWindowStyle() const; DWORD GetWindowStyle() const;
@ -117,8 +119,8 @@ namespace kiwano
bool resizable_; bool resizable_;
bool is_fullscreen_; bool is_fullscreen_;
HWND handle_; HWND handle_;
int width_; int width_;
int height_; int height_;
WCHAR* device_name_; WCHAR* device_name_;
CursorType mouse_cursor_; CursorType mouse_cursor_;
}; };

View File

@ -116,19 +116,12 @@ namespace kiwano
{ {
KGE_ASSERT(inited_ && "Calling Application::Run before Application::Init"); 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()->PollEvents();
Window::GetInstance()->Prepare();
MSG msg = {};
while (::GetMessageW(&msg, nullptr, 0, 0) && !end_)
{
::TranslateMessage(&msg);
::DispatchMessageW(&msg);
}
} }
} }
@ -351,14 +344,14 @@ namespace kiwano
evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON);
evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON);
if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; } 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_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_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; } 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; } 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_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; }
else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; } else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; }
app->DispatchEvent(evt); app->DispatchEvent(evt);
} }
@ -444,6 +437,8 @@ namespace kiwano
if (!app->OnClosing()) if (!app->OnClosing())
{ {
Event evt(Event::WindowClosed);
app->DispatchEvent(evt);
return 0; return 0;
} }
} }
@ -453,8 +448,7 @@ namespace kiwano
{ {
KGE_LOG(L"Window was destroyed"); KGE_LOG(L"Window was destroyed");
Event evt(Event::WindowClosed); app->Quit();
app->DispatchEvent(evt);
app->OnDestroy(); app->OnDestroy();
::PostQuitMessage(0); ::PostQuitMessage(0);