Selaa lähdekoodia

Improve looping animations slightly more
Also formatting, please.

Streetwalrus Einstein 10 vuotta sitten
vanhempi
commit
72147f5c66
4 muutettua tiedostoa jossa 24 lisäystä ja 19 poistoa
  1. 2 1
      src/engine/StateMachine.cpp
  2. 2 2
      src/engine/main.cpp
  3. 19 16
      src/render/Animator.cpp
  4. 1 0
      src/render/Animator.h

+ 2 - 1
src/engine/StateMachine.cpp

@@ -67,7 +67,8 @@ void STATEMACHINE::run()
             this->pop();
         }
 
-        while (Timers::read(0) < loop_next);
+        while (Timers::read(0) < loop_next)
+            ;
         loop_next += loop_time;
     }
 }

+ 2 - 2
src/engine/main.cpp

@@ -73,8 +73,8 @@ int main(int argc, char *argv[])
     stripe21.push_back({22, 31 * 546});
     stripe22.push_back({22, 37 * 546});
     stripe22.push_back({21, 41 * 546});
-    map.anim.add_animation(21, {stripe21, true});
-    map.anim.add_animation(22, {stripe22, true});
+    map.anim.add_animation(21, {stripe21, true, 0});
+    map.anim.add_animation(22, {stripe22, true, 0});
 
     StateMachine machine(new States::StateMap(0, 0, map));
     machine.run();

+ 19 - 16
src/render/Animator.cpp

@@ -6,23 +6,25 @@ using namespace WalrusRPG;
 
 namespace
 {
-	unsigned get_animation_duration(const WalrusRPG::Animation &anim) {
-		unsigned duration = 0;
-		for (unsigned index = 0; index < anim.stripe.size(); index++) {
-			duration += anim.stripe[index].duration;
-		}
-		return duration;
-	}
-	
-    unsigned find_frame(const WalrusRPG::Animation &anim, signed frame_time)
+    unsigned get_animation_duration(const WalrusRPG::Animation &anim)
     {
-		unsigned duration = get_animation_duration(anim);
-		if (frame_time >= duration && !anim.looping) {
-			// simple looping checking
-			// Also, a flag to detect if the animation ended could be an option
-			return anim.stripe[anim.stripe.size() - 1].frame;
-		}
-		frame_time %= duration;
+        unsigned duration = 0;
+        for (unsigned index = 0; index < anim.stripe.size(); index++)
+        {
+            duration += anim.stripe[index].duration;
+        }
+        return duration;
+    }
+
+    unsigned find_frame(const WalrusRPG::Animation &anim, int frame_time)
+    {
+        if (frame_time >= anim.duration && !anim.looping)
+        {
+            // simple looping checking
+            // Also, a flag to detect if the animation ended could be an option
+            return anim.stripe[anim.stripe.size() - 1].frame;
+        }
+        frame_time %= anim.duration;
 
         unsigned index = 0;
         do
@@ -49,6 +51,7 @@ ANIMATOR::Animator()
 void ANIMATOR::add_animation(int index, Animation anim)
 {
     animations[index] = anim;
+    animations[index].duration = get_animation_duration(anim);
 }
 
 unsigned ANIMATOR::get_animation_frame(unsigned id)

+ 1 - 0
src/render/Animator.h

@@ -15,6 +15,7 @@ namespace WalrusRPG
     {
         tinystl::vector<WalrusRPG::Frame> stripe;
         bool looping;
+        int duration;
     };
 
     class Animator