From f61e1d67dea756476c6c8fc59bae01e4ab9b5bec Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Sun, 15 Apr 2012 13:26:34 +0100
Subject: [PATCH] [Add] Added animation sequence data serializer.

---
 LibDQt/LibDQt.pro                   |   6 +-
 src/Animation/AnimationSequence.cpp | 112 ++++++++++++++++++++++++++++
 src/Animation/AnimationSequence.h   |  37 +++++++++
 src/System/String.cpp               |   2 +-
 4 files changed, 154 insertions(+), 3 deletions(-)
 create mode 100644 src/Animation/AnimationSequence.cpp
 create mode 100644 src/Animation/AnimationSequence.h

diff --git a/LibDQt/LibDQt.pro b/LibDQt/LibDQt.pro
index 6fafcd7..c8443f7 100644
--- a/LibDQt/LibDQt.pro
+++ b/LibDQt/LibDQt.pro
@@ -60,7 +60,8 @@ HEADERS += ../src/Actor/Player.h \
     ../src/Animation/AnimimationSequence.h \
     ../src/System/FileReader.h \
     ../src/Animation/AnimationSequence.h \
-    ../src/System/String.h
+    ../src/System/String.h \
+    ../src/Font/Font.h
 SOURCES += ../src/Actor/Player.cpp \
     ../src/Collision/AABB.cpp \
     ../src/Global/Globals.cpp \
@@ -95,5 +96,6 @@ SOURCES += ../src/Actor/Player.cpp \
     ../src/Actor/Actor.cpp \
     ../src/System/FileReader.cpp \
     ../src/Animation/AnimationSequence.cpp \
-    ../src/System/String.cpp
+    ../src/System/String.cpp \
+    ../src/Font/Font.cpp
 OTHER_FILES += 
diff --git a/src/Animation/AnimationSequence.cpp b/src/Animation/AnimationSequence.cpp
new file mode 100644
index 0000000..573c1ec
--- /dev/null
+++ b/src/Animation/AnimationSequence.cpp
@@ -0,0 +1,112 @@
+#include <stdlib.h>
+
+#include "../System/Debug.h"
+#include "AnimationSequence.h"
+
+/*
+ * Load and read a sequence file for an animating sprite then
+ * stores the result so you can call the sprites animation at any time.
+ * This allows us to define animation cycles externally.
+ *
+ * The file stucture is:
+ * [# of animation cycles] [name] [start frame] [end frame]
+ * A typical cycle file should look something like:
+ *
+ * 3;                       -- Three cycles.
+ * WalkLeft 1 20            -- [Name] [Frame 1 to 20]
+ * WalkRight 42 62          -- [Name] [Frame 42 to 62]
+ */
+
+AnimationSequence::AnimationSequence(void) {
+
+}
+
+AnimationSequence::AnimationSequence(const char* filename) {
+  _numberOfFrames   = 0;
+  _sequenceID       = filename;
+  _sequenceID.Concatenate(".txt");
+  ReadFile();
+}
+
+AnimationSequence::~AnimationSequence(void) {
+
+}
+
+void AnimationSequence::ReadFile(void) {
+  // Open and read a file, storing it into a char buffer which
+  // is then sorted character by character arranging the data into
+  // usable animations using the scan method, each result is stored into an
+  // animation array.
+  if(_file.Exists(_sequenceID)) {
+    String name;
+    String loop;
+    char* temp;
+    _file.OpenFile(_sequenceID, "r");
+    _file.ReadBuffer(temp);
+    _file.CloseFile();
+
+    int counter = 0;
+
+    _numberOfFrames = atoi(Scan(temp, counter));
+
+    for(int index = 0; index < _numberOfFrames; index++) {
+      name              = "";
+      int startFrame    = 0;
+      int endFrame      = 0;
+      loop              = "";
+
+      name          = Scan(temp, counter);
+      startFrame    = atoi(Scan(temp, counter));
+      endFrame      = atoi(Scan(temp, counter));
+
+      if(temp[counter - 1] == SPACE) {
+        loop = Scan(temp, counter);
+      }
+
+      _animations[index] = new Animation();
+      _animations[index]->_animationID  = name;
+      _animations[index]->frameBegin    = startFrame;
+      _animations[index]->frameEnd      = endFrame;
+      _animations[index]->_loopTo       = loop;
+    }
+  } else {
+    //Debug::logger->message("%s does not exist.", _sequenceID);
+    assert(false);
+  }
+}
+
+const char* AnimationSequence::Scan(char* source, int &counter) {
+  String temp;
+  temp = "";
+  bool terminate = false;
+  while(!terminate) {
+    if(source[counter] != SPACE && source[counter] != ENDOFLINE) {
+      if(source[counter] == BLANK) {
+        counter++;
+      } else {
+        temp.Concatenate(source[counter]);
+        counter++;
+      }
+    } else {
+      terminate = true;
+      counter++;
+    }
+  }
+  return temp;
+}
+
+AnimationSequence::Animation* AnimationSequence::GetAnimation(const char* filename) {
+  for(int i = 0; i < _numberOfFrames; i++) {
+    if(strcmp(filename, _animations[i]->_animationID) == 0) {
+      return _animations[i];
+    }
+  }
+  return 0;
+}
+
+AnimationSequence::Animation* AnimationSequence::GetAnimation(int index) {
+  if(index < _numberOfFrames) {
+    return _animations[index];
+  }
+  return 0;
+}
diff --git a/src/Animation/AnimationSequence.h b/src/Animation/AnimationSequence.h
new file mode 100644
index 0000000..fc8de86
--- /dev/null
+++ b/src/Animation/AnimationSequence.h
@@ -0,0 +1,37 @@
+#pragma once
+#include <string>
+#include <assert.h>
+
+#include "../System/FileReader.h"
+#include "../System/String.h"
+
+#define MAX_FRAMES    16
+#define BLANK         10
+#define ENDOFLINE     59
+#define SPACE         32
+
+class AnimationSequence {
+  struct Animation {
+    String _animationID;
+    int frameBegin;
+    int frameEnd;
+    String _loopTo;
+  };
+
+public:
+  AnimationSequence(void);
+  AnimationSequence(const char* filename);
+  ~AnimationSequence(void);
+
+  void ReadFile(void);
+  Animation* GetAnimation(int index);
+  Animation* GetAnimation(const char* filename);
+
+private:
+  const char* Scan(char* source, int& counter);
+
+  String _sequenceID;
+  int _numberOfFrames;
+  FileReader _file;
+  Animation* _animations[MAX_FRAMES];
+};
diff --git a/src/System/String.cpp b/src/System/String.cpp
index eac51f5..187e1ef 100644
--- a/src/System/String.cpp
+++ b/src/System/String.cpp
@@ -81,7 +81,7 @@ bool String::operator==(const char* value) const {
 }
 
 bool String::operator==(String& value) const {
-  if(strcmp(_string, value.GetPointer) == 0) {
+  if(strcmp(_string, value.GetPointer()) == 0) {
     return true;
   }
   return false;