]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
new language, new compiler
[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 #include "../loader/noinit.h"
15
16 #include <memory>
17 #include <SDL.h>
18
19 namespace graphics {
20
21 class Sprite;
22
23 class Animation {
24
25 public:
26         static const int TYPE_ID = 401;
27
28 public:
29         Animation()
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() { };
34 protected:
35         Animation(loader::noinit_t);
36
37 public:
38         const Sprite *GetSprite() const { return sprite; }
39         int FrameTime() const { return frameTime; }
40         bool Repeat() const { return repeat; }
41
42 public:
43         void SetSprite(const Sprite *s) { sprite = s; }
44         void SetFrameTime(int t) { frameTime = t; }
45         void SetRepeat(bool r) { repeat = r; }
46
47 public:
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>(); }
52
53         static void CreateTypeDescription();
54
55 protected:
56         static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
57
58 private:
59         const Sprite *sprite;
60         int frameTime;
61         bool repeat;
62
63 };
64
65
66 class AnimationRunner {
67
68 public:
69         explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
70         : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
71
72 public:
73         bool Valid() const { return animation; }
74         void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
75
76         void Start(app::State &ctrl);
77         void Start(app::Application &ctrl);
78         void Synchronize(const AnimationRunner &other) { timer = other.timer; }
79         void Stop();
80         bool Started() const;
81         bool Running() const;
82         bool Finished() const;
83         bool JustFinished() const;
84
85         const app::Timer<Uint32> &GetTimer() { return timer; }
86
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; }
93
94         void ChangeAnimation(const Animation *a) { animation = a; }
95         const Animation *GetAnimation() const { return animation; }
96
97         void ChangeSprite(const Sprite *s) { sprite = s; }
98         const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
99
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;
104
105         int Frame() const;
106
107 private:
108         const Animation *animation;
109         const graphics::Sprite *sprite;
110         app::Timer<Uint32> timer;
111         int frameShift;
112         int colOffset;
113         int rowOffset;
114
115 };
116
117 }
118
119 #endif