From 60f101f2e6db29f6898800e9260d21781e346980 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Sun, 30 Nov 2014 17:46:28 +0100 Subject: [PATCH] Tilemap(VX): Factor out common code into tilemap-common.h Renamed flashmap.h to tilemap-common.h as it already contained shared functions. --- CMakeLists.txt | 2 +- mkxp.pro | 2 +- src/autotiles.cpp | 108 +++++++++++++-------------- src/tileatlasvx.cpp | 45 +---------- src/{flashmap.h => tilemap-common.h} | 60 ++++++++++++--- src/tilemap.cpp | 23 ++---- src/tilemapvx.cpp | 2 +- 7 files changed, 114 insertions(+), 128 deletions(-) rename src/{flashmap.h => tilemap-common.h} (80%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b78d95..e5cd77c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,7 +124,7 @@ set(MAIN_HEADERS src/glstate.h src/quad.h src/tilemap.h - src/flashmap.h + src/tilemap-common.h src/graphics.h src/gl-debug.h src/global-ibo.h diff --git a/mkxp.pro b/mkxp.pro index c9a1bda..f21f264 100644 --- a/mkxp.pro +++ b/mkxp.pro @@ -103,7 +103,7 @@ HEADERS += \ src/glstate.h \ src/quad.h \ src/tilemap.h \ - src/flashmap.h \ + src/tilemap-common.h \ src/graphics.h \ src/gl-debug.h \ src/global-ibo.h \ diff --git a/src/autotiles.cpp b/src/autotiles.cpp index 3a740d0..fee11c6 100644 --- a/src/autotiles.cpp +++ b/src/autotiles.cpp @@ -4,196 +4,196 @@ extern const StaticRect autotileRects[] = { { 32.5, 64.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 32.5, 80.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 48.5, 80.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 48.5, 80.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 48.5, 64.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 32.5, 64.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 0.5, 64.5, 15, 15 }, { 16.5, 64.5, 15, 15 }, - { 16.5, 80.5, 15, 15 }, { 0.5, 80.5, 15, 15 }, + { 16.5, 80.5, 15, 15 }, { 0.5, 64.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 16.5, 80.5, 15, 15 }, { 0.5, 80.5, 15, 15 }, + { 16.5, 80.5, 15, 15 }, { 0.5, 64.5, 15, 15 }, { 16.5, 64.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 0.5, 80.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 0.5, 64.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 0.5, 80.5, 15, 15 }, - { 32.5, 32.5, 15, 15 }, - { 48.5, 32.5, 15, 15 }, - { 48.5, 48.5, 15, 15 }, - { 32.5, 48.5, 15, 15 }, - { 32.5, 32.5, 15, 15 }, - { 48.5, 32.5, 15, 15 }, { 80.5, 16.5, 15, 15 }, + { 32.5, 32.5, 15, 15 }, + { 48.5, 32.5, 15, 15 }, { 32.5, 48.5, 15, 15 }, - { 32.5, 32.5, 15, 15 }, - { 48.5, 32.5, 15, 15 }, { 48.5, 48.5, 15, 15 }, - { 64.5, 16.5, 15, 15 }, { 32.5, 32.5, 15, 15 }, { 48.5, 32.5, 15, 15 }, + { 32.5, 48.5, 15, 15 }, { 80.5, 16.5, 15, 15 }, + { 32.5, 32.5, 15, 15 }, + { 48.5, 32.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 48.5, 48.5, 15, 15 }, + { 32.5, 32.5, 15, 15 }, + { 48.5, 32.5, 15, 15 }, + { 64.5, 16.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 64.5, 64.5, 15, 15 }, { 80.5, 64.5, 15, 15 }, - { 80.5, 80.5, 15, 15 }, { 64.5, 80.5, 15, 15 }, + { 80.5, 80.5, 15, 15 }, { 64.5, 64.5, 15, 15 }, { 80.5, 64.5, 15, 15 }, - { 80.5, 80.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 80.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 64.5, 15, 15 }, - { 80.5, 80.5, 15, 15 }, { 64.5, 80.5, 15, 15 }, + { 80.5, 80.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 64.5, 15, 15 }, - { 80.5, 80.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 80.5, 15, 15 }, { 32.5, 96.5, 15, 15 }, { 48.5, 96.5, 15, 15 }, - { 48.5, 112.5, 15, 15 }, { 32.5, 112.5, 15, 15 }, + { 48.5, 112.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 48.5, 96.5, 15, 15 }, - { 48.5, 112.5, 15, 15 }, { 32.5, 112.5, 15, 15 }, + { 48.5, 112.5, 15, 15 }, { 32.5, 96.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 48.5, 112.5, 15, 15 }, { 32.5, 112.5, 15, 15 }, + { 48.5, 112.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 48.5, 112.5, 15, 15 }, { 32.5, 112.5, 15, 15 }, + { 48.5, 112.5, 15, 15 }, { 0.5, 64.5, 15, 15 }, { 80.5, 64.5, 15, 15 }, - { 80.5, 80.5, 15, 15 }, { 0.5, 80.5, 15, 15 }, + { 80.5, 80.5, 15, 15 }, { 32.5, 32.5, 15, 15 }, { 48.5, 32.5, 15, 15 }, - { 48.5, 112.5, 15, 15 }, { 32.5, 112.5, 15, 15 }, + { 48.5, 112.5, 15, 15 }, { 0.5, 32.5, 15, 15 }, { 16.5, 32.5, 15, 15 }, + { 0.5, 48.5, 15, 15 }, { 16.5, 48.5, 15, 15 }, - { 0.5, 48.5, 15, 15 }, { 0.5, 32.5, 15, 15 }, { 16.5, 32.5, 15, 15 }, - { 80.5, 16.5, 15, 15 }, { 0.5, 48.5, 15, 15 }, + { 80.5, 16.5, 15, 15 }, { 64.5, 32.5, 15, 15 }, { 80.5, 32.5, 15, 15 }, - { 80.5, 48.5, 15, 15 }, { 64.5, 48.5, 15, 15 }, + { 80.5, 48.5, 15, 15 }, { 64.5, 32.5, 15, 15 }, { 80.5, 32.5, 15, 15 }, - { 80.5, 48.5, 15, 15 }, { 64.5, 16.5, 15, 15 }, + { 80.5, 48.5, 15, 15 }, { 64.5, 96.5, 15, 15 }, { 80.5, 96.5, 15, 15 }, - { 80.5, 112.5, 15, 15 }, { 64.5, 112.5, 15, 15 }, + { 80.5, 112.5, 15, 15 }, { 64.5, 0.5, 15, 15 }, { 80.5, 96.5, 15, 15 }, - { 80.5, 112.5, 15, 15 }, { 64.5, 112.5, 15, 15 }, + { 80.5, 112.5, 15, 15 }, { 0.5, 96.5, 15, 15 }, { 16.5, 96.5, 15, 15 }, - { 16.5, 112.5, 15, 15 }, { 0.5, 112.5, 15, 15 }, + { 16.5, 112.5, 15, 15 }, { 0.5, 96.5, 15, 15 }, { 80.5, 0.5, 15, 15 }, - { 16.5, 112.5, 15, 15 }, { 0.5, 112.5, 15, 15 }, + { 16.5, 112.5, 15, 15 }, { 0.5, 32.5, 15, 15 }, { 80.5, 32.5, 15, 15 }, - { 80.5, 48.5, 15, 15 }, { 0.5, 48.5, 15, 15 }, + { 80.5, 48.5, 15, 15 }, { 0.5, 32.5, 15, 15 }, { 16.5, 32.5, 15, 15 }, - { 16.5, 112.5, 15, 15 }, { 0.5, 112.5, 15, 15 }, + { 16.5, 112.5, 15, 15 }, { 0.5, 96.5, 15, 15 }, { 80.5, 96.5, 15, 15 }, - { 80.5, 112.5, 15, 15 }, { 0.5, 112.5, 15, 15 }, + { 80.5, 112.5, 15, 15 }, { 64.5, 32.5, 15, 15 }, { 80.5, 32.5, 15, 15 }, - { 80.5, 112.5, 15, 15 }, { 64.5, 112.5, 15, 15 }, + { 80.5, 112.5, 15, 15 }, { 0.5, 32.5, 15, 15 }, { 80.5, 32.5, 15, 15 }, - { 80.5, 112.5, 15, 15 }, { 0.5, 112.5, 15, 15 }, + { 80.5, 112.5, 15, 15 }, { 0.5, 0.5, 15, 15 }, { 16.5, 0.5, 15, 15 }, - { 16.5, 16.5, 15, 15 }, - { 0.5, 16.5, 15, 15 } + { 0.5, 16.5, 15, 15 }, + { 16.5, 16.5, 15, 15 } }; extern const int autotileRectsN = sizeof(autotileRects) / sizeof(autotileRects[0]); diff --git a/src/tileatlasvx.cpp b/src/tileatlasvx.cpp index 5af8060..8726335 100644 --- a/src/tileatlasvx.cpp +++ b/src/tileatlasvx.cpp @@ -21,6 +21,7 @@ #include "tileatlasvx.h" +#include "tilemap-common.h" #include "bitmap.h" #include "table.h" #include "etc-internal.h" @@ -65,21 +66,6 @@ static elementsN(autotileVXRectsC); namespace TileAtlasVX { -static int -wrap(int value, int range) -{ - int res = value % range; - return res < 0 ? res + range : res; -} - -static int16_t -tableGetWrapped(const Table &t, int x, int y, int z = 0) -{ - return t.at(wrap(x, t.xSize()), - wrap(y, t.ySize()), - z); -} - static int16_t tableGetSafe(const Table *t, int x) { @@ -333,35 +319,6 @@ void build(TEXFBO &tf, Bitmap *bitmaps[BM_COUNT]) #define OVER_PLAYER_FLAG (1 << 4) #define TABLE_FLAG (1 << 7) -static void -atSelectSubPos(FloatRect &pos, int i) -{ - switch (i) - { - case 0: - return; - case 1: - pos.x += 16; - return; - case 2: - pos.y += 16; - return; - case 3: - pos.x += 16; - pos.y += 16; - return; - case 4: - pos.y += 24; - return; - case 5: - pos.x += 16; - pos.y += 24; - return; - default: - assert(!"Unreachable"); - } -} - /* Reference: http://www.tktkgame.com/tkool/memo/vx/tile_id.html */ static void diff --git a/src/flashmap.h b/src/tilemap-common.h similarity index 80% rename from src/flashmap.h rename to src/tilemap-common.h index 2e916ed..45ee91c 100644 --- a/src/flashmap.h +++ b/src/tilemap-common.h @@ -1,5 +1,5 @@ /* -** flashmap.h +** tilemap-common.h ** ** This file is part of mkxp. ** @@ -19,8 +19,8 @@ ** along with mkxp. If not, see . */ -#ifndef FLASHMAP_H -#define FLASHMAP_H +#ifndef TILEMAPCOMMON_H +#define TILEMAPCOMMON_H #include "table.h" #include "gl-util.h" @@ -30,8 +30,11 @@ #include "glstate.h" #include "shader.h" #include "vertex.h" +#include "quad.h" +#include "etc-internal.h" #include +#include #include #include @@ -44,11 +47,50 @@ wrap(int value, int range) } static inline int16_t -tableGetWrapped(const Table *t, int x, int y, int z = 0) +tableGetWrapped(const Table &t, int x, int y, int z = 0) { - return t->get(wrap(x, t->xSize()), - wrap(y, t->ySize()), - z); + return t.get(wrap(x, t.xSize()), + wrap(y, t.ySize()), + z); +} + +enum AtSubPos +{ + TopLeft = 0, + TopRight = 1, + BottomLeft = 2, + BottomRight = 3, + BottomLeftTable = 4, + BottomRightTable = 5 +}; + +static inline void +atSelectSubPos(FloatRect &pos, int i) +{ + switch (i) + { + case TopLeft: + return; + case TopRight: + pos.x += 16; + return; + case BottomLeft: + pos.y += 16; + return; + case BottomRight: + pos.x += 16; + pos.y += 16; + return; + case BottomLeftTable: + pos.y += 24; + return; + case BottomRightTable: + pos.x += 16; + pos.y += 24; + return; + default: + assert(!"Unreachable"); + } } struct FlashMap @@ -144,7 +186,7 @@ private: bool sampleFlashColor(Vec4 &out, int x, int y) const { - int16_t packed = tableGetWrapped(data, x, y); + int16_t packed = tableGetWrapped(*data, x, y); if (packed == 0) return false; @@ -216,4 +258,4 @@ private: std::vector vertices; }; -#endif // FLASHMAP_H +#endif // TILEMAPCOMMON_H diff --git a/src/tilemap.cpp b/src/tilemap.cpp index 2acdb49..3d1ab58 100644 --- a/src/tilemap.cpp +++ b/src/tilemap.cpp @@ -36,7 +36,7 @@ #include "quad.h" #include "vertex.h" #include "tileatlas.h" -#include "flashmap.h" +#include "tilemap-common.h" #include @@ -590,21 +590,6 @@ struct TilemapPrivate return value; } - FloatRect getAutotilePieceRect(int x, int y, /* in pixel coords */ - int corner) - { - switch (corner) - { - case 0 : break; - case 1 : x += 16; break; - case 2 : x += 16; y += 16; break; - case 3 : y += 16; break; - default: abort(); - } - - return FloatRect(x, y, 16, 16); - } - void handleAutotile(int x, int y, int tileInd, SVVector *array) { /* Which autotile [0-7] */ @@ -617,7 +602,9 @@ struct TilemapPrivate /* Iterate over the 4 tile pieces */ for (int i = 0; i < 4; ++i) { - FloatRect posRect = getAutotilePieceRect(x*32, y*32, i); + FloatRect posRect(x*32, y*32, 16, 16); + atSelectSubPos(posRect, i); + FloatRect texRect = pieceRect[i]; /* Adjust to atlas coordinates */ @@ -635,7 +622,7 @@ struct TilemapPrivate void handleTile(int x, int y, int z) { int tileInd = - tableGetWrapped(mapData, x + viewpPos.x, y + viewpPos.y, z); + tableGetWrapped(*mapData, x + viewpPos.x, y + viewpPos.y, z); /* Check for empty space */ if (tileInd < 48) diff --git a/src/tilemapvx.cpp b/src/tilemapvx.cpp index 815f937..a405cb8 100644 --- a/src/tilemapvx.cpp +++ b/src/tilemapvx.cpp @@ -33,7 +33,7 @@ #include "quad.h" #include "quadarray.h" #include "shader.h" -#include "flashmap.h" +#include "tilemap-common.h" #include #include