| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | // 
 | 
					
						
							|  |  |  | // 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 "Application.h"
 | 
					
						
							|  |  |  | #include "modules.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | #include "../base/window.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | #include "../base/logs.h"
 | 
					
						
							|  |  |  | #include "../base/input.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:30:42 +08:00
										 |  |  | #include "../base/Director.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | #include "../renderer/render.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | #include "../utils/ResourceCache.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  | #include <windowsx.h>  // GET_X_LPARAM, GET_Y_LPARAM
 | 
					
						
							|  |  |  | #include <imm.h>  // ImmAssociateContext
 | 
					
						
							|  |  |  | #include <mutex>  // std::mutex
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #pragma comment(lib, "imm32.lib")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  | 	namespace | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		using FunctionToPerform = Function<void()>; | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  | 		std::mutex				 perform_mutex_; | 
					
						
							|  |  |  | 		Queue<FunctionToPerform> functions_to_perform_; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Options::Options(String const& title, int width, int height, LPCWSTR icon, Color clear_color, bool vsync, bool fullscreen, bool debug) | 
					
						
							|  |  |  | 		: title(title) | 
					
						
							|  |  |  | 		, width(width) | 
					
						
							|  |  |  | 		, height(height) | 
					
						
							|  |  |  | 		, icon(icon) | 
					
						
							|  |  |  | 		, clear_color(clear_color) | 
					
						
							|  |  |  | 		, vsync(vsync) | 
					
						
							|  |  |  | 		, fullscreen(fullscreen) | 
					
						
							|  |  |  | 		, debug(debug) | 
					
						
							|  |  |  | 	{} | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	Application::Application() | 
					
						
							|  |  |  | 		: end_(true) | 
					
						
							|  |  |  | 		, inited_(false) | 
					
						
							|  |  |  | 		, time_scale_(1.f) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  | 		ThrowIfFailed( | 
					
						
							|  |  |  | 			::CoInitialize(nullptr) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		Use(Renderer::GetInstance()); | 
					
						
							|  |  |  | 		Use(Input::GetInstance()); | 
					
						
							|  |  |  | 		Use(Director::GetInstance()); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Application::~Application() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Destroy(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		::CoUninitialize(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Init(const Options& options) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ThrowIfFailed( | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 			Window::GetInstance()->Create( | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 				options.title, | 
					
						
							|  |  |  | 				options.width, | 
					
						
							|  |  |  | 				options.height, | 
					
						
							|  |  |  | 				options.icon, | 
					
						
							|  |  |  | 				options.fullscreen, | 
					
						
							|  |  |  | 				Application::WndProc | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		Renderer::GetInstance()->SetClearColor(options.clear_color); | 
					
						
							|  |  |  | 		Renderer::GetInstance()->SetVSyncEnabled(options.vsync); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Setup all components
 | 
					
						
							|  |  |  | 		for (Component* c : components_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			c->SetupComponent(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (options.debug) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 			Director::GetInstance()->ShowDebugInfo(true); | 
					
						
							|  |  |  | 			Renderer::GetInstance()->SetCollectingStatus(true); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Everything is ready
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		OnReady(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		HWND hwnd = Window::GetInstance()->GetHandle(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// disable imm
 | 
					
						
							|  |  |  | 		::ImmAssociateContext(hwnd, nullptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// use Application instance in message loop
 | 
					
						
							|  |  |  | 		::SetWindowLongPtr(hwnd, GWLP_USERDATA, LONG_PTR(this)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		inited_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Run() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		HWND hwnd = Window::GetInstance()->GetHandle(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		if (!inited_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			throw std::exception("Calling Application::Run before Application::Init"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (hwnd) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			end_ = false; | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 			Window::GetInstance()->Prepare(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			MSG msg = {}; | 
					
						
							|  |  |  | 			while (::GetMessageW(&msg, nullptr, 0, 0) && !end_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				::TranslateMessage(&msg); | 
					
						
							|  |  |  | 				::DispatchMessageW(&msg); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Quit() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		end_ = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Destroy() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		// Clear all stages
 | 
					
						
							|  |  |  | 		Director::GetInstance()->ClearStages(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		if (inited_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			inited_ = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for (auto iter = components_.rbegin(); iter != components_.rend(); ++iter) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				(*iter)->DestroyComponent(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-30 15:35:47 +08:00
										 |  |  | 			components_.clear(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-30 15:35:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Destroy all instances
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		Director::DestroyInstance(); | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		ResourceCache::DestroyInstance(); | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		Input::DestroyInstance(); | 
					
						
							|  |  |  | 		Renderer::DestroyInstance(); | 
					
						
							|  |  |  | 		Window::DestroyInstance(); | 
					
						
							| 
									
										
										
										
											2019-08-03 23:28:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// DO NOT destroy Logger instance manually
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 		// Logger::DestroyInstance();
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Use(Component* component) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (component) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | #if defined(KGE_DEBUG)
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			if (components_.contains(component)) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 				KGE_ASSERT(false && "Component already exists!"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			components_.push_back(component); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Remove(Component* component) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (component) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			for (auto iter = components_.begin(); iter != components_.end(); ++iter) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if ((*iter) == component) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					(*iter)->DestroyComponent(); | 
					
						
							|  |  |  | 					components_.erase(iter); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::SetTimeScale(float scale_factor) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		time_scale_ = scale_factor; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Update() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 		// Before update
 | 
					
						
							|  |  |  | 		for (Component* c : components_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			c->BeforeUpdate(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// perform functions
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (!functions_to_perform_.empty()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				perform_mutex_.lock(); | 
					
						
							|  |  |  | 				auto functions = std::move(functions_to_perform_); | 
					
						
							|  |  |  | 				perform_mutex_.unlock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				while (!functions.empty()) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					auto& func = functions.front(); | 
					
						
							|  |  |  | 					if (func) | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						func(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					functions.pop(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		// Updating
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			static auto last = Time::Now(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			const auto now = Time::Now(); | 
					
						
							|  |  |  | 			const auto dt = (now - last) * time_scale_; | 
					
						
							|  |  |  | 			last = now; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			for (Component* c : components_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				c->OnUpdate(dt); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 		// After update
 | 
					
						
							|  |  |  | 		for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			(*rit)->AfterUpdate(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void Application::Render() | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 		// Before render
 | 
					
						
							|  |  |  | 		for (Component* c : components_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			c->BeforeRender(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 		// Rendering
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 		for (Component* c : components_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			c->OnRender(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 		// After render
 | 
					
						
							|  |  |  | 		for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			(*rit)->AfterRender(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 	void Application::DispatchEvent(Event& evt) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		for (Component* c : components_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			c->HandleEvent(evt); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 	void Application::PreformInMainThread(Function<void()> Function) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		std::lock_guard<std::mutex> lock(perform_mutex_); | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  | 		functions_to_perform_.push(Function); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 		Application * app = reinterpret_cast<Application*>(static_cast<LONG_PTR>(::GetWindowLongPtrW(hwnd, GWLP_USERDATA))); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!app) | 
					
						
							|  |  |  | 			return ::DefWindowProcW(hwnd, msg, wparam, lparam); | 
					
						
							| 
									
										
										
										
											2019-07-30 13:32:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Handle Message
 | 
					
						
							|  |  |  | 		for (Component* c : app->components_) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			c->HandleMessage(hwnd, msg, wparam, lparam); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		switch (msg) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		case WM_PAINT: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			app->Update(); | 
					
						
							|  |  |  | 			app->Render(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			::InvalidateRect(hwnd, NULL, FALSE); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_KEYDOWN: | 
					
						
							|  |  |  | 		case WM_SYSKEYDOWN: | 
					
						
							|  |  |  | 		case WM_KEYUP: | 
					
						
							|  |  |  | 		case WM_SYSKEYUP: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; | 
					
						
							|  |  |  | 			Event evt(down ? Event::KeyDown : Event::KeyUp); | 
					
						
							|  |  |  | 			evt.key.code = static_cast<int>(wparam); | 
					
						
							|  |  |  | 			evt.key.count = static_cast<int>(lparam & 0xFF); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_CHAR: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			Event evt(Event::Char); | 
					
						
							|  |  |  | 			evt.key.c = static_cast<char>(wparam); | 
					
						
							|  |  |  | 			evt.key.count = static_cast<int>(lparam & 0xFF); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		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: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			Event evt; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			evt.mouse.x = static_cast<float>(GET_X_LPARAM(lparam)); | 
					
						
							|  |  |  | 			evt.mouse.y = static_cast<float>(GET_Y_LPARAM(lparam)); | 
					
						
							|  |  |  | 			evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); | 
					
						
							|  |  |  | 			evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; } | 
					
						
							|  |  |  | 			else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; } | 
					
						
							|  |  |  | 			else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; } | 
					
						
							|  |  |  | 			else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; } | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; } | 
					
						
							|  |  |  | 			else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; } | 
					
						
							|  |  |  | 			else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; } | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_SIZE: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			if (SIZE_MAXHIDE == wparam || SIZE_MINIMIZED == wparam) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 				KGE_LOG(L"Window minimized"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 				KGE_LOG(L"Window resized"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 				Window::GetInstance()->UpdateWindowRect(); | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Event evt(Event::WindowResized); | 
					
						
							|  |  |  | 				evt.win.width = LOWORD(lparam); | 
					
						
							|  |  |  | 				evt.win.height = HIWORD(lparam); | 
					
						
							|  |  |  | 				app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_MOVE: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			int x = (int)(short)LOWORD(lparam); | 
					
						
							|  |  |  | 			int y = (int)(short)HIWORD(lparam); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			Event evt(Event::WindowMoved); | 
					
						
							|  |  |  | 			evt.win.x = x; | 
					
						
							|  |  |  | 			evt.win.y = y; | 
					
						
							|  |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_ACTIVATE: | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bool active = (LOWORD(wparam) != WA_INACTIVE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  | 			Window::GetInstance()->SetActive(active); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			Event evt(Event::WindowFocusChanged); | 
					
						
							|  |  |  | 			evt.win.focus = active; | 
					
						
							|  |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_SETTEXT: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 			KGE_LOG(L"Window title changed"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			Event evt(Event::WindowTitleChanged); | 
					
						
							|  |  |  | 			evt.win.title = reinterpret_cast<const wchar_t*>(lparam); | 
					
						
							|  |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_SETICON: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 			KGE_LOG(L"Window icon changed"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_DISPLAYCHANGE: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 			KGE_LOG(L"The display resolution has changed"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			::InvalidateRect(hwnd, nullptr, FALSE); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_CLOSE: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 			KGE_LOG(L"Window is closing"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (!app->OnClosing()) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				return 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		case WM_DESTROY: | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  | 			KGE_LOG(L"Window was destroyed"); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 14:51:54 +08:00
										 |  |  | 			Event evt(Event::WindowClosed); | 
					
						
							|  |  |  | 			app->DispatchEvent(evt); | 
					
						
							| 
									
										
										
										
											2019-08-04 00:11:47 +08:00
										 |  |  | 			app->OnDestroy(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  | 			::PostQuitMessage(0); | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return ::DefWindowProcW(hwnd, msg, wparam, lparam); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |