Main > Getting Started > Displaying Animated Sprite

Sprites can have multiple animations and are described using
simple XML files like this:

<?xml version="1.0"?>
<sprite material="common/default" atlas="my_sprite_sheet.png">
	<animation name="idle" frameTime="0.15" isDefault="true">
		<frame texture="idle0.png"/>
		<frame texture="idle1.png"/>
	</animation>
	<animation name="walk" frameTime="0.1">
		<frame texture="walk0.png"/>
		<frame texture="walk1.png"/>
	</animation>
</sprite>

Above XML file describes sprite with 2 animations "idle" and "walk",
each having only 2 animation frames. "frameTime" parameter of each
animation indicates how long each frame takes in seconds.

Sprite Sheet / Atlas Support

Having each animation frame in a separate file might be inefficient,
so instead you might want to consider putting them all into single
texture (aka sprite sheet).

Tiny2D supports sprite sheet / atlas via an optional "atlas" XML
attribute that points to sprite sheet atlas. Specifying atlas image
also requires that an atlas description file is present. So, for
example, if you have my_sheet.png, you also need my_sheet.xml.
Currently Tiny2D only supports XML atlas description files generated
by free and simple to use tool called SpriteSheetPacker.

Sample Code

And here's full source code for the sample:
First sprite (with all dependent textures) gets loaded into memory.
Then "walk" animation is started.
Then, every frame, sprite gets updated and rendered.

#include "Tiny2D.h"

using namespace Tiny2D;

class SpriteApp : public App::Callbacks
{
public:
	virtual bool OnInit()
	{
		sprite.Create("my_sprite");
		sprite.PlayAnimation("walk");
		return true;
	}
	
	virtual void OnUpdate(float deltaTime)
	{
		sprite.Update(deltaTime);
	}

	virtual void OnDraw(Texture& renderTarget)
	{
		renderTarget.BeginDrawing(&Color::Black);
		sprite.Draw(Vec2(50.0f, 50.0f));
		renderTarget.EndDrawing();
	}

private:
	Sprite sprite;
};

TINY2D_DEFINE_APP(SpriteApp);





Contact: contact at pixelelephant dot com
(C) 2013 Pixel Elephant - All Rights Reserved