1 #ifndef GRAPHICS_ANIMATION_H_
2 #define GRAPHICS_ANIMATION_H_
12 #include "../app/Timer.h"
13 #include "../math/Vector.h"
25 static const int TYPE_ID = 401;
29 : sprite(0), frameTime(0), repeat(false) { }
30 Animation(const Sprite *sprite, int frameTime, bool repeat = false)
31 : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
32 virtual ~Animation() { };
35 const Sprite *GetSprite() const { return sprite; }
36 int FrameTime() const { return frameTime; }
37 bool Repeat() const { return repeat; }
40 void SetSprite(const Sprite *s) { sprite = s; }
41 void SetFrameTime(int t) { frameTime = t; }
42 void SetRepeat(bool r) { repeat = r; }
45 virtual int NumFrames() const = 0;
46 virtual int Col(int frame) const = 0;
47 virtual int Row(int frame) const = 0;
48 virtual math::Vector<int> Offset(int frame) const { return math::Vector<int>(); }
50 static void CreateTypeDescription();
53 static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
63 class AnimationRunner {
66 explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
67 : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
70 bool Valid() const { return animation; }
71 void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
73 void Start(app::State &ctrl);
74 void Start(app::Application &ctrl);
75 void Synchronize(const AnimationRunner &other) { timer = other.timer; }
79 bool Finished() const;
80 bool JustFinished() const;
82 const app::Timer<Uint32> &GetTimer() { return timer; }
84 void SetFrameShift(int offset) { frameShift = offset; }
85 int FrameShift() const { return frameShift; }
86 void SetColOffset(int offset) { colOffset = offset; }
87 int ColOffset() const { return colOffset; }
88 void SetRowOffset(int offset) { rowOffset = offset; }
89 int RowOffset() const { return rowOffset; }
91 void ChangeAnimation(const Animation *a) { animation = a; }
92 const Animation *GetAnimation() const { return animation; }
94 void ChangeSprite(const Sprite *s) { sprite = s; }
95 const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
97 void Draw(SDL_Surface *dest, math::Vector<int> position) const;
98 void DrawTopRight(SDL_Surface *dest, math::Vector<int> position) const;
99 void DrawCenter(SDL_Surface *dest, math::Vector<int> position) const;
100 void DrawCenterBottom(SDL_Surface *dest, math::Vector<int> position) const;
105 const Animation *animation;
106 const graphics::Sprite *sprite;
107 app::Timer<Uint32> timer;