Пример #1
0
	bool Imageset::Load(xml::node* imgset, const std::string& name, System& sys)
	{
		if(imgset)
		{
			xml::node texsnode = imgset->child("Textures");
			if(!texsnode)
			{
				sys.logEvent(log::warning, std::string("The imageset ")
					+ name + " doesn't have any texture information. Imageset is unaffected");
				return false;
			}

			xml::node imgsnode = imgset->child("Images");
			if(!imgsnode)
			{
				sys.logEvent(log::warning, std::string("The imageset ")
					+ name + " doesn't have any image information. Imageset is unaffected");
				return false;
			}

			typedef boost::unordered_map<std::string, size_t> TextureOrdinals;
			TextureOrdinals textureOrdinals;

			xml::node texnode = texsnode.first_child();
			while(!texnode.empty())
			{
				std::string nodename(texnode.name());
				if(nodename == "Texture")
				{
					std::string texname = texnode["Name"].value();
					std::string filename = texnode["Filename"].value();
					if(!texname.size())
						texname = filename;
					TextureOrdinals::iterator it = textureOrdinals.find(texname);
					if(it == textureOrdinals.end())
					{
						TexturePtr tex = sys.getRenderer().createTexture("imageset\\" + filename);
						if(tex)
						{
							m_textures.push_back(tex);
							textureOrdinals.insert(std::make_pair(texname, m_textures.size() - 1));
						}
						else
						{
							sys.logEvent(log::warning, std::string("The imageset ")
								+ name + " unable to load texture '" + texname + "', file '"
								+ filename + "'.");
						}
					}
				}
				texnode = texnode.next_sibling();
			}

			xml::node imgnode = imgsnode.first_child();
			while(!imgnode.empty())
			{
				std::string nodename(imgnode.name());
				if(nodename == "Image")
				{
					std::string imgname = imgnode["Name"].value();
					float width = imgnode["Width"].as_float();
					float height = imgnode["Height"].as_float();

					Image::SubImages subImages;

					xml::node rectnode = imgnode.first_child();
					while(!rectnode.empty())
					{
						if(std::string(rectnode.name()) == "Rect")
						{
							std::string texname = rectnode["Texture"].value();
							TextureOrdinals::iterator it = textureOrdinals.find(texname);
							if(it != textureOrdinals.end())
							{
								SubImage sub;
								sub.m_ordinal = it->second;

								float left = rectnode["SrcLeft"].as_float();
								float top = rectnode["SrcTop"].as_float();
								float right = rectnode["SrcRight"].as_float();
								float bottom = rectnode["SrcBottom"].as_float();

								sub.m_src = Rect(left, top, right, bottom);

								float y = rectnode["YPos"].as_float();
								float x = rectnode["XPos"].as_float();
								sub.m_offset = point(x, y);

								if(!rectnode["CropLeft"].empty())
								{
									float cropx = rectnode["CropLeft"].as_float();
									float cropy = rectnode["CropTop"].as_float();
									float w = rectnode["OrigWidth"].as_float();
									float h = rectnode["OrigHeight"].as_float();
									sub.m_crop = Rect(point(cropx, cropy), Size(w, h));
								}
								subImages.push_back(sub);
							}
							else
							{
								sys.logEvent(log::warning, std::string("The imageset ")
									+ name + " can't find texture '" + texname + "' for the image '"
									+ imgname + "'.");
							}
						}
						rectnode = rectnode.next_sibling();
					}
					if(subImages.size())
					{
						// TODO: optimize multiple copy
						m_images.insert(std::make_pair(imgname, Image(this, imgname, Size(width, height), subImages)));
					}
					else
					{
						sys.logEvent(log::warning, std::string("The imageset ")
							+ name + " can't find any rectangles for the image '"
							+ imgname + "'. Skipping this image...");
					}
				}
				imgnode = imgnode.next_sibling();
			}

			return true;
		}
		return false;
	}
Пример #2
0
	bool Imageset::Load(xml::node* imgset, const std::string& name, System& sys)
	{
		if(imgset)
		{
			xml::node texsnode = imgset->child("Textures");
			if(!texsnode)
			{
				sys.logEvent(log::warning, std::string("The imageset ")
					+ name + " doesn't have any texture information. imageset is unaffected");
				return false;
			}

			xml::node imgsnode = imgset->child("Images");
			if(!imgsnode)
			{
				sys.logEvent(log::warning, std::string("The imageset ")
					+ name + " doesn't have any image information. imageset is unaffected");
				return false;
			}

			typedef std::unordered_map<std::string, size_t> TextureOrdinals;
			TextureOrdinals textureOrdinals;

			xml::node texnode = texsnode.first_child();
			while(!texnode.empty())
			{
				std::string nodename(texnode.name());
				if(nodename == "Texture")
				{
					std::string texname = texnode["id"].value();
					std::string filename = texnode["file"].value();
					if(!texname.size())
						texname = filename;
					TextureOrdinals::iterator it = textureOrdinals.find(texname);
					if(it == textureOrdinals.end())
					{
						TexturePtr tex = sys.getRenderer().createTexture("imageset/" + filename);
						if(tex)
						{
							m_textures.push_back(tex);
							textureOrdinals.insert(std::make_pair(texname, m_textures.size() - 1));
						}
						else
						{
							sys.logEvent(log::warning, std::string("The imageset ")
								+ name + " unable to load texture '" + texname + "', file '"
								+ filename + "'.");
						}
					}
				}
				texnode = texnode.next_sibling();
			}

			xml::node imgnode = imgsnode.first_child();
			while(!imgnode.empty())
			{
				std::string nodename(imgnode.name());
				if(nodename == "Image")
				{
					std::string imgname = imgnode["id"].value();
					float width = imgnode["width"].as_float();
					float height = imgnode["height"].as_float();
					bool isAdditiveBlend = imgnode["additive"].as_bool();

					Image::SubImages subImages;

					xml::node rectnode = imgnode.first_child();
					while(!rectnode.empty())
					{
						if (std::string(rectnode.name()) != "Rect") continue;
						
						std::string texname = rectnode["texture"].value();
						TextureOrdinals::iterator it = texname.empty() ? textureOrdinals.begin() : textureOrdinals.find(texname);
						if (it == textureOrdinals.end()) {
							sys.logEvent(log::warning, std::string("The imageset ")
								+ name + " can't find texture '" + texname + "' for the image '"
								+ imgname + "'.");
							continue;
						}

						SubImage sub;
						sub.m_ordinal = it->second;

						sub.m_src.m_left = rectnode["left"].as_int();
						sub.m_src.m_top = rectnode["top"].as_int();
						sub.m_src.m_right = rectnode["right"].as_int();
						sub.m_src.m_bottom = rectnode["bottom"].as_int();

						sub.m_offset.x = rectnode["x"].as_int();
						sub.m_offset.y = rectnode["y"].as_int();

						if (!rectnode["CropLeft"].empty())
						{
							float cropx = rectnode["CropLeft"].as_int();
							float cropy = rectnode["CropTop"].as_int();
							float w = rectnode["OrigWidth"].as_int();
							float h = rectnode["OrigHeight"].as_int();
							sub.m_crop = Rect(point(cropx, cropy), Size(w, h));
						}
						subImages.push_back(sub);
						
						rectnode = rectnode.next_sibling();
					}
					if(!subImages.empty())
					{
						// TODO: optimize multiple copy
						m_images.insert(std::make_pair(imgname, Image(this, imgname, Size(width, height), subImages, isAdditiveBlend)));
					}
					else
					{
						sys.logEvent(log::warning, std::string("The imageset ")
							+ name + " can't find any rectangles for the image '"
							+ imgname + "'. Skipping this image...");
					}
				}
				imgnode = imgnode.next_sibling();
			}

			return true;
		}
		return false;
	}