| 
									
										
										
										
											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 "Game.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | #include "input.h"
 | 
					
						
							|  |  |  |  | #include "audio.h"
 | 
					
						
							|  |  |  |  | #include "modules.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-21 17:18:59 +08:00
										 |  |  |  | #include "Factory.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | #include "Scene.h"
 | 
					
						
							|  |  |  |  | #include "Transition.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-19 19:32:12 +08:00
										 |  |  |  | #include "Debuger.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | #include "../math/Matrix.hpp"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | #include <thread>
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | #include <imm.h>
 | 
					
						
							|  |  |  |  | #pragma comment (lib ,"imm32.lib")
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace easy2d | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	Game::Game() | 
					
						
							| 
									
										
										
										
											2018-11-21 16:26:52 +08:00
										 |  |  |  | 		: curr_scene_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		, next_scene_(nullptr) | 
					
						
							|  |  |  |  | 		, transition_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		, debug_enabled_(false) | 
					
						
							|  |  |  |  | 		, initialized_(false) | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		, hwnd_(nullptr) | 
					
						
							|  |  |  |  | 		, window_inactived_(false) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		::CoInitialize(nullptr); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 	Game::Game(Options const & options) | 
					
						
							|  |  |  |  | 		: Game() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		Init(options); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 	Game::~Game() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		::CoUninitialize(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 	void Game::Init(const Options& options) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		if (initialized_) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		debug_enabled_ = options.debug; | 
					
						
							| 
									
										
										
										
											2018-11-12 22:36:50 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		Window::Instance()->Init( | 
					
						
							|  |  |  |  | 			options.title, | 
					
						
							|  |  |  |  | 			options.width, | 
					
						
							|  |  |  |  | 			options.height, | 
					
						
							|  |  |  |  | 			options.icon, | 
					
						
							|  |  |  |  | 			Game::WndProc, | 
					
						
							|  |  |  |  | 			debug_enabled_ | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const auto window = Window::Instance(); | 
					
						
							|  |  |  |  | 		hwnd_ = window->GetHandle(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		::SetWindowLongW(hwnd_, GWLP_USERDATA, PtrToUlong(this)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 17:18:59 +08:00
										 |  |  |  | 		Factory::Instance()->Init(debug_enabled_); | 
					
						
							| 
									
										
										
										
											2018-11-21 16:26:52 +08:00
										 |  |  |  | 		devices::Graphics::Instance()->Init(hwnd_, options.graphics_options, debug_enabled_); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		devices::Input::Instance()->Init(hwnd_, window->GetContentScaleX(), window->GetContentScaleY(), debug_enabled_); | 
					
						
							| 
									
										
										
										
											2018-11-15 14:35:19 +08:00
										 |  |  |  | 		devices::Audio::Instance()->Init(debug_enabled_); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		// disable imm
 | 
					
						
							|  |  |  |  | 		::ImmAssociateContext(hwnd_, nullptr); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		HWND console = ::GetConsoleWindow(); | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		if (debug_enabled_) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (console == nullptr) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				if (::AllocConsole()) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					console = ::GetConsoleWindow(); | 
					
						
							|  |  |  |  | 					FILE * stdoutStream, *stdinStream, *stderrStream; | 
					
						
							|  |  |  |  | 					freopen_s(&stdoutStream, "conout$", "w+t", stdout); | 
					
						
							|  |  |  |  | 					freopen_s(&stdinStream, "conin$", "r+t", stdin); | 
					
						
							|  |  |  |  | 					freopen_s(&stderrStream, "conout$", "w+t", stderr); | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (console) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				::ShowWindow(console, SW_HIDE); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-20 23:47:56 +08:00
										 |  |  |  | 		if (console) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// disable the close button of console
 | 
					
						
							|  |  |  |  | 			HMENU hmenu = ::GetSystemMenu(console, FALSE); | 
					
						
							|  |  |  |  | 			::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		initialized_ = true; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void Game::Run() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (next_scene_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			next_scene_->OnEnter(); | 
					
						
							|  |  |  |  | 			curr_scene_ = next_scene_; | 
					
						
							|  |  |  |  | 			next_scene_ = nullptr; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		::ShowWindow(hwnd_, SW_SHOWNORMAL); | 
					
						
							|  |  |  |  | 		::UpdateWindow(hwnd_); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		MSG msg = {}; | 
					
						
							| 
									
										
										
										
											2018-11-21 16:26:52 +08:00
										 |  |  |  | 		while (::GetMessageW(&msg, nullptr, 0, 0)) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 			::TranslateMessage(&msg); | 
					
						
							|  |  |  |  | 			::DispatchMessageW(&msg); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void Game::Quit() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-21 16:26:52 +08:00
										 |  |  |  | 		::DestroyWindow(hwnd_); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 	bool Game::EnterScene(spScene const & scene) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		if (!scene) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-15 17:59:18 +08:00
										 |  |  |  | 			logs::Warningln("Game::EnterScene failed, scene is nullptr"); | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 		if (curr_scene_ == scene || | 
					
						
							|  |  |  |  | 			next_scene_ == scene) | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		next_scene_ = scene; | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 	bool Game::EnterScene(spScene const& scene, spTransition const& transition) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (!EnterScene(scene)) | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							|  |  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		if (transition) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (transition_) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				transition_->Stop(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			transition_ = transition; | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 			transition_->Init(curr_scene_, next_scene_); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 	spScene const& Game::GetCurrentScene() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return curr_scene_; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 	void Game::Update() | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		static auto last = time::Now(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		const auto now = time::Now(); | 
					
						
							|  |  |  |  | 		const auto dt = now - last; | 
					
						
							|  |  |  |  | 		last = now; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		devices::Input::Instance()->Update(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 		if (curr_scene_) | 
					
						
							|  |  |  |  | 			curr_scene_->Update(dt); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 		if (next_scene_) | 
					
						
							|  |  |  |  | 			next_scene_->Update(dt); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 19:32:12 +08:00
										 |  |  |  | 		if (debug_enabled_) | 
					
						
							|  |  |  |  | 			Debuger::Instance()->Update(dt); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		if (transition_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 			transition_->Update(dt); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (transition_->IsDone()) | 
					
						
							|  |  |  |  | 				transition_ = nullptr; | 
					
						
							|  |  |  |  | 			else | 
					
						
							|  |  |  |  | 				return; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (next_scene_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (curr_scene_) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				curr_scene_->OnExit(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			next_scene_->OnEnter(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			curr_scene_ = next_scene_; | 
					
						
							|  |  |  |  | 			next_scene_ = nullptr; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 	void Game::Render() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		auto graphics = devices::Graphics::Instance(); | 
					
						
							|  |  |  |  | 		graphics->BeginDraw(hwnd_); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (transition_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			transition_->Render(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else if (curr_scene_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			curr_scene_->Visit(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (debug_enabled_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			graphics->SetTransform(math::Matrix()); | 
					
						
							|  |  |  |  | 			graphics->SetOpacity(1.f); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (curr_scene_) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				curr_scene_->DrawBorder(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (next_scene_) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				next_scene_->DrawBorder(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 19:32:12 +08:00
										 |  |  |  | 			Debuger::Instance()->Visit(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		graphics->EndDraw(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (!window_inactived_) | 
					
						
							|  |  |  |  | 			::InvalidateRect(hwnd_, NULL, FALSE); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 16:39:24 +08:00
										 |  |  |  | 	void Game::Dispatch(MouseEvent const & e) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (transition_) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (curr_scene_) | 
					
						
							|  |  |  |  | 			curr_scene_->Dispatch(e, false); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void Game::Dispatch(KeyEvent const & e) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (transition_) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (curr_scene_) | 
					
						
							|  |  |  |  | 			curr_scene_->Dispatch(e, false); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 	LRESULT CALLBACK Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		LRESULT result = 0; | 
					
						
							|  |  |  |  | 		bool was_handled = false; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		Game * game = reinterpret_cast<Game*>( | 
					
						
							|  |  |  |  | 			static_cast<LONG_PTR>(::GetWindowLongW(hwnd, GWLP_USERDATA)) | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		switch (msg) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		case WM_PAINT: | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 			PAINTSTRUCT ps; | 
					
						
							|  |  |  |  | 			::BeginPaint(hwnd, &ps); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			game->Update(); | 
					
						
							|  |  |  |  | 			game->Render(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			::EndPaint(hwnd, &ps); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		result = 0; | 
					
						
							|  |  |  |  | 		was_handled = true; | 
					
						
							|  |  |  |  | 		break; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		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-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 			game->Dispatch(MouseEvent(msg, w_param, l_param)); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		result = 0; | 
					
						
							|  |  |  |  | 		was_handled = true; | 
					
						
							|  |  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		case WM_KEYDOWN: | 
					
						
							|  |  |  |  | 		case WM_KEYUP: | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 			game->Dispatch(KeyEvent(msg, w_param, l_param)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		result = 0; | 
					
						
							|  |  |  |  | 		was_handled = true; | 
					
						
							|  |  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		case WM_SIZE: | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (SIZE_MAXHIDE == w_param || SIZE_MINIMIZED == w_param) | 
					
						
							|  |  |  |  | 				game->window_inactived_ = true; | 
					
						
							|  |  |  |  | 			else | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 				game->window_inactived_ = false; | 
					
						
							|  |  |  |  | 				::InvalidateRect(hwnd, nullptr, FALSE); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 			UINT width = LOWORD(l_param); | 
					
						
							|  |  |  |  | 			UINT height = HIWORD(l_param); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			// <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>
 | 
					
						
							|  |  |  |  | 			devices::Graphics::Instance()->Resize(width, height); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		break; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		case WM_DISPLAYCHANGE: | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			::InvalidateRect(hwnd, nullptr, FALSE); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		result = 0; | 
					
						
							|  |  |  |  | 		was_handled = true; | 
					
						
							|  |  |  |  | 		break; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		case WM_CLOSE: | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (game->OnClose()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-21 16:26:52 +08:00
										 |  |  |  | 				::DestroyWindow(hwnd); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 		result = 0; | 
					
						
							|  |  |  |  | 		was_handled = true; | 
					
						
							|  |  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		case WM_DESTROY: | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-21 16:26:52 +08:00
										 |  |  |  | 			game->OnExit(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 			::PostQuitMessage(0); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		result = 1; | 
					
						
							|  |  |  |  | 		was_handled = true; | 
					
						
							|  |  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (!was_handled) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			result = ::DefWindowProcW(hwnd, msg, w_param, l_param); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } |