| 
									
										
										
										
											2019-01-22 21:58:01 +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 "ResLoader.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-14 17:55:06 +08:00
										 |  |  | #include "../platform/modules.h"
 | 
					
						
							|  |  |  | #include "../2d/Image.h"
 | 
					
						
							|  |  |  | #include "../2d/Frames.h"
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace easy2d | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	namespace | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		Resource LocateRes(Resource const& res, List<String> const& paths) | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 			if (res.IsFileType()) | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 				String file_name = res.GetFileName(); | 
					
						
							|  |  |  | 				for (const auto& path : paths) | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 					if (modules::Shlwapi::Get().PathFileExistsW((path + file_name).c_str())) | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						return Resource{ path + file_name }; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 			return res; | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 	bool ResLoader::AddImage(String const& id, Resource const& image) | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		ImagePtr ptr = new (std::nothrow) Image; | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 		if (ptr) | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 			if (ptr->Load(LocateRes(image, search_paths_))) | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				res_.insert(std::make_pair(id, ptr)); | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 	bool ResLoader::AddImage(String const & id, ImagePtr const & image) | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		if (image) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			res_.insert(std::make_pair(id, image)); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 	int ResLoader::AddFrames(String const& id, Array<Resource> const& images) | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		if (images.empty()) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 		Array<ImagePtr> image_arr; | 
					
						
							|  |  |  | 		image_arr.reserve(images.size()); | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 		for (const auto& image : images) | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 			ImagePtr ptr = new (std::nothrow) Image; | 
					
						
							|  |  |  | 			if (ptr) | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 				if (ptr->Load(LocateRes(image, search_paths_))) | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					image_arr.push_back(ptr); | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		if (!image_arr.empty()) | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			FramesPtr frames = new (std::nothrow) Frames(image_arr); | 
					
						
							|  |  |  | 			if (frames) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				res_.insert(std::make_pair(id, frames)); | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 				return frames->GetFrames().size(); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 	int ResLoader::AddFrames(String const& id, Array<ImagePtr> const& images) | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		if (images.empty()) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 		FramesPtr frames = new (std::nothrow) Frames(images); | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		if (frames) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			res_.insert(std::make_pair(id, frames)); | 
					
						
							| 
									
										
										
										
											2019-02-07 00:43:35 +08:00
										 |  |  | 			return frames->GetFrames().size(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 	int ResLoader::AddFrames(String const & id, Resource const & image, int cols, int rows) | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 		if (cols <= 0 || rows <= 0) | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		ImagePtr raw = new (std::nothrow) Image; | 
					
						
							|  |  |  | 		if (!raw || !raw->Load(LocateRes(image, search_paths_))) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		float raw_width = raw->GetSourceWidth(); | 
					
						
							|  |  |  | 		float raw_height = raw->GetSourceHeight(); | 
					
						
							|  |  |  | 		float width = raw_width / cols; | 
					
						
							|  |  |  | 		float height = raw_height / rows; | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		Array<ImagePtr> image_arr; | 
					
						
							|  |  |  | 		image_arr.reserve(rows * cols); | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		for (int i = 0; i < rows; i++) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			for (int j = 0; j < cols; j++) | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 				ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); | 
					
						
							|  |  |  | 				if (ptr) | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 					ptr->Crop(Rect{ i * width, j * height, width, height }); | 
					
						
							|  |  |  | 					image_arr.push_back(ptr); | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		FramesPtr frames = new (std::nothrow) Frames(image_arr); | 
					
						
							|  |  |  | 		if (frames) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 			res_.insert(std::make_pair(id, frames)); | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 			return frames->GetFrames().size(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int ResLoader::AddFrames(String const & id, Resource const & image, Array<Rect> const & crop_rects) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		ImagePtr raw = new (std::nothrow) Image; | 
					
						
							|  |  |  | 		if (!raw || !raw->Load(LocateRes(image, search_paths_))) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		Array<ImagePtr> image_arr; | 
					
						
							|  |  |  | 		image_arr.reserve(crop_rects.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (const auto& rect : crop_rects) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			ImagePtr ptr = new (std::nothrow) Image(raw->GetBitmap()); | 
					
						
							|  |  |  | 			if (ptr) | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 				ptr->Crop(rect); | 
					
						
							|  |  |  | 				image_arr.push_back(ptr); | 
					
						
							| 
									
										
										
										
											2019-01-26 17:44:37 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		FramesPtr frames = new (std::nothrow) Frames(image_arr); | 
					
						
							|  |  |  | 		if (frames) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 			res_.insert(std::make_pair(id, frames)); | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 			return frames->GetFrames().size(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 	bool ResLoader::AddFrames(String const & id, FramesPtr const & frames) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (frames) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			res_.insert(std::make_pair(id, frames)); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 	bool ResLoader::AddObj(String const& id, ObjectPtr const& obj) | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-27 09:07:51 +08:00
										 |  |  | 		if (obj) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			res_.insert(std::make_pair(id, obj)); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ImagePtr ResLoader::GetImage(String const & id) const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-03-13 19:40:41 +08:00
										 |  |  | 		return Get<Image>(id); | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	FramesPtr ResLoader::GetFrames(String const & id) const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-03-13 19:40:41 +08:00
										 |  |  | 		return Get<Frames>(id); | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	ObjectPtr ResLoader::GetObj(String const & id) const | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-03-13 19:40:41 +08:00
										 |  |  | 		return Get<Object>(id); | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void ResLoader::Delete(String const & id) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		res_.erase(id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void ResLoader::Destroy() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		res_.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void ResLoader::AddSearchPath(String const & path) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		String tmp = path; | 
					
						
							|  |  |  | 		size_t pos = 0; | 
					
						
							|  |  |  | 		while ((pos = tmp.find(L"/", pos)) != String::npos) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			tmp.replace(pos, 1, L"\\"); | 
					
						
							|  |  |  | 			pos++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (tmp.at(tmp.length() - 1) != L'\\') | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			tmp.append(L"\\"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		auto iter = std::find(search_paths_.cbegin(), search_paths_.cend(), tmp); | 
					
						
							|  |  |  | 		if (iter == search_paths_.cend()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			search_paths_.push_front(tmp); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |