From c4170c07c56dde0337070a7df205cc94ffae0bd6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 14 Nov 2024 09:30:41 +0300 Subject: [PATCH] refactor --- src/frontend/ContentGfxCache.cpp | 16 +++--- src/frontend/ContentGfxCache.hpp | 4 +- src/frontend/LevelFrontend.cpp | 35 ++++++++------ src/frontend/LevelFrontend.hpp | 13 ++--- src/frontend/debug_panel.cpp | 63 ++++++++++++------------ src/frontend/hud.cpp | 54 ++++++++++----------- src/frontend/hud.hpp | 4 +- src/frontend/screens/LevelScreen.cpp | 17 ++++--- src/graphics/render/BlocksPreview.cpp | 67 +++++++++++++------------- src/graphics/render/BlocksPreview.hpp | 16 +++--- src/graphics/render/BlocksRenderer.cpp | 26 +++++----- src/graphics/render/BlocksRenderer.hpp | 13 +++-- src/graphics/render/ChunksRenderer.cpp | 40 +++++++-------- src/graphics/render/ChunksRenderer.hpp | 12 ++--- src/graphics/render/Skybox.cpp | 39 +++++++++------ src/graphics/render/Skybox.hpp | 4 +- src/graphics/render/WorldRenderer.cpp | 47 +++++++++--------- src/graphics/render/WorldRenderer.hpp | 9 +--- src/logic/scripting/scripting_hud.cpp | 2 +- src/world/Level.cpp | 4 ++ src/world/Level.hpp | 2 + 21 files changed, 255 insertions(+), 232 deletions(-) diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 7d3944b7..c6ba4e1c 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -11,25 +11,25 @@ #include "maths/UVRegion.hpp" #include "voxels/Block.hpp" -ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) +ContentGfxCache::ContentGfxCache(const Content* content, const Assets& assets) : content(content) { auto indices = content->getIndices(); sideregions = std::make_unique(indices->blocks.count() * 6); - auto atlas = assets->get("blocks"); + const auto& atlas = assets.require("blocks"); const auto& blocks = indices->blocks.getIterable(); for (blockid_t i = 0; i < blocks.size(); i++) { auto def = blocks[i]; for (uint side = 0; side < 6; side++) { const std::string& tex = def->textureFaces[side]; - if (atlas->has(tex)) { - sideregions[i * 6 + side] = atlas->get(tex); - } else if (atlas->has(TEXTURE_NOTFOUND)) { - sideregions[i * 6 + side] = atlas->get(TEXTURE_NOTFOUND); + if (atlas.has(tex)) { + sideregions[i * 6 + side] = atlas.get(tex); + } else if (atlas.has(TEXTURE_NOTFOUND)) { + sideregions[i * 6 + side] = atlas.get(TEXTURE_NOTFOUND); } } if (def->model == BlockModel::custom) { - auto model = assets->require(def->modelName); + auto model = assets.require(def->modelName); // temporary dirty fix tbh if (def->modelName.find(':') == std::string::npos) { for (auto& mesh : model.meshes) { @@ -37,7 +37,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) if (pos == std::string::npos) { continue; } - if (auto region = atlas->getIf(mesh.texture.substr(pos+1))) { + if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) { for (auto& vertex : mesh.vertices) { vertex.uv = region->apply(vertex.uv); } diff --git a/src/frontend/ContentGfxCache.hpp b/src/frontend/ContentGfxCache.hpp index 3d3161d3..96d46ea5 100644 --- a/src/frontend/ContentGfxCache.hpp +++ b/src/frontend/ContentGfxCache.hpp @@ -22,7 +22,7 @@ class ContentGfxCache { std::unique_ptr sideregions; std::unordered_map models; public: - ContentGfxCache(const Content* content, Assets* assets); + ContentGfxCache(const Content* content, const Assets& assets); ~ContentGfxCache(); inline const UVRegion& getRegion(blockid_t id, int side) const { @@ -30,6 +30,6 @@ public: } const model::Model& getModel(blockid_t id) const; - + const Content* getContent() const; }; diff --git a/src/frontend/LevelFrontend.cpp b/src/frontend/LevelFrontend.cpp index 977a0350..79784375 100644 --- a/src/frontend/LevelFrontend.cpp +++ b/src/frontend/LevelFrontend.cpp @@ -14,25 +14,28 @@ #include "world/Level.hpp" LevelFrontend::LevelFrontend( - Player* currentPlayer, LevelController* controller, Assets* assets -) : level(controller->getLevel()), + Player* currentPlayer, LevelController* controller, Assets& assets +) : level(*controller->getLevel()), controller(controller), assets(assets), - contentCache(std::make_unique(level->content, assets)) + contentCache(std::make_unique(level.content, assets)) { - assets->store( - BlocksPreview::build(contentCache.get(), assets, level->content), + assets.store( + BlocksPreview::build( + *contentCache, assets, *level.content->getIndices() + ), "block-previews" ); controller->getBlocksController()->listenBlockInteraction( - [=](auto player, const auto& pos, const auto& def, BlockInteraction type) { - auto material = level->content->findBlockMaterial(def.material); + [currentPlayer, controller, &assets](auto player, const auto& pos, const auto& def, BlockInteraction type) { + const auto& level = *controller->getLevel(); + auto material = level.content->findBlockMaterial(def.material); if (material == nullptr) { return; } if (type == BlockInteraction::step) { - auto sound = assets->get(material->stepsSound); + auto sound = assets.get(material->stepsSound); glm::vec3 pos {}; auto soundsCamera = currentPlayer->currentCamera.get(); if (soundsCamera == currentPlayer->spCamera.get() || @@ -58,10 +61,10 @@ LevelFrontend::LevelFrontend( audio::Sound* sound = nullptr; switch (type) { case BlockInteraction::placing: - sound = assets->get(material->placeSound); + sound = assets.get(material->placeSound); break; case BlockInteraction::destruction: - sound = assets->get(material->breakSound); + sound = assets.get(material->breakSound); break; default: break; @@ -83,16 +86,20 @@ LevelFrontend::LevelFrontend( LevelFrontend::~LevelFrontend() = default; -Level* LevelFrontend::getLevel() const { +Level& LevelFrontend::getLevel() { return level; } -Assets* LevelFrontend::getAssets() const { +const Level& LevelFrontend::getLevel() const { + return level; +} + +const Assets& LevelFrontend::getAssets() const { return assets; } -ContentGfxCache* LevelFrontend::getContentGfxCache() const { - return contentCache.get(); +const ContentGfxCache& LevelFrontend::getContentGfxCache() const { + return *contentCache; } LevelController* LevelFrontend::getController() const { diff --git a/src/frontend/LevelFrontend.hpp b/src/frontend/LevelFrontend.hpp index ed149598..163fc678 100644 --- a/src/frontend/LevelFrontend.hpp +++ b/src/frontend/LevelFrontend.hpp @@ -9,16 +9,17 @@ class ContentGfxCache; class LevelController; class LevelFrontend { - Level* level; + Level& level; LevelController* controller; - Assets* assets; + const Assets& assets; std::unique_ptr contentCache; public: - LevelFrontend(Player* currentPlayer, LevelController* controller, Assets* assets); + LevelFrontend(Player* currentPlayer, LevelController* controller, Assets& assets); ~LevelFrontend(); - Level* getLevel() const; - Assets* getAssets() const; - ContentGfxCache* getContentGfxCache() const; + Level& getLevel(); + const Level& getLevel() const; + const Assets& getAssets() const; + const ContentGfxCache& getContentGfxCache() const; LevelController* getController() const; }; diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index a8d205b3..9043f5b9 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -42,8 +42,8 @@ static std::shared_ptr