add: virtual functions on application starts & destroys
minor fixes minor fixes minor
This commit is contained in:
		
							parent
							
								
									63afe9bf39
								
							
						
					
					
						commit
						1e16f9d815
					
				|  | @ -90,7 +90,7 @@ namespace easy2d | ||||||
| 			Audio::Instance()->Init(debug_) | 			Audio::Instance()->Init(debug_) | ||||||
| 		); | 		); | ||||||
| 
 | 
 | ||||||
| 		Setup(); | 		OnStart(); | ||||||
| 
 | 
 | ||||||
| 		// disable imm
 | 		// disable imm
 | ||||||
| 		::ImmAssociateContext(hwnd, nullptr); | 		::ImmAssociateContext(hwnd, nullptr); | ||||||
|  | @ -416,8 +416,13 @@ namespace easy2d | ||||||
| 		break; | 		break; | ||||||
| 
 | 
 | ||||||
| 		case WM_CLOSE: | 		case WM_CLOSE: | ||||||
|  | 		{ | ||||||
|  | 			E2D_LOG(L"Window is closing"); | ||||||
|  | 
 | ||||||
|  | 			if (app->OnClosing()) | ||||||
| 			{ | 			{ | ||||||
| 				Window::Instance()->Destroy(); | 				Window::Instance()->Destroy(); | ||||||
|  | 			} | ||||||
| 			return 0; | 			return 0; | ||||||
| 		} | 		} | ||||||
| 		break; | 		break; | ||||||
|  | @ -432,6 +437,8 @@ namespace easy2d | ||||||
| 				app->curr_scene_->Dispatch(evt); | 				app->curr_scene_->Dispatch(evt); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
|  | 			app->OnDestroy(); | ||||||
|  | 
 | ||||||
| 			::PostQuitMessage(0); | 			::PostQuitMessage(0); | ||||||
| 			return 0; | 			return 0; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | @ -60,14 +60,20 @@ namespace easy2d | ||||||
| 
 | 
 | ||||||
| 		virtual ~Application(); | 		virtual ~Application(); | ||||||
| 
 | 
 | ||||||
| 		// 启动
 |  | ||||||
| 		virtual void Setup() {} |  | ||||||
| 
 |  | ||||||
| 		// 初始化
 | 		// 初始化
 | ||||||
| 		void Init( | 		void Init( | ||||||
| 			Options const& options | 			Options const& options | ||||||
| 		); | 		); | ||||||
| 
 | 
 | ||||||
|  | 		// 启动时
 | ||||||
|  | 		virtual void OnStart() {} | ||||||
|  | 
 | ||||||
|  | 		// 关闭时
 | ||||||
|  | 		virtual bool OnClosing() { return true; } | ||||||
|  | 
 | ||||||
|  | 		// 销毁时
 | ||||||
|  | 		virtual void OnDestroy() {} | ||||||
|  | 
 | ||||||
| 		// 运行
 | 		// 运行
 | ||||||
| 		void Run(); | 		void Run(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -41,64 +41,131 @@ namespace easy2d | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddImage(String const& id, Resource const& image) | 	bool ResLoader::AddImage(String const& id, Resource const& image) | ||||||
|  | 	{ | ||||||
|  | 		ImagePtr ptr = new (std::nothrow) Image; | ||||||
|  | 		if (ptr && ptr->Load(FindRes(image))) | ||||||
| 		{ | 		{ | ||||||
| 		String path = Search(image.GetFileName(), search_paths_); |  | ||||||
| 		ImagePtr ptr = new Image(path.c_str()); |  | ||||||
| 			res_.insert(std::make_pair(id, ptr)); | 			res_.insert(std::make_pair(id, ptr)); | ||||||
|  | 			return true; | ||||||
|  | 		} | ||||||
|  | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddImage(String const & id, ImagePtr const & image) | 	bool ResLoader::AddImage(String const & id, ImagePtr const & image) | ||||||
|  | 	{ | ||||||
|  | 		if (image) | ||||||
| 		{ | 		{ | ||||||
| 			res_.insert(std::make_pair(id, image)); | 			res_.insert(std::make_pair(id, image)); | ||||||
|  | 			return true; | ||||||
|  | 		} | ||||||
|  | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddFrames(String const& id, Array<Resource> const& images) | 	int ResLoader::AddFrames(String const& id, Array<Resource> const& images) | ||||||
|  | 	{ | ||||||
|  | 		if (images.empty()) | ||||||
|  | 			return 0; | ||||||
|  | 
 | ||||||
|  | 		int total = 0; | ||||||
|  | 		FramesPtr frames = new (std::nothrow) Frames; | ||||||
|  | 		if (frames) | ||||||
| 		{ | 		{ | ||||||
| 		FramesPtr frames = new Frames; |  | ||||||
| 			for (const auto& image : images) | 			for (const auto& image : images) | ||||||
| 			{ | 			{ | ||||||
| 			String path = Search(image.GetFileName(), search_paths_); | 				ImagePtr ptr = new (std::nothrow) Image; | ||||||
| 			ImagePtr ptr = new Image(path.c_str()); | 				if (ptr && ptr->Load(FindRes(image))) | ||||||
|  | 				{ | ||||||
| 					frames->Add(ptr); | 					frames->Add(ptr); | ||||||
|  | 					++total; | ||||||
| 				} | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if (total) | ||||||
| 			res_.insert(std::make_pair(id, frames)); | 			res_.insert(std::make_pair(id, frames)); | ||||||
|  | 		return total; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images) | 	int ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images) | ||||||
|  | 	{ | ||||||
|  | 		if (images.empty()) | ||||||
|  | 			return 0; | ||||||
|  | 
 | ||||||
|  | 		int total = 0; | ||||||
|  | 		FramesPtr frames = new (std::nothrow) Frames; | ||||||
|  | 		if (frames) | ||||||
| 		{ | 		{ | ||||||
| 		FramesPtr frames = new Frames; |  | ||||||
| 			for (const auto& image : images) | 			for (const auto& image : images) | ||||||
|  | 			{ | ||||||
|  | 				if (image) | ||||||
|  | 				{ | ||||||
| 					frames->Add(image); | 					frames->Add(image); | ||||||
|  | 					total++; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if (total) | ||||||
| 			res_.insert(std::make_pair(id, frames)); | 			res_.insert(std::make_pair(id, frames)); | ||||||
|  | 		return total; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddFrames(String const& id, Array<std::pair<Resource, Rect>> const& images) | 	int ResLoader::AddFrames(String const& id, Array<std::pair<Resource, Rect>> const& images) | ||||||
|  | 	{ | ||||||
|  | 		if (images.empty()) | ||||||
|  | 			return 0; | ||||||
|  | 
 | ||||||
|  | 		int total = 0; | ||||||
|  | 		FramesPtr frames = new (std::nothrow) Frames; | ||||||
|  | 		if (frames) | ||||||
| 		{ | 		{ | ||||||
| 		FramesPtr frames = new Frames; |  | ||||||
| 			for (const auto& pair : images) | 			for (const auto& pair : images) | ||||||
| 			{ | 			{ | ||||||
| 			String path = Search(pair.first.GetFileName(), search_paths_); | 				ImagePtr ptr = new (std::nothrow) Image; | ||||||
| 			ImagePtr image = new Image(path.c_str()); | 				if (ptr && ptr->Load(FindRes(pair.first))) | ||||||
|  | 				{ | ||||||
| 					if (!pair.second.IsEmpty()) | 					if (!pair.second.IsEmpty()) | ||||||
| 					{ | 					{ | ||||||
| 				image->Crop(pair.second); | 						ptr->Crop(pair.second); | ||||||
| 					} | 					} | ||||||
| 			frames->Add(image); | 					frames->Add(ptr); | ||||||
|  | 					++total; | ||||||
| 				} | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		if (total) | ||||||
| 			res_.insert(std::make_pair(id, frames)); | 			res_.insert(std::make_pair(id, frames)); | ||||||
|  | 		return total; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddMusic(String const & id, Resource const & music) | 	bool ResLoader::AddMusic(String const & id, Resource const & music) | ||||||
|  | 	{ | ||||||
|  | 		MusicPtr ptr = new (std::nothrow) Music; | ||||||
|  | 		if (ptr && ptr->Load(FindRes(music))) | ||||||
| 		{ | 		{ | ||||||
| 		String path = Search(music.GetFileName(), search_paths_); |  | ||||||
| 		MusicPtr ptr = new Music(path.c_str()); |  | ||||||
| 			res_.insert(std::make_pair(id, ptr)); | 			res_.insert(std::make_pair(id, ptr)); | ||||||
|  | 			return true; | ||||||
|  | 		} | ||||||
|  | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	void ResLoader::AddObj(String const& id, ObjectPtr const& obj) | 	bool ResLoader::AddMusic(String const & id, MusicPtr const & music) | ||||||
|  | 	{ | ||||||
|  | 		if (music) | ||||||
|  | 		{ | ||||||
|  | 			res_.insert(std::make_pair(id, music)); | ||||||
|  | 			return true; | ||||||
|  | 		} | ||||||
|  | 		return false; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	bool ResLoader::AddObj(String const& id, ObjectPtr const& obj) | ||||||
|  | 	{ | ||||||
|  | 		if (obj) | ||||||
| 		{ | 		{ | ||||||
| 			res_.insert(std::make_pair(id, obj)); | 			res_.insert(std::make_pair(id, obj)); | ||||||
|  | 			return true; | ||||||
|  | 		} | ||||||
|  | 		return false; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	ImagePtr ResLoader::GetImage(String const & id) const | 	ImagePtr ResLoader::GetImage(String const & id) const | ||||||
|  | @ -153,4 +220,14 @@ namespace easy2d | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	Resource ResLoader::FindRes(Resource const & res) const | ||||||
|  | 	{ | ||||||
|  | 		if (res.IsFile()) | ||||||
|  | 		{ | ||||||
|  | 			String path = Search(res.GetFileName(), search_paths_); | ||||||
|  | 			return Resource(path.c_str()); | ||||||
|  | 		} | ||||||
|  | 		return res; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | @ -27,19 +27,21 @@ namespace easy2d | ||||||
| 	class ResLoader | 	class ResLoader | ||||||
| 	{ | 	{ | ||||||
| 	public: | 	public: | ||||||
| 		void AddImage(String const& id, Resource const& image); | 		bool AddImage(String const& id, Resource const& image); | ||||||
| 
 | 
 | ||||||
| 		void AddImage(String const& id, ImagePtr const& image); | 		bool AddImage(String const& id, ImagePtr const& image); | ||||||
| 
 | 
 | ||||||
| 		void AddFrames(String const& id, Array<Resource> const& images); | 		int AddFrames(String const& id, Array<Resource> const& images); | ||||||
| 
 | 
 | ||||||
| 		void AddFrames(String const& id, Array<Pair<Resource, Rect>> const& images); | 		int AddFrames(String const& id, Array<Pair<Resource, Rect>> const& images); | ||||||
| 
 | 
 | ||||||
| 		void AddFrames(String const& id, Array<ImagePtr> const& images); | 		int AddFrames(String const& id, Array<ImagePtr> const& images); | ||||||
| 
 | 
 | ||||||
| 		void AddMusic(String const& id, Resource const& music); | 		bool AddMusic(String const& id, Resource const& music); | ||||||
| 
 | 
 | ||||||
| 		void AddObj(String const& id, ObjectPtr const& obj); | 		bool AddMusic(String const& id, MusicPtr const& music); | ||||||
|  | 
 | ||||||
|  | 		bool AddObj(String const& id, ObjectPtr const& obj); | ||||||
| 
 | 
 | ||||||
| 		ImagePtr GetImage(String const& id) const; | 		ImagePtr GetImage(String const& id) const; | ||||||
| 
 | 
 | ||||||
|  | @ -58,6 +60,8 @@ namespace easy2d | ||||||
| 			String const& path | 			String const& path | ||||||
| 		); | 		); | ||||||
| 
 | 
 | ||||||
|  | 		Resource FindRes(Resource const& res) const; | ||||||
|  | 
 | ||||||
| 		template<typename T> | 		template<typename T> | ||||||
| 		auto Get(String const& id) const -> decltype(auto) | 		auto Get(String const& id) const -> decltype(auto) | ||||||
| 		{ | 		{ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue