| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Easy2D - 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 "render.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | #include "Game.h"
 | 
					
						
							|  |  |  |  | #include "KeyEvent.h"
 | 
					
						
							|  |  |  |  | #include "MouseEvent.h"
 | 
					
						
							|  |  |  |  | #include "../math/scalar.hpp"
 | 
					
						
							|  |  |  |  | #include <imm.h>
 | 
					
						
							|  |  |  |  | #pragma comment (lib ,"imm32.lib")
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define WINDOW_STYLE	WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME
 | 
					
						
							|  |  |  |  | #define REGISTER_CLASS	L"Easy2DApp"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace easy2d | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	namespace | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		void GetContentScale(float* scale_x, float* scale_y); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		Rect LocateWindow(int width, int height, float scale_x, float scale_y); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	WindowImpl::WindowImpl() | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		: handle(nullptr) | 
					
						
							|  |  |  |  | 		, scale_x(1.f) | 
					
						
							|  |  |  |  | 		, scale_y(1.f) | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 		, initialized(false) | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	WindowImpl::~WindowImpl() | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 		E2D_LOG("Destroying window"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		if (handle) | 
					
						
							|  |  |  |  | 			::DestroyWindow(handle); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 	void WindowImpl::Init(String title, int width, int height, LPCWSTR icon, bool debug) | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 		if (initialized) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 		E2D_LOG("Initing window"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		HINSTANCE hinstance	= GetModuleHandle(nullptr); | 
					
						
							|  |  |  |  | 		WNDCLASSEX wcex		= { 0 }; | 
					
						
							|  |  |  |  | 		wcex.cbSize			= sizeof(WNDCLASSEX); | 
					
						
							|  |  |  |  | 		wcex.lpszClassName	= REGISTER_CLASS; | 
					
						
							|  |  |  |  | 		wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; | 
					
						
							|  |  |  |  | 		wcex.lpfnWndProc	= WndProc; | 
					
						
							|  |  |  |  | 		wcex.hIcon			= nullptr; | 
					
						
							|  |  |  |  | 		wcex.cbClsExtra		= 0; | 
					
						
							|  |  |  |  | 		wcex.cbWndExtra		= sizeof(LONG_PTR); | 
					
						
							|  |  |  |  | 		wcex.hInstance		= hinstance; | 
					
						
							|  |  |  |  | 		wcex.hbrBackground	= nullptr; | 
					
						
							|  |  |  |  | 		wcex.lpszMenuName	= nullptr; | 
					
						
							|  |  |  |  | 		wcex.hCursor		= ::LoadCursor(nullptr, IDC_ARROW); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (icon) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			wcex.hIcon = (HICON)::LoadImage( | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 				hinstance, | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 				icon, | 
					
						
							|  |  |  |  | 				IMAGE_ICON, | 
					
						
							|  |  |  |  | 				0, | 
					
						
							|  |  |  |  | 				0, | 
					
						
							|  |  |  |  | 				LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		::RegisterClassEx(&wcex); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		GetContentScale(&scale_x, &scale_y); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD>㴰<EFBFBD>ڴ<EFBFBD>С
 | 
					
						
							|  |  |  |  | 		Rect client_rect = LocateWindow(width, height, scale_x, scale_y); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		handle = ::CreateWindowEx( | 
					
						
							|  |  |  |  | 			NULL, | 
					
						
							|  |  |  |  | 			REGISTER_CLASS, | 
					
						
							|  |  |  |  | 			title.c_str(), | 
					
						
							|  |  |  |  | 			WINDOW_STYLE, | 
					
						
							|  |  |  |  | 			static_cast<int>(client_rect.origin.x), | 
					
						
							|  |  |  |  | 			static_cast<int>(client_rect.origin.y), | 
					
						
							|  |  |  |  | 			static_cast<int>(client_rect.size.width), | 
					
						
							|  |  |  |  | 			static_cast<int>(client_rect.size.height), | 
					
						
							|  |  |  |  | 			nullptr, | 
					
						
							|  |  |  |  | 			nullptr, | 
					
						
							|  |  |  |  | 			hinstance, | 
					
						
							|  |  |  |  | 			this | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (handle == nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			::UnregisterClass(REGISTER_CLASS, hinstance); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			const char* err = "Create window failed!"; | 
					
						
							| 
									
										
										
										
											2018-11-16 17:19:03 +08:00
										 |  |  |  | 			logs::Errorln(HRESULT_FROM_WIN32(GetLastError()), err); | 
					
						
							| 
									
										
										
										
											2018-11-16 15:53:39 +08:00
										 |  |  |  | 			throw std::runtime_error(err); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뷨
 | 
					
						
							|  |  |  |  | 		::ImmAssociateContext(handle, nullptr); | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		initialized = true; | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	String WindowImpl::GetTitle() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (handle) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			wchar_t title[256]; | 
					
						
							|  |  |  |  | 			GetWindowTextW(handle, title, 256); | 
					
						
							|  |  |  |  | 			return title; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		return String(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  |  | 	void WindowImpl::SetTitle(String const& title) | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (handle) | 
					
						
							|  |  |  |  | 			::SetWindowText(handle, title.c_str()); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	Size WindowImpl::GetSize() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (handle) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			RECT rect; | 
					
						
							|  |  |  |  | 			GetClientRect(handle, &rect); | 
					
						
							|  |  |  |  | 			return Size( | 
					
						
							|  |  |  |  | 				static_cast<float>(rect.right - rect.left), | 
					
						
							|  |  |  |  | 				static_cast<float>(rect.bottom - rect.top) | 
					
						
							|  |  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		return Size(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	float WindowImpl::GetWidth() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return GetSize().width; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	float WindowImpl::GetHeight() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return GetSize().height; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	void WindowImpl::SetSize(int width, int height) | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (handle) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			Rect rect = LocateWindow(width, height, scale_x, scale_y); | 
					
						
							|  |  |  |  | 			::MoveWindow( | 
					
						
							|  |  |  |  | 				handle, | 
					
						
							|  |  |  |  | 				static_cast<int>(rect.origin.x), | 
					
						
							|  |  |  |  | 				static_cast<int>(rect.origin.y), | 
					
						
							|  |  |  |  | 				static_cast<int>(rect.size.width), | 
					
						
							|  |  |  |  | 				static_cast<int>(rect.size.height), | 
					
						
							|  |  |  |  | 				TRUE | 
					
						
							|  |  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	void WindowImpl::SetIcon(LPCWSTR icon_resource) | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (handle) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			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); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	HWND WindowImpl::GetHandle() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return handle; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	float WindowImpl::GetContentScaleX() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return scale_x; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 	float WindowImpl::GetContentScaleY() const | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return scale_y; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  |  | 	void WindowImpl::Poll() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		static MSG msg = {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			::TranslateMessage(&msg); | 
					
						
							|  |  |  |  | 			::DispatchMessage(&msg); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 	namespace | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		void GetContentScale(float* scale_x, float* scale_y) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			const float DEFAULT_SCREEN_DPI = 96.f; | 
					
						
							|  |  |  |  | 			const HDC dc = GetDC(NULL); | 
					
						
							|  |  |  |  | 			float xdpi = static_cast<float>(GetDeviceCaps(dc, LOGPIXELSX)); | 
					
						
							|  |  |  |  | 			float ydpi = static_cast<float>(GetDeviceCaps(dc, LOGPIXELSY)); | 
					
						
							|  |  |  |  | 			ReleaseDC(NULL, dc); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (scale_x) | 
					
						
							|  |  |  |  | 				*scale_x = xdpi / DEFAULT_SCREEN_DPI; | 
					
						
							|  |  |  |  | 			if (scale_y) | 
					
						
							|  |  |  |  | 				*scale_y = ydpi / DEFAULT_SCREEN_DPI; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		Rect LocateWindow(int width, int height, float scale_x, float scale_y) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			int max_width = ::GetSystemMetrics(SM_CXSCREEN); | 
					
						
							|  |  |  |  | 			int max_height = ::GetSystemMetrics(SM_CYSCREEN); | 
					
						
							|  |  |  |  | 			RECT rect = | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 				0, | 
					
						
							|  |  |  |  | 				0, | 
					
						
							|  |  |  |  | 				static_cast<LONG>(math::Ceil(width * scale_x)), | 
					
						
							|  |  |  |  | 				static_cast<LONG>(math::Ceil(height * scale_y)) | 
					
						
							|  |  |  |  | 			}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			::AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, NULL); | 
					
						
							|  |  |  |  | 			width = static_cast<int>(rect.right - rect.left); | 
					
						
							|  |  |  |  | 			height = static_cast<int>(rect.bottom - rect.top); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			if (max_width < width || max_height < height) | 
					
						
							|  |  |  |  | 				logs::Warningln("The window is larger than screen!"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			width = std::min(width, max_width); | 
					
						
							|  |  |  |  | 			height = std::min(height, max_height); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			return Rect( | 
					
						
							|  |  |  |  | 				static_cast<float>((max_width - width) / 2), | 
					
						
							|  |  |  |  | 				static_cast<float>((max_height - height) / 2), | 
					
						
							|  |  |  |  | 				static_cast<float>(width), | 
					
						
							|  |  |  |  | 				static_cast<float>(height) | 
					
						
							|  |  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 		LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			LRESULT result = 0; | 
					
						
							|  |  |  |  | 			bool was_handled = false; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			Game * game = reinterpret_cast<Game*>( | 
					
						
							|  |  |  |  | 				static_cast<LONG_PTR>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA)) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 				); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			switch (msg) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			{ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
 | 
					
						
							|  |  |  |  | 			case WM_LBUTTONUP: | 
					
						
							|  |  |  |  | 			case WM_LBUTTONDOWN: | 
					
						
							|  |  |  |  | 			case WM_LBUTTONDBLCLK: | 
					
						
							|  |  |  |  | 			case WM_MBUTTONUP: | 
					
						
							|  |  |  |  | 			case WM_MBUTTONDOWN: | 
					
						
							|  |  |  |  | 			case WM_MBUTTONDBLCLK: | 
					
						
							|  |  |  |  | 			case WM_RBUTTONUP: | 
					
						
							|  |  |  |  | 			case WM_RBUTTONDOWN: | 
					
						
							|  |  |  |  | 			case WM_RBUTTONDBLCLK: | 
					
						
							|  |  |  |  | 			case WM_MOUSEMOVE: | 
					
						
							|  |  |  |  | 			case WM_MOUSEWHEEL: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 				game->Dispatch(MouseEvent(msg, w_param, l_param)); | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			result = 0; | 
					
						
							|  |  |  |  | 			was_handled = true; | 
					
						
							|  |  |  |  | 			break; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
 | 
					
						
							|  |  |  |  | 			case WM_KEYDOWN: | 
					
						
							|  |  |  |  | 			case WM_KEYUP: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 				game->Dispatch(KeyEvent(msg, w_param, l_param)); | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			result = 0; | 
					
						
							|  |  |  |  | 			was_handled = true; | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>С<EFBFBD>仯<EFBFBD><E4BBAF>Ϣ
 | 
					
						
							|  |  |  |  | 			case WM_SIZE: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				UINT width = LOWORD(l_param); | 
					
						
							|  |  |  |  | 				UINT height = HIWORD(l_param); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 				// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD>һ<EFBFBD><D2BB> WM_SIZE <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ
 | 
					
						
							|  |  |  |  | 				// Ŀ<><C4BF><EFBFBD>Ĵ<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><D4BA><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܵ<EFBFBD>
 | 
					
						
							|  |  |  |  | 				// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD> EndDraw ʱ<><CAB1><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2018-11-15 14:35:19 +08:00
										 |  |  |  | 				devices::Graphics::Instance()->Resize(width, height); | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ʱ仯<CAB1><E4BBAF>Ϣ
 | 
					
						
							|  |  |  |  | 			case WM_DISPLAYCHANGE: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				// <20>ػ<EFBFBD><D8BB>ͻ<EFBFBD><CDBB><EFBFBD>
 | 
					
						
							|  |  |  |  | 				::InvalidateRect(hwnd, nullptr, FALSE); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			result = 0; | 
					
						
							|  |  |  |  | 			was_handled = true; | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			// <20>ػ洰<D8BB><E6B4B0>
 | 
					
						
							|  |  |  |  | 			case WM_PAINT: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				game->DrawScene(); | 
					
						
							|  |  |  |  | 				::ValidateRect(hwnd, nullptr); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			result = 0; | 
					
						
							|  |  |  |  | 			was_handled = true; | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			// <20><><EFBFBD>ڹر<DAB9><D8B1><EFBFBD>Ϣ
 | 
					
						
							|  |  |  |  | 			case WM_CLOSE: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				if (game->OnClose()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 					game->Quit(); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 			result = 0; | 
					
						
							|  |  |  |  | 			was_handled = true; | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
 | 
					
						
							|  |  |  |  | 			case WM_DESTROY: | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				::PostQuitMessage(0); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			result = 1; | 
					
						
							|  |  |  |  | 			was_handled = true; | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			if (!was_handled) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				result = ::DefWindowProc(hwnd, msg, w_param, l_param); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | 			return result; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  |  | } |