4 #include "../app/Application.h"
5 #include "../app/State.h"
6 #include "../loader/Interpreter.h"
7 #include "../loader/TypeDescription.h"
11 using loader::FieldDescription;
12 using loader::Interpreter;
13 using loader::TypeDescription;
17 Animation::Animation(loader::noinit_t) {
22 void Animation::CreateTypeDescription() {
23 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Animation"));
24 td.SetDescription("Abstract base type for animations.");
25 td.SetSize(sizeof(Animation));
28 void Animation::AddFields(TypeDescription &td, const Animation &a, std::ptrdiff_t offset) {
29 td.AddField("sprite", FieldDescription(((char *)&a.sprite) - ((char *)&a) - offset, Sprite::TYPE_ID).SetReferenced().SetDescription("the sprite used for cutting out frames"));
30 td.AddField("frametime", FieldDescription(((char *)&a.frameTime) - ((char *)&a) - offset, Interpreter::NUMBER_ID).SetDescription("duration of a frame in miliseconds"));
31 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"));
35 void AnimationRunner::Start(app::State &ctrl) {
37 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
40 void AnimationRunner::Start(app::Application &ctrl) {
42 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
45 void AnimationRunner::Stop() {
46 timer = app::Timer<Uint32>();
49 bool AnimationRunner::Started() const {
50 return timer.Started();
53 bool AnimationRunner::Running() const {
56 && (animation->Repeat()
57 || timer.Iteration() < animation->NumFrames());
60 bool AnimationRunner::Finished() const {
61 return Started() && !Running();
64 bool AnimationRunner::JustFinished() const {
67 && timer.Iteration() == animation->NumFrames();
71 void AnimationRunner::Draw(SDL_Surface *dest, math::Vector<int> position) const {
72 GetSprite()->Draw(dest,
73 position + animation->Offset(Frame()),
74 animation->Col(Frame()) + ColOffset(),
75 animation->Row(Frame()) + RowOffset());
78 void AnimationRunner::DrawTopRight(SDL_Surface *dest, math::Vector<int> position) const {
79 math::Vector<int> offset(-GetSprite()->Width(), 0);
80 Draw(dest, position + offset);
83 void AnimationRunner::DrawCenter(SDL_Surface *dest, math::Vector<int> position) const {
84 Draw(dest, position - (GetSprite()->Size() / 2));
87 void AnimationRunner::DrawCenterBottom(SDL_Surface *dest, math::Vector<int> position) const {
88 math::Vector<int> offset(-GetSprite()->Width() / 2, -GetSprite()->Height());
89 Draw(dest, position + offset);
92 int AnimationRunner::Frame() const {
94 ? ((timer.Iteration() + frameShift) % animation->NumFrames())