]> git.localhorst.tv Git - l2e.git/commitdiff
added ComplexAnimation
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 11 Aug 2012 22:42:05 +0000 (00:42 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sat, 11 Aug 2012 23:16:31 +0000 (01:16 +0200)
src/app/Timer.h
src/graphics/Animation.h [new file with mode: 0644]
src/graphics/ComplexAnimation.h [new file with mode: 0644]
src/graphics/SimpleAnimation.h

index 2d6326f76576c582bb36d2c49a3ee49e4cbdb2ed..b7711d679481746e8be3c96d41a28e7fd3260770 100644 (file)
@@ -94,7 +94,6 @@ public:
 public:
        void Update(Time delta) {
                for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
-                       TimerData<Time> &t(*i);
                        if (i->target > 0) {
                                Time intervalTime(i->time);
                                while (intervalTime > i->target) intervalTime -= i->target;
diff --git a/src/graphics/Animation.h b/src/graphics/Animation.h
new file mode 100644 (file)
index 0000000..55a16d9
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ *  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_ */
diff --git a/src/graphics/ComplexAnimation.h b/src/graphics/ComplexAnimation.h
new file mode 100644 (file)
index 0000000..a1ffaaa
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * ComplexAnimation.h
+ *
+ *  Created on: Aug 12, 2012
+ *      Author: holy
+ */
+
+#ifndef GRAPHICS_COMPLEXANIMATION_H_
+#define GRAPHICS_COMPLEXANIMATION_H_
+
+#include "Animation.h"
+
+#include <vector>
+
+namespace graphics {
+
+class ComplexAnimation
+: public Animation {
+
+public:
+       ComplexAnimation() { }
+       ComplexAnimation(const Sprite *sprite, int frameTime, bool repeat = false)
+       : Animation(sprite, frameTime, repeat) { }
+
+public:
+       void AddFrame(int col, int row, const geometry::Vector<int> &disposition = geometry::Vector<int>()) {
+               frames.push_back(FrameProp(col, row, disposition));
+       }
+       void AddFrames(int col, int row, const geometry::Vector<int> &disposition, int amount) {
+               for (int i(0); i < amount; ++i) {
+                       AddFrame(col, row, disposition);
+               }
+       }
+
+       virtual void Draw(SDL_Surface *dest, geometry::Point<int> position) const {
+               Animation::Draw(dest, position + frames[Frame()].disposition);
+       }
+
+protected:
+       virtual int Col() const { return frames[Frame()].col; }
+       virtual int Row() const { return frames[Frame()].row; }
+       virtual int NumFrames() const { return frames.size(); };
+
+private:
+       struct FrameProp {
+               FrameProp(int col, int row, const geometry::Vector<int> &disposition)
+               : col(col), row(row), disposition(disposition) {}
+               int col;
+               int row;
+               geometry::Vector<int> disposition;
+       };
+       std::vector<FrameProp> frames;
+
+};
+
+}
+
+#endif /* GRAPHICS_COMPLEXANIMATION_H_ */
index dffd863a2be6bbf287039e2a44179d457b2aae0e..3354328b5e6cb5281ba69d6f35d14df05f40704c 100644 (file)
@@ -8,52 +8,28 @@
 #ifndef GRAPHICS_SIMPLEANIMATION_H_
 #define GRAPHICS_SIMPLEANIMATION_H_
 
-#include "Sprite.h"
-#include "../app/Application.h"
-#include "../app/State.h"
-#include "../app/Timer.h"
-#include "../geometry/Point.h"
-
-#include <SDL.h>
+#include "Animation.h"
 
 namespace graphics {
 
-class SimpleAnimation {
+class SimpleAnimation
+: public Animation {
 
 public:
        SimpleAnimation()
-       : sprite(0), frameTime(0), numFrames(0), col(0), row(0), repeat(false) { }
+       : numFrames(0), col(0), row(0) { }
        SimpleAnimation(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) { }
+       : Animation(sprite, frameTime, repeat), numFrames(numFrames), col(col), row(row) { }
 
-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);
-       }
-       void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
-               sprite->DrawCenterBottom(dest, position, col, Running() ? row + (timer.Iteration() % numFrames) : row);
-       }
+protected:
+       virtual int Col() const { return col; }
+       virtual int Row() const { return row + Frame(); }
+       virtual int NumFrames() const { return numFrames; };
 
 private:
-       const Sprite *sprite;
-       app::Timer<Uint32> timer;
-       int frameTime;
        int numFrames;
        int col;
        int row;
-       bool repeat;
 
 };