From c82fd84a0ddb9211884825c633164842098f10b2 Mon Sep 17 00:00:00 2001 From: Nomango Date: Tue, 26 May 2020 00:12:36 +0800 Subject: [PATCH] fix bugs --- src/kiwano/core/Allocator.cpp | 6 +- src/kiwano/platform/Runner.cpp | 3 + src/kiwano/platform/Window.h | 33 +++----- src/kiwano/platform/win32/WindowImpl.cpp | 82 +++++++------------- src/kiwano/render/DirectX/RendererImpl.cpp | 88 ++++++++-------------- src/kiwano/utils/Ticker.cpp | 14 ++++ src/kiwano/utils/Ticker.h | 10 --- 7 files changed, 95 insertions(+), 141 deletions(-) diff --git a/src/kiwano/core/Allocator.cpp b/src/kiwano/core/Allocator.cpp index 7b890e6a..99c37088 100644 --- a/src/kiwano/core/Allocator.cpp +++ b/src/kiwano/core/Allocator.cpp @@ -25,10 +25,14 @@ namespace kiwano namespace memory { -MemoryAllocator* current_allocator_ = GetGlobalAllocator(); +MemoryAllocator* current_allocator_ = nullptr; MemoryAllocator* GetAllocator() { + if (!current_allocator_) + { + current_allocator_ = GetGlobalAllocator(); + } return current_allocator_; } diff --git a/src/kiwano/platform/Runner.cpp b/src/kiwano/platform/Runner.cpp index 045e2dd4..333df2da 100644 --- a/src/kiwano/platform/Runner.cpp +++ b/src/kiwano/platform/Runner.cpp @@ -18,6 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +#include #include #include @@ -86,6 +87,8 @@ bool Runner::MainLoop(Duration dt) Application& app = Application::GetInstance(); + KGE_LOG(dt.Milliseconds()); + // Update modules before poll events app.Update(dt); diff --git a/src/kiwano/platform/Window.h b/src/kiwano/platform/Window.h index 61324ba9..08b9d25a 100644 --- a/src/kiwano/platform/Window.h +++ b/src/kiwano/platform/Window.h @@ -77,11 +77,10 @@ public: * @param height 高度 * @param icon 图标资源ID * @param resizable 窗口大小可拉伸 - * @param fullscreen 全屏模式 * @throw kiwano::SystemError 窗口创建失败时抛出 */ static WindowPtr Create(const String& title, uint32_t width, uint32_t height, uint32_t icon = 0, - bool resizable = false, bool fullscreen = false); + bool resizable = false); /** * \~chinese @@ -117,6 +116,12 @@ public: */ WindowHandle GetHandle() const; + /** + * \~chinese + * @brief 获取支持的屏幕分辨率列表 + */ + virtual Vector GetResolutions() = 0; + /** * \~chinese * @brief 设置标题 @@ -133,11 +138,12 @@ public: /** * \~chinese - * @brief 重设窗口大小 - * @param width 窗口宽度 - * @param height 窗口高度 + * @brief 设置窗口分辨率 + * @param width 分辨率宽度 + * @param height 分辨率高度 + * @param fullscreen 是否全屏 */ - virtual void Resize(uint32_t width, uint32_t height) = 0; + virtual void SetResolution(uint32_t width, uint32_t height, bool fullscreen) = 0; /** * \~chinese @@ -162,21 +168,6 @@ public: */ virtual void SetCursor(CursorType cursor) = 0; - /** - * \~chinese - * @brief 设置分辨率 - * @param width 分辨率宽度 - * @param height 分辨率高度 - * @param fullscreen 是否全屏 - */ - virtual void SetResolution(uint32_t width, uint32_t height, bool fullscreen) = 0; - - /** - * \~chinese - * @brief 获取支持的屏幕分辨率列表 - */ - virtual Vector GetResolutions() = 0; - /** * \~chinese * @brief 轮询窗口事件 diff --git a/src/kiwano/platform/win32/WindowImpl.cpp b/src/kiwano/platform/win32/WindowImpl.cpp index 4025a954..f6fe696f 100644 --- a/src/kiwano/platform/win32/WindowImpl.cpp +++ b/src/kiwano/platform/win32/WindowImpl.cpp @@ -47,14 +47,12 @@ public: virtual ~WindowWin32Impl(); - void Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, bool fullscreen); + void Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable); void SetTitle(const String& title) override; void SetIcon(uint32_t icon_resource) override; - void Resize(uint32_t width, uint32_t height) override; - void SetMinimumSize(uint32_t width, uint32_t height) override; void SetMaximumSize(uint32_t width, uint32_t height) override; @@ -86,13 +84,12 @@ private: std::array key_map_; }; -WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, - bool fullscreen) +WindowPtr Window::Create(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable) { WindowWin32ImplPtr ptr = memory::New(); if (ptr) { - ptr->Init(title, width, height, icon, resizable, fullscreen); + ptr->Init(title, width, height, icon, resizable); } return ptr; } @@ -199,8 +196,7 @@ WindowWin32Impl::~WindowWin32Impl() } } -void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable, - bool fullscreen) +void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, uint32_t icon, bool resizable) { HINSTANCE hinst = GetModuleHandle(nullptr); WNDCLASSEXA wcex = { 0 }; @@ -236,38 +232,22 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, // Save the device name device_name_ = monitor_info_ex.szDevice; - int left = -1, top = -1; + uint32_t screenw = monitor_info_ex.rcWork.right - monitor_info_ex.rcWork.left; + uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top; - if (fullscreen) - { - top = monitor_info_ex.rcMonitor.top; - left = monitor_info_ex.rcMonitor.left; + uint32_t win_width, win_height; + AdjustWindow(width, height, GetStyle(), &win_width, &win_height); - if (width > static_cast(monitor_info_ex.rcWork.right - left)) - width = static_cast(monitor_info_ex.rcWork.right - left); - - if (height > static_cast(monitor_info_ex.rcWork.bottom - top)) - height = static_cast(monitor_info_ex.rcWork.bottom - top); - } - else - { - uint32_t screenw = monitor_info_ex.rcWork.right - monitor_info_ex.rcWork.left; - uint32_t screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top; - - uint32_t win_width, win_height; - AdjustWindow(width, height, GetStyle(), &win_width, &win_height); - - left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2; - top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2; - width = win_width; - height = win_height; - } + int left = monitor_info_ex.rcWork.left + (screenw - win_width) / 2; + int top = monitor_info_ex.rcWork.top + (screenh - win_height) / 2; + width = win_width; + height = win_height; width_ = width; height_ = height; resizable_ = resizable; - handle_ = ::CreateWindowExA(fullscreen ? WS_EX_TOPMOST : 0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top, - width, height, nullptr, nullptr, hinst, nullptr); + handle_ = ::CreateWindowExA(0, "KiwanoAppWnd", title.c_str(), GetStyle(), left, top, width, height, nullptr, + nullptr, hinst, nullptr); if (handle_ == nullptr) { @@ -283,6 +263,21 @@ void WindowWin32Impl::Init(const String& title, uint32_t width, uint32_t height, ::ShowWindow(handle_, SW_SHOWNORMAL); ::UpdateWindow(handle_); + + // Initialize Direct3D resources + auto d3d_res = graphics::directx::GetD3DDeviceResources(); + + HRESULT hr = d3d_res->Initialize(handle_); + + // Initialize Direct2D resources + if (SUCCEEDED(hr)) + { + auto d2d_res = graphics::directx::GetD2DDeviceResources(); + + hr = d2d_res->Initialize(d3d_res->GetDXGIDevice(), d3d_res->GetDXGISwapChain()); + } + + KGE_THROW_IF_FAILED(hr, "Create DirectX resources failed"); } void WindowWin32Impl::PumpEvents() @@ -313,25 +308,6 @@ void WindowWin32Impl::SetIcon(uint32_t icon_resource) ::SendMessage(handle_, WM_SETICON, ICON_SMALL, (LPARAM)icon); } -void WindowWin32Impl::Resize(uint32_t width, uint32_t height) -{ - KGE_ASSERT(handle_); - - RECT rc = { 0, 0, LONG(width), LONG(height) }; - ::AdjustWindowRect(&rc, GetStyle(), false); - - width = rc.right - rc.left; - height = rc.bottom - rc.top; - - MONITORINFOEXA info = GetMoniterInfoEx(handle_); - uint32_t screenw = uint32_t(info.rcWork.right - info.rcWork.left); - uint32_t screenh = uint32_t(info.rcWork.bottom - info.rcWork.top); - int left = screenw > width ? ((screenw - width) / 2) : 0; - int top = screenh > height ? ((screenh - height) / 2) : 0; - - ::SetWindowPos(handle_, 0, left, top, width, height, SWP_NOZORDER | SWP_NOACTIVATE); -} - void WindowWin32Impl::SetMinimumSize(uint32_t width, uint32_t height) { min_width_ = width; diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index 13e325b7..49f496d8 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -58,75 +58,51 @@ void RendererImpl::MakeContextForWindow(WindowPtr window) HWND target_window = window->GetHandle(); output_size_ = window->GetSize(); - d2d_res_ = nullptr; - d3d_res_ = nullptr; + d2d_res_ = graphics::directx::GetD2DDeviceResources(); + d3d_res_ = graphics::directx::GetD3DDeviceResources(); HRESULT hr = target_window ? S_OK : E_FAIL; - // Direct3D device resources + // Initialize other device resources if (SUCCEEDED(hr)) { - auto d3d_res = graphics::directx::GetD3DDeviceResources(); + RenderContextImplPtr ctx = memory::New(); - // Initialize Direct3D resources - hr = d3d_res->Initialize(target_window); - - // Direct2D device resources + hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext()); if (SUCCEEDED(hr)) { - d3d_res_ = d3d_res; + render_ctx_ = ctx; + } + } - auto d2d_res = graphics::directx::GetD2DDeviceResources(); + // FontFileLoader and FontCollectionLoader + if (SUCCEEDED(hr)) + { + hr = IFontCollectionLoader::Create(&font_collection_loader_); - // Initialize Direct2D resources - hr = d2d_res->Initialize(d3d_res_->GetDXGIDevice(), d3d_res_->GetDXGISwapChain()); + if (SUCCEEDED(hr)) + { + hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(font_collection_loader_.Get()); + } + } + + // ResourceFontFileLoader and ResourceFontCollectionLoader + if (SUCCEEDED(hr)) + { + hr = IResourceFontFileLoader::Create(&res_font_file_loader_); + + if (SUCCEEDED(hr)) + { + hr = d2d_res_->GetDWriteFactory()->RegisterFontFileLoader(res_font_file_loader_.Get()); + } + + if (SUCCEEDED(hr)) + { + hr = IResourceFontCollectionLoader::Create(&res_font_collection_loader_, res_font_file_loader_.Get()); if (SUCCEEDED(hr)) { - d2d_res_ = d2d_res; - - // Initialize other device resources - RenderContextImplPtr ctx = memory::New(); - - hr = ctx->CreateDeviceResources(d2d_res_->GetFactory(), d2d_res_->GetDeviceContext()); - if (SUCCEEDED(hr)) - { - render_ctx_ = ctx; - } - } - - // FontFileLoader and FontCollectionLoader - if (SUCCEEDED(hr)) - { - hr = IFontCollectionLoader::Create(&font_collection_loader_); - - if (SUCCEEDED(hr)) - { - hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(font_collection_loader_.Get()); - } - } - - // ResourceFontFileLoader and ResourceFontCollectionLoader - if (SUCCEEDED(hr)) - { - hr = IResourceFontFileLoader::Create(&res_font_file_loader_); - - if (SUCCEEDED(hr)) - { - hr = d2d_res_->GetDWriteFactory()->RegisterFontFileLoader(res_font_file_loader_.Get()); - } - - if (SUCCEEDED(hr)) - { - hr = IResourceFontCollectionLoader::Create(&res_font_collection_loader_, - res_font_file_loader_.Get()); - - if (SUCCEEDED(hr)) - { - hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader( - res_font_collection_loader_.Get()); - } - } + hr = d2d_res_->GetDWriteFactory()->RegisterFontCollectionLoader(res_font_collection_loader_.Get()); } } } diff --git a/src/kiwano/utils/Ticker.cpp b/src/kiwano/utils/Ticker.cpp index c072f580..c2037095 100644 --- a/src/kiwano/utils/Ticker.cpp +++ b/src/kiwano/utils/Ticker.cpp @@ -78,6 +78,20 @@ bool Ticker::Tick(Duration dt) return false; } +void Ticker::Pause() +{ + is_paused_ = true; + if (timer_) + timer_->Pause(); +} + +void Ticker::Resume() +{ + is_paused_ = false; + if (timer_) + timer_->Resume(); +} + Duration Ticker::GetDeltaTime() { return delta_time_; diff --git a/src/kiwano/utils/Ticker.h b/src/kiwano/utils/Ticker.h index 0fb86ab0..5ec16cdd 100644 --- a/src/kiwano/utils/Ticker.h +++ b/src/kiwano/utils/Ticker.h @@ -110,16 +110,6 @@ private: TimerPtr timer_; }; -inline void Ticker::Pause() -{ - is_paused_ = true; -} - -inline void Ticker::Resume() -{ - is_paused_ = false; -} - inline bool Ticker::IsPausing() const { return is_paused_;