| 
									
										
										
										
											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 "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 19:24:18 +08:00
										 |  |  |  | 		: initialized_(false) | 
					
						
							|  |  |  |  | 		, window_inactived_(false) | 
					
						
							|  |  |  |  | 		, curr_scene_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		, next_scene_(nullptr) | 
					
						
							|  |  |  |  | 		, transition_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		, window_(nullptr) | 
					
						
							|  |  |  |  | 		, graphics_(nullptr) | 
					
						
							|  |  |  |  | 		, input_(nullptr) | 
					
						
							|  |  |  |  | 		, audio_(nullptr) | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		, debug_enabled_(false) | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		, time_scale_(1.f) | 
					
						
							| 
									
										
										
										
											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-21 19:24:18 +08:00
										 |  |  |  | 		window_ = Window::Instance(); | 
					
						
							|  |  |  |  | 		graphics_ = devices::Graphics::Instance(); | 
					
						
							|  |  |  |  | 		input_ = devices::Input::Instance(); | 
					
						
							|  |  |  |  | 		audio_ = devices::Audio::Instance(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			Factory::Instance()->Init(debug_enabled_) | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			window_->Init( | 
					
						
							|  |  |  |  | 				options.title, | 
					
						
							|  |  |  |  | 				options.width, | 
					
						
							|  |  |  |  | 				options.height, | 
					
						
							|  |  |  |  | 				options.icon, | 
					
						
							|  |  |  |  | 				Game::WndProc, | 
					
						
							|  |  |  |  | 				debug_enabled_ | 
					
						
							|  |  |  |  | 			) | 
					
						
							|  |  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		HWND hwnd = window_->GetHandle(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			graphics_->Init( | 
					
						
							|  |  |  |  | 				hwnd, | 
					
						
							|  |  |  |  | 				options.vsync, | 
					
						
							|  |  |  |  | 				debug_enabled_ | 
					
						
							|  |  |  |  | 			) | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			input_->Init( | 
					
						
							|  |  |  |  | 				hwnd, | 
					
						
							|  |  |  |  | 				window_->GetContentScaleX(), | 
					
						
							|  |  |  |  | 				window_->GetContentScaleY(), | 
					
						
							|  |  |  |  | 				debug_enabled_ | 
					
						
							|  |  |  |  | 			) | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 		 | 
					
						
							|  |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			audio_->Init(debug_enabled_) | 
					
						
							|  |  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		// disable imm
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		::ImmAssociateContext(hwnd, nullptr); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		// show console if debug mode enabled
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		HWND console = ::GetConsoleWindow(); | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		if (debug_enabled_ && !console) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 			if (::AllocConsole()) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 				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); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		else if (!debug_enabled_ && console) | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 			::ShowWindow(console, SW_HIDE); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		// disable the close button of console
 | 
					
						
							| 
									
										
										
										
											2018-11-20 23:47:56 +08:00
										 |  |  |  | 		if (console) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			HMENU hmenu = ::GetSystemMenu(console, FALSE); | 
					
						
							|  |  |  |  | 			::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		// use Game instance in message loop
 | 
					
						
							|  |  |  |  | 		::SetWindowLongW(hwnd, GWLP_USERDATA, PtrToUlong(this)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 01:34:41 +08:00
										 |  |  |  | 		initialized_ = true; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void Game::Run() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		if (!initialized_) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		::ShowWindow(window_->GetHandle(), SW_SHOWNORMAL); | 
					
						
							|  |  |  |  | 		::UpdateWindow(window_->GetHandle()); | 
					
						
							| 
									
										
										
										
											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 19:24:18 +08:00
										 |  |  |  | 		if (window_) | 
					
						
							|  |  |  |  | 			::DestroyWindow(window_->GetHandle()); | 
					
						
							| 
									
										
										
										
											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-21 19:24:18 +08:00
										 |  |  |  | 	void Game::SetTimeScale(float scale) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		time_scale_ = scale; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		const auto dt = (now - last) * time_scale_; | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		last = now; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		input_->Update(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		 | 
					
						
							|  |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			graphics->BeginDraw(window_->GetHandle()) | 
					
						
							|  |  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (transition_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			transition_->Render(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else if (curr_scene_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 			curr_scene_->Render(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		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-21 19:24:18 +08:00
										 |  |  |  | 			Debuger::Instance()->Render(); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  |  | 			graphics->EndDraw() | 
					
						
							|  |  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (!window_inactived_) | 
					
						
							| 
									
										
										
										
											2018-11-21 19:24:18 +08:00
										 |  |  |  | 			::InvalidateRect(window_->GetHandle(), NULL, FALSE); | 
					
						
							| 
									
										
										
										
											2018-11-19 17:56:17 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } |