X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2FAnimation.cpp;h=602d7ef5d7613ada5118239b3ebf870e381633fc;hb=a3ba4dc677ad7c92eeb78b20b642241563605c9d;hp=d4a17d950e127ed0f0316c37b0bd2ce8dba5bd58;hpb=cc3d698b8c1ad09d7a3f9e3f28bc84e0ac1735ea;p=l2e.git diff --git a/src/graphics/Animation.cpp b/src/graphics/Animation.cpp index d4a17d9..602d7ef 100644 --- a/src/graphics/Animation.cpp +++ b/src/graphics/Animation.cpp @@ -1,9 +1,13 @@ #include "Animation.h" #include "Sprite.h" +#include "../app/Application.h" +#include "../app/State.h" #include "../loader/Interpreter.h" #include "../loader/TypeDescription.h" +#include + using loader::FieldDescription; using loader::Interpreter; using loader::TypeDescription; @@ -22,4 +26,69 @@ void Animation::AddFields(TypeDescription &td, const Animation &a, std::ptrdiff_ td.AddField("repeat", FieldDescription(((char *)&a.repeat) - ((char *)&a) - offset, Interpreter::BOOLEAN_ID).SetDescription("whether the animation should start over at the beginning after reaching the last frame")); } + +void AnimationRunner::Start(app::State &ctrl) { + assert(animation); + timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime()); +} + +void AnimationRunner::Start(app::Application &ctrl) { + assert(animation); + timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime()); +} + +void AnimationRunner::Stop() { + timer = app::Timer(); +} + +bool AnimationRunner::Started() const { + return timer.Started(); +} + +bool AnimationRunner::Running() const { + return animation + && timer.Running() + && (animation->Repeat() + || timer.Iteration() < animation->NumFrames()); +} + +bool AnimationRunner::Finished() const { + return Started() && !Running(); +} + +bool AnimationRunner::JustFinished() const { + return animation + && timer.JustHit() + && timer.Iteration() == animation->NumFrames(); +} + + +void AnimationRunner::Draw(SDL_Surface *dest, math::Vector position) const { + GetSprite()->Draw(dest, + position + animation->Offset(Frame()), + animation->Col(Frame()) + ColOffset(), + animation->Row(Frame()) + RowOffset()); +} + +void AnimationRunner::DrawTopRight(SDL_Surface *dest, math::Vector position) const { + math::Vector offset(-GetSprite()->Width(), 0); + Draw(dest, position + offset); +} + +void AnimationRunner::DrawCenter(SDL_Surface *dest, math::Vector position) const { + Draw(dest, position - (GetSprite()->Size() / 2)); +} + +void AnimationRunner::DrawCenterBottom(SDL_Surface *dest, math::Vector position) const { + math::Vector offset(-GetSprite()->Width() / 2, -GetSprite()->Height()); + Draw(dest, position + offset); +} + +int AnimationRunner::Frame() const { + return Running() + ? ((timer.Iteration() + frameShift) % animation->NumFrames()) + : 0; +} + + }