Example #1
0
// LoadSprites
//	- reading all the sprites from the XML file
std::string AnimationManager::LoadSprites(std::string fileName)
{
	TiXmlDocument animationDoc;

	// Check to see if there is anything to load from the file
	if (animationDoc.LoadFile(fileName.c_str()) == false)
		return false;

	Sprite* newSprite = new Sprite();
	TiXmlElement* root = animationDoc.RootElement();
	int numSprites;
	root->Attribute("Sprites", &numSprites);
	// Check to see if there is actually anything to read in the XML
	if (root == nullptr)
		return false;
	// Getting rid of any past data
	if (made == false)
	{
		m_mSprites.clear();
		made = true;
	}

	std::string ID;
	for (int i = 0; i < numSprites; i++)
	{
		TiXmlElement* spriteImg = root->FirstChildElement("sprite");
		while (spriteImg != nullptr)
		{
			if (spriteImg != nullptr)
			{
				std::string file = "resource/animation/";
				file += spriteImg->GetText();

				if (file.c_str() != nullptr)
					newSprite->SetImage(file.c_str());
			}
			spriteImg = spriteImg->NextSiblingElement("spriteID");
			if (spriteImg != nullptr)
			{
				std::string id = spriteImg->GetText();
				if (id.c_str() != nullptr)
				{
					newSprite->SetSpriteID(id);
					ID = id;
				}
			}
			spriteImg = spriteImg->NextSiblingElement("isLooping");
			if (spriteImg != nullptr)
			{
				int boolNum;
				spriteImg->Attribute("isIt", &boolNum);
				if (boolNum == 0)
					newSprite->SetLooping(false);
				else
					newSprite->SetLooping(true);

			}
			spriteImg = spriteImg->NextSiblingElement("Frames");
			int numFrames;
			if (spriteImg != nullptr)
			{
				spriteImg->Attribute("numbers", &numFrames);
			}
			TiXmlElement* frames = spriteImg->NextSiblingElement("frame");
			for (int i = 0; i < numFrames; i++)
			{
				if (frames != nullptr)
				{
					TiXmlElement* info = frames->FirstChildElement("anchorPoint");
					Frame* newFrame = new Frame();
					if (info != nullptr)
					{
						SGD::Point pointTemp;
						double x, y;
						info->Attribute("X", &x);
						pointTemp.x = (float)x;
						info->Attribute("Y", &y);
						pointTemp.y = (float)y;
						newFrame->SetAnchorPoint(pointTemp);
					}
					info = info->NextSiblingElement("collisionRect");

					if (info != nullptr)
					{
						SGD::Rectangle collTemp;
						double l, t, r, b;
						info->Attribute("left", &l);
						collTemp.left = (float)l;
						info->Attribute("right", &r);
						collTemp.right = (float)r;
						info->Attribute("top", &t);
						collTemp.top = (float)t;
						info->Attribute("bottom", &b);
						collTemp.bottom = (float)b;

						newFrame->SetCollisionRect(collTemp);
					}

					info = info->NextSiblingElement("drawRect");
					if (info != nullptr)
					{
						SGD::Rectangle drawTemp;
						double l, t, r, b;
						info->Attribute("left", &l);
						drawTemp.left = (float)l;
						info->Attribute("right", &r);
						drawTemp.right = (float)r;
						info->Attribute("top", &t);
						drawTemp.top = (float)t;
						info->Attribute("bottom", &b);
						drawTemp.bottom = (float)b;

						newFrame->SetFrameRect(drawTemp);
					}

					info = info->NextSiblingElement("duration");
					if (info != nullptr)
					{
						double durtemp;
						info->Attribute("time", &durtemp);
						newFrame->SetDuration((float)durtemp);

					}

					info = info->NextSiblingElement("triggerID");
					if (info != nullptr)
					{
						std::string trigid = info->GetText();
						if (trigid.c_str() != nullptr)
							newFrame->SetTriggerID(trigid);
					}
					newSprite->AddFrame(newFrame);
					//frames->
					frames = frames->NextSiblingElement("frame");
				}
			}
			m_mSprites[newSprite->GetSpriteID()] = newSprite;
			m_vSpriteNames.push_back(newSprite->GetSpriteID());
			spriteImg = spriteImg->NextSiblingElement("sprite");
		}
	}
	if (m_mSprites.size() > 0)
		return ID;
	else
		return nullptr;
}