1 #ifndef GRAPHICS_ANIMATION_H_
2 #define GRAPHICS_ANIMATION_H_
12 #include "../app/Timer.h"
13 #include "../math/Vector.h"
14 #include "../loader/noinit.h"
26 static const int TYPE_ID = 401;
30 : sprite(0), frameTime(0), repeat(false) { }
31 Animation(const Sprite *sprite, int frameTime, bool repeat = false)
32 : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
33 virtual ~Animation() { };
35 Animation(loader::noinit_t);
38 const Sprite *GetSprite() const { return sprite; }
39 int FrameTime() const { return frameTime; }
40 bool Repeat() const { return repeat; }
43 void SetSprite(const Sprite *s) { sprite = s; }
44 void SetFrameTime(int t) { frameTime = t; }
45 void SetRepeat(bool r) { repeat = r; }
48 virtual int NumFrames() const = 0;
49 virtual int Col(int frame) const = 0;
50 virtual int Row(int frame) const = 0;
51 virtual math::Vector<int> Offset(int frame) const { return math::Vector<int>(); }
53 static void CreateTypeDescription();
56 static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
66 class AnimationRunner {
69 explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
70 : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
73 bool Valid() const { return animation; }
74 void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
76 void Start(app::State &ctrl);
77 void Start(app::Application &ctrl);
78 void Synchronize(const AnimationRunner &other) { timer = other.timer; }
82 bool Finished() const;
83 bool JustFinished() const;
85 const app::Timer<Uint32> &GetTimer() { return timer; }
87 void SetFrameShift(int offset) { frameShift = offset; }
88 int FrameShift() const { return frameShift; }
89 void SetColOffset(int offset) { colOffset = offset; }
90 int ColOffset() const { return colOffset; }
91 void SetRowOffset(int offset) { rowOffset = offset; }
92 int RowOffset() const { return rowOffset; }
94 void ChangeAnimation(const Animation *a) { animation = a; }
95 const Animation *GetAnimation() const { return animation; }
97 void ChangeSprite(const Sprite *s) { sprite = s; }
98 const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
100 void Draw(SDL_Surface *dest, math::Vector<int> position) const;
101 void DrawTopRight(SDL_Surface *dest, math::Vector<int> position) const;
102 void DrawCenter(SDL_Surface *dest, math::Vector<int> position) const;
103 void DrawCenterBottom(SDL_Surface *dest, math::Vector<int> position) const;
108 const Animation *animation;
109 const graphics::Sprite *sprite;
110 app::Timer<Uint32> timer;