]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
frame shiftability in animation runner
[l2e.git] / src / graphics / Animation.h
1 /*
2  *  Animation.h
3  *
4  *  Created on: Aug 11, 2012
5  *      Author: holy
6  */
7
8 #ifndef GRAPHICS_ANIMATION_H_
9 #define GRAPHICS_ANIMATION_H_
10
11 #include "Sprite.h"
12 #include "../app/Application.h"
13 #include "../app/State.h"
14 #include "../app/Timer.h"
15 #include "../loader/fwd.h"
16 #include "../geometry/Vector.h"
17
18 #include <memory>
19 #include <SDL.h>
20
21 namespace graphics {
22
23 class Animation {
24
25 public:
26         Animation()
27         : sprite(0), frameTime(0), repeat(false) { }
28         Animation(const Sprite *sprite, int frameTime, bool repeat = false)
29         : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
30         virtual ~Animation() { };
31
32 public:
33         const Sprite *GetSprite() const { return sprite; }
34         int FrameTime() const { return frameTime; }
35         bool Repeat() const { return repeat; }
36
37 public:
38         void SetSprite(const Sprite *s) { sprite = s; }
39         void SetFrameTime(int t) { frameTime = t; }
40         void SetRepeat(bool r) { repeat = r; }
41
42 public:
43         virtual int NumFrames() const = 0;
44         virtual int Col(int frame) const = 0;
45         virtual int Row(int frame) const = 0;
46         virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
47
48 protected:
49         static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset, int boolId, int numberId, int spriteId);
50
51 private:
52         const Sprite *sprite;
53         int frameTime;
54         bool repeat;
55
56 };
57
58
59 class AnimationRunner {
60
61 public:
62         explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
63         : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
64
65 public:
66         bool Valid() const { return animation; }
67         void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
68
69         void Start(app::State &ctrl) {
70                 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
71         }
72         void Start(app::Application &ctrl) {
73                 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
74         }
75         void Stop() {
76                 timer = app::Timer<Uint32>();
77         }
78         bool Started() const {
79                 return timer.Started();
80         }
81         bool Running() const {
82                 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
83         }
84         bool Finished() const {
85                 return Started() && !Running();
86         }
87         bool JustFinished() const {
88                 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
89         }
90
91         const app::Timer<Uint32> &GetTimer() { return timer; }
92
93         void SetFrameShift(int offset) { frameShift = offset; }
94         int FrameShift() const { return frameShift; }
95         void SetColOffset(int offset) { colOffset = offset; }
96         int ColOffset() const { return colOffset; }
97         void SetRowOffset(int offset) { rowOffset = offset; }
98         int RowOffset() const { return rowOffset; }
99
100         void ChangeAnimation(const Animation *a) { animation = a; }
101         const Animation *GetAnimation() const { return animation; }
102
103         void ChangeSprite(const Sprite *s) { sprite = s; }
104         const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
105
106         void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
107                 GetSprite()->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
108         }
109         void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
110                 geometry::Vector<int> offset(-GetSprite()->Width(), 0);
111                 Draw(dest, position + offset);
112         }
113         void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
114                 Draw(dest, position - (GetSprite()->Size() / 2));
115         }
116         void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
117                 geometry::Vector<int> offset(-GetSprite()->Width() / 2, -GetSprite()->Height());
118                 Draw(dest, position + offset);
119         }
120
121         int Frame() const { return Running() ? ((timer.Iteration() + frameShift) % animation->NumFrames()) : 0; }
122
123 private:
124         const Animation *animation;
125         const graphics::Sprite *sprite;
126         app::Timer<Uint32> timer;
127         int frameShift;
128         int colOffset;
129         int rowOffset;
130
131 };
132
133 }
134
135 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */