Skip to content

Commit

Permalink
Extract tile data recreation method
Browse files Browse the repository at this point in the history
  • Loading branch information
Primekick committed Nov 27, 2024
1 parent b41a0cc commit 9b1f154
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/tilemap_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ void TilemapLayer::CreateTileCacheAt(int x, int y, int tile_id) {
GetDataCache(x, y) = tile;
}

void TilemapLayer::RecreateTileDataAt(int x, int y, int tile_id) {
map_data[x + y * width] = static_cast<short>(tile_id);
Game_Map::ReplaceTileAt(x, y, tile_id, layer);
CreateTileCacheAt(x, y, tile_id);
}

void TilemapLayer::GenerateAutotileAB(short ID, short animID) {
// Calculate the block to use
// 1: A1 + Upper B (Grass + Coast)
Expand Down Expand Up @@ -716,16 +722,18 @@ void TilemapLayer::SetMapData(std::vector<short> nmap_data) {
map_data = std::move(nmap_data);
}

static inline bool IsAutotileAB(int tile_id) {
return tile_id >= BLOCK_A && tile_id < BLOCK_C;
}

void TilemapLayer::SetMapTileDataAt(int x, int y, int tile_id, bool disable_autotile) {
if(!IsInMapBounds(x, y))
return;

substitutions = Game_Map::GetTilesLayer(layer);

if (disable_autotile) {
map_data[x + y * width] = static_cast<short>(tile_id);
Game_Map::ReplaceTileAt(x, y, tile_id, layer);
CreateTileCacheAt(x, y, tile_id);
RecreateTileDataAt(x, y, tile_id);
} else {
// Recalculate the replaced tile itself + every neighboring tile
static constexpr struct { int dx; int dy; } adjacent[8] = {
Expand All @@ -747,10 +755,6 @@ void TilemapLayer::SetMapTileDataAt(int x, int y, int tile_id, bool disable_auto
SetMapData(map_data);
}

static inline bool IsAutotileAB(int tile_id) {
return tile_id >= BLOCK_A && tile_id < BLOCK_C;
}

static inline bool IsAutotileD(int tile_id) {
return tile_id >= BLOCK_D && tile_id < BLOCK_E;
}
Expand Down Expand Up @@ -815,9 +819,7 @@ void TilemapLayer::RecalculateAutotile(int x, int y, int tile_id) {

// Recalculate tile id using the neighbors -> variant map
const int new_tile_id = BLOCK_D + block * BLOCK_D_STRIDE + AUTOTILE_D_VARIANTS_MAP.at(neighbors);
map_data[x + y * width] = static_cast<short>(new_tile_id);
Game_Map::ReplaceTileAt(x, y, new_tile_id, layer);
CreateTileCacheAt(x, y, tile_id);
RecreateTileDataAt(x, y, new_tile_id);
}

void TilemapLayer::SetPassable(std::vector<unsigned char> npassable) {
Expand Down
1 change: 1 addition & 0 deletions src/tilemap_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class TilemapLayer {

void CreateTileCache(const std::vector<short>& nmap_data);
void CreateTileCacheAt(int x, int y, int tile_id);
void RecreateTileDataAt(int x, int y, int tile_id);
void GenerateAutotileAB(short ID, short animID);
void GenerateAutotileD(short ID);
void DrawTile(Bitmap& dst, Bitmap& tile, Bitmap& tone_tile, int x, int y, int row, int col, uint32_t tone_hash, bool allow_fast_blit = true);
Expand Down

0 comments on commit 9b1f154

Please sign in to comment.