#include "../app/Application.h"
#include "../app/State.h"
#include "../app/Timer.h"
-#include "../geometry/operators.h"
-#include "../geometry/Point.h"
+#include "../loader/fwd.h"
#include "../geometry/Vector.h"
+#include <memory>
#include <SDL.h>
namespace graphics {
class Animation {
+public:
+ static const int TYPE_ID = 401;
+
public:
Animation()
- : sprite(0), frameTime(0), colOffset(0), rowOffset(0), repeat(false) { }
+ : sprite(0), frameTime(0), repeat(false) { }
Animation(const Sprite *sprite, int frameTime, bool repeat = false)
- : sprite(sprite), frameTime(frameTime), colOffset(0), rowOffset(0), repeat(repeat) { }
+ : sprite(sprite), frameTime(frameTime), repeat(repeat) { }
virtual ~Animation() { };
public:
+ const Sprite *GetSprite() const { return sprite; }
+ int FrameTime() const { return frameTime; }
+ bool Repeat() const { return repeat; }
+
+public:
+ void SetSprite(const Sprite *s) { sprite = s; }
+ void SetFrameTime(int t) { frameTime = t; }
+ void SetRepeat(bool r) { repeat = r; }
+
+public:
+ virtual int NumFrames() const = 0;
+ virtual int Col(int frame) const = 0;
+ virtual int Row(int frame) const = 0;
+ virtual geometry::Vector<int> Offset(int frame) const { return geometry::Vector<int>(); }
+
+ static void CreateTypeDescription();
+
+protected:
+ static void AddFields(loader::TypeDescription &, const Animation &, std::ptrdiff_t offset);
+
+private:
+ const Sprite *sprite;
+ int frameTime;
+ bool repeat;
+
+};
+
+
+class AnimationRunner {
+
+public:
+ explicit AnimationRunner(const Animation *a = 0, int colOffset = 0, int rowOffset = 0)
+ : animation(a), sprite(0), frameShift(0), colOffset(colOffset), rowOffset(rowOffset) { }
+
+public:
+ bool Valid() const { return animation; }
+ void Clear() { animation = 0; timer = app::Timer<Uint32>(); }
+
void Start(app::State &ctrl) {
- timer = ctrl.GraphicsTimers().StartInterval(frameTime);
+ timer = ctrl.GraphicsTimers().StartInterval(animation->FrameTime());
}
void Start(app::Application &ctrl) {
- timer = ctrl.GlobalTimers().StartInterval(frameTime);
+ timer = ctrl.GlobalTimers().StartInterval(animation->FrameTime());
}
void Stop() {
timer = app::Timer<Uint32>();
}
+ bool Started() const {
+ return timer.Started();
+ }
bool Running() const {
- return timer.Running() && (repeat || timer.Iteration() < NumFrames());
+ return timer.Running() && (animation->Repeat() || timer.Iteration() < animation->NumFrames());
+ }
+ bool Finished() const {
+ return Started() && !Running();
+ }
+ bool JustFinished() const {
+ return timer.JustHit() && timer.Iteration() == animation->NumFrames();
}
const app::Timer<Uint32> &GetTimer() { return timer; }
+ void SetFrameShift(int offset) { frameShift = offset; }
+ int FrameShift() const { return frameShift; }
void SetColOffset(int offset) { colOffset = offset; }
int ColOffset() const { return colOffset; }
void SetRowOffset(int offset) { rowOffset = offset; }
int RowOffset() const { return rowOffset; }
- const Sprite *GetSprite() const { return sprite; }
+ void ChangeAnimation(const Animation *a) { animation = a; }
+ const Animation *GetAnimation() const { return animation; }
+
void ChangeSprite(const Sprite *s) { sprite = s; }
+ const Sprite *GetSprite() const { return sprite ? sprite : animation->GetSprite(); }
- virtual void Draw(SDL_Surface *dest, geometry::Point<int> position) const {
- sprite->Draw(dest, position, Col() + ColOffset(), Row() + RowOffset());
+ void Draw(SDL_Surface *dest, geometry::Vector<int> position) const {
+ GetSprite()->Draw(dest, position + animation->Offset(Frame()), animation->Col(Frame()) + ColOffset(), animation->Row(Frame()) + RowOffset());
}
- void DrawTopRight(SDL_Surface *dest, geometry::Point<int> position) const {
- geometry::Vector<int> offset(-sprite->Width(), 0);
+ void DrawTopRight(SDL_Surface *dest, geometry::Vector<int> position) const {
+ geometry::Vector<int> offset(-GetSprite()->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 DrawCenter(SDL_Surface *dest, geometry::Vector<int> position) const {
+ Draw(dest, position - (GetSprite()->Size() / 2));
}
- void DrawCenterBottom(SDL_Surface *dest, geometry::Point<int> position) const {
- geometry::Vector<int> offset(-sprite->Width() / 2, -sprite->Height());
+ void DrawCenterBottom(SDL_Surface *dest, geometry::Vector<int> position) const {
+ geometry::Vector<int> offset(-GetSprite()->Width() / 2, -GetSprite()->Height());
Draw(dest, position + offset);
}
- int Frame() const { return Running() ? (timer.Iteration() % NumFrames()) : 0; }
-
-protected:
- virtual int Col() const = 0;
- virtual int Row() const = 0;
- virtual int NumFrames() const = 0;
+ int Frame() const { return Running() ? ((timer.Iteration() + frameShift) % animation->NumFrames()) : 0; }
private:
- const Sprite *sprite;
+ const Animation *animation;
+ const graphics::Sprite *sprite;
app::Timer<Uint32> timer;
- int frameTime;
+ int frameShift;
int colOffset;
int rowOffset;
- bool repeat;
};