15 int tile_size,
int num_tiles,
SnesPalette &palette) {
56 if (tile_data.empty()) {
74 if (tiles_per_row <= 0) {
78 int tile_x = (tile_id % tiles_per_row) * tilemap.
tile_size.
x;
79 int tile_y = (tile_id / tiles_per_row) * tilemap.
tile_size.
y;
82 if (tile_x < 0 || tile_x >= tilemap.
atlas.
width() ||
96 int tile_x = (tile_id % tiles_per_row) * tilemap.
tile_size.
x;
97 int tile_y = (tile_id / tiles_per_row) * tilemap.
tile_size.
y;
99 int tile_data_offset = 0;
114 const std::vector<uint8_t> &data,
int tile_id,
int sheet_offset) {
115 const int tile_width = 8;
116 const int tile_height = 8;
117 const int buffer_width = 128;
118 const int sheet_height = 32;
120 const int tiles_per_row = buffer_width / tile_width;
121 const int rows_per_sheet = sheet_height / tile_height;
122 const int tiles_per_sheet = tiles_per_row * rows_per_sheet;
124 int sheet = (tile_id / tiles_per_sheet) % 4 + sheet_offset;
125 int position_in_sheet = tile_id % tiles_per_sheet;
126 int row_in_sheet = position_in_sheet / tiles_per_row;
127 int column_in_sheet = position_in_sheet % tiles_per_row;
129 assert(sheet >= sheet_offset && sheet <= sheet_offset + 3);
131 std::vector<uint8_t> tile_data(tile_width * tile_height);
132 for (
int y = 0; y < tile_height; ++y) {
133 for (
int x = 0; x < tile_width; ++x) {
134 int src_x = column_in_sheet * tile_width + x;
135 int src_y = (sheet * sheet_height) + (row_in_sheet * tile_height) + y;
137 int src_index = (src_y * buffer_width) + src_x;
138 int dest_index = y * tile_width + x;
139 tile_data[dest_index] = data[src_index];
148 for (
int y = 0; y < 4; ++y) {
149 for (
int x = 0; x < 8; ++x) {
150 std::swap(tile_data[y * 8 + x], tile_data[(7 - y) * 8 + x]);
156 for (
int y = 0; y < 8; ++y) {
157 for (
int x = 0; x < 4; ++x) {
158 std::swap(tile_data[y * 8 + x], tile_data[y * 8 + (7 - x)]);
164 const TileInfo &tile_info,
int base_x,
int base_y,
166 std::vector<uint8_t> tile_data =
176 for (
int y = 0; y < 8; ++y) {
177 for (
int x = 0; x < 8; ++x) {
178 int src_index = y * 8 + x;
179 int dest_x = base_x + x;
180 int dest_y = base_y + y;
181 int dest_index = (dest_y * tilemap.
atlas.
width()) + dest_x;
191 int sheet_offset,
int tile_id) {
194 int tile16_row = tile_id / tiles_per_row;
195 int tile16_column = tile_id % tiles_per_row;
196 int base_x = tile16_column * tilemap.
tile_size.
x;
197 int base_y = tile16_row * tilemap.
tile_size.
y;
200 ComposeAndPlaceTilePart(tilemap, data, top_left, base_x, base_y,
202 ComposeAndPlaceTilePart(tilemap, data, top_right, base_x + 8, base_y,
204 ComposeAndPlaceTilePart(tilemap, data, bottom_left, base_x, base_y + 8,
206 ComposeAndPlaceTilePart(tilemap, data, bottom_right, base_x + 8, base_y + 8,
209 tilemap.
tile_info[tile_id] = {top_left, top_right, bottom_left, bottom_right};
216 int num_tiles = tilemap.
tile_info.size();
218 int tile16_row = num_tiles / tiles_per_row;
219 int tile16_column = num_tiles % tiles_per_row;
220 int base_x = tile16_column * tilemap.
tile_size.
x;
221 int base_y = tile16_row * tilemap.
tile_size.
y;
223 ComposeAndPlaceTilePart(tilemap, data, top_left, base_x, base_y,
225 ComposeAndPlaceTilePart(tilemap, data, top_right, base_x + 8, base_y,
227 ComposeAndPlaceTilePart(tilemap, data, bottom_left, base_x, base_y + 8,
229 ComposeAndPlaceTilePart(tilemap, data, bottom_right, base_x + 8, base_y + 8,
232 tilemap.
tile_info.push_back({top_left, top_right, bottom_left, bottom_right});
239 SDL_Log(
"GetTilemapData: Invalid tile_id %d (negative)", tile_id);
240 return std::vector<uint8_t>(256, 0);
244 SDL_Log(
"GetTilemapData: Atlas is not active for tile_id %d", tile_id);
245 return std::vector<uint8_t>(256, 0);
249 SDL_Log(
"GetTilemapData: Atlas vector is empty for tile_id %d", tile_id);
250 return std::vector<uint8_t>(256, 0);
254 SDL_Log(
"GetTilemapData: Invalid tile size (%d, %d) for tile_id %d",
256 return std::vector<uint8_t>(256, 0);
265 if (width <= 0 || height <= 0) {
266 SDL_Log(
"GetTilemapData: Invalid atlas dimensions (%d, %d) for tile_id %d",
267 width, height, tile_id);
268 return std::vector<uint8_t>(tile_size * tile_size, 0);
272 int tiles_per_row = width / tile_size;
273 int tiles_per_column = height / tile_size;
274 int max_tile_id = tiles_per_row * tiles_per_column - 1;
276 if (tile_id > max_tile_id) {
277 SDL_Log(
"GetTilemapData: tile_id %d exceeds maximum %d (atlas: %dx%d, tile_size: %d)",
278 tile_id, max_tile_id, width, height, tile_size);
279 return std::vector<uint8_t>(tile_size * tile_size, 0);
282 std::vector<uint8_t> data(tile_size * tile_size);
285 for (
int ty = 0; ty < tile_size; ty++) {
286 for (
int tx = 0; tx < tile_size; tx++) {
288 int tile_row = tile_id / tiles_per_row;
289 int tile_col = tile_id % tiles_per_row;
290 int atlas_x = tile_col * tile_size + tx;
291 int atlas_y = tile_row * tile_size + ty;
292 int atlas_index = atlas_y * width + atlas_x;
295 if (atlas_x >= 0 && atlas_x < width &&
296 atlas_y >= 0 && atlas_y < height &&
297 atlas_index >= 0 && atlas_index <
static_cast<int>(tilemap.
atlas.
vector().size())) {
298 uint8_t value = tilemap.
atlas.
vector()[atlas_index];
299 data[ty * tile_size + tx] = value;
301 SDL_Log(
"GetTilemapData: Atlas position (%d, %d) or index %d out of bounds (atlas: %dx%d, size: %zu)",
302 atlas_x, atlas_y, atlas_index, width, height, tilemap.
atlas.
vector().size());
303 data[ty * tile_size + tx] = 0;
312 const std::vector<std::pair<float, float>>& positions,
313 const std::vector<std::pair<float, float>>& scales) {
314 if (tile_ids.empty() || positions.empty() || tile_ids.size() != positions.size()) {
329 std::vector<RenderCommand> render_commands;
330 render_commands.reserve(tile_ids.size());
332 for (
size_t i = 0; i < tile_ids.size(); ++i) {
333 int tile_id = tile_ids[i];
334 float x = positions[i].first;
335 float y = positions[i].second;
338 float scale_x = 1.0F;
339 float scale_y = 1.0F;
340 if (i < scales.size()) {
341 scale_x = scales[i].first;
342 scale_y = scales[i].second;
359 if (cached_tile && cached_tile->
is_active()) {
366 int atlas_id = atlas_renderer.AddBitmap(*cached_tile);
368 render_commands.emplace_back(atlas_id, x, y, scale_x, scale_y);
374 if (!render_commands.empty()) {
375 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.
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)
void UpdateTile16(IRenderer *renderer, Tilemap &tilemap, int tile_id)
std::vector< uint8_t > GetTilemapData(Tilemap &tilemap, int tile_id)
Tilemap CreateTilemap(IRenderer *renderer, std::vector< uint8_t > &data, int width, int height, int tile_size, int num_tiles, SnesPalette &palette)
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)
Main namespace for the application.
int y
Y coordinate or height.
int x
X coordinate or width.
void CacheTile(int tile_id, Bitmap &&bitmap)
Cache a tile bitmap.
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)