Skip to content

Commit

Permalink
store parent layer type in tile object
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen committed Jan 5, 2023
1 parent 8ea3dab commit bea7f1c
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 125 deletions.
5 changes: 5 additions & 0 deletions src/layer/GameLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ namespace mdcii::layer
// Member
//-------------------------------------------------

/**
* The type of the Layer.
*/
LayerType layerType{ LayerType::NOTHING };

/**
* The Layer width.
*/
Expand Down
11 changes: 9 additions & 2 deletions src/layer/TerrainLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@ void mdcii::layer::from_json(const nlohmann::json& t_json, Tile& t_tile)
// Ctors. / Dtor.
//-------------------------------------------------

mdcii::layer::TerrainLayer::TerrainLayer(std::shared_ptr<state::Context> t_context, world::World* t_world, world::Island* t_island)
mdcii::layer::TerrainLayer::TerrainLayer(
std::shared_ptr<state::Context> t_context,
world::World* t_world,
world::Island* t_island,
const LayerType t_layerType
)
: GameLayer(std::move(t_context), t_world)
, m_island{ t_island }
{
Log::MDCII_LOG_DEBUG("[TerrainLayer::TerrainLayer()] Create TerrainLayer.");
Log::MDCII_LOG_DEBUG("[TerrainLayer::TerrainLayer()] Create TerrainLayer of type {}.", magic_enum::enum_name(t_layerType));
layerType = t_layerType;

MDCII_ASSERT(m_island, "[TerrainLayer::TerrainLayer()] Null pointer.")

Expand Down Expand Up @@ -463,4 +469,5 @@ void mdcii::layer::TerrainLayer::StoreBuildingIdsInGpu()
void mdcii::layer::TerrainLayer::AddTileFromJson(const nlohmann::json& t_json)
{
tiles.emplace_back(std::make_unique<Tile>(t_json.get<Tile>()));
tiles.back()->layerType = layerType;
}
3 changes: 2 additions & 1 deletion src/layer/TerrainLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ namespace mdcii::layer
* @param t_context Access to shared objects.
* @param t_world The World object.
* @param t_island The parent Island object.
* @param t_layerType The type of this Layer.
*/
TerrainLayer(std::shared_ptr<state::Context> t_context, world::World* t_world, world::Island* t_island);
TerrainLayer(std::shared_ptr<state::Context> t_context, world::World* t_world, world::Island* t_island, LayerType t_layerType);

TerrainLayer(const TerrainLayer& t_other) = delete;
TerrainLayer(TerrainLayer&& t_other) noexcept = delete;
Expand Down
10 changes: 10 additions & 0 deletions src/layer/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
#include "Tile.h"
#include "Game.h"
#include "data/Text.h"
#include "GameLayer.h"

//-------------------------------------------------
// Ctors. / Dtor.
//-------------------------------------------------

mdcii::layer::Tile::Tile()
{
layerType = LayerType::NOTHING;
}

//-------------------------------------------------
// Helper
Expand Down
28 changes: 28 additions & 0 deletions src/layer/Tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,28 @@

namespace mdcii::layer
{
//-------------------------------------------------
// Forward declarations
//-------------------------------------------------

/**
* Forward declaration enum class LayerType.
*/
enum class LayerType;

//-------------------------------------------------
// Tile
//-------------------------------------------------

/**
* Represents a single part of a Layer object.
*/
struct Tile
{
//-------------------------------------------------
// Member
//-------------------------------------------------

/**
* The Building Id from the haeuser.cod file.
*/
Expand Down Expand Up @@ -111,6 +128,17 @@ namespace mdcii::layer
*/
std::vector<int32_t> connectedTiles{};

/**
* The parent LayerType.
*/
LayerType layerType;

//-------------------------------------------------
// Ctors. / Dtor.
//-------------------------------------------------

Tile();

//-------------------------------------------------
// Helper
//-------------------------------------------------
Expand Down
36 changes: 25 additions & 11 deletions src/renderer/TerrainRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Island& t_is
{
MDCII_ASSERT(t_tile.HasBuilding(), "[TerrainRenderer::DeleteBuildingFromGpu()] No building to delete.")

Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromGpu()] Delete building with Id {} from the world ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0);
Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromGpu()] Delete building Gpu data with Id {} from world position ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0);

magic_enum::enum_for_each<world::Zoom>([this, &t_island, &t_tile](const world::Zoom t_zoom) {
magic_enum::enum_for_each<world::Rotation>([this, &t_zoom, &t_island, &t_tile](const world::Rotation t_rotation) {
Expand Down Expand Up @@ -223,6 +223,15 @@ void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Terrain& t_t
std::vector<std::unique_ptr<layer::Tile>>().swap(t_terrain.tilesToAdd.tiles);
}

void mdcii::renderer::TerrainRenderer::DeleteBuildingFromGpu(world::Island& t_island, const std::vector<int32_t>& t_tileIndices)
{
MDCII_ASSERT(!t_tileIndices.empty(), "[TerrainRenderer::DeleteBuildingFromGpu()] No Tile indices available.")
for (const auto tileIndex : t_tileIndices)
{
DeleteBuildingFromGpu(t_island, *t_island.buildingsLayer->tiles.at(tileIndex));
}
}

void mdcii::renderer::TerrainRenderer::AddBuildingToGpu(
const layer::Tile& t_selectedBuildingTile,
const glm::ivec2& t_startWorldPosition,
Expand All @@ -237,9 +246,6 @@ void mdcii::renderer::TerrainRenderer::AddBuildingToGpu(

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 };

for (auto y{ 0 }; y < building.size.h; ++y)
{
for (auto x{ 0 }; x < building.size.w; ++x)
Expand Down Expand Up @@ -268,6 +274,7 @@ void mdcii::renderer::TerrainRenderer::AddBuildingToGpu(
tile->islandYDeg0 = buildingIslandPosition.y;
tile->worldXDeg0 = buildingWorldPosition.x;
tile->worldYDeg0 = buildingWorldPosition.y;
tile->layerType = layer::LayerType::BUILDINGS;

// pre-calc screen positions / indices / gfx
t_terrain.currentIslandUnderMouse->terrainLayer->PreCalcTile(*tile);
Expand Down Expand Up @@ -311,7 +318,6 @@ void mdcii::renderer::TerrainRenderer::AddBuildingToGpu(

t_terrain.tilesToAdd.tiles.emplace_back(std::move(tile));
t_terrain.tilesToAdd.island = t_terrain.currentIslandUnderMouse;
t_terrain.tilesToAdd.startPosition = t_startWorldPosition;
}
}

Expand All @@ -328,7 +334,7 @@ void mdcii::renderer::TerrainRenderer::AddBuildingToGpu(
tile->connectedTiles = connected;
}

Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToGpu()] Added building with Id {} to the world on ({}, {}).", building.id, t_startWorldPosition.x, t_startWorldPosition.y);
Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToGpu()] Add building Gpu data with Id {} to world position ({}, {}).", building.id, t_startWorldPosition.x, t_startWorldPosition.y);
}

void mdcii::renderer::TerrainRenderer::UpdateGpuData(
Expand Down Expand Up @@ -378,24 +384,32 @@ void mdcii::renderer::TerrainRenderer::UpdateGpuData(
// Remove / add building - Cpu
//-------------------------------------------------

void mdcii::renderer::TerrainRenderer::DeleteBuildingFromCpu(layer::Tile& t_tile) const
void mdcii::renderer::TerrainRenderer::DeleteBuildingFromCpu(layer::Tile& t_tile)
{
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);
Log::MDCII_LOG_DEBUG("[TerrainRenderer::DeleteBuildingFromCpu()] Delete building Cpu data with Id {} from world position ({}, {}).", t_tile.buildingId, t_tile.worldXDeg0, t_tile.worldYDeg0);

t_tile.ResetBuildingInfo();
}

void mdcii::renderer::TerrainRenderer::DeleteBuildingFromCpu(world::Island& t_island, const std::vector<int32_t>& t_tileIndices)
{
MDCII_ASSERT(!t_tileIndices.empty(), "[TerrainRenderer::DeleteBuildingFromCpu()] No Tile indices available.")
for (const auto tileIndex : t_tileIndices)
{
DeleteBuildingFromCpu(*t_island.buildingsLayer->tiles.at(tileIndex));
}
}

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 };
const 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);
Log::MDCII_LOG_DEBUG("[TerrainRenderer::AddBuildingToCpu()] Add building Cpu data with Id {} to world position ({}, {}).", tile->buildingId, tile->worldXDeg0, tile->worldYDeg0);

buildingsLayer->ResetTilePointersAt(tile->instanceIds);
buildingsLayer->StoreTile(std::move(tile));
Expand Down
29 changes: 25 additions & 4 deletions src/renderer/TerrainRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,25 @@ namespace mdcii::renderer
* Deletes a building from the Gpu.
*
* @param t_island The Island object.
* @param t_tile The Tile object to delete.
* @param t_tile The Tile object of the building to delete.
*/
void DeleteBuildingFromGpu(world::Island& t_island, const layer::Tile& t_tile);

/**
* Deletes a building from the Gpu.
*
* @param t_terrain The Terrain object.
* @param t_terrain The Terrain object for access to the temp building tiles (tilesToAdd).
*/
void DeleteBuildingFromGpu(world::Terrain& t_terrain);

/**
* Deletes a building from the Gpu.
*
* @param t_island The Island object.
* @param t_tileIndices The Tile object indices of the building to delete.
*/
void DeleteBuildingFromGpu(world::Island& t_island, const std::vector<int32_t>& t_tileIndices);

/**
* Adds a building to the Gpu.
*
Expand Down Expand Up @@ -186,9 +194,22 @@ namespace mdcii::renderer
*
* @param t_tile Tile object where building information should be deleted/overwritten.
*/
void DeleteBuildingFromCpu(layer::Tile& t_tile) const;
static void DeleteBuildingFromCpu(layer::Tile& t_tile);

/**
* Deletes a building from the Cpu.
*
* @param t_island The Island object.
* @param t_tileIndices The tile indices of the building.
*/
static void DeleteBuildingFromCpu(world::Island& t_island, const std::vector<int32_t>& t_tileIndices);

void AddBuildingToCpu(world::Terrain& t_terrain);
/**
* Adds a building to the Cpu.
*
* @param t_terrain The Terrain object for access to the temp building tiles (tilesToAdd).
*/
static void AddBuildingToCpu(world::Terrain& t_terrain);

protected:

Expand Down
23 changes: 4 additions & 19 deletions src/world/Island.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,6 @@ glm::ivec2 mdcii::world::Island::GetIslandPositionFromWorldPosition(const glm::i
return { t_position.x - startWorldX, t_position.y - startWorldY };
}

bool mdcii::world::Island::IsPositionInIsland(const int32_t t_x, const int32_t t_y) const
{
if (t_x >= 0 && t_x < width && t_y >= 0 && t_y < height)
{
return true;
}

return false;
}

bool mdcii::world::Island::IsPositionInIsland(const glm::ivec2& t_position) const
{
return IsPositionInIsland(t_position.x, t_position.y);
}

//-------------------------------------------------
// Render
//-------------------------------------------------
Expand All @@ -148,23 +133,23 @@ void mdcii::world::Island::CreateLayersFromJson(const nlohmann::json& t_json)
{
if (layerNameJson == "coast")
{
coastLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this);
coastLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this, layer::LayerType::COAST);
coastLayer->CreateTilesFromJson(layerTilesJson);
coastLayer->PrepareCpuDataForRendering();
coastLayer->PrepareGpuDataForRendering();
}

if (layerNameJson == "terrain")
{
terrainLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this);
terrainLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this, layer::LayerType::TERRAIN);
terrainLayer->CreateTilesFromJson(layerTilesJson);
terrainLayer->PrepareCpuDataForRendering();
terrainLayer->PrepareGpuDataForRendering();
}

if (layerNameJson == "buildings")
{
buildingsLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this);
buildingsLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this, layer::LayerType::BUILDINGS);
buildingsLayer->CreateTilesFromJson(layerTilesJson);
buildingsLayer->PrepareCpuDataForRendering();
buildingsLayer->PrepareGpuDataForRendering();
Expand All @@ -175,7 +160,7 @@ void mdcii::world::Island::CreateLayersFromJson(const nlohmann::json& t_json)
MDCII_ASSERT(terrainLayer, "[Island::CreateLayersFromJson()] Null pointer.")
MDCII_ASSERT(buildingsLayer, "[Island::CreateLayersFromJson()] Null pointer.")

mixedLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this);
mixedLayer = std::make_unique<layer::TerrainLayer>(m_context, m_terrain->world, this, layer::LayerType::MIXED);
mixedLayer->instancesToRender = terrainLayer->instancesToRender;
mixedLayer->modelMatrices = terrainLayer->modelMatrices;
mixedLayer->gfxNumbers = terrainLayer->gfxNumbers;
Expand Down
19 changes: 0 additions & 19 deletions src/world/Island.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,6 @@ namespace mdcii::world
*/
[[nodiscard]] glm::ivec2 GetIslandPositionFromWorldPosition(const glm::ivec2& t_position) const;

/**
* Checks whether a given position is in this island width and height.
*
* @param t_x The x position to check.
* @param t_y The y position to check.
*
* @return True or false.
*/
[[nodiscard]] bool IsPositionInIsland(int32_t t_x, int32_t t_y) const;

/**
* Checks whether a given position is in this island width and height.
*
* @param t_position The position to check.
*
* @return True or false.
*/
[[nodiscard]] bool IsPositionInIsland(const glm::ivec2& t_position) const;

//-------------------------------------------------
// Render
//-------------------------------------------------
Expand Down
12 changes: 10 additions & 2 deletions src/world/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,19 @@ bool mdcii::world::Terrain::IsBuildableOnIslandUnderMouse(const glm::ivec2& t_st
}
}

Log::MDCII_LOG_DEBUG("[Terrain::IsBuildableOnIslandUnderMouse()] The tile is buildable at world ({}, {}).", t_startWorldPosition.x, t_startWorldPosition.y);
Log::MDCII_LOG_DEBUG("[Terrain::IsBuildableOnIslandUnderMouse()] The tile is buildable at world position ({}, {}).", t_startWorldPosition.x, t_startWorldPosition.y);

return true;
}

bool mdcii::world::Terrain::IsCurrentSelectedTileRemovable() const
{
return currentSelectedIsland &&
currentSelectedIsland->currentSelectedTile &&
currentSelectedIsland->currentSelectedTile->HasBuilding() &&
currentSelectedIsland->currentSelectedTile->layerType == layer::LayerType::BUILDINGS;
}

//-------------------------------------------------
// Logic
//-------------------------------------------------
Expand Down Expand Up @@ -173,7 +181,7 @@ void mdcii::world::Terrain::OnLeftMouseButtonPressed()
}

currentSelectedIsland = nullptr;
for (auto& island : islands)
for (auto const& island : islands)
{
if (island->IsWorldPositionInAabb(world->mousePicker->currentPosition))
{
Expand Down
Loading

0 comments on commit bea7f1c

Please sign in to comment.