diff --git a/src/renderer/TerrainRenderer.cpp b/src/renderer/TerrainRenderer.cpp index b8520e0..76611fa 100644 --- a/src/renderer/TerrainRenderer.cpp +++ b/src/renderer/TerrainRenderer.cpp @@ -169,14 +169,14 @@ void mdcii::renderer::TerrainRenderer::Render(const layer::TerrainLayer& t_terra } //------------------------------------------------- -// Add / remove building +// Remove / add building - Gpu //------------------------------------------------- -void mdcii::renderer::TerrainRenderer::DeleteBuilding(world::Island& t_island, const layer::Tile& t_tile) +void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Island& t_island, const layer::Tile& t_tile) { - MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuilding()] No building to delete.") + MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuildingFromGpu()] No building to delete.") - Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuilding()] Delete building with Id {} from the world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0); + Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromGpu()] Delete building with Id {} from the world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0); magic_enum::enum_for_each([this, &t_island, &t_tile](const world::Zoom t_zoom) { magic_enum::enum_for_each([this, &t_zoom, &t_island, &t_tile](const world::Rotation t_rotation) { @@ -211,19 +211,19 @@ void mdcii::renderer::TerrainRenderer::DeleteBuilding(world::Island& t_island, c }); } -void mdcii::renderer::TerrainRenderer::DeleteBuilding(world::Terrain& t_terrain) +void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Terrain& t_terrain) { - MDCII_ASSERT(!t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::DeleteBuilding()] No Tile objects available.") + MDCII_ASSERT(!t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::DeleteBuildingFromGpu()] No Tile objects available.") for (const auto& tile : t_terrain.tilesToAdd.tiles) { - DeleteBuilding(*t_terrain.tilesToAdd.island, *tile); + DeleteBuildingFromGpu(*t_terrain.tilesToAdd.island, *tile); } // clear vector std::vector>().swap(t_terrain.tilesToAdd.tiles); } -void mdcii::renderer::TerrainRenderer::AddBuilding( +void mdcii::renderer::TerrainRenderer::AddBuildingToGpu( const layer::Tile& t_selectedBuildingTile, const glm::ivec2& t_startWorldPosition, world::Terrain& t_terrain @@ -235,7 +235,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding( return; } - MDCII_ASSERT(t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::AddBuilding()] Invalid number of tiles.") + MDCII_ASSERT(t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::AddBuildingToGpu()] Invalid number of tiles.") const auto& terrainLayer{ t_terrain.tilesToAdd.island->terrainLayer }; const auto& mixedLayer{ t_terrain.tilesToAdd.island->mixedLayer }; @@ -253,7 +253,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding( // calc final world position const auto buildingWorldPosition{ glm::ivec2(t_startWorldPosition.x + rp.x, t_startWorldPosition.y + rp.y) }; - MDCII_ASSERT(t_terrain.currentIslandUnderMouse->IsWorldPositionInAabb(buildingWorldPosition), "[TerrainRenderer::AddBuilding()] Invalid world position.") + MDCII_ASSERT(t_terrain.currentIslandUnderMouse->IsWorldPositionInAabb(buildingWorldPosition), "[TerrainRenderer::AddBuildingToGpu()] Invalid world position.") // get position on island from world position const auto buildingIslandPosition{ t_terrain.currentIslandUnderMouse->GetIslandPositionFromWorldPosition(buildingWorldPosition) }; @@ -315,7 +315,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding( } } - MDCII_ASSERT(t_terrain.tilesToAdd.tiles.size() == building.size.w * building.size.h, "[TerrainRenderer::AddBuilding()] Invalid number of created tiles.") + MDCII_ASSERT(t_terrain.tilesToAdd.tiles.size() == building.size.w * building.size.h, "[TerrainRenderer::AddBuildingToGpu()] Invalid number of created tiles.") std::vector connected; for (const auto& tile : t_terrain.tilesToAdd.tiles) @@ -328,7 +328,7 @@ void mdcii::renderer::TerrainRenderer::AddBuilding( tile->connectedTiles = connected; } - Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuilding()] Added building with Id {} to the world on ({}, {}).", building.id, t_startWorldPosition.x, t_startWorldPosition.y); + Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToGpu()] Added building with Id {} to the world on ({}, {}).", building.id, t_startWorldPosition.x, t_startWorldPosition.y); } void mdcii::renderer::TerrainRenderer::UpdateGpuData( @@ -374,6 +374,37 @@ void mdcii::renderer::TerrainRenderer::UpdateGpuData( ogl::buffer::Vao::Unbind(); } +//------------------------------------------------- +// Remove / add building - Cpu +//------------------------------------------------- + +void mdcii::renderer::TerrainRenderer::DeleteBuildingFromCpu(layer::Tile& t_tile) const +{ + MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuildingFromCpu()] No building to delete.") + + Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromCpu()] Delete building Id {} Cpu data of Tile in world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0); + + t_tile.ResetBuildingInfo(); +} + +void mdcii::renderer::TerrainRenderer::AddBuildingToCpu(world::Terrain& t_terrain) +{ + MDCII_ASSERT(!t_terrain.tilesToAdd.tiles.empty(), "[TerrainRenderer::AddBuildingToCpu()] No Tile objects available.") + + // reset Tile pointers and replace with new tile + auto& buildingsLayer{ t_terrain.tilesToAdd.island->buildingsLayer }; + for (auto& tile : t_terrain.tilesToAdd.tiles) + { + Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToCpu()] Add building Id {} Cpu data of Tile to world ({}, {}).", tile->buildingId, tile->worldXDeg0, tile->worldYDeg0); + + buildingsLayer->ResetTilePointersAt(tile->instanceIds); + buildingsLayer->StoreTile(std::move(tile)); + } + + // clear vector + std::vector>().swap(t_terrain.tilesToAdd.tiles); +} + //------------------------------------------------- // Init //------------------------------------------------- diff --git a/src/renderer/TerrainRenderer.h b/src/renderer/TerrainRenderer.h index ccdfafd..27ea57e 100644 --- a/src/renderer/TerrainRenderer.h +++ b/src/renderer/TerrainRenderer.h @@ -125,7 +125,7 @@ namespace mdcii::renderer void Render(const layer::TerrainLayer& t_terrainLayer, world::Zoom t_zoom, world::Rotation t_rotation) const; //------------------------------------------------- - // Add / remove building + // Remove / add building - Gpu //------------------------------------------------- /** @@ -134,14 +134,14 @@ namespace mdcii::renderer * @param t_island The Island object. * @param t_tile The Tile object to delete. */ - void DeleteBuilding(world::Island& t_island, const layer::Tile& t_tile); + void DeleteBuildingFromGpu(world::Island& t_island, const layer::Tile& t_tile); /** * Deletes a building from the Gpu. * * @param t_terrain The Terrain object. */ - void DeleteBuilding(world::Terrain& t_terrain); + void DeleteBuildingFromGpu(world::Terrain& t_terrain); /** * Adds a building to the Gpu. @@ -150,7 +150,7 @@ namespace mdcii::renderer * @param t_startWorldPosition The starting position in the world. * @param t_terrain The Terrain object. */ - void AddBuilding( + void AddBuildingToGpu( const layer::Tile& t_selectedBuildingTile, const glm::ivec2& t_startWorldPosition, world::Terrain& t_terrain @@ -177,6 +177,19 @@ namespace mdcii::renderer int32_t t_buildingId ); + //------------------------------------------------- + // Remove / add building - Cpu + //------------------------------------------------- + + /** + * Deletes a building from the Cpu. + * + * @param t_tile Tile object where building information should be deleted/overwritten. + */ + void DeleteBuildingFromCpu(layer::Tile& t_tile) const; + + void AddBuildingToCpu(world::Terrain& t_terrain); + protected: private: diff --git a/src/world/World.cpp b/src/world/World.cpp index e46af97..018572f 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -241,8 +241,6 @@ void mdcii::world::World::RenderImGui() { if (ImGui::CollapsingHeader("Buildings")) { - m_demolishTileIndex = -1; - m_worldGui->ShowBuildingsGui(); } } @@ -254,12 +252,32 @@ void mdcii::world::World::RenderImGui() m_worldGui->selectedBuildingTile.Reset(); } - if (m_demolishTileIndex >= 0) + if (terrain->currentSelectedIsland) { - // todo: remove building + if (terrain->currentSelectedIsland->currentSelectedTile && terrain->currentSelectedIsland->currentSelectedTile->HasBuilding()) + { + // todo: store parent layer ptr in tile + const auto& buildingsLayer{ terrain->currentSelectedIsland->buildingsLayer }; + const auto idx{ terrain->currentSelectedIsland->currentSelectedTile->indices.at(0) }; + if (buildingsLayer->tiles.at(idx)->HasBuilding()) + { + if (terrain->currentSelectedIsland->currentSelectedTile->connectedTiles.empty()) + { + terrainRenderer->DeleteBuildingFromGpu(*terrain->currentSelectedIsland, *terrain->currentSelectedIsland->currentSelectedTile); + terrainRenderer->DeleteBuildingFromCpu(*terrain->currentSelectedIsland->currentSelectedTile); + } + else + { + for (const auto tileIndex : terrain->currentSelectedIsland->currentSelectedTile->connectedTiles) + { + auto& tile{ buildingsLayer->tiles.at(tileIndex) }; + terrainRenderer->DeleteBuildingFromGpu(*terrain->currentSelectedIsland, *tile); + terrainRenderer->DeleteBuildingFromCpu(*tile); + } + } + } + } } - - m_demolishTileIndex = -1; } if (currentAction == Action::STATUS) @@ -280,8 +298,6 @@ void mdcii::world::World::RenderImGui() if (currentAction == Action::OPTIONS) { - m_demolishTileIndex = -1; - if (m_worldGui->selectedBuildingTile.HasBuilding()) { m_worldGui->selectedBuildingTile.Reset(); @@ -347,19 +363,7 @@ void mdcii::world::World::OnLeftMouseButtonPressed() !terrain->tilesToAdd.tiles.empty() ) { - // reset Tile pointers and replace with new Tile - // todo: method AddToCpu - /* - auto& buildingsLayer{ GetLayer(WorldLayerType::BUILDINGS) }; - for (auto& tile : m_tilesToAdd) - { - buildingsLayer.ResetTilePointersAt(tile->instanceIds); - buildingsLayer.StoreTile(std::move(tile)); - } - */ - - // clear vector - std::vector>().swap(terrain->tilesToAdd.tiles); + terrainRenderer->AddBuildingToCpu(*terrain); } MDCII_ASSERT(terrain->tilesToAdd.tiles.empty(), "[World::OnLeftMouseButtonPressed()] Invalid number of tiles to add.") @@ -383,17 +387,17 @@ void mdcii::world::World::OnMouseMoved() { if (!terrain->tilesToAdd.tiles.empty()) { - terrainRenderer->DeleteBuilding(*terrain); + terrainRenderer->DeleteBuildingFromGpu(*terrain); } if (terrain->tilesToAdd.tiles.empty()) { - terrainRenderer->AddBuilding(m_worldGui->selectedBuildingTile, mousePicker->currentPosition, *terrain); + terrainRenderer->AddBuildingToGpu(m_worldGui->selectedBuildingTile, mousePicker->currentPosition, *terrain); } } if (currentAction == Action::BUILD && !IsPositionInWorld(mousePicker->currentPosition) && !terrain->tilesToAdd.tiles.empty()) { - terrainRenderer->DeleteBuilding(*terrain); + terrainRenderer->DeleteBuildingFromGpu(*terrain); } } diff --git a/src/world/World.h b/src/world/World.h index 4c0b401..bc8f5cf 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -389,11 +389,6 @@ namespace mdcii::world */ bool m_runAnimations{ true }; - /** - * To demolish a building. - */ - int m_demolishTileIndex{ -1 }; - /** * The mouse button pressed listener handle. */ diff --git a/src/world/WorldGui.cpp b/src/world/WorldGui.cpp index 6f0a214..f126646 100644 --- a/src/world/WorldGui.cpp +++ b/src/world/WorldGui.cpp @@ -19,6 +19,8 @@ #include #include "WorldGui.h" #include "World.h" +#include "Terrain.h" +#include "Island.h" #include "MdciiAssert.h" #include "Game.h" #include "state/State.h" @@ -166,6 +168,11 @@ void mdcii::world::WorldGui::ShowActionsGui() const if (ImGui::Button(data::Text::GetMenuText(Game::INI.Get("locale", "lang"), World::ACTION_NAMES[i].data()).c_str())) { std::fill(m_world->actionButtons.begin(), m_world->actionButtons.end(), false); + if (m_world->terrain->currentSelectedIsland && m_world->terrain->currentSelectedIsland->currentSelectedTile) + { + m_world->terrain->currentSelectedIsland->currentSelectedTile = nullptr; + m_world->terrain->currentSelectedIsland = nullptr; + } m_world->actionButtons[i] = true; m_world->currentAction = action;