diff --git a/projects/kiwano.vcxproj b/projects/kiwano.vcxproj index 6ae1c33b..2931f641 100644 --- a/projects/kiwano.vcxproj +++ b/projects/kiwano.vcxproj @@ -48,7 +48,7 @@ - + @@ -56,7 +56,7 @@ - + @@ -76,7 +76,7 @@ - + @@ -112,14 +112,14 @@ - + - + @@ -129,7 +129,7 @@ - + diff --git a/projects/kiwano.vcxproj.filters b/projects/kiwano.vcxproj.filters index f70612b5..1be66a3b 100644 --- a/projects/kiwano.vcxproj.filters +++ b/projects/kiwano.vcxproj.filters @@ -90,9 +90,6 @@ base - - base - base @@ -114,9 +111,6 @@ renderer - - renderer - renderer @@ -158,9 +152,6 @@ base - - base - base @@ -288,6 +279,15 @@ 2d\action + + base + + + base + + + renderer + @@ -320,9 +320,6 @@ base - - base - base @@ -338,9 +335,6 @@ renderer - - renderer - renderer @@ -356,9 +350,6 @@ base - - base - base @@ -443,5 +434,14 @@ 2d\action + + base + + + base + + + renderer + \ No newline at end of file diff --git a/src/kiwano-audio/src/Sound.cpp b/src/kiwano-audio/src/Sound.cpp index 12201b09..ddf88d6f 100644 --- a/src/kiwano-audio/src/Sound.cpp +++ b/src/kiwano-audio/src/Sound.cpp @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include #include #include "Sound.h" #include "audio.h" diff --git a/src/kiwano-audio/src/Transcoder.cpp b/src/kiwano-audio/src/Transcoder.cpp index 3f70722f..4d2d7baf 100644 --- a/src/kiwano-audio/src/Transcoder.cpp +++ b/src/kiwano-audio/src/Transcoder.cpp @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include "audio-modules.h" #include "Transcoder.h" diff --git a/src/kiwano-audio/src/audio-modules.cpp b/src/kiwano-audio/src/audio-modules.cpp index 7993ea69..19fb126c 100644 --- a/src/kiwano-audio/src/audio-modules.cpp +++ b/src/kiwano-audio/src/audio-modules.cpp @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include #include "audio-modules.h" namespace kiwano diff --git a/src/kiwano-audio/src/audio.cpp b/src/kiwano-audio/src/audio.cpp index 38fce8ce..b703054a 100644 --- a/src/kiwano-audio/src/audio.cpp +++ b/src/kiwano-audio/src/audio.cpp @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include #include "audio-modules.h" #include "audio.h" diff --git a/src/kiwano-imgui/src/ImGuiModule.cpp b/src/kiwano-imgui/src/ImGuiModule.cpp index 9a7b68aa..cd8eca18 100644 --- a/src/kiwano-imgui/src/ImGuiModule.cpp +++ b/src/kiwano-imgui/src/ImGuiModule.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include "ImGuiModule.h" #include "imgui_impl.h" diff --git a/src/kiwano-imgui/src/imgui_impl_dx11.cpp b/src/kiwano-imgui/src/imgui_impl_dx11.cpp index a5586bf4..d155a40e 100644 --- a/src/kiwano-imgui/src/imgui_impl_dx11.cpp +++ b/src/kiwano-imgui/src/imgui_impl_dx11.cpp @@ -1,6 +1,6 @@ // dear imgui: Renderer for Kiwano (DirectX11) -#include +#include #include "imgui_impl_dx11.h" // DirectX diff --git a/src/kiwano-network/src/HttpClient.cpp b/src/kiwano-network/src/HttpClient.cpp index 8dd8a82c..e175d539 100644 --- a/src/kiwano-network/src/HttpClient.cpp +++ b/src/kiwano-network/src/HttpClient.cpp @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include +#include #include #include "helper.h" #include "HttpRequest.hpp" diff --git a/src/kiwano/2d/Actor.cpp b/src/kiwano/2d/Actor.cpp index 455ef8c5..b5278b31 100644 --- a/src/kiwano/2d/Actor.cpp +++ b/src/kiwano/2d/Actor.cpp @@ -20,8 +20,8 @@ #include "Actor.h" #include "Stage.h" -#include "../base/logs.h" -#include "../renderer/render.h" +#include "../base/Logger.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/Canvas.cpp b/src/kiwano/2d/Canvas.cpp index 8bb6d660..33caed18 100644 --- a/src/kiwano/2d/Canvas.cpp +++ b/src/kiwano/2d/Canvas.cpp @@ -19,8 +19,8 @@ // THE SOFTWARE. #include "Canvas.h" -#include "../base/logs.h" -#include "../renderer/render.h" +#include "../base/Logger.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/DebugActor.cpp b/src/kiwano/2d/DebugActor.cpp index aaef5c99..e7429567 100644 --- a/src/kiwano/2d/DebugActor.cpp +++ b/src/kiwano/2d/DebugActor.cpp @@ -20,7 +20,7 @@ #include "DebugActor.h" #include "Text.h" -#include "../renderer/render.h" +#include "../renderer/Renderer.h" #include #include diff --git a/src/kiwano/2d/FrameSequence.cpp b/src/kiwano/2d/FrameSequence.cpp index 0783e483..4957e1f0 100644 --- a/src/kiwano/2d/FrameSequence.cpp +++ b/src/kiwano/2d/FrameSequence.cpp @@ -20,7 +20,7 @@ #include "FrameSequence.h" #include "Frame.h" -#include "../base/logs.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/2d/GifSprite.cpp b/src/kiwano/2d/GifSprite.cpp index de4bb118..bccf256c 100644 --- a/src/kiwano/2d/GifSprite.cpp +++ b/src/kiwano/2d/GifSprite.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "GifSprite.h" -#include "../base/logs.h" +#include "../base/Logger.h" #include "../platform/modules.h" namespace kiwano diff --git a/src/kiwano/2d/GifSprite.h b/src/kiwano/2d/GifSprite.h index c72ddc28..0c730c7d 100644 --- a/src/kiwano/2d/GifSprite.h +++ b/src/kiwano/2d/GifSprite.h @@ -21,7 +21,7 @@ #pragma once #include "Actor.h" #include "../base/Resource.h" -#include "../renderer/render.h" +#include "../renderer/Renderer.h" #include "../renderer/GifImage.h" namespace kiwano diff --git a/src/kiwano/2d/Layer.cpp b/src/kiwano/2d/Layer.cpp index c535088e..2b709eac 100644 --- a/src/kiwano/2d/Layer.cpp +++ b/src/kiwano/2d/Layer.cpp @@ -20,7 +20,7 @@ #pragma once #include "Layer.h" -#include "../renderer/render.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/ShapeActor.cpp b/src/kiwano/2d/ShapeActor.cpp index 1e4689b5..9ff124bb 100644 --- a/src/kiwano/2d/ShapeActor.cpp +++ b/src/kiwano/2d/ShapeActor.cpp @@ -19,8 +19,8 @@ // THE SOFTWARE. #include "ShapeActor.h" -#include "../base/logs.h" -#include "../renderer/render.h" +#include "../base/Logger.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/Sprite.cpp b/src/kiwano/2d/Sprite.cpp index 0223fe08..ac4ffcba 100644 --- a/src/kiwano/2d/Sprite.cpp +++ b/src/kiwano/2d/Sprite.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "Sprite.h" -#include "../renderer/render.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/Stage.cpp b/src/kiwano/2d/Stage.cpp index 63a151ab..b694a39f 100644 --- a/src/kiwano/2d/Stage.cpp +++ b/src/kiwano/2d/Stage.cpp @@ -19,8 +19,8 @@ // THE SOFTWARE. #include "Stage.h" -#include "../base/logs.h" -#include "../renderer/render.h" +#include "../base/Logger.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/Text.cpp b/src/kiwano/2d/Text.cpp index 1c36567a..84fae7f8 100644 --- a/src/kiwano/2d/Text.cpp +++ b/src/kiwano/2d/Text.cpp @@ -19,8 +19,8 @@ // THE SOFTWARE. #include "Text.h" -#include "../base/logs.h" -#include "../renderer/render.h" +#include "../base/Logger.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/Transition.cpp b/src/kiwano/2d/Transition.cpp index b17b98c3..af2876c5 100644 --- a/src/kiwano/2d/Transition.cpp +++ b/src/kiwano/2d/Transition.cpp @@ -21,9 +21,9 @@ #include "Transition.h" #include "Actor.h" #include "Stage.h" -#include "../base/window.h" -#include "../base/logs.h" -#include "../renderer/render.h" +#include "../base/Window.h" +#include "../base/Logger.h" +#include "../renderer/Renderer.h" namespace kiwano { diff --git a/src/kiwano/2d/action/ActionGroup.cpp b/src/kiwano/2d/action/ActionGroup.cpp index 1e71db99..fd1a646a 100644 --- a/src/kiwano/2d/action/ActionGroup.cpp +++ b/src/kiwano/2d/action/ActionGroup.cpp @@ -20,7 +20,7 @@ #include "ActionGroup.h" #include "../Actor.h" -#include "../../base/logs.h" +#include "../../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/2d/action/ActionManager.cpp b/src/kiwano/2d/action/ActionManager.cpp index b23df82f..3428bd8a 100644 --- a/src/kiwano/2d/action/ActionManager.cpp +++ b/src/kiwano/2d/action/ActionManager.cpp @@ -20,7 +20,7 @@ #include "ActionManager.h" #include "../Actor.h" -#include "../../base/logs.h" +#include "../../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/2d/action/ActionTween.h b/src/kiwano/2d/action/ActionTween.h index 46854a7c..bb4d3b56 100644 --- a/src/kiwano/2d/action/ActionTween.h +++ b/src/kiwano/2d/action/ActionTween.h @@ -20,7 +20,7 @@ #pragma once #include "Action.h" -#include "../../base/logs.h" +#include "../../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/base/EventDispatcher.cpp b/src/kiwano/base/EventDispatcher.cpp index 41fa71b9..8dc93e26 100644 --- a/src/kiwano/base/EventDispatcher.cpp +++ b/src/kiwano/base/EventDispatcher.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "EventDispatcher.h" -#include "../base/logs.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/base/Input.cpp b/src/kiwano/base/Input.cpp index ba52a7f5..6d4f86af 100644 --- a/src/kiwano/base/Input.cpp +++ b/src/kiwano/base/Input.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "input.h" -#include "logs.h" +#include "Logger.h" #include // GET_X_LPARAM, GET_Y_LPARAM namespace kiwano diff --git a/src/kiwano/base/logs.cpp b/src/kiwano/base/Logger.cpp similarity index 99% rename from src/kiwano/base/logs.cpp rename to src/kiwano/base/Logger.cpp index a8f2d257..7f72fc9e 100644 --- a/src/kiwano/base/logs.cpp +++ b/src/kiwano/base/Logger.cpp @@ -18,7 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include "logs.h" +#include "Logger.h" #include #include diff --git a/src/kiwano/base/logs.h b/src/kiwano/base/Logger.h similarity index 93% rename from src/kiwano/base/logs.h rename to src/kiwano/base/Logger.h index 368e2afb..af05f629 100644 --- a/src/kiwano/base/logs.h +++ b/src/kiwano/base/Logger.h @@ -27,18 +27,26 @@ #ifndef KGE_LOG # ifdef KGE_DEBUG -# define KGE_LOG(FORMAT, ...) kiwano::Logger::GetInstance()->Messagef((FORMAT ## "\n"), __VA_ARGS__) +# define KGE_LOG(FORMAT, ...) ::kiwano::Logger::GetInstance()->Messagef((FORMAT ## "\n"), __VA_ARGS__) # else # define KGE_LOG __noop # endif #endif #ifndef KGE_WARNING_LOG -# define KGE_WARNING_LOG(FORMAT, ...) kiwano::Logger::GetInstance()->Warningf((FORMAT ## "\n"), __VA_ARGS__) +# define KGE_WARNING_LOG(FORMAT, ...) ::kiwano::Logger::GetInstance()->Warningf((FORMAT ## "\n"), __VA_ARGS__) #endif #ifndef KGE_ERROR_LOG -# define KGE_ERROR_LOG(FORMAT, ...) kiwano::Logger::GetInstance()->Errorf((FORMAT ## "\n"), __VA_ARGS__) +# define KGE_ERROR_LOG(FORMAT, ...) ::kiwano::Logger::GetInstance()->Errorf((FORMAT ## "\n"), __VA_ARGS__) +#endif + +#ifndef KGE_PRINT +# define KGE_PRINT(...) ::kiwano::Logger::GetInstance()->Println(__VA_ARGS__) +#endif + +#ifndef KGE_PRINTF +# define KGE_PRINTF(FORMAT, ...) ::kiwano::Logger::GetInstance()->Printf((FORMAT), __VA_ARGS__) #endif namespace kiwano diff --git a/src/kiwano/base/Object.cpp b/src/kiwano/base/Object.cpp index d0b2b430..888e9108 100644 --- a/src/kiwano/base/Object.cpp +++ b/src/kiwano/base/Object.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "Object.h" -#include "logs.h" +#include "Logger.h" #include namespace kiwano diff --git a/src/kiwano/base/Resource.cpp b/src/kiwano/base/Resource.cpp index be2387ca..ee6c84e7 100644 --- a/src/kiwano/base/Resource.cpp +++ b/src/kiwano/base/Resource.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "Resource.h" -#include "../base/logs.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/base/TimerManager.cpp b/src/kiwano/base/TimerManager.cpp index 205f7613..1476e157 100644 --- a/src/kiwano/base/TimerManager.cpp +++ b/src/kiwano/base/TimerManager.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "TimerManager.h" -#include "../base/logs.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/base/Window.cpp b/src/kiwano/base/Window.cpp new file mode 100644 index 00000000..bc2a5f80 --- /dev/null +++ b/src/kiwano/base/Window.cpp @@ -0,0 +1,418 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "Window.h" +#include "Logger.h" + +#define WINDOW_STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX +#define WINDOW_FULLSCREEN_STYLE WS_CLIPCHILDREN | WS_POPUP +#define KGE_WND_CLASS_NAME L"KiwanoAppWnd" + +namespace kiwano +{ + namespace + { + MONITORINFOEX GetMoniterInfoEx(HWND hwnd); + + void AdjustWindow(UINT width, UINT height, DWORD style, UINT* win_width, UINT* win_height); + + void ChangeFullScreenResolution(int width, int height, WCHAR* device_name); + + void RestoreResolution(WCHAR* device_name); + } + + Window::Window() + : handle_(nullptr) + , width_(0) + , height_(0) + , device_name_(nullptr) + , is_fullscreen_(false) + , mouse_cursor_(MouseCursor(-1)) + { + } + + Window::~Window() + { + if (is_fullscreen_) + RestoreResolution(device_name_); + + if (device_name_) + { + delete[] device_name_; + device_name_ = nullptr; + } + + if (handle_) + { + ::DestroyWindow(handle_); + handle_ = nullptr; + } + } + + HRESULT Window::Create(String const& title, int width, int height, LPCWSTR icon, bool fullscreen, WNDPROC proc) + { + HINSTANCE hinst = GetModuleHandleW(nullptr); + WNDCLASSEX wcex = { 0 }; + wcex.cbSize = sizeof(WNDCLASSEX); + wcex.lpszClassName = KGE_WND_CLASS_NAME; + wcex.style = CS_HREDRAW | CS_VREDRAW /* | CS_DBLCLKS */; + wcex.lpfnWndProc = proc; + wcex.hIcon = nullptr; + wcex.cbClsExtra = 0; + wcex.cbWndExtra = sizeof(LONG_PTR); + wcex.hInstance = hinst; + wcex.hbrBackground = nullptr; + wcex.lpszMenuName = nullptr; + wcex.hCursor = nullptr; + + if (icon) + { + wcex.hIcon = (HICON)::LoadImageW(hinst, icon, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); + } + + ::RegisterClassExW(&wcex); + + // Get the nearest monitor to this window + HMONITOR monitor = ::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY); + + // Get the target monitor info + MONITORINFOEX monitor_info_ex; + memset(&monitor_info_ex, 0, sizeof(MONITORINFOEX)); + monitor_info_ex.cbSize = sizeof(MONITORINFOEX); + ::GetMonitorInfoW(monitor, &monitor_info_ex); + + // Save the device name + int len = lstrlenW(monitor_info_ex.szDevice); + device_name_ = new WCHAR[len + 1]; + lstrcpyW(device_name_, monitor_info_ex.szDevice); + + int left = -1; + int top = -1; + + is_fullscreen_ = fullscreen; + + if (is_fullscreen_) + { + top = monitor_info_ex.rcMonitor.top; + left = monitor_info_ex.rcMonitor.left; + + if (width > monitor_info_ex.rcWork.right - left) + width = monitor_info_ex.rcWork.right - left; + + if (height > monitor_info_ex.rcWork.bottom - top) + height = monitor_info_ex.rcWork.bottom - top; + } + else + { + UINT screenw = monitor_info_ex.rcWork.right - monitor_info_ex.rcWork.left; + UINT screenh = monitor_info_ex.rcWork.bottom - monitor_info_ex.rcWork.top; + + UINT win_width, win_height; + AdjustWindow( + width, + height, + GetWindowStyle(), + &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; + } + + handle_ = ::CreateWindowExW( + is_fullscreen_ ? WS_EX_TOPMOST : 0, + KGE_WND_CLASS_NAME, + title.c_str(), + GetWindowStyle(), + left, + top, + width, + height, + nullptr, + nullptr, + hinst, + nullptr + ); + + if (handle_ == nullptr) + { + ::UnregisterClass(KGE_WND_CLASS_NAME, hinst); + return HRESULT_FROM_WIN32(GetLastError()); + } + + RECT rc; + GetClientRect(handle_, &rc); + width_ = rc.right - rc.left; + height_ = rc.bottom - rc.top; + + SetMouseCursor(MouseCursor::Arrow); + return S_OK; + } + + void Window::Prepare() + { + ::ShowWindow(handle_, SW_SHOWNORMAL); + ::UpdateWindow(handle_); + + if (is_fullscreen_) + { + ChangeFullScreenResolution(width_, height_, device_name_); + } + } + + String Window::GetTitle() const + { + if (handle_) + { + wchar_t title[256]; + ::GetWindowTextW(handle_, title, 256); + return title; + } + return String(); + } + + void Window::SetTitle(String const& title) + { + if (handle_) + ::SetWindowTextW(handle_, title.c_str()); + } + + Size Window::GetSize() const + { + return Size{ + static_cast(width_), + static_cast(height_) + }; + } + + float Window::GetWidth() const + { + return static_cast(width_); + } + + float Window::GetHeight() const + { + return static_cast(height_); + } + + void Window::SetIcon(LPCWSTR icon_resource) + { + if (handle_) + { + HINSTANCE hinstance = GetModuleHandle(nullptr); + HICON icon = (HICON)::LoadImage( + hinstance, + icon_resource, + IMAGE_ICON, + 0, + 0, + LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE + ); + + ::SendMessage(handle_, WM_SETICON, ICON_BIG, (LPARAM)icon); + ::SendMessage(handle_, WM_SETICON, ICON_SMALL, (LPARAM)icon); + } + } + + void Window::Resize(int width, int height) + { + if (handle_ && !is_fullscreen_) + { + RECT rc = { 0, 0, int(width), int(height) }; + ::AdjustWindowRect(&rc, GetWindowStyle(), false); + + width = rc.right - rc.left; + height = rc.bottom - rc.top; + ::SetWindowPos(handle_, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE); + } + } + + void Window::SetFullscreen(bool fullscreen, int width, int height) + { + if (is_fullscreen_ != fullscreen || width != width_ || height != height_) + { + is_fullscreen_ = fullscreen; + + if (is_fullscreen_) + { + // move window to (0, 0) before display switch + ::SetWindowPos(handle_, HWND_TOPMOST, 0, 0, width_, height_, SWP_NOACTIVATE); + + ChangeFullScreenResolution(width, height, device_name_); + + MONITORINFOEX info = GetMoniterInfoEx(handle_); + + ::SetWindowLongPtr(handle_, GWL_STYLE, GetWindowStyle()); + ::SetWindowPos(handle_, HWND_TOPMOST, info.rcMonitor.top, info.rcMonitor.left, width, height, SWP_NOACTIVATE); + + width_ = width; + height_ = height; + } + else + { + RestoreResolution(device_name_); + + MONITORINFOEX info = GetMoniterInfoEx(handle_); + + UINT screenw = info.rcWork.right - info.rcWork.left; + UINT screenh = info.rcWork.bottom - info.rcWork.top; + + UINT win_width, win_height; + AdjustWindow(width, height, GetWindowStyle(), &win_width, &win_height); + + int left = screenw > win_width ? ((screenw - win_width) / 2) : 0; + int top = screenh > win_height ? ((screenh - win_height) / 2) : 0; + + ::SetWindowLongPtr(handle_, GWL_STYLE, GetWindowStyle()); + ::SetWindowPos(handle_, HWND_NOTOPMOST, left, top, win_width, win_height, SWP_DRAWFRAME | SWP_FRAMECHANGED); + + UpdateWindowRect(); + } + + ::ShowWindow(handle_, SW_SHOWNORMAL); + } + } + + void Window::SetMouseCursor(MouseCursor cursor) + { + if (mouse_cursor_ != cursor) + { + mouse_cursor_ = cursor; + + LPTSTR win32_cursor = IDC_ARROW; + switch (cursor) + { + case MouseCursor::Arrow: win32_cursor = IDC_ARROW; break; + case MouseCursor::TextInput: win32_cursor = IDC_IBEAM; break; + case MouseCursor::SizeAll: win32_cursor = IDC_SIZEALL; break; + case MouseCursor::SizeWE: win32_cursor = IDC_SIZEWE; break; + case MouseCursor::SizeNS: win32_cursor = IDC_SIZENS; break; + case MouseCursor::SizeNESW: win32_cursor = IDC_SIZENESW; break; + case MouseCursor::SizeNWSE: win32_cursor = IDC_SIZENWSE; break; + case MouseCursor::Hand: win32_cursor = IDC_HAND; break; + } + ::SetCursor(::LoadCursorW(nullptr, win32_cursor)); + } + } + + HWND Window::GetHandle() const + { + return handle_; + } + + DWORD Window::GetWindowStyle() const + { + return is_fullscreen_ ? (WINDOW_FULLSCREEN_STYLE) : (WINDOW_STYLE); + } + + void Window::UpdateWindowRect() + { + if (!handle_) + return; + + RECT rc; + ::GetClientRect(handle_, &rc); + + width_ = rc.right - rc.left; + height_ = rc.bottom - rc.top; + } + + void Window::SetActive(bool actived) + { + if (!handle_) + return; + + if (is_fullscreen_) + { + if (actived) + { + ChangeFullScreenResolution(width_, height_, device_name_); + + MONITORINFOEX info = GetMoniterInfoEx(handle_); + ::SetWindowPos(handle_, HWND_TOPMOST, info.rcMonitor.top, info.rcMonitor.left, width_, height_, SWP_NOACTIVATE); + } + else + { + RestoreResolution(device_name_); + + ::SetWindowPos(handle_, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + ::ShowWindow(handle_, SW_MINIMIZE); + } + } + } + + namespace + { + MONITORINFOEX GetMoniterInfoEx(HWND hwnd) + { + HMONITOR monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + MONITORINFOEX monitor_info; + + memset(&monitor_info, 0, sizeof(MONITORINFOEX)); + monitor_info.cbSize = sizeof(MONITORINFOEX); + ::GetMonitorInfoW(monitor, &monitor_info); + + return monitor_info; + } + + void AdjustWindow(UINT width, UINT height, DWORD style, UINT* win_width, UINT* win_height) + { + RECT rc; + ::SetRect(&rc, 0, 0, (int)width, (int)height); + ::AdjustWindowRect(&rc, style, false); + + *win_width = rc.right - rc.left; + *win_height = rc.bottom - rc.top; + + MONITORINFOEX info = GetMoniterInfoEx(NULL); + + UINT screenw = info.rcWork.right - info.rcWork.left; + UINT screenh = info.rcWork.bottom - info.rcWork.top; + + if (*win_width > screenw) + *win_width = screenw; + if (*win_height > screenh) + *win_height = screenh; + } + + void ChangeFullScreenResolution(int width, int height, WCHAR* device_name) + { + DEVMODE mode; + + memset(&mode, 0, sizeof(mode)); + mode.dmSize = sizeof(DEVMODE); + mode.dmBitsPerPel = ::GetDeviceCaps(::GetDC(0), BITSPIXEL);; + mode.dmPelsWidth = width; + mode.dmPelsHeight = height; + mode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; + + if (::ChangeDisplaySettingsExW(device_name, &mode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL) + KGE_ERROR_LOG(L"ChangeDisplaySettings failed"); + } + + void RestoreResolution(WCHAR* device_name) + { + ::ChangeDisplaySettingsExW(device_name, NULL, NULL, 0, NULL); + } + } +} diff --git a/src/kiwano/base/Window.h b/src/kiwano/base/Window.h new file mode 100644 index 00000000..bd1dc631 --- /dev/null +++ b/src/kiwano/base/Window.h @@ -0,0 +1,95 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#pragma once +#include "../macros.h" +#include "../core/core.h" +#include "../math/helper.h" +#include "types.h" + +namespace kiwano +{ + class KGE_API Window + : public Singleton + { + KGE_DECLARE_SINGLETON(Window); + + public: + // 获取标题 + String GetTitle() const; + + // 获取窗口大小 + Size GetSize() const; + + // 获取窗口宽度 + float GetWidth() const; + + // 获取窗口高度 + float GetHeight() const; + + // 设置标题 + void SetTitle(String const& title); + + // 设置窗口图标 + void SetIcon(LPCWSTR icon_resource); + + // 重设窗口大小 + void Resize(int width, int height); + + // 设置全屏模式 + void SetFullscreen(bool fullscreen, int width, int height); + + // 设置鼠标指针 + void SetMouseCursor(MouseCursor cursor); + + public: + HRESULT Create( + String const& title, + int width, + int height, + LPCWSTR icon, + bool fullscreen, + WNDPROC proc + ); + + void Prepare(); + + HWND GetHandle() const; + + DWORD GetWindowStyle() const; + + void UpdateWindowRect(); + + void SetActive(bool actived); + + protected: + Window(); + + ~Window(); + + private: + HWND handle_; + bool is_fullscreen_; + int width_; + int height_; + WCHAR* device_name_; + MouseCursor mouse_cursor_; + }; +} diff --git a/src/kiwano/base/time.cpp b/src/kiwano/base/time.cpp index 0a5349d1..eba6b604 100644 --- a/src/kiwano/base/time.cpp +++ b/src/kiwano/base/time.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "time.h" -#include "logs.h" +#include "Logger.h" #include #include diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index 76c05fd6..92597682 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -59,15 +59,15 @@ // base // -#include "renderer/render.h" +#include "renderer/Renderer.h" #include "renderer/Image.h" #include "renderer/GifImage.h" #include "base/time.h" -#include "base/window.h" +#include "base/Window.h" #include "base/input.h" #include "base/Director.h" -#include "base/logs.h" +#include "base/Logger.h" #include "base/SmartPtr.hpp" #include "base/ComPtr.hpp" diff --git a/src/kiwano/platform/Application.cpp b/src/kiwano/platform/Application.cpp index da311c13..c47fa142 100644 --- a/src/kiwano/platform/Application.cpp +++ b/src/kiwano/platform/Application.cpp @@ -20,11 +20,11 @@ #include "Application.h" #include "modules.h" -#include "../base/window.h" -#include "../base/logs.h" +#include "../base/Window.h" +#include "../base/Logger.h" #include "../base/input.h" #include "../base/Director.h" -#include "../renderer/render.h" +#include "../renderer/Renderer.h" #include "../utils/ResourceCache.h" #include // GET_X_LPARAM, GET_Y_LPARAM #include // ImmAssociateContext diff --git a/src/kiwano/platform/modules.cpp b/src/kiwano/platform/modules.cpp index 555597af..93559f02 100644 --- a/src/kiwano/platform/modules.cpp +++ b/src/kiwano/platform/modules.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "modules.h" -#include "../base/logs.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/renderer/D2DDeviceResources.cpp b/src/kiwano/renderer/D2DDeviceResources.cpp index 9dfba175..e930829a 100644 --- a/src/kiwano/renderer/D2DDeviceResources.cpp +++ b/src/kiwano/renderer/D2DDeviceResources.cpp @@ -20,7 +20,7 @@ #include "D2DDeviceResources.h" #include "ImageCache.h" -#include "../base/logs.h" +#include "../base/Logger.h" #include "../utils/FileUtil.h" #pragma comment(lib, "d2d1.lib") diff --git a/src/kiwano/renderer/D3D10DeviceResources.cpp b/src/kiwano/renderer/D3D10DeviceResources.cpp index b030ccbf..db3b3839 100644 --- a/src/kiwano/renderer/D3D10DeviceResources.cpp +++ b/src/kiwano/renderer/D3D10DeviceResources.cpp @@ -20,7 +20,7 @@ #include "D3D10DeviceResources.h" -#include "../base/logs.h" +#include "../base/Logger.h" #pragma comment(lib, "d3d10_1.lib") diff --git a/src/kiwano/renderer/D3D11DeviceResources.cpp b/src/kiwano/renderer/D3D11DeviceResources.cpp index 1c7b04fa..07f052dc 100644 --- a/src/kiwano/renderer/D3D11DeviceResources.cpp +++ b/src/kiwano/renderer/D3D11DeviceResources.cpp @@ -20,7 +20,7 @@ #include "D3D11DeviceResources.h" -#include "../base/logs.h" +#include "../base/Logger.h" #include // IsWindows10OrGreater #pragma comment(lib, "d3d11.lib") diff --git a/src/kiwano/renderer/Geometry.cpp b/src/kiwano/renderer/Geometry.cpp index 2814ac8a..9d5ada2e 100644 --- a/src/kiwano/renderer/Geometry.cpp +++ b/src/kiwano/renderer/Geometry.cpp @@ -19,8 +19,8 @@ // THE SOFTWARE. #include "Geometry.h" -#include "render.h" -#include "../base/logs.h" +#include "Renderer.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/renderer/GifImage.cpp b/src/kiwano/renderer/GifImage.cpp index a7f565bb..d2546c51 100644 --- a/src/kiwano/renderer/GifImage.cpp +++ b/src/kiwano/renderer/GifImage.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "GifImage.h" -#include "../base/logs.h" +#include "../base/Logger.h" #include "../utils/FileUtil.h" namespace kiwano diff --git a/src/kiwano/renderer/GifImage.h b/src/kiwano/renderer/GifImage.h index 6e7d5c0c..556a2b82 100644 --- a/src/kiwano/renderer/GifImage.h +++ b/src/kiwano/renderer/GifImage.h @@ -20,7 +20,7 @@ #pragma once #include "../base/Resource.h" -#include "render.h" +#include "Renderer.h" namespace kiwano { diff --git a/src/kiwano/renderer/Image.cpp b/src/kiwano/renderer/Image.cpp index d1189c14..d01a8d91 100644 --- a/src/kiwano/renderer/Image.cpp +++ b/src/kiwano/renderer/Image.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "Image.h" -#include "../base/logs.h" +#include "../base/Logger.h" #include "../platform/modules.h" namespace kiwano diff --git a/src/kiwano/renderer/ImageCache.cpp b/src/kiwano/renderer/ImageCache.cpp index e0a048b8..342f2268 100644 --- a/src/kiwano/renderer/ImageCache.cpp +++ b/src/kiwano/renderer/ImageCache.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "ImageCache.h" -#include "../base/logs.h" +#include "../base/Logger.h" namespace kiwano { diff --git a/src/kiwano/renderer/ImageCache.h b/src/kiwano/renderer/ImageCache.h index 925e15ed..2d8365df 100644 --- a/src/kiwano/renderer/ImageCache.h +++ b/src/kiwano/renderer/ImageCache.h @@ -20,7 +20,7 @@ #pragma once #include "../core/singleton.hpp" -#include "render.h" +#include "Renderer.h" namespace kiwano { diff --git a/src/kiwano/renderer/render.cpp b/src/kiwano/renderer/Renderer.cpp similarity index 99% rename from src/kiwano/renderer/render.cpp rename to src/kiwano/renderer/Renderer.cpp index 3829fb2c..9cfe13b1 100644 --- a/src/kiwano/renderer/render.cpp +++ b/src/kiwano/renderer/Renderer.cpp @@ -18,9 +18,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include "render.h" -#include "../base/logs.h" -#include "../base/window.h" +#include "Renderer.h" +#include "../base/Logger.h" +#include "../base/Window.h" namespace kiwano { diff --git a/src/kiwano/renderer/render.h b/src/kiwano/renderer/Renderer.h similarity index 100% rename from src/kiwano/renderer/render.h rename to src/kiwano/renderer/Renderer.h diff --git a/src/kiwano/ui/Button.cpp b/src/kiwano/ui/Button.cpp index 746b2c49..9a8a841c 100644 --- a/src/kiwano/ui/Button.cpp +++ b/src/kiwano/ui/Button.cpp @@ -20,7 +20,7 @@ #include "Button.h" #include "../2d/Stage.h" -#include "../base/window.h" +#include "../base/Window.h" namespace kiwano { diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index 19122e59..b4c111c8 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "ResourceCache.h" -#include "../base/logs.h" +#include "../base/Logger.h" #include "../2d/Frame.h" #include "../2d/FrameSequence.h" #include "../renderer/GifImage.h"