Example #1
0
static inline void setAttr(ticpp::Element& element, const char* attr, const T& value)
{
	if (!attr) {
		return;
	}
	element.SetAttribute(attr, value);
}
Example #2
0
//Save parameters of sprite in an xml element
void SpriteBuilder::SaveSprite(SpritePointer thesprite, ticpp::Element& xmlelement)
{
	/*XML DEFINITION EXPECTED FOR SPRITE ELEMENT:
	Element: SPRITE Atts: Id (string) x(number) y(number) h(number) w(number) Layer(number)
					 [Element: Image Atts: Id(string) [CollisionPath(string)]]
					 [Element: Font Atts: Id(string) [LineSpacing(number)] [CharSpacing(number)] [Align(string)] Text(string)]
					 [Element: Animation Atts: Id(string)] 
	*/
	
	//Get needed values from GFX system for calculations
	float globalscale = SingletonIndieLib::Instance()->GetGeneralScale();

	//-------Get attributes to save from entity---------
	float h,w; //Later is initialized
	float x = thesprite->GetPosX();
	float y = thesprite->GetPosY();
	Vector2 coordpos = SingletonIndieLib::Instance()->FromPixToCoord(Vector2(x,y));
	int z = thesprite->GetPosZ();
	float rotation = thesprite->GetAngleZ();
	int transparency = thesprite->GetTransparency();
	bool mirrorx = thesprite->GetMirrorX();
	bool mirrory = thesprite->GetMirrorY();
	bool wrap = thesprite->IfWrap();
	int wrapx = thesprite->GetRegionWidth();
	int wrapy = thesprite->GetRegionHeight();
	int layer = thesprite->GetLayer();

	//----Get elements associated with entity and remaining attributes------
	//Only one element can be associated to an entity: Image,Font,Animation
	//IF - Sprite is an image
	if(thesprite->GetSurface() != NULL)
	{
		IND_Surface* thesurface = thesprite->GetSurface();
		//Get Id of related surface
		std::string surfaceid = SingletonResourceMgr::Instance()->GetSurfaceId(thesurface);
		//Create "Image" element inside sprite
		ticpp::Element imageelement("Image");
		imageelement.SetAttribute("Id",surfaceid);
		xmlelement.InsertEndChild(imageelement);

		//Set remaining attributes from sprite
		w = (thesurface->GetWidth() * thesprite->GetScaleX()) / globalscale; 
		h = (thesurface->GetHeight() * thesprite->GetScaleY()) / globalscale;
	}//ELSE IF - Sprite is an animation
	else if(thesprite->GetAnimation() != NULL)
	{
		IND_Animation* theanimation = thesprite->GetAnimation();
		//Get Id of related animation
		std::string animationid = SingletonResourceMgr::Instance()->GetAnimationId(theanimation);
		//Create "Animation" element inside sprite
		ticpp::Element animationelement("Animation");
		animationelement.SetAttribute("Id",animationid);
		xmlelement.InsertEndChild(animationelement);

		//Find higher Width and Height in all sequences to calculate h and w
		int numsequences = theanimation->GetNumSequences();
		int hightwidth, highheight; 
		hightwidth = highheight = 0;
		//LOOP  Find bigger x and y to scale
		for(int i = 0;i<numsequences;i++)
		{
			int animwidth = theanimation->GetHighWidth(i);  //Bigger width of this sequence
			int animheight = theanimation->GetHighHeight(i); //Bigger heigth of this sequence

			//Assignment if it is bigger
			if(animwidth > hightwidth)
				hightwidth =  animwidth;
			if(animheight > highheight)
				highheight =  animheight;
		}//LOOP END

		//Set remaining attributes from sprite
		w = (hightwidth * thesprite->GetScaleX()) / globalscale; 
		h = (highheight * thesprite->GetScaleY()) / globalscale;
	}
	//TODO: SAVE FONT SETTINGS
	/*else if(////CODE \\\\\ IS A FONT)
	{

	}*/

	//Set all attributes of entity in order (to ease reading of file)
	xmlelement.SetAttribute("x",coordpos.x);
	xmlelement.SetAttribute("y",coordpos.y);
	xmlelement.SetAttribute("z",z);
	xmlelement.SetAttribute("w",w);
	xmlelement.SetAttribute("h",h);
	xmlelement.SetAttribute("Rotation",rotation);
	xmlelement.SetAttribute("Transp",transparency);
	xmlelement.SetAttribute("FlipX",mirrorx);
	xmlelement.SetAttribute("FlipY",mirrory);
	xmlelement.SetAttribute("Wrap",wrap);
	xmlelement.SetAttribute("WrapX",wrapx);
	xmlelement.SetAttribute("WrapY",wrapy);
	xmlelement.SetAttribute("Layer",layer);

}