Просмотр исходного кода

Refactor to split Tileset into more SOLID-like classes.

Florian DORMONT лет назад: 10
Родитель
Сommit
0b3cd4d8d1
13 измененных файлов с 196 добавлено и 106 удалено
  1. 35 0
      include/Animator.h
  2. 3 3
      include/Entity.h
  3. 4 2
      include/Map.h
  4. 16 0
      include/Renderer.h
  5. 21 0
      include/SpriteRenderer.h
  6. 26 0
      include/TileRenderer.h
  7. 0 33
      include/Tileset.h
  8. 40 0
      src/Animator.cpp
  9. 5 2
      src/Entity.cpp
  10. 11 6
      src/Map.cpp
  11. 33 0
      src/TileRenderer.cpp
  12. 0 59
      src/Tileset.cpp
  13. 2 1
      src/main.cpp

+ 35 - 0
include/Animator.h

@@ -0,0 +1,35 @@
+#ifndef INCLUDE_ANIMATOR_H
+#define INCLUDE_ANIMATOR_H
+
+#include <TINYSTL/vector.h>
+#include <TINYSTL/unordered_map.h>
+#include "Rect.h"
+
+namespace WalrusRPG
+{
+    struct Frame
+    {
+        unsigned frame;
+        unsigned duration; // 1f = 1s
+    };
+    struct Animation
+    {
+        tinystl::vector<WalrusRPG::Frame> stripe;
+        bool looping;
+    };
+
+    class Animator
+    {
+      protected:
+        unsigned elapsed_time;
+        tinystl::unordered_map<unsigned, Animation> animations;
+
+      public:
+        Animator();
+        void add_animation(int index, Animation anim);
+        void update(unsigned dt);
+        unsigned get_animation_frame(unsigned id);
+    };
+}
+
+#endif

+ 3 - 3
include/Entity.h

@@ -3,7 +3,7 @@
 
 #include "Rect.h"
 #include "Camera.h"
-#include "Tileset.h"
+#include "TileRenderer.h"
 
 namespace WalrusRPG
 {
@@ -16,11 +16,11 @@ namespace WalrusRPG
     {
       protected:
         WalrusRPG::Utils::Rect coords;
-        WalrusRPG::Tileset *tset;
+        WalrusRPG::TileRenderer *tset;
         unsigned sprite_id;
 
       public:
-        Entity(int x, int y, unsigned w, unsigned h, WalrusRPG::Tileset *tset, unsigned sprite_id);
+        Entity(int x, int y, unsigned w, unsigned h, WalrusRPG::TileRenderer *tset, unsigned sprite_id);
         ~Entity();
         void render(Camera &camera, unsigned dt) const;
         void update(unsigned dt);

+ 4 - 2
include/Map.h

@@ -3,7 +3,8 @@
 
 #include "Camera.h"
 #include "Entity.h"
-#include "Tileset.h"
+#include "TileRenderer.h"
+#include "Animator.h"
 
 namespace WalrusRPG
 {
@@ -16,7 +17,8 @@ namespace WalrusRPG
         unsigned int height;
         unsigned *layer0;
         unsigned *layer1;
-        Tileset tset;
+        TileRenderer *renderer;
+        Animator anim;
         unsigned time_render;
         // TODO?: add a boolean/getter to know if a second layer exist?
       public:

+ 16 - 0
include/Renderer.h

@@ -0,0 +1,16 @@
+#ifndef INCLUDE_RENDERER_H
+#define INCLUDE_RENDERER_H
+
+#include "Rect.h"
+
+namespace WalrusRPG
+{
+    class Renderer
+    {
+      public:
+        virtual ~Renderer(){};
+        virtual void render(const unsigned id, const WalrusRPG::Utils::Rect &rect) const = 0;
+    };
+}
+
+#endif

+ 21 - 0
include/SpriteRenderer.h

@@ -0,0 +1,21 @@
+#ifndef INCLUDE_SPRITERENDERER_H
+#define INCLUDE_SPRITERENDERER_H
+
+#include <TINYSTL/unordered_map.h>
+#include "Rect.h"
+#include "Renderer.h"
+
+namespace WalrusRPG
+{
+    class SpriteRenderer : public Renderer
+    {
+      protected:
+        tinystl::unordered_map<unsigned, WalrusRPG::Uils::Rect> sprites;
+
+      public:
+        SpriteRenderer(/*char *tilesheet*/);
+        virtual void render(const unsigned id, const WalrusRPG::Utils::Rect &rect) const throw;
+    }
+}
+
+#endif

+ 26 - 0
include/TileRenderer.h

@@ -0,0 +1,26 @@
+#ifndef INCLUDE_TILERENDERER_H
+#define INCLUDE_TILERENDERER_H
+
+#include "Rect.h"
+#include "Renderer.h"
+
+namespace WalrusRPG
+{
+    class TileRenderer : public Renderer
+    {
+      protected:
+        unsigned short *tilesheet;
+        unsigned tile_width;
+        unsigned tile_height;
+
+      public:
+        TileRenderer(unsigned short *tilesheet, unsigned tile_width, unsigned tile_height);
+        void render(const unsigned id, const WalrusRPG::Utils::Rect &rect) const;
+
+        int get_tile_width() const;
+        int get_tile_height() const;
+        ~TileRenderer();
+    };
+}
+
+#endif

+ 0 - 33
include/Tileset.h

@@ -1,33 +0,0 @@
-#ifndef INCLUDE_TILESET_H
-#define INCLUDE_TILESET_H
-
-#include <TINYSTL/vector.h>
-#include <TINYSTL/unordered_map.h>
-
-namespace WalrusRPG
-{
-    struct Frame
-    {
-        unsigned frame;
-        unsigned duration;
-    };
-    class Tileset
-    {
-      protected:
-        const unsigned short *sheet;
-        unsigned tile_width;
-        unsigned tile_height;
-        tinystl::unordered_map<unsigned, tinystl::vector<Frame>> animations;
-
-      public:
-        Tileset(unsigned short *sheet, unsigned tile_width, unsigned tile_heihgt);
-        void add_animation(int index, tinystl::vector<WalrusRPG::Frame> anim);
-        void render_tile(unsigned int index, unsigned x, unsigned y) const;
-        void render_tile(unsigned int index, unsigned x, unsigned y, unsigned time);
-
-        int get_tile_width() const;
-        int get_tile_height() const;
-    };
-}
-
-#endif

+ 40 - 0
src/Animator.cpp

@@ -0,0 +1,40 @@
+#include "Animator.h"
+#include "Graphics.h"
+#include "Rect.h"
+
+#define ANIMATOR WalrusRPG::Animator
+#define FRAME WalrusRPG::Frame
+#define UTILS WalrusRPG::Utils
+
+namespace
+{
+    unsigned find_frame(const tinystl::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;
+    }
+}
+
+ANIMATOR::Animator()
+{
+}
+
+void ANIMATOR::add_animation(int index, Animation anim)
+{
+    animations[index] = anim;
+}
+
+unsigned ANIMATOR::get_animation_frame(unsigned id)
+{
+    return find_frame(animations[id].stripe, elapsed_time);
+}
+
+void ANIMATOR::update(unsigned dt)
+{
+    elapsed_time += dt;
+}

+ 5 - 2
src/Entity.cpp

@@ -1,10 +1,12 @@
 #include "Entity.h"
 #include "misc.h"
+#include "Rect.h"
 #include "Text.h"
 
 #define ENTITY WalrusRPG::Entity
+#define RECT WalrusRPG::Utils::Rect
 
-ENTITY::Entity(int x, int y, unsigned w, unsigned h, WalrusRPG::Tileset *tset, unsigned sprite_id)
+ENTITY::Entity(int x, int y, unsigned w, unsigned h, WalrusRPG::TileRenderer *tset, unsigned sprite_id)
     : coords(x, y, w, h), tset(tset), sprite_id(sprite_id)
 {
 }
@@ -18,7 +20,8 @@ void ENTITY::render(Camera &camera, unsigned dt) const
 {
     if (camera.is_visible(coords))
     {
-        (*tset).render_tile(sprite_id, coords.x - camera.get_x(), coords.y - camera.get_y(), dt);
+        tset->render(sprite_id, RECT(coords.x - camera.get_x(), coords.y - camera.get_y()));
+        //(*tset).render_tile(sprite_id, coords.x - camera.get_x(), coords.y - camera.get_y(), dt);
     }
 }
 

+ 11 - 6
src/Map.cpp

@@ -2,13 +2,18 @@
 #include "Camera.h"
 #include "Graphics.h"
 #include "sprites.h"
+#include "Rect.h"
+#include "TileRenderer.h"
 #include "misc.h"
 
 #define MAP WalrusRPG::Map
+#define RECT WalrusRPG::Utils::Rect
+#define TILERENDERER WalrusRPG::TileRenderer
 
 MAP::Map(int width, int height, unsigned *layer0, unsigned *layer1)
-    : tset(overworld, 16, 16), time_render(0)
+    : anim(), time_render(0)
 {
+    this->renderer = new TileRenderer(overworld, 16, 16);
     this->width = width;
     this->height = height;
     this->layer0 = layer0;
@@ -29,8 +34,8 @@ void MAP::update(unsigned dt)
 void MAP::render(WalrusRPG::Camera &camera, unsigned dt)
 {
     time_render += dt;
-    signed t_width = tset.get_tile_width();
-    signed t_height = tset.get_tile_height();
+    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.
     // 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() % t_width * -1;
@@ -77,13 +82,13 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt)
             unsigned index = (start_x + i) + (start_y + j) * this->width;
             unsigned tile_over = this->layer0[index];
             if (tile_over != 0)
-                tset.render_tile(this->layer0[index], offset_x + i * t_width, offset_y + j * t_height, time_render);
+                renderer->render(this->layer0[index], RECT(offset_x + i * t_width, offset_y + j * t_height));
             // layer1 : Over-layer
             if (this->layer1 == NULL)
                 continue;
             tile_over = this->layer1[index];
             if (tile_over != 0)
-                tset.render_tile(tile_over, offset_x + i * t_width, offset_y + j * t_height, time_render);
+                renderer->render(tile_over, RECT(offset_x + i * t_width, offset_y + j * t_height));
         }
     }
 }
@@ -97,7 +102,7 @@ bool MAP::is_tile_solid(unsigned x, unsigned y) const
 
 bool MAP::is_pixel_solid(unsigned x, unsigned y) const
 {
-    return is_tile_solid(x / tset.get_tile_width(), y / tset.get_tile_height());
+    return is_tile_solid(x / renderer->get_tile_width(), y / renderer->get_tile_height());
 }
 
 unsigned MAP::get_width() const

+ 33 - 0
src/TileRenderer.cpp

@@ -0,0 +1,33 @@
+#include "TileRenderer.h"
+#include "Graphics.h"
+#include "Rect.h"
+
+#define GRAPHICS WalrusRPG::Graphics
+#define TILERENDERER WalrusRPG::TileRenderer
+#define RECT WalrusRPG::Utils::Rect
+
+TILERENDERER::TileRenderer(unsigned short *_tilesheet, unsigned tile_width, unsigned tile_height)
+    : tilesheet(_tilesheet), tile_width(tile_width), tile_height(tile_height)
+{
+}
+
+void TILERENDERER::render(const unsigned id, const RECT &rect) const
+{
+    unsigned num_tiles_x = tilesheet[0] / tile_width;
+    //unsigned num_tiles_y = sheet_height / tile_height;
+    GRAPHICS::draw_sprite_sheet(tilesheet, rect.x, rect.y, RECT(tile_width * (id % num_tiles_x), tile_height * (id / num_tiles_x), tile_width, tile_height));
+}
+
+int TILERENDERER::get_tile_width() const
+{
+    return tile_width;
+}
+int TILERENDERER::get_tile_height() const
+{
+    return tile_height;
+}
+
+
+TILERENDERER::~TileRenderer()
+{
+}

+ 0 - 59
src/Tileset.cpp

@@ -1,59 +0,0 @@
-#include "Tileset.h"
-#include "Graphics.h"
-#include "Rect.h"
-
-#define GRAPHICS WalrusRPG::Graphics
-#define TILESET WalrusRPG::Tileset
-#define FRAME WalrusRPG::Frame
-#define UTILS WalrusRPG::Utils
-
-namespace
-{
-    unsigned find_frame(const tinystl::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 tile_width, unsigned tile_height)
-    : sheet(sheet), tile_width(tile_width), tile_height(tile_height)
-{
-}
-
-void TILESET::add_animation(int index, tinystl::vector<WalrusRPG::Frame> anim)
-{
-    animations[index] = anim;
-}
-
-void TILESET::render_tile(unsigned int index, unsigned x, unsigned y) const
-{
-    unsigned num_tiles_x = sheet[0] / tile_width;
-    //unsigned num_tiles_y = sheet_height / tile_height;
-    GRAPHICS::draw_sprite_sheet(sheet, x, y, UTILS::Rect(tile_width * (index % num_tiles_x), tile_height * (index / num_tiles_x), tile_width, tile_height));
-}
-
-void TILESET::render_tile(unsigned int index, unsigned x, unsigned y, unsigned time)
-{
-    if (!animations[index].empty())
-    {
-        render_tile(animations[index][find_frame(this->animations[index], time)].frame, x, y);
-        // If an animation already exists.
-    }
-    else
-        render_tile(index, x, y);
-}
-
-int TILESET::get_tile_width() const
-{
-    return tile_width;
-}
-int TILESET::get_tile_height() const
-{
-    return tile_height;
-}

+ 2 - 1
src/main.cpp

@@ -36,7 +36,8 @@ void map_loop(unsigned x, unsigned y, Map &map)
     unsigned keep_running = 1;
     Camera camera((signed) x, (signed) y);
 
-    Tileset asdf(better_character, 9, 16);
+    //Tileset asdf(better_character, 9, 16);
+    TileRenderer asdf(better_character, 9, 16);
     Entity test_char(115, 90, 9, 16, &asdf, 0);
 
     while (keep_running)