]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
a224f548590c4af16edad31b32c1ab596881df3d
[l2e.git] / src / graphics / Animation.h
1 #ifndef GRAPHICS_ANIMATION_H_
2 #define GRAPHICS_ANIMATION_H_
3
4 namespace app {
5         class Application;
6         class State;
7 }
8 namespace loader {
9         class TypeDescription;
10 }
11
12 #include "../app/Timer.h"
13 #include "../math/Vector.h"
14
15 #include <memory>
16 #include <SDL.h>
17
18 namespace graphics {
19
20 class Sprite;
21
22 class Animation {
23
24 public:
25         static const int TYPE_ID = 401;
26
27 public:
28         Animation()
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() { };
33
34 public:
35         const Sprite *GetSprite() const { return sprite; }
36         int FrameTime() const { return frameTime; }
37         bool Repeat() const { return repeat; }
38
39 public:
40         void SetSprite(const Sprite *s) { sprite = s; }
41         void SetFrameTime(int t) { frameTime = t; }
42         void SetRepeat(bool r) { repeat = r; }
43
44 public:
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>(); }
49
50         static void CreateTypeDescription();
51
52 protected:
53         static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
54
55 private:
56         const Sprite *sprite;
57         int frameTime;
58         bool repeat;
59
60 };
61
62
63 class AnimationRunner {
64
65 public:
66         explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
67         : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
68
69 public:
70         bool Valid() const { return animation; }
71         void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
72
73         void Start(app::State &ctrl);
74         void Start(app::Application &ctrl);
75         void Synchronize(const AnimationRunner &other) { timer = other.timer; }
76         void Stop();
77         bool Started() const;
78         bool Running() const;
79         bool Finished() const;
80         bool JustFinished() const;
81
82         const app::Timer<Uint32> &GetTimer() { return timer; }
83
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; }
90
91         void ChangeAnimation(const Animation *a) { animation = a; }
92         const Animation *GetAnimation() const { return animation; }
93
94         void ChangeSprite(const Sprite *s) { sprite = s; }
95         const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
96
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;
101
102         int Frame() const;
103
104 private:
105         const Animation *animation;
106         const graphics::Sprite *sprite;
107         app::Timer<Uint32> timer;
108         int frameShift;
109         int colOffset;
110         int rowOffset;
111
112 };
113
114 }
115
116 #endif