Fix TileDef rendering at (0,0) and open editor with current level
Remove the (tex_x || tex_y) guard in tilemap_render_layer() that silently ignored TileDefs pointing to tileset position (0,0). The tile_def_count check alone is sufficient to distinguish defined from undefined tiles. Sync the editor palette to use TileDef tex coords when available, fixing the mismatch where the palette showed tiles by sequential grid position while the canvas used TileDef coordinates. Save all TileDef entries unconditionally to prevent round-trip data loss for tiles at position (0,0) with no flags. Track the active level file path so pressing E during gameplay opens the editor with that level loaded instead of a blank canvas.
This commit is contained in:
@@ -489,12 +489,11 @@ static bool save_tilemap(const Tilemap *map, const char *path) {
|
||||
}
|
||||
if (map->exit_zone_count > 0) fprintf(f, "\n");
|
||||
|
||||
/* Tile definitions */
|
||||
/* Tile definitions — save all entries so flags and tex coords survive
|
||||
* round-trips, including tiles at position (0,0) with no flags. */
|
||||
for (int id = 1; id < map->tile_def_count; id++) {
|
||||
const TileDef *td = &map->tile_defs[id];
|
||||
if (td->flags || td->tex_x || td->tex_y) {
|
||||
fprintf(f, "TILEDEF %d %d %d %u\n", id, td->tex_x, td->tex_y, td->flags);
|
||||
}
|
||||
fprintf(f, "TILEDEF %d %d %d %u\n", id, td->tex_x, td->tex_y, td->flags);
|
||||
}
|
||||
fprintf(f, "\n");
|
||||
|
||||
@@ -1586,8 +1585,15 @@ void editor_render(Editor *ed, float interpolation) {
|
||||
if (draw_y + TILE_SIZE < pal_y_start ||
|
||||
draw_y > ent_section_y) continue;
|
||||
|
||||
int src_col = (id - 1) % ed->tileset_cols;
|
||||
int src_row = (id - 1) / ed->tileset_cols;
|
||||
/* Use TileDef tex coords when available, else grid position */
|
||||
int src_col, src_row;
|
||||
if (id < ed->map.tile_def_count) {
|
||||
src_col = ed->map.tile_defs[id].tex_x;
|
||||
src_row = ed->map.tile_defs[id].tex_y;
|
||||
} else {
|
||||
src_col = (id - 1) % ed->tileset_cols;
|
||||
src_row = (id - 1) / ed->tileset_cols;
|
||||
}
|
||||
SDL_Rect src = {
|
||||
src_col * TILE_SIZE, src_row * TILE_SIZE,
|
||||
TILE_SIZE, TILE_SIZE
|
||||
|
||||
Reference in New Issue
Block a user