Explorar o código

Opened a bit some members (should do accessors instead), fixed animator, added a tile animation test to be sure that it works. Also anim should be updated in render functions with dt.

Eiyeron Fulmincendii %!s(int64=10) %!d(string=hai) anos
pai
achega
10eb1151bb
Modificáronse 5 ficheiros con 29 adicións e 16 borrados
  1. 4 1
      include/Animator.h
  2. 3 2
      include/Map.h
  3. 8 5
      src/Animator.cpp
  4. 6 6
      src/Map.cpp
  5. 8 2
      src/main.cpp

+ 4 - 1
include/Animator.h

@@ -20,9 +20,12 @@ namespace WalrusRPG
 
     class Animator
     {
+      public:
+        tinystl::unordered_map<unsigned, Animation> animations;
+
+
       protected:
         unsigned elapsed_time;
-        tinystl::unordered_map<unsigned, Animation> animations;
 
       public:
         Animator();

+ 3 - 2
include/Map.h

@@ -10,6 +10,9 @@ namespace WalrusRPG
 {
     class Map
     {
+      public:
+        Animator anim;
+
       protected:
         // <Tiles> data;
         // <Tileset> tileset;
@@ -18,8 +21,6 @@ namespace WalrusRPG
         unsigned *layer0;
         unsigned *layer1;
         TileRenderer *renderer;
-        Animator anim;
-        unsigned time_render;
         // TODO?: add a boolean/getter to know if a second layer exist?
       public:
         Map(int width, int height, unsigned *layer0, unsigned *layer1);

+ 8 - 5
src/Animator.cpp

@@ -15,12 +15,15 @@ namespace
         {
             frame_time -= anim.stripe[index].duration;
             index++;
-            if (index >= anim.stripe.size() && anim.looping)
-                index -= anim.stripe.size();
-            else
-                return anim.stripe.size() - 1;
+            if (index >= anim.stripe.size())
+            {
+                if (anim.looping)
+                    index = 0;
+                else
+                    return anim.stripe[anim.stripe.size() - 1].frame;
+            }
         } while (frame_time > 0);
-        return index;
+        return anim.stripe[index].frame;
     }
 }
 

+ 6 - 6
src/Map.cpp

@@ -11,7 +11,7 @@
 #define TILERENDERER WalrusRPG::TileRenderer
 
 MAP::Map(int width, int height, unsigned *layer0, unsigned *layer1)
-    : anim(), time_render(0)
+    : anim()
 {
     this->renderer = new TileRenderer(overworld, 16, 16);
     this->width = width;
@@ -27,13 +27,12 @@ MAP::~Map()
 
 void MAP::update(unsigned dt)
 {
-    anim.update(dt);
     // TODO update map's data according to elasped time
 }
 
 void MAP::render(WalrusRPG::Camera &camera, unsigned dt)
 {
-    time_render += dt;
+    anim.update(dt);
     signed t_width = renderer->get_tile_width();
     signed t_height = renderer->get_tile_height();
     // By Eiyeron : I assumed that the camera's position is the top left pixel.
@@ -80,13 +79,14 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt)
         for (signed i = 0; i < delta_x; i++)
         {
             unsigned index = (start_x + i) + (start_y + j) * this->width;
-            unsigned tile_over = this->layer0[index];
+            unsigned tile_over = anim.get_animation_frame(this->layer0[index]);
             if (tile_over != 0)
-                renderer->render(anim.get_animation_frame(this->layer0[index]), RECT(offset_x + i * t_width, offset_y + j * t_height));
+                renderer->render(tile_over, RECT(offset_x + i * t_width, offset_y + j * t_height));
+
             // layer1 : Over-layer
             if (this->layer1 == NULL)
                 continue;
-            tile_over = this->layer1[index];
+            tile_over = anim.get_animation_frame(this->layer1[index]);
             if (tile_over != 0)
                 renderer->render(anim.get_animation_frame(tile_over), RECT(offset_x + i * t_width, offset_y + j * t_height));
         }

+ 8 - 2
src/main.cpp

@@ -173,8 +173,14 @@ int main(int argc, char *argv[])
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
     Map map(20, 20, dungeonTest, dungeonTest2);
-
-
+    tinystl::vector<Frame> stripe21;
+    tinystl::vector<Frame> stripe22;
+    stripe21.push_back({21, 23});
+    stripe21.push_back({22, 31});
+    stripe22.push_back({22, 37});
+    stripe22.push_back({21, 41});
+    map.anim.add_animation(21, {stripe21, true});
+    map.anim.add_animation(22, {stripe22, true});
     map_loop(0, 0, map);
 
     timer_restore(0);