+/*
+ * 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/operators.h"
+#include "../geometry/Point.h"
+#include "../geometry/Vector.h"
+
+#include <SDL.h>
+
+namespace graphics {
+
+class Animation {
+
+public:
+ Animation()
+ : sprite(0), frameTime(0), repeat(false) { }
+ Animation(const Sprite *sprite, int frameTime, bool repeat = false)
+ : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
+ virtual ~Animation() { };
+
+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());
+ }
+ virtual void Draw(SDL_Surface *dest, geometry::Point<int> position) const {
+ sprite->Draw(dest, position, Col(), Row());
+ }
+ void DrawTopRight(SDL_Surface *dest, geometry::Point<int> position) const {
+ geometry::Vector<int> offset(-sprite->Width(), 0);
+ Draw(dest, position + offset);
+ }
+ void DrawCenter(SDL_Surface *dest, geometry::Point<int> position) const {
+ geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height() / 2);
+ Draw(dest, position + offset);
+ }
+ void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
+ geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
+ Draw(dest, position + offset);
+ }
+
+protected:
+ int Frame() const { return Running() ? (timer.Iteration() % NumFrames()) : 0; }
+ virtual int Col() const = 0;
+ virtual int Row() const = 0;
+ virtual int NumFrames() const = 0;
+
+private:
+ const Sprite *sprite;
+ app::Timer<Uint32> timer;
+ int frameTime;
+ bool repeat;
+
+};
+
+}
+
+#endif /* GRAPHICS_SIMPLEANIMATION_H_ */