Преглед изворни кода

Prototype animated tiles. Bulky, heavy and makes the binary grows like no one

Florian DORMONT пре 10 година
родитељ
комит
e1e9c763cc
5 измењених фајлова са 75 додато и 13 уклоњено
  1. 4 1
      include/Map.h
  2. 13 3
      include/Tileset.h
  3. 8 8
      src/Map.cpp
  4. 49 0
      src/Tileset.cpp
  5. 1 1
      src/main.cpp

+ 4 - 1
include/Map.h

@@ -3,6 +3,7 @@
 
 #include "Camera.h"
 #include "Entity.h"
+#include "Tileset.h"
 
 namespace WalrusRPG
 {
@@ -15,11 +16,13 @@ namespace WalrusRPG
         unsigned int height;
         unsigned *layer0;
         unsigned *layer1;
+        Tileset tset;
+        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);
         ~Map();
-        void render(Camera &camera, unsigned dt) const;
+        void render(Camera &camera, unsigned dt);
         void update(unsigned dt);
         bool is_tile_solid(unsigned x, unsigned y) const;
         bool is_pixel_solid(unsigned x, unsigned y) const;

+ 13 - 3
include/Tileset.h

@@ -1,20 +1,30 @@
 #ifndef INCLUDE_TILESET_H
 #define INCLUDE_TILESET_H
 
+#include <vector>
+#include <map>
+
 namespace WalrusRPG
 {
+    struct Frame
+    {
+        unsigned frame;
+        unsigned duration;
+    };
     class Tileset
     {
       protected:
-        char *sheet;
+        const unsigned short *sheet;
         unsigned sheet_width;
         unsigned sheet_height;
         unsigned tile_width;
-        unsigned tile_heihgt;
+        unsigned tile_height;
+        std::map<unsigned, std::vector<Frame>> animations;
 
       public:
-        Tileset(char *sheet, unsigned sheet_width, unsigned sheet_height, unsigned tile_width, unsigned tile_heihgt);
+        Tileset(unsigned short *sheet, unsigned sheet_width, unsigned sheet_height, unsigned tile_width, unsigned tile_heihgt);
         void render_tile(unsigned int index, unsigned x, unsigned y) const;
+        void render_tile(unsigned int index, unsigned x, unsigned y, unsigned time) const;
     };
 }
 

+ 8 - 8
src/Map.cpp

@@ -7,6 +7,7 @@
 #define MAP WalrusRPG::Map
 
 MAP::Map(int width, int height, unsigned *layer0, unsigned *layer1)
+    : tset(tiles, 192u, 24u, 24u, 24u), time_render(0)
 {
     this->width = width;
     this->height = height;
@@ -25,9 +26,9 @@ void MAP::update(unsigned dt)
     // TODO update map's data according to elasped time
 }
 
-void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
+void MAP::render(WalrusRPG::Camera &camera, unsigned dt)
 {
-    UNUSED(dt);
+    time_render += dt;
     // By Eiyeron : I assumed that the camera's position is the top left pixel.
     // Margins moves the rendered map if we go outside of the bounds (specially on the left or on the top).
     signed offset_x = camera.get_x() % 24 * -1;
@@ -61,10 +62,10 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
     signed delta_y = end_y - start_y;
 
     // Creating a region clip. Why does it smell like SDL?
-    WalrusRPG::Graphics::Rect_t sprite;
+    /*WalrusRPG::Graphics::Rect_t sprite;
     sprite.y = 0;
     sprite.w = 24;
-    sprite.h = 24;
+    sprite.h = 24;*/
 
     // rendering part.
     for (signed j = 0; j < delta_y; j++)
@@ -72,15 +73,14 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
         for (signed i = 0; i < delta_x; i++)
         {
             unsigned index = (start_x + i) + (start_y + j) * this->width;
-            sprite.x = this->layer0[index] * 24;
-            draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
+            tset.render_tile(this->layer0[index], offset_x + i * 24, offset_y + j * 24, time_render);
 
             unsigned tile_over = this->layer1[index];
             // layer1 : Over-layer
             if (tile_over != 0)
             {
-                sprite.x = tile_over * 24;
-                draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
+                //sprite.x = tile_over * 24;
+                tset.render_tile(tile_over, offset_x + i * 24, offset_y + j * 24, time_render);
             }
         }
     }

+ 49 - 0
src/Tileset.cpp

@@ -1 +1,50 @@
 #include "Tileset.h"
+#include "Graphics.h"
+
+#define TILESET WalrusRPG::Tileset
+#define FRAME WalrusRPG::Frame
+
+namespace
+{
+    unsigned find_frame(const std::vector<WalrusRPG::Frame> &anim, signed frame_time)
+    {
+        unsigned index = 0;
+        do
+        {
+            frame_time -= anim[index].duration;
+            index = (index + 1) % anim.size();
+        } while (frame_time > 0);
+        return index;
+    }
+}
+
+TILESET::Tileset(unsigned short *sheet, unsigned sheet_width, unsigned sheet_height, unsigned tile_width, unsigned tile_height)
+    : sheet(sheet), sheet_width(sheet_width), sheet_height(sheet_height), tile_width(tile_width), tile_height(tile_height)
+{
+    animations[2].push_back({2, 10}); // Testing
+    animations[2].push_back({3, 10}); // Testing
+}
+
+void TILESET::render_tile(unsigned int index, unsigned x, unsigned y) const
+{
+    unsigned num_tiles_x = sheet_width / tile_width;
+    //unsigned num_tiles_y = sheet_height / tile_height;
+
+    WalrusRPG::Graphics::Rect_t sprite;
+    sprite.w = tile_width;
+    sprite.h = tile_height;
+    sprite.x = tile_width * (index % num_tiles_x);
+    sprite.y = tile_height * (index / num_tiles_x);
+    draw_sprite_sheet(sheet, x, y, &sprite);
+}
+
+void TILESET::render_tile(unsigned int index, unsigned x, unsigned y, unsigned time) const
+{
+    if (animations.count(index))
+    {
+        render_tile(animations.at(index).at(find_frame(this->animations.at(index), time)).frame, x, y);
+        // If an animation already exists.
+    }
+    else
+        render_tile(index, x, y);
+}

+ 1 - 1
src/main.cpp

@@ -48,7 +48,7 @@ void map_loop(unsigned x, unsigned y, Map &map)
             Pixel pix(Green);
             // TODO?: Preset color macros/consts?
             buffer_fill(pix);
-            map.render(camera, loop_next);
+            map.render(camera, 1);
             print_string(
                 "WalrusRPG test build \001", 0, 0);