169 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C
		
	
	
	
		
		
			
		
	
	
			169 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C
		
	
	
	
|  | // 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.
 | |||
|  | 
 | |||
|  | #pragma once
 | |||
|  | #include "../2d/include-forwards.h"
 | |||
|  | #include "../base/time.h"
 | |||
|  | #include "../base/window.h"
 | |||
|  | #include "../base/Component.h"
 | |||
|  | #include <mutex>
 | |||
|  | 
 | |||
|  | namespace easy2d | |||
|  | { | |||
|  | 	struct Options | |||
|  | 	{ | |||
|  | 		String	title;				// <20><><EFBFBD><EFBFBD>
 | |||
|  | 		int		width;				// <20><><EFBFBD><EFBFBD>
 | |||
|  | 		int		height;				// <20>߶<EFBFBD>
 | |||
|  | 		LPCWSTR	icon;				// ͼ<><CDBC>
 | |||
|  | 		Color	clear_color;		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
 | |||
|  | 		bool	vsync;				// <20><>ֱͬ<D6B1><CDAC>
 | |||
|  | 		bool	fullscreen;			// ȫ<><C8AB>ģʽ
 | |||
|  | 
 | |||
|  | 		Options( | |||
|  | 			String const& title = L"Easy2D Game", | |||
|  | 			int width = 640, | |||
|  | 			int height = 480, | |||
|  | 			LPCWSTR icon = nullptr, | |||
|  | 			Color clear_color = Color::Black, | |||
|  | 			bool vsync = true, | |||
|  | 			bool fullscreen = false | |||
|  | 		) | |||
|  | 			: title(title) | |||
|  | 			, width(width) | |||
|  | 			, height(height) | |||
|  | 			, icon(icon) | |||
|  | 			, clear_color(clear_color) | |||
|  | 			, vsync(vsync) | |||
|  | 			, fullscreen(fullscreen) | |||
|  | 		{} | |||
|  | 	}; | |||
|  | 
 | |||
|  | 
 | |||
|  | 	class E2D_API Application | |||
|  | 		: protected Noncopyable | |||
|  | 	{ | |||
|  | 	public: | |||
|  | 		Application(); | |||
|  | 
 | |||
|  | 		virtual ~Application(); | |||
|  | 
 | |||
|  | 		// <20><>ʼ<EFBFBD><CABC>
 | |||
|  | 		void Init( | |||
|  | 			Options const& options | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>ʱ
 | |||
|  | 		virtual void OnStart() {} | |||
|  | 
 | |||
|  | 		// <20>ر<EFBFBD>ʱ
 | |||
|  | 		virtual bool OnClosing() { return true; } | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>ʱ
 | |||
|  | 		virtual void OnDestroy() {} | |||
|  | 
 | |||
|  | 		// <20><>Ⱦʱ
 | |||
|  | 		virtual void OnRender() {} | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>ʱ
 | |||
|  | 		virtual void OnUpdate(Duration dt) { E2D_NOT_USED(dt); } | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>
 | |||
|  | 		void Run(); | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>
 | |||
|  | 		void Quit(); | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>
 | |||
|  | 		void Destroy(); | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		void Use( | |||
|  | 			Component* component | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// ж<><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		void Remove( | |||
|  | 			Component* component | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		void EnterScene( | |||
|  | 			ScenePtr const& scene			/* <20><><EFBFBD><EFBFBD> */ | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		void EnterScene( | |||
|  | 			ScenePtr const& scene,			/* <20><><EFBFBD><EFBFBD> */ | |||
|  | 			TransitionPtr const& transition	/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */ | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
 | |||
|  | 		ScenePtr const& GetCurrentScene(); | |||
|  | 
 | |||
|  | 		// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		inline Window* GetWindow() const { return main_window_; } | |||
|  | 
 | |||
|  | 		// <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		void SetTimeScale( | |||
|  | 			float scale_factor | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
 | |||
|  | 		void ShowDebugInfo( | |||
|  | 			bool show = true | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20><> Easy2D <20><><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>ִ<EFBFBD>к<EFBFBD><D0BA><EFBFBD>
 | |||
|  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̵߳<DFB3><CCB5><EFBFBD> Easy2D <20><><EFBFBD><EFBFBD>ʱʹ<CAB1><CAB9>
 | |||
|  | 		void PreformFunctionInMainThread( | |||
|  | 			std::function<void()> function | |||
|  | 		); | |||
|  | 
 | |||
|  | 		// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>̨
 | |||
|  | 		static void ShowConsole( | |||
|  | 			bool show | |||
|  | 		); | |||
|  | 
 | |||
|  | 	protected: | |||
|  | 		void Render(); | |||
|  | 
 | |||
|  | 		void Update(); | |||
|  | 
 | |||
|  | 		static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); | |||
|  | 
 | |||
|  | 	protected: | |||
|  | 		bool			end_; | |||
|  | 		bool			inited_; | |||
|  | 		float			time_scale_; | |||
|  | 
 | |||
|  | 		ScenePtr		curr_scene_; | |||
|  | 		ScenePtr		next_scene_; | |||
|  | 		NodePtr			debug_node_; | |||
|  | 		TransitionPtr	transition_; | |||
|  | 
 | |||
|  | 		Window*				main_window_; | |||
|  | 		Array<Component*>	components_; | |||
|  | 
 | |||
|  | 		std::mutex						perform_mutex_; | |||
|  | 		Queue<std::function<void()>>	functions_to_perform_; | |||
|  | 	}; | |||
|  | } |