X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2FAnimation.cpp;h=bdd4b2905e75728c3d64523156203a714e4acff2;hb=5cca794c5b6549b7750c88b5c2217d659fa963dd;hp=c1ba63797ab7ac061163a600a7d50fd5104f144f;hpb=ac3755adc509404528ef7de58695bf8e3bfb7dcd;p=l2e.git diff --git a/src/graphics/Animation.cpp b/src/graphics/Animation.cpp index c1ba637..bdd4b29 100644 --- a/src/graphics/Animation.cpp +++ b/src/graphics/Animation.cpp @@ -1,27 +1,99 @@ -/* - * Animation.cpp - * - * Created on: Sep 5, 2012 - * Author: holy - */ - #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; namespace graphics { +Animation::Animation(loader::noinit_t) { + +} + + +void Animation::CreateTypeDescription() { + TypeDescription &td(TypeDescription::Create(TYPE_ID, "Animation")); + td.SetDescription("Abstract base type for animations."); + td.SetSize(sizeof(Animation)); +} + void Animation::AddFields(TypeDescription &td, const Animation &a, std::ptrdiff_t offset) { - int boolId(TypeDescription::GetTypeId("Boolean")); - int numberId(TypeDescription::GetTypeId("Number")); - int spriteId(TypeDescription::GetTypeId("Sprite")); + td.AddField("sprite", FieldDescription(((char *)&a.sprite) - ((char *)&a) - offset, Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for cutting out frames")); + td.AddField("frametime", FieldDescription(((char *)&a.frameTime) - ((char *)&a) - offset, Interpreter::NUMBER_ID).SetDescription("duration of a frame in miliseconds")); + 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(); +} - td.AddField("sprite", FieldDescription(((char *)&a.sprite) - ((char *)&a) - offset, spriteId, true)); - td.AddField("frametime", FieldDescription(((char *)&a.frameTime) - ((char *)&a) - offset, numberId, false)); - td.AddField("repeat", FieldDescription(((char *)&a.repeat) - ((char *)&a) - offset, boolId, false)); +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; +} + + }