]> git.localhorst.tv Git - l2e.git/commitdiff
added animation class
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 11 Aug 2012 20:16:13 +0000 (22:16 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 11 Aug 2012 20:22:19 +0000 (22:22 +0200)
src/graphics/Animation.h [new file with mode: 0644]

diff --git a/src/graphics/Animation.h b/src/graphics/Animation.h
new file mode 100644 (file)
index 0000000..fc9de19
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Animation.h
+ *
+ *  Created on: Aug 11, 2012
+ *      Author: holy
+ */
+
+#ifndef GRAPHICS_ANIMATION_H_
+#define GRAPHICS_ANIMATION_H_
+
+#include "Sprite.h"
+#include "../app/Application.h"
+#include "../app/State.h"
+#include "../app/Timer.h"
+#include "../geometry/Point.h"
+
+#include <SDL.h>
+
+namespace graphics {
+
+class Animation {
+
+public:
+       Animation()
+       : sprite(0), frameTime(0), numFrames(0), col(0), row(0), repeat(false) { }
+       Animation(const Sprite *sprite, int frameTime, int numFrames, int col = 0, int row = 0, bool repeat = false)
+       : sprite(sprite), frameTime(frameTime), numFrames(numFrames), col(col), row(row), repeat(repeat) { }
+
+public:
+       void Start(app::State &ctrl) {
+               timer = ctrl.GraphicsTimers().StartInterval(frameTime);
+       }
+       void Start(app::Application &ctrl) {
+               timer = ctrl.GlobalTimers().StartInterval(frameTime);
+       }
+       void Stop() {
+               timer = app::Timer<Uint32>();
+       }
+       bool Running() const {
+               return timer.Running() && (repeat || timer.Iteration() < numFrames);
+       }
+       void Draw(SDL_Surface *dest, geometry::Point<int> position) {
+               sprite->Draw(dest, position, col, Running() ? row + (timer.Iteration() % numFrames) : row);
+       }
+
+private:
+       const Sprite *sprite;
+       app::Timer<Uint32> timer;
+       int frameTime;
+       int numFrames;
+       int col;
+       int row;
+       bool repeat;
+
+};
+
+}
+
+#endif /* GRAPHICS_ANIMATION_H_ */