Game Development Reference
In-Depth Information
auto spriteSheet = std::unique_ptr<render::spritesheet>(new render::spritesheet(m_core->GetDevice(), spritePath));
size_t defaultAnimID = SIZE_MAX;
auto& defaultAnim = m_spriteSheet->CreateAnimation(L"default", defaultAnimID);
defaultAnim.AddFrame(math::rectangle(0,0,200,200), 0.33f);
defaultAnim.AddFrame(math::rectangle(200,0,200,200), 0.33f);
defaultAnim.AddFrame(math::rectangle(400,0,200,200), 0.33f);
size_t pressedAnimID = SIZE_MAX;
auto& pressedAnim = m_spriteSheet->CreateAnimation(L"pressed", pressedAnimID);
pressedAnim.AddFrame(math::rectangle(0,200,200,200), 0.33f);
pressedAnim.AddFrame(math::rectangle(200,200,200,200), 0.33f);
auto sprite = m_spriteSheet->Create();
sprite->Play(defaultAnimID);
This would load a sprite sheet, which is a texture at the location specified by spritePath . It
will then create two animations, default and pressed, the first one consists of three frames,
each frame lasting one third of a second and bound by the region specified in the rectangle
we pass in. Once the sprite sheet is created and its animations configured, we can create
one or more sprites from it, each of which will be an instance capable of playing its own
animations.
4.1.2.1Animation Events
It is not always enough to play sprite animations, in the context of a game, we often want
animations to cause damage to other entities at a precise point in the animation, for user
interfaces we may want to play a sound, an animation or invoke a function.
To support this, we can provide events on the animation track which are invoked when a
frame starts, another when the frame ends and for non-looping animations we can provide
onewhentheanimationisfinished.Thisallowsustointroducecustomfunctionalityatpre-
cise moments during a sprite's animation.
sprite = m_spriteSheet->Create();
sprite->OnEnterFrame() += [&](void*, const animation_track & track)
{
if ( track.FrameNumber() == 3 )
{ PlaySound(SOUND_SLIDE); }
};
sprite->OnExitFrame() += [&](void*, const animation_track& track)
{
if ( track.IsLastFrame() )
{ PlaySound(SOUND_CLICK); }
};
Search WWH ::




Custom Search