From 10ecc736f818b11e41ba6d9e655d47bfd4549293 Mon Sep 17 00:00:00 2001 From: Nomango Date: Sun, 2 Feb 2020 15:47:48 +0800 Subject: [PATCH] [deploy] update window --- src/kiwano-network/kiwano-network.h | 2 +- src/kiwano/platform/Window.cpp | 11 ---- src/kiwano/platform/Window.h | 73 +++++++++--------------- src/kiwano/platform/win32/WindowImpl.cpp | 38 ++++++------ 4 files changed, 46 insertions(+), 78 deletions(-) diff --git a/src/kiwano-network/kiwano-network.h b/src/kiwano-network/kiwano-network.h index eb134a7a..47b8801f 100644 --- a/src/kiwano-network/kiwano-network.h +++ b/src/kiwano-network/kiwano-network.h @@ -20,6 +20,6 @@ #pragma once -#include #include #include +#include diff --git a/src/kiwano/platform/Window.cpp b/src/kiwano/platform/Window.cpp index 293937e2..6e005868 100644 --- a/src/kiwano/platform/Window.cpp +++ b/src/kiwano/platform/Window.cpp @@ -23,17 +23,6 @@ namespace kiwano { -WindowConfig::WindowConfig(String const& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, - bool fullscreen) - : title(title) - , width(width) - , height(height) - , icon(icon) - , resizable(resizable) - , fullscreen(fullscreen) -{ -} - Window::Window() : should_close_(false) , width_(0) diff --git a/src/kiwano/platform/Window.h b/src/kiwano/platform/Window.h index 400d06d0..bee2916a 100644 --- a/src/kiwano/platform/Window.h +++ b/src/kiwano/platform/Window.h @@ -26,6 +26,7 @@ namespace kiwano { + /** * \~chinese * @brief 鼠标指针类型 @@ -42,35 +43,15 @@ enum class CursorType SizeNWSE, ///< 指向左上到右下方向的箭头 }; -/** - * \~chinese - * @brief 窗口设置 - */ -struct WindowConfig -{ - String title; ///< 标题 - uint32_t width; ///< 宽度 - uint32_t height; ///< 高度 - uint32_t icon; ///< 图标资源 ID - bool resizable; ///< 窗口大小可拉伸 - bool fullscreen; ///< 全屏模式 - - /** - * \~chinese - * @brief 构建窗口设置 - * @param title 标题 - * @param width 宽度 - * @param height 高度 - * @param icon 图标资源ID - * @param resizable 窗口大小可拉伸 - * @param fullscreen 全屏模式 - */ - WindowConfig(String const& title = L"Kiwano Game", uint32_t width = 640, uint32_t height = 480, uint32_t icon = 0, - bool resizable = false, bool fullscreen = false); -}; #if defined(KGE_WIN32) + +/** + * \~chinese + * @brief 窗口句柄 + */ typedef HWND WindowHandle; + #endif /** @@ -89,9 +70,16 @@ public: /** * \~chinese * @brief 初始化窗口 - * @param config 窗口设置 + * @param title 标题 + * @param width 宽度 + * @param height 高度 + * @param icon 图标资源ID + * @param resizable 窗口大小可拉伸 + * @param fullscreen 全屏模式 + * @throw std::runtime_error 窗口创建失败时抛出 */ - virtual bool Create(WindowConfig const& config) = 0; + virtual void Create(String const& title, uint32_t width, uint32_t height, uint32_t icon = 0, bool resizable = false, + bool fullscreen = false) = 0; /** * \~chinese @@ -168,11 +156,18 @@ public: /** * \~chinese * @brief 轮询窗口事件 - * @return - * 返回事件队列中的第一个事件并将其从队列中移除,若事件队列为空则返回空指针 + * @return 返回事件队列中的第一个事件并将其从队列中移除\n + * 若事件队列为空则返回空指针 */ EventPtr PollEvent(); + /** + * \~chinese + * @brief 将窗口事件放入队列 + * @param evt 窗口事件 + */ + void PushEvent(EventPtr evt); + /** * \~chinese * @brief 窗口是否需要关闭 @@ -190,15 +185,9 @@ protected: ~Window(); - void PushEvent(EventPtr evt); - - void SetInternalSize(uint32_t width, uint32_t height); - - void SetInternalTitle(String const& title); - virtual void PumpEvents() = 0; -private: +protected: bool should_close_; uint32_t width_; uint32_t height_; @@ -206,14 +195,4 @@ private: std::queue event_queue_; }; -inline void Window::SetInternalSize(uint32_t width, uint32_t height) -{ - width_ = width; - height_ = height; -} - -inline void Window::SetInternalTitle(String const& title) -{ - title_ = title; -} } // namespace kiwano diff --git a/src/kiwano/platform/win32/WindowImpl.cpp b/src/kiwano/platform/win32/WindowImpl.cpp index 5d31f5b3..131101c5 100644 --- a/src/kiwano/platform/win32/WindowImpl.cpp +++ b/src/kiwano/platform/win32/WindowImpl.cpp @@ -103,7 +103,8 @@ public: ~WindowImpl(); - bool Create(WindowConfig const& config) override; + void Create(String const& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, + bool fullscreen) override; WindowHandle GetHandle() const override; @@ -192,7 +193,8 @@ WindowImpl::WindowImpl() WindowImpl::~WindowImpl() {} -bool WindowImpl::Create(WindowConfig const& config) +void WindowImpl::Create(String const& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, + bool fullscreen) { HINSTANCE hinst = GetModuleHandleW(nullptr); WNDCLASSEX wcex = { 0 }; @@ -208,9 +210,9 @@ bool WindowImpl::Create(WindowConfig const& config) wcex.lpszMenuName = nullptr; wcex.hCursor = ::LoadCursorW(hinst, IDC_ARROW); - if (config.icon) + if (icon) { - wcex.hIcon = (HICON)::LoadImageW(hinst, MAKEINTRESOURCE(config.icon), IMAGE_ICON, 0, 0, + wcex.hIcon = (HICON)::LoadImageW(hinst, MAKEINTRESOURCE(icon), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); } @@ -231,13 +233,10 @@ bool WindowImpl::Create(WindowConfig const& config) device_name_ = new wchar_t[len + 1]; lstrcpyW(device_name_, monitor_info_ex.szDevice); - uint32_t width = config.width; - uint32_t height = config.height; - int left = -1; - int top = -1; + int left = -1, top = -1; - resizable_ = config.resizable; - is_fullscreen_ = config.fullscreen; + resizable_ = resizable; + is_fullscreen_ = fullscreen; if (is_fullscreen_) { @@ -264,17 +263,19 @@ bool WindowImpl::Create(WindowConfig const& config) height = win_height; } - handle_ = ::CreateWindowExW(is_fullscreen_ ? WS_EX_TOPMOST : 0, KGE_WND_CLASS_NAME, config.title.c_str(), - GetStyle(), left, top, width, height, nullptr, nullptr, hinst, nullptr); + handle_ = ::CreateWindowExW(is_fullscreen_ ? WS_EX_TOPMOST : 0, KGE_WND_CLASS_NAME, title.c_str(), GetStyle(), left, + top, width, height, nullptr, nullptr, hinst, nullptr); if (handle_ == nullptr) { ::UnregisterClass(KGE_WND_CLASS_NAME, hinst); + KGE_ERROR(L"Failed with HRESULT of %08X", HRESULT_FROM_WIN32(GetLastError())); - return false; + throw std::runtime_error("Create window failed"); } - SetInternalSize(width, height); + width_ = width; + height_ = height; // disable imm ::ImmAssociateContext(handle_, nullptr); @@ -289,7 +290,6 @@ bool WindowImpl::Create(WindowConfig const& config) { ChangeFullScreenResolution(width, height, device_name_); } - return true; } WindowHandle WindowImpl::GetHandle() const @@ -591,7 +591,8 @@ LRESULT CALLBACK WindowImpl::WndProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA { // KGE_SYS_LOG(L"Window resized"); - window->SetInternalSize(((uint32_t)(short)LOWORD(lparam)), ((uint32_t)(short)HIWORD(lparam))); + window->width_ = ((uint32_t)(short)LOWORD(lparam)); + window->height_ = ((uint32_t)(short)HIWORD(lparam)); WindowResizedEventPtr evt = new WindowResizedEvent; evt->width = window->GetWidth(); @@ -626,11 +627,10 @@ LRESULT CALLBACK WindowImpl::WndProc(HWND hwnd, UINT32 msg, WPARAM wparam, LPARA { KGE_SYS_LOG(L"Window title changed"); - String title = String::cstr(reinterpret_cast(lparam)); - window->SetInternalTitle(title); + window->title_ = String::cstr(reinterpret_cast(lparam)); WindowTitleChangedEventPtr evt = new WindowTitleChangedEvent; - evt->title = title; + evt->title = window->title_; window->PushEvent(evt); } break;