]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Animation.h
added simple attack animations
[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/Point.h"
16
17 #include <SDL.h>
18
19 namespace graphics {
20
21 class Animation {
22
23 public:
24         Animation()
25         : sprite(0), frameTime(0), numFrames(0), col(0), row(0), repeat(false) { }
26         Animation(const Sprite *sprite, int frameTime, int numFrames, int col = 0, int row = 0, bool repeat = false)
27         : sprite(sprite), frameTime(frameTime), numFrames(numFrames), col(col), row(row), repeat(repeat) { }
28
29 public:
30         void Start(app::State &ctrl) {
31                 timer = ctrl.GraphicsTimers().StartInterval(frameTime);
32         }
33         void Start(app::Application &ctrl) {
34                 timer = ctrl.GlobalTimers().StartInterval(frameTime);
35         }
36         void Stop() {
37                 timer = app::Timer<Uint32>();
38         }
39         bool Running() const {
40                 return timer.Running() && (repeat || timer.Iteration() < numFrames);
41         }
42         void Draw(SDL_Surface *dest, geometry::Point<int> position) {
43                 sprite->Draw(dest, position, col, Running() ? row + (timer.Iteration() % numFrames) : row);
44         }
45         void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
46                 sprite->DrawCenterBottom(dest, position, col, Running() ? row + (timer.Iteration() % numFrames) : row);
47         }
48
49 private:
50         const Sprite *sprite;
51         app::Timer<Uint32> timer;
52         int frameTime;
53         int numFrames;
54         int col;
55         int row;
56         bool repeat;
57
58 };
59
60 }
61
62 #endif /* GRAPHICS_ANIMATION_H_ */