Ver código fonte

Added another example of tileset/map using and made map tile size independant

Florian DORMONT 10 anos atrás
pai
commit
b902ea086e
5 arquivos alterados com 73 adições e 14 exclusões
  1. BIN
      art/overworld.png
  2. 3 0
      include/Tileset.h
  3. 16 13
      src/Map.cpp
  4. 9 0
      src/Tileset.cpp
  5. 45 1
      src/main.cpp

BIN
art/overworld.png


+ 3 - 0
include/Tileset.h

@@ -26,6 +26,9 @@ namespace WalrusRPG
         void add_animation(int index, std::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) const;
+
+        int get_tile_width() const;
+        int get_tile_height() const;
     };
 }
 

+ 16 - 13
src/Map.cpp

@@ -7,13 +7,12 @@
 #define MAP WalrusRPG::Map
 
 MAP::Map(int width, int height, unsigned *layer0, unsigned *layer1)
-    : tset(tiles, 192u, 24u, 24u, 24u), time_render(0)
+    : tset(overworld, 336, 144, 16, 16), time_render(0)
 {
     this->width = width;
     this->height = height;
     this->layer0 = layer0;
     this->layer1 = layer1;
-    tset.add_animation(2, {{2, 20}, {3, 10}});
 }
 
 MAP::~Map()
@@ -30,26 +29,28 @@ 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();
     // 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;
-    signed offset_y = camera.get_y() % 24 * -1;
-    signed start_x = camera.get_x() / 24;
-    signed start_y = camera.get_y() / 24;
-    signed end_x = start_x + 15;
-    signed end_y = start_y + 16;
+    signed offset_x = camera.get_x() % t_width * -1;
+    signed offset_y = camera.get_y() % t_height * -1;
+    signed start_x = camera.get_x() / t_width;
+    signed start_y = camera.get_y() / t_height;
+    signed end_x = start_x + 320 / t_width + 2; // Why 2? I don't freaking know.
+    signed end_y = start_y + 240 / t_height + 1;
 
     // Bound-checking code. To avoid reading outside of the map.
     // The offset edition allows the map to be correctly moved to the right to make up for the lack of renderable tiles.
     if (start_x < 0)
     {
-        offset_x -= start_x * 24;
+        offset_x -= start_x * t_width;
         start_x = 0;
     }
 
     if (start_y < 0)
     {
-        offset_y -= start_y * 24;
+        offset_y -= start_y * t_height;
         start_y = 0;
     }
     // warning fix. Even if end_x/y is negative, it should be higher than width/height.
@@ -76,11 +77,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 * 24, offset_y + j * 24, time_render);
+                tset.render_tile(this->layer0[index], offset_x + i * t_width, offset_y + j * t_height, time_render);
             // 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 * 24, offset_y + j * 24, time_render);
+                tset.render_tile(tile_over, offset_x + i * t_width, offset_y + j * t_height, time_render);
         }
     }
 }
@@ -94,7 +97,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 / 24, y / 24);
+    return is_tile_solid(x / tset.get_tile_width(), y / tset.get_tile_height());
 }
 
 unsigned MAP::get_width() const

+ 9 - 0
src/Tileset.cpp

@@ -51,3 +51,12 @@ void TILESET::render_tile(unsigned int index, unsigned x, unsigned y, unsigned t
     else
         render_tile(index, x, y);
 }
+
+int TILESET::get_tile_width() const
+{
+    return tile_width;
+}
+int TILESET::get_tile_height() const
+{
+    return tile_height;
+}

+ 45 - 1
src/main.cpp

@@ -73,6 +73,50 @@ int main(int argc, char *argv[])
     buffer_allocate();
     timer_init(0);
 
+    unsigned dungeonTest[] = {
+        21, 21, 1, 1, 1, 1, 21, 22, 21, 22, 21, 22, 21, 21, 1, 22, 21, 1, 22, 22,
+        22, 1, 21, 2, 3, 3, 3, 3, 3, 4, 21, 1, 22, 21, 22, 22, 21, 21, 21, 1,
+        22, 22, 22, 23, 108, 109, 109, 109, 24, 87, 4, 21, 21, 22, 5, 6, 6, 7, 1, 1,
+        22, 21, 21, 23, 66, 67, 108, 109, 24, 109, 25, 21, 22, 5, 132, 43, 43, 28, 1, 1,
+        21, 22, 1, 23, 25, 23, 109, 109, 108, 108, 25, 1, 1, 26, 42, 110, 48, 49, 22, 21,
+        1, 21, 21, 23, 87, 88, 109, 24, 109, 109, 25, 1, 1, 26, 43, 131, 6, 7, 21, 1,
+        22, 1, 21, 44, 67, 109, 24, 24, 24, 66, 46, 1, 22, 26, 27, 43, 42, 131, 7, 21,
+        22, 1, 22, 21, 44, 45, 45, 45, 45, 46, 1, 22, 1, 26, 27, 27, 43, 27, 28, 22,
+        21, 22, 21, 1, 21, 1, 22, 22, 21, 1, 21, 22, 1, 47, 48, 111, 42, 27, 28, 21,
+        21, 1, 21, 21, 22, 2, 3, 3, 4, 1, 2, 3, 4, 1, 5, 132, 27, 27, 28, 1,
+        1, 22, 1, 22, 21, 23, 24, 66, 46, 1, 23, 24, 25, 1, 26, 42, 42, 110, 49, 21,
+        22, 21, 22, 22, 2, 88, 24, 25, 2, 3, 88, 24, 87, 4, 26, 43, 110, 49, 21, 21,
+        1, 1, 1, 2, 88, 24, 24, 87, 88, 108, 24, 24, 109, 25, 47, 48, 49, 1, 22, 22,
+        21, 1, 21, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 87, 4, 21, 21, 22, 22, 22,
+        22, 1, 21, 23, 24, 24, 109, 24, 24, 24, 24, 24, 108, 109, 25, 21, 21, 22, 22, 22,
+        22, 21, 21, 44, 45, 45, 67, 24, 24, 24, 66, 45, 45, 45, 46, 1, 22, 1, 22, 22,
+        22, 21, 22, 22, 22, 1, 44, 67, 108, 108, 25, 22, 22, 1, 22, 21, 22, 21, 21, 1,
+        21, 22, 1, 22, 22, 1, 22, 44, 45, 45, 46, 1, 1, 1, 1, 21, 21, 21, 21, 21,
+        21, 22, 21, 21, 21, 1, 21, 1, 22, 22, 22, 1, 21, 22, 21, 1, 1, 22, 21, 1,
+        1, 21, 1, 1, 21, 21, 21, 1, 22, 22, 1, 21, 22, 21, 22, 1, 22, 21, 21, 21,
+    };
+    unsigned dungeonTest2[] = {
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 52, 53, 54, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 73, 74, 75, 76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 157, 158, 140, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 178, 179, 161, 181, 0, 12, 14, 12, 14, 162, 163, 164, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 184, 185, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 166, 0, 0, 186, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 145, 167, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 133, 134, 134, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 154, 155, 155, 71, 135, 39, 40, 41, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 154, 155, 155, 155, 156, 60, 61, 62, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 175, 176, 51, 155, 156, 81, 82, 83, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 175, 176, 177, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+    };
 
     unsigned mapdata0[] =
         {
@@ -122,7 +166,7 @@ int main(int argc, char *argv[])
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
-    Map map(15, 21, mapdata0, mapdata1);
+    Map map(20, 20, dungeonTest, dungeonTest2);
 
 
     map_loop(0, 0, map);