From 102f7b95e0721c32ba7909b22d82908d1563189e Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 29 Apr 2025 00:09:32 -0400 Subject: [PATCH] Add Tilemap functionality with creation, updating, and rendering capabilities; implement data retrieval for individual tiles, enhancing graphics management in the application. --- src/app/gfx/tilemap.cc | 61 ++++++++++++++++++++++++++++++++++++++++++ src/app/gfx/tilemap.h | 34 +++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 src/app/gfx/tilemap.cc create mode 100644 src/app/gfx/tilemap.h diff --git a/src/app/gfx/tilemap.cc b/src/app/gfx/tilemap.cc new file mode 100644 index 00000000..8d6798b3 --- /dev/null +++ b/src/app/gfx/tilemap.cc @@ -0,0 +1,61 @@ +#include "app/gfx/tilemap.h" + +#include + +#include "app/core/platform/renderer.h" +#include "app/gfx/bitmap.h" + +namespace yaze { +namespace gfx { + +Tilemap CreateTilemap(std::vector &data, int width, int height, + int tile_size, int num_tiles, SnesPalette &palette) { + Tilemap tilemap; + tilemap.tile_size.x = tile_size; + tilemap.tile_size.y = tile_size; + tilemap.map_size.x = num_tiles; + tilemap.map_size.y = num_tiles; + tilemap.atlas = Bitmap(width, height, 8, data); + tilemap.atlas.SetPalette(palette); + core::Renderer::GetInstance().RenderBitmap(&tilemap.atlas); + return tilemap; +} + +void UpdateTilemap(Tilemap &tilemap, const std::vector &data) { + tilemap.atlas.set_data(data); + core::Renderer::GetInstance().UpdateBitmap(&tilemap.atlas); +} + +void RenderTile(Tilemap &tilemap, int tile_id) { + if (tilemap.tile_bitmaps.find(tile_id) == tilemap.tile_bitmaps.end()) { + tilemap.tile_bitmaps[tile_id] = + Bitmap(tilemap.tile_size.x, tilemap.tile_size.y, 8, + GetTilemapData(tilemap, tile_id)); + auto bitmap_ptr = &tilemap.tile_bitmaps[tile_id]; + core::Renderer::GetInstance().RenderBitmap(bitmap_ptr); + } +} + +std::vector GetTilemapData(Tilemap &tilemap, int tile_id) { + int tile_size = tilemap.tile_size.x; + std::vector data(tile_size * tile_size); + + int num_tiles = tilemap.map_size.x; + int index = tile_id * tile_size * tile_size; + int width = tilemap.atlas.width(); + + for (int ty = 0; ty < tile_size; ty++) { + for (int tx = 0; tx < tile_size; tx++) { + uint8_t value = + tilemap.atlas + .vector()[(tile_id % 8 * tile_size) + + (tile_id / 8 * tile_size * width) + ty * width + tx]; + data[ty * tile_size + tx] = value; + } + } + + return data; +} + +} // namespace gfx +} // namespace yaze diff --git a/src/app/gfx/tilemap.h b/src/app/gfx/tilemap.h new file mode 100644 index 00000000..52cfa134 --- /dev/null +++ b/src/app/gfx/tilemap.h @@ -0,0 +1,34 @@ +#ifndef YAZE_GFX_TILEMAP_H +#define YAZE_GFX_TILEMAP_H + +#include "app/gfx/bitmap.h" +#include "absl/container/flat_hash_map.h" + +namespace yaze { +namespace gfx { + +struct Pair { + int x; + int y; +}; + +struct Tilemap { + Bitmap atlas; + absl::flat_hash_map tile_bitmaps; + Pair tile_size; + Pair map_size; +}; + +Tilemap CreateTilemap(std::vector &data, int width, int height, + int tile_size, int num_tiles, SnesPalette &palette); + +void UpdateTilemap(Tilemap &tilemap, const std::vector &data); + +void RenderTile(Tilemap &tilemap, int tile_id); + +std::vector GetTilemapData(Tilemap &tilemap, int tile_id); + +} // namespace gfx +} // namespace yaze + +#endif // YAZE_GFX_TILEMAP_H