15 int width,
int height,
int tile_size,
int num_tiles,
35 const std::vector<uint8_t>& data) {
65 if (tile_data.empty()) {
86 if (tiles_per_row <= 0) {
90 int tile_x = (tile_id % tiles_per_row) * tilemap.
tile_size.
x;
91 int tile_y = (tile_id / tiles_per_row) * tilemap.
tile_size.
y;
94 if (tile_x < 0 || tile_x >= tilemap.
atlas.
width() || tile_y < 0 ||
103 if (tile_data.empty()) {
119 int tile_x = (tile_id % tiles_per_row) * tilemap.
tile_size.
x;
120 int tile_y = (tile_id / tiles_per_row) * tilemap.
tile_size.
y;
123 int tile_data_offset = 0;
139 const std::vector<uint8_t>& data,
int tile_id,
int sheet_offset) {
140 const int tile_width = 8;
141 const int tile_height = 8;
142 const int buffer_width = 128;
143 const int sheet_height = 32;
145 const int tiles_per_row = buffer_width / tile_width;
146 const int rows_per_sheet = sheet_height / tile_height;
147 const int tiles_per_sheet = tiles_per_row * rows_per_sheet;
149 int sheet = (tile_id / tiles_per_sheet) % 4 + sheet_offset;
150 int position_in_sheet = tile_id % tiles_per_sheet;
151 int row_in_sheet = position_in_sheet / tiles_per_row;
152 int column_in_sheet = position_in_sheet % tiles_per_row;
155 if (sheet < sheet_offset || sheet > sheet_offset + 3) {
156 return std::vector<uint8_t>(tile_width * tile_height, 0);
159 const int data_size =
static_cast<int>(data.size());
160 std::vector<uint8_t> tile_data(tile_width * tile_height, 0);
161 for (
int y = 0; y < tile_height; ++y) {
162 for (
int x = 0; x < tile_width; ++x) {
163 int src_x = column_in_sheet * tile_width + x;
164 int src_y = (sheet * sheet_height) + (row_in_sheet * tile_height) + y;
166 int src_index = (src_y * buffer_width) + src_x;
167 int dest_index = y * tile_width + x;
170 if (src_index >= 0 && src_index < data_size) {
171 tile_data[dest_index] = data[src_index];
181 for (
int y = 0; y < 4; ++y) {
182 for (
int x = 0; x < 8; ++x) {
183 std::swap(tile_data[y * 8 + x], tile_data[(7 - y) * 8 + x]);
189 for (
int y = 0; y < 8; ++y) {
190 for (
int x = 0; x < 4; ++x) {
191 std::swap(tile_data[y * 8 + x], tile_data[y * 8 + (7 - x)]);
197 const TileInfo& tile_info,
int base_x,
int base_y,
199 std::vector<uint8_t> tile_data =
209 for (
int y = 0; y < 8; ++y) {
210 for (
int x = 0; x < 8; ++x) {
211 int src_index = y * 8 + x;
212 int dest_x = base_x + x;
213 int dest_y = base_y + y;
214 int dest_index = (dest_y * tilemap.
atlas.
width()) + dest_x;
224 int sheet_offset,
int tile_id) {
227 int tile16_row = tile_id / tiles_per_row;
228 int tile16_column = tile_id % tiles_per_row;
229 int base_x = tile16_column * tilemap.
tile_size.
x;
230 int base_y = tile16_row * tilemap.
tile_size.
y;
233 ComposeAndPlaceTilePart(tilemap, data, top_left, base_x, base_y,
235 ComposeAndPlaceTilePart(tilemap, data, top_right, base_x + 8, base_y,
237 ComposeAndPlaceTilePart(tilemap, data, bottom_left, base_x, base_y + 8,
239 ComposeAndPlaceTilePart(tilemap, data, bottom_right, base_x + 8, base_y + 8,
242 tilemap.
tile_info[tile_id] = {top_left, top_right, bottom_left, bottom_right};
249 int num_tiles = tilemap.
tile_info.size();
251 int tile16_row = num_tiles / tiles_per_row;
252 int tile16_column = num_tiles % tiles_per_row;
253 int base_x = tile16_column * tilemap.
tile_size.
x;
254 int base_y = tile16_row * tilemap.
tile_size.
y;
256 ComposeAndPlaceTilePart(tilemap, data, top_left, base_x, base_y,
258 ComposeAndPlaceTilePart(tilemap, data, top_right, base_x + 8, base_y,
260 ComposeAndPlaceTilePart(tilemap, data, bottom_left, base_x, base_y + 8,
262 ComposeAndPlaceTilePart(tilemap, data, bottom_right, base_x + 8, base_y + 8,
265 tilemap.
tile_info.push_back({top_left, top_right, bottom_left, bottom_right});
271 SDL_Log(
"GetTilemapData: Invalid tile_id %d (negative)", tile_id);
272 return std::vector<uint8_t>(256, 0);
276 SDL_Log(
"GetTilemapData: Atlas is not active for tile_id %d", tile_id);
277 return std::vector<uint8_t>(256, 0);
281 SDL_Log(
"GetTilemapData: Atlas vector is empty for tile_id %d", tile_id);
282 return std::vector<uint8_t>(256, 0);
286 SDL_Log(
"GetTilemapData: Invalid tile size (%d, %d) for tile_id %d",
288 return std::vector<uint8_t>(256, 0);
296 if (width <= 0 || height <= 0) {
297 SDL_Log(
"GetTilemapData: Invalid atlas dimensions (%d, %d) for tile_id %d",
298 width, height, tile_id);
299 return std::vector<uint8_t>(tile_size * tile_size, 0);
303 int tiles_per_row = width / tile_size;
304 int tiles_per_column = height / tile_size;
305 int max_tile_id = tiles_per_row * tiles_per_column - 1;
307 if (tile_id > max_tile_id) {
309 "GetTilemapData: tile_id %d exceeds maximum %d (atlas: %dx%d, "
311 tile_id, max_tile_id, width, height, tile_size);
312 return std::vector<uint8_t>(tile_size * tile_size, 0);
315 std::vector<uint8_t> data(tile_size * tile_size);
317 for (
int ty = 0; ty < tile_size; ty++) {
318 for (
int tx = 0; tx < tile_size; tx++) {
320 int tile_row = tile_id / tiles_per_row;
321 int tile_col = tile_id % tiles_per_row;
322 int atlas_x = tile_col * tile_size + tx;
323 int atlas_y = tile_row * tile_size + ty;
324 int atlas_index = atlas_y * width + atlas_x;
327 if (atlas_x >= 0 && atlas_x < width && atlas_y >= 0 && atlas_y < height &&
329 atlas_index <
static_cast<int>(tilemap.
atlas.
vector().size())) {
330 uint8_t value = tilemap.
atlas.
vector()[atlas_index];
331 data[ty * tile_size + tx] = value;
334 "GetTilemapData: Atlas position (%d, %d) or index %d out of bounds "
335 "(atlas: %dx%d, size: %zu)",
336 atlas_x, atlas_y, atlas_index, width, height,
338 data[ty * tile_size + tx] = 0;
347 const std::vector<int>& tile_ids,
348 const std::vector<std::pair<float, float>>& positions,
349 const std::vector<std::pair<float, float>>& scales) {
350 if (tile_ids.empty() || positions.empty() ||
351 tile_ids.size() != positions.size()) {
366 std::vector<RenderCommand> render_commands;
367 render_commands.reserve(tile_ids.size());
369 for (
size_t i = 0; i < tile_ids.size(); ++i) {
370 int tile_id = tile_ids[i];
371 float x = positions[i].first;
372 float y = positions[i].second;
375 float scale_x = 1.0F;
376 float scale_y = 1.0F;
377 if (i < scales.size()) {
378 scale_x = scales[i].first;
379 scale_y = scales[i].second;
396 if (cached_tile && cached_tile->
is_active()) {
404 int atlas_id = atlas_renderer.AddBitmap(*cached_tile);
406 render_commands.emplace_back(atlas_id, x, y, scale_x, scale_y);
412 if (!render_commands.empty()) {
413 atlas_renderer.RenderBatch(render_commands);
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
static AtlasRenderer & Get()
Represents a bitmap image optimized for SNES ROM hacking.
const SnesPalette & palette() const
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
TextureHandle texture() const
const std::vector< uint8_t > & vector() const
void CreateTexture()
Creates the underlying SDL_Texture to be displayed.
void set_data(const std::vector< uint8_t > &data)
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
SDL_Surface * surface() const
void Get16x16Tile(int tile_x, int tile_y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Extract a 16x16 tile from the bitmap (SNES metatile size)
Defines an abstract interface for all rendering operations.
RAII timer for automatic timing management.
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
SNES 16-bit tile metadata container.
void ComposeAndPlaceTilePart(Tilemap &tilemap, const std::vector< uint8_t > &data, const TileInfo &tile_info, int base_x, int base_y, int sheet_offset)
void MirrorTileDataVertically(std::vector< uint8_t > &tile_data)
void MirrorTileDataHorizontally(std::vector< uint8_t > &tile_data)
void RenderTile16(IRenderer *renderer, Tilemap &tilemap, int tile_id)
void ModifyTile16(Tilemap &tilemap, const std::vector< uint8_t > &data, const TileInfo &top_left, const TileInfo &top_right, const TileInfo &bottom_left, const TileInfo &bottom_right, int sheet_offset, int tile_id)
void UpdateTilemap(IRenderer *renderer, Tilemap &tilemap, const std::vector< uint8_t > &data)
Tilemap CreateTilemap(IRenderer *renderer, const std::vector< uint8_t > &data, int width, int height, int tile_size, int num_tiles, const SnesPalette &palette)
void UpdateTile16(IRenderer *renderer, Tilemap &tilemap, int tile_id)
std::vector< uint8_t > GetTilemapData(Tilemap &tilemap, int tile_id)
void RenderTilesBatch(IRenderer *renderer, Tilemap &tilemap, const std::vector< int > &tile_ids, const std::vector< std::pair< float, float > > &positions, const std::vector< std::pair< float, float > > &scales)
Render multiple tiles using atlas rendering for improved performance.
std::vector< uint8_t > FetchTileDataFromGraphicsBuffer(const std::vector< uint8_t > &data, int tile_id, int sheet_offset)
void RenderTile(IRenderer *renderer, Tilemap &tilemap, int tile_id)
void ComposeTile16(Tilemap &tilemap, const std::vector< uint8_t > &data, const TileInfo &top_left, const TileInfo &top_right, const TileInfo &bottom_left, const TileInfo &bottom_right, int sheet_offset)
int y
Y coordinate or height.
int x
X coordinate or width.
void CacheTile(int tile_id, const Bitmap &bitmap)
Cache a tile bitmap by copying it.
Bitmap * GetTile(int tile_id)
Get a cached tile by ID.
Tilemap structure for SNES tile-based graphics management.
Pair tile_size
Size of individual tiles (8x8 or 16x16)
TileCache tile_cache
Smart tile cache with LRU eviction.
Pair map_size
Size of tilemap in tiles.
Bitmap atlas
Master bitmap containing all tiles.
std::vector< std::array< gfx::TileInfo, 4 > > tile_info
Tile metadata (4 tiles per 16x16)