]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
79bfa852b887f02316afad317a5bfdd912348eb5
[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 "../geometry/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 geometry::Vector<int> Offset(int frame) const { return geometry::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 Stop();
76         bool Started() const;
77         bool Running() const;
78         bool Finished() const;
79         bool JustFinished() const;
80
81         const app::Timer<Uint32> &GetTimer() { return timer; }
82
83         void SetFrameShift(int offset) { frameShift = offset; }
84         int FrameShift() const { return frameShift; }
85         void SetColOffset(int offset) { colOffset = offset; }
86         int ColOffset() const { return colOffset; }
87         void SetRowOffset(int offset) { rowOffset = offset; }
88         int RowOffset() const { return rowOffset; }
89
90         void ChangeAnimation(const Animation *a) { animation = a; }
91         const Animation *GetAnimation() const { return animation; }
92
93         void ChangeSprite(const Sprite *s) { sprite = s; }
94         const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
95
96         void Draw(SDL_Surface *dest, geometry::Vector<int> position) const;
97         void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const;
98         void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const;
99         void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const;
100
101         int Frame() const;
102
103 private:
104         const Animation *animation;
105         const graphics::Sprite *sprite;
106         app::Timer<Uint32> timer;
107         int frameShift;
108         int colOffset;
109         int rowOffset;
110
111 };
112
113 }
114
115 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */