]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
merged Point into Vector
[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 "../geometry/Vector.h"
16
17 #include <SDL.h>
18
19 namespace graphics {
20
21 class Animation {
22
23 public:
24         Animation()
25         : sprite(0), frameTime(0), repeat(false) { }
26         Animation(const Sprite *sprite, int frameTime, bool repeat = false)
27         : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
28         virtual ~Animation() { };
29
30 public:
31
32         const Sprite *GetSprite() const { return sprite; }
33         int FrameTime() const { return frameTime; }
34         bool Repeat() const { return repeat; }
35
36 public:
37         virtual int NumFrames() const = 0;
38         virtual int Col(int frame) const = 0;
39         virtual int Row(int frame) const = 0;
40         virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
41
42 private:
43         const Sprite *sprite;
44         int frameTime;
45         bool repeat;
46
47 };
48
49
50 class AnimationRunner {
51
52 public:
53         explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
54         : animation(a), sprite(a ? a->GetSprite() : 0), colOffset(colOffset), rowOffset(rowOffset) { }
55
56 public:
57         bool Valid() const { return animation; }
58         void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
59
60         void Start(app::State &ctrl) {
61                 timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
62         }
63         void Start(app::Application &ctrl) {
64                 timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
65         }
66         void Stop() {
67                 timer = app::Timer<Uint32>();
68         }
69         bool Started() const {
70                 return timer.Started();
71         }
72         bool Running() const {
73                 return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
74         }
75         bool Finished() const {
76                 return Started() && !Running();
77         }
78         bool JustFinished() const {
79                 return timer.JustHit() && timer.Iteration() == animation->NumFrames();
80         }
81
82         const app::Timer<Uint32> &GetTimer() { return timer; }
83
84         void SetColOffset(int offset) { colOffset = offset; }
85         int ColOffset() const { return colOffset; }
86         void SetRowOffset(int offset) { rowOffset = offset; }
87         int RowOffset() const { return rowOffset; }
88
89         void ChangeSprite(const Sprite *s) { sprite = s; }
90         const Sprite *GetSprite() const { return sprite; }
91
92         void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
93                 sprite->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
94         }
95         void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
96                 geometry::Vector<int> offset(-sprite->Width(), 0);
97                 Draw(dest, position + offset);
98         }
99         void DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
100                 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height() / 2);
101                 Draw(dest, position + offset);
102         }
103         void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
104                 geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
105                 Draw(dest, position + offset);
106         }
107
108         int Frame() const { return Running() ? (timer.Iteration() % animation->NumFrames()) : 0; }
109
110 private:
111         const Animation *animation;
112         const graphics::Sprite *sprite;
113         app::Timer<Uint32> timer;
114         int colOffset;
115         int rowOffset;
116
117 };
118
119 }
120
121 #endif /* GRAPHICS_SIMPLEANIMATION_H_ */