Enhance tile rendering and update logic in OverworldEditor and Canvas

- Added checks to ensure tile16 blockset is loaded and active before drawing in OverworldEditor.
- Improved tile rendering logic in Canvas to pre-render tiles and ensure they are active before drawing, preventing timing issues.
- Cleared cached tile bitmaps to force re-rendering when the tile16 blockset is updated, enhancing visual consistency.
This commit is contained in:
scawful
2025-09-27 11:36:12 -04:00
parent c6490314f3
commit 7e46b2b7be
2 changed files with 43 additions and 4 deletions

View File

@@ -844,8 +844,11 @@ void OverworldEditor::CheckForSelectRectangle() {
}
}
// Create a composite image of all the tile16s selected
if (!tile16_ids.empty()) {
ow_map_canvas_.DrawBitmapGroup(tile16_ids, tile16_blockset_, 0x10, ow_map_canvas_.global_scale());
if (!tile16_ids.empty() && map_blockset_loaded_) {
// Ensure the tilemap is ready before drawing
if (tile16_blockset_.atlas.is_active()) {
ow_map_canvas_.DrawBitmapGroup(tile16_ids, tile16_blockset_, 0x10, ow_map_canvas_.global_scale());
}
}
}
@@ -1008,6 +1011,15 @@ absl::Status OverworldEditor::CheckForCurrentMap() {
ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
RefreshOverworldMap();
RETURN_IF_ERROR(RefreshTile16Blockset());
// Ensure tile16 blockset is fully updated before rendering
if (tile16_blockset_.atlas.is_active()) {
Renderer::Get().UpdateBitmap(&tile16_blockset_.atlas);
// Clear any cached tile bitmaps to force re-rendering
tile16_blockset_.tile_bitmaps.clear();
}
Renderer::Get().UpdateBitmap(&maps_bmp_[current_map_]);
maps_bmp_[current_map_].set_modified(false);
}

View File

@@ -745,6 +745,20 @@ void Canvas::DrawBitmapGroup(std::vector<int> &group, gfx::Tilemap &tilemap,
return;
}
// Pre-render all tiles to avoid timing issues
auto tilemap_size = tilemap.atlas.width() * tilemap.atlas.height() / tilemap.map_size.x;
for (int tile_id : group) {
if (tile_id >= 0 && tile_id < tilemap_size) {
gfx::RenderTile(tilemap, tile_id);
// Ensure the tile is actually rendered and active
auto tile_it = tilemap.tile_bitmaps.find(tile_id);
if (tile_it != tilemap.tile_bitmaps.end() && !tile_it->second.is_active()) {
core::Renderer::Get().RenderBitmap(&tile_it->second);
}
}
}
// Top-left and bottom-right corners of the rectangle
ImVec2 rect_top_left = selected_points_[0];
ImVec2 rect_bottom_right = selected_points_[1];
@@ -789,8 +803,21 @@ void Canvas::DrawBitmapGroup(std::vector<int> &group, gfx::Tilemap &tilemap,
// Draw the tile bitmap at the calculated position
gfx::RenderTile(tilemap, tile_id);
if (tilemap.tile_bitmaps.find(tile_id) != tilemap.tile_bitmaps.end()) {
DrawBitmap(tilemap.tile_bitmaps[tile_id], tile_pos_x, tile_pos_y, scale, 150);
// Ensure the tile bitmap exists and is properly rendered
auto tile_it = tilemap.tile_bitmaps.find(tile_id);
if (tile_it != tilemap.tile_bitmaps.end()) {
auto& tile_bitmap = tile_it->second;
// Ensure the bitmap is active before drawing
if (tile_bitmap.is_active()) {
DrawBitmap(tile_bitmap, tile_pos_x, tile_pos_y, scale, 150);
} else {
// Force render if not active
core::Renderer::Get().RenderBitmap(&tile_bitmap);
if (tile_bitmap.is_active()) {
DrawBitmap(tile_bitmap, tile_pos_x, tile_pos_y, scale, 150);
}
}
}
}
i++;