From d54b6b2e58a22b147a49c89bfb51c826095abf6b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 22 Jun 2024 22:30:14 +0300 Subject: [PATCH] update Assets container - template-based now --- src/assets/Assets.cpp | 75 ---------------------- src/assets/Assets.hpp | 55 ++++++++-------- src/assets/AssetsLoader.cpp | 2 +- src/frontend/ContentGfxCache.cpp | 2 +- src/frontend/LevelFrontend.cpp | 6 +- src/frontend/hud.cpp | 8 +-- src/frontend/screens/MenuScreen.cpp | 4 +- src/graphics/render/BlocksPreview.cpp | 4 +- src/graphics/render/ModelBatch.cpp | 2 +- src/graphics/render/Skybox.cpp | 6 +- src/graphics/render/WorldRenderer.cpp | 14 ++-- src/graphics/ui/GUI.cpp | 2 +- src/graphics/ui/elements/Image.cpp | 2 +- src/graphics/ui/elements/InventoryView.cpp | 9 +-- src/graphics/ui/elements/Label.cpp | 2 +- src/graphics/ui/elements/Plotter.cpp | 2 +- src/graphics/ui/elements/TextBox.cpp | 2 +- src/logic/scripting/lua/libaudio.cpp | 2 +- src/logic/scripting/lua/libgui.cpp | 6 +- src/logic/scripting/lua/libhud.cpp | 6 +- 20 files changed, 68 insertions(+), 143 deletions(-) diff --git a/src/assets/Assets.cpp b/src/assets/Assets.cpp index 73ed5ede..c7ef6946 100644 --- a/src/assets/Assets.cpp +++ b/src/assets/Assets.cpp @@ -1,72 +1,8 @@ #include "Assets.hpp" -#include "../audio/audio.hpp" -#include "../graphics/core/Texture.hpp" -#include "../graphics/core/Shader.hpp" -#include "../graphics/core/Atlas.hpp" -#include "../graphics/core/Font.hpp" -#include "../frontend/UiDocument.hpp" -#include "../logic/scripting/scripting.hpp" - Assets::~Assets() { } -Texture* Assets::getTexture(const std::string& name) const { - auto found = textures.find(name); - if (found == textures.end()) - return nullptr; - return found->second.get(); -} - -void Assets::store(std::unique_ptr texture, const std::string& name){ - textures.emplace(name, std::move(texture)); -} - - -Shader* Assets::getShader(const std::string& name) const{ - auto found = shaders.find(name); - if (found == shaders.end()) - return nullptr; - return found->second.get(); -} - -void Assets::store(std::unique_ptr shader, const std::string& name){ - shaders.emplace(name, std::move(shader)); -} - -Font* Assets::getFont(const std::string& name) const { - auto found = fonts.find(name); - if (found == fonts.end()) - return nullptr; - return found->second.get(); -} - -void Assets::store(std::unique_ptr font, const std::string& name){ - fonts.emplace(name, std::move(font)); -} - -Atlas* Assets::getAtlas(const std::string& name) const { - auto found = atlases.find(name); - if (found == atlases.end()) - return nullptr; - return found->second.get(); -} - -void Assets::store(std::unique_ptr atlas, const std::string& name){ - atlases.emplace(name, std::move(atlas)); -} - -audio::Sound* Assets::getSound(const std::string& name) const { - auto found = sounds.find(name); - if (found == sounds.end()) - return nullptr; - return found->second.get(); -} - -void Assets::store(std::unique_ptr sound, const std::string& name) { - sounds.emplace(name, std::move(sound)); -} - const std::vector& Assets::getAnimations() { return animations; } @@ -74,14 +10,3 @@ const std::vector& Assets::getAnimations() { void Assets::store(const TextureAnimation& animation) { animations.emplace_back(animation); } - -UiDocument* Assets::getLayout(const std::string& name) const { - auto found = layouts.find(name); - if (found == layouts.end()) - return nullptr; - return found->second.get(); -} - -void Assets::store(std::unique_ptr layout, const std::string& name) { - layouts[name] = std::shared_ptr(std::move(layout)); -} diff --git a/src/assets/Assets.hpp b/src/assets/Assets.hpp index d4b0f31d..8338eb7e 100644 --- a/src/assets/Assets.hpp +++ b/src/assets/Assets.hpp @@ -7,57 +7,56 @@ #include #include #include +#include +#include #include -class Texture; -class Shader; -class Font; -class Atlas; class Assets; -class UiDocument; namespace audio { class Sound; } +namespace model { + struct Model; +} + namespace assetload { /// @brief final work to do in the main thread using postfunc = std::function; } class Assets { - std::unordered_map> textures; - std::unordered_map> shaders; - std::unordered_map> fonts; - std::unordered_map> atlases; - std::unordered_map> layouts; - std::unordered_map> sounds; std::vector animations; + + using assets_map = std::unordered_map>; + std::unordered_map assets; public: Assets() {} Assets(const Assets&) = delete; ~Assets(); - - Texture* getTexture(const std::string& name) const; - void store(std::unique_ptr texture, const std::string& name); - - Shader* getShader(const std::string& name) const; - void store(std::unique_ptr shader, const std::string& name); - - Font* getFont(const std::string& name) const; - void store(std::unique_ptr font, const std::string& name); - - Atlas* getAtlas(const std::string& name) const; - void store(std::unique_ptr atlas, const std::string& name); - - audio::Sound* getSound(const std::string& name) const; - void store(std::unique_ptr sound, const std::string& name); const std::vector& getAnimations(); void store(const TextureAnimation& animation); - UiDocument* getLayout(const std::string& name) const; - void store(std::unique_ptr layout, const std::string& name); + template + void store(std::unique_ptr asset, const std::string& name) { + assets[typeid(T)][name].reset(asset.release()); + } + + template + T* get(const std::string& name) const { + const auto& mapIter = assets.find(typeid(T)); + if (mapIter == assets.end()) { + return nullptr; + } + const auto& map = mapIter->second; + const auto& found = map.find(name); + if (found == map.end()) { + return nullptr; + } + return static_cast(found->second.get()); + } }; #endif // ASSETS_ASSETS_HPP_ diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index f55902e9..bf0ac243 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -212,7 +212,7 @@ bool AssetsLoader::loadExternalTexture( const std::string& name, const std::vector& alternatives ) { - if (assets->getTexture(name) != nullptr) { + if (assets->get(name) != nullptr) { return true; } for (auto& path : alternatives) { diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index 373e9730..de180760 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -15,7 +15,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : content(content) { auto indices = content->getIndices(); sideregions = std::make_unique(indices->countBlockDefs() * 6); - Atlas* atlas = assets->getAtlas("blocks"); + auto atlas = assets->get("blocks"); for (uint i = 0; i < indices->countBlockDefs(); i++) { Block* def = indices->getBlockDef(i); diff --git a/src/frontend/LevelFrontend.cpp b/src/frontend/LevelFrontend.cpp index 7a62ca50..3dffae5c 100644 --- a/src/frontend/LevelFrontend.cpp +++ b/src/frontend/LevelFrontend.cpp @@ -30,7 +30,7 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets) } if (type == BlockInteraction::step) { - auto sound = assets->getSound(material->stepsSound); + auto sound = assets->get(material->stepsSound); audio::play( sound, glm::vec3(), @@ -45,10 +45,10 @@ LevelFrontend::LevelFrontend(LevelController* controller, Assets* assets) audio::Sound* sound = nullptr; switch (type) { case BlockInteraction::placing: - sound = assets->getSound(material->placeSound); + sound = assets->get(material->placeSound); break; case BlockInteraction::destruction: - sound = assets->getSound(material->breakSound); + sound = assets->get(material->breakSound); break; case BlockInteraction::step: break; diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index bc8f2cbf..53f85baf 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -208,7 +208,7 @@ void Hud::processInput(bool visible) { } } if (!pause && Events::active(BIND_DEVTOOLS_CONSOLE)) { - showOverlay(assets->getLayout("core:console"), false); + showOverlay(assets->get("core:console"), false); } if (!Window::isFocused() && !pause && !isInventoryOpen()) { setPause(true); @@ -305,7 +305,7 @@ void Hud::openInventory() { inventoryOpen = true; auto inventory = player->getInventory(); - auto inventoryDocument = assets->getLayout("core:inventory"); + auto inventoryDocument = assets->get("core:inventory"); inventoryView = std::dynamic_pointer_cast(inventoryDocument->getRoot()); inventoryView->bind(inventory, content); add(HudElement(hud_element_mode::inventory_bound, inventoryDocument, inventoryView, false)); @@ -460,7 +460,7 @@ void Hud::draw(const DrawContext& ctx){ auto batch = ctx.getBatch2D(); batch->begin(); - Shader* uishader = assets->getShader("ui"); + auto uishader = assets->get("ui"); uishader->use(); uishader->uniformMatrix("u_projview", uicamera->getProjView()); @@ -468,7 +468,7 @@ void Hud::draw(const DrawContext& ctx){ if (!pause && !inventoryOpen && !player->debug) { DrawContext chctx = ctx.sub(); chctx.setBlendMode(BlendMode::inversion); - auto texture = assets->getTexture("gui/crosshair"); + auto texture = assets->get("gui/crosshair"); batch->texture(texture); int chsizex = texture != nullptr ? texture->getWidth() : 16; int chsizey = texture != nullptr ? texture->getHeight() : 16; diff --git a/src/frontend/screens/MenuScreen.cpp b/src/frontend/screens/MenuScreen.cpp index 96b7656c..74ad0421 100644 --- a/src/frontend/screens/MenuScreen.cpp +++ b/src/frontend/screens/MenuScreen.cpp @@ -35,14 +35,14 @@ void MenuScreen::draw(float delta) { Window::setBgColor(glm::vec3(0.2f)); uicamera->setFov(Window::height); - Shader* uishader = assets->getShader("ui"); + auto uishader = assets->get("ui"); uishader->use(); uishader->uniformMatrix("u_projview", uicamera->getProjView()); uint width = Window::width; uint height = Window::height; - auto bg = assets->getTexture("gui/menubg"); + auto bg = assets->get("gui/menubg"); batch->begin(); batch->texture(bg); batch->rect( diff --git a/src/graphics/render/BlocksPreview.cpp b/src/graphics/render/BlocksPreview.cpp index eb7e72cf..88a6f111 100644 --- a/src/graphics/render/BlocksPreview.cpp +++ b/src/graphics/render/BlocksPreview.cpp @@ -126,8 +126,8 @@ std::unique_ptr BlocksPreview::build( size_t count = indices->countBlockDefs(); size_t iconSize = ITEM_ICON_SIZE; - Shader* shader = assets->getShader("ui3d"); - Atlas* atlas = assets->getAtlas("blocks"); + auto shader = assets->get("ui3d"); + auto atlas = assets->get("blocks"); Viewport viewport(iconSize, iconSize); DrawContext pctx(nullptr, viewport, nullptr); diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index 4bc1be32..99aef358 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -60,7 +60,7 @@ void ModelBatch::draw(const model::Model& model) { Lightmap::extract(light, 3) / 15.0f ); for (const auto& mesh : model.meshes) { - auto texture = assets->getTexture(mesh.texture); + auto texture = assets->get(mesh.texture); if (texture) { texture->bind(); } else { diff --git a/src/graphics/render/Skybox.cpp b/src/graphics/render/Skybox.cpp index 65b255a9..49310609 100644 --- a/src/graphics/render/Skybox.cpp +++ b/src/graphics/render/Skybox.cpp @@ -59,7 +59,7 @@ Skybox::~Skybox() { } void Skybox::drawBackground(Camera* camera, Assets* assets, int width, int height) { - Shader* backShader = assets->getShader("background"); + auto backShader = assets->get("background"); backShader->use(); backShader->uniformMatrix("u_view", camera->getView(false)); backShader->uniform1f("u_zoom", camera->zoom*camera->getFov()/(M_PI*0.5f)); @@ -107,7 +107,7 @@ void Skybox::draw( DrawContext ctx = pctx.sub(); ctx.setBlendMode(BlendMode::addition); - Shader* shader = assets->getShader("ui3d"); + auto shader = assets->get("ui3d"); shader->use(); shader->uniformMatrix("u_projview", camera->getProjView(false)); shader->uniformMatrix("u_apply", glm::mat4(1.0f)); @@ -117,7 +117,7 @@ void Skybox::draw( float opacity = glm::pow(1.0f-fog, 7.0f); for (auto& sprite : sprites) { - batch3d->texture(assets->getTexture(sprite.texture)); + batch3d->texture(assets->get(sprite.texture)); float sangle = daytime * M_PI*2 + sprite.phase; float distance = sprite.distance; diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index a09a33b3..940cef92 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -68,7 +68,7 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl auto assets = engine->getAssets(); skybox = std::make_unique( settings.graphics.skyboxResolution.get(), - assets->getShader("skybox_gen") + assets->get("skybox_gen") ); } @@ -154,9 +154,9 @@ void WorldRenderer::renderLevel( Camera* camera, const EngineSettings& settings ) { - Assets* assets = engine->getAssets(); - Atlas* atlas = assets->getAtlas("blocks"); - Shader* shader = assets->getShader("main"); + auto assets = engine->getAssets(); + auto atlas = assets->get("blocks"); + auto shader = assets->get("main"); auto indices = level->content->getIndices(); @@ -292,8 +292,8 @@ void WorldRenderer::draw( const EngineSettings& settings = engine->getSettings(); skybox->refresh(pctx, world->daytime, 1.0f+world->fog*2.0f, 4); - Assets* assets = engine->getAssets(); - Shader* linesShader = assets->getShader("lines"); + auto assets = engine->getAssets(); + auto linesShader = assets->get("lines"); // World render scope with diegetic HUD included { @@ -323,7 +323,7 @@ void WorldRenderer::draw( } // Rendering fullscreen quad with - auto screenShader = assets->getShader("screen"); + auto screenShader = assets->get("screen"); screenShader->use(); screenShader->uniform1f("u_timer", Window::time()); screenShader->uniform1f("u_dayTime", level->getWorld()->daytime); diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 7881d547..54c04476 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -205,7 +205,7 @@ void GUI::draw(const DrawContext* pctx, Assets* assets) { menu->setPos((wsize - menu->getSize()) / 2.0f); uicamera->setFov(wsize.y); - Shader* uishader = assets->getShader("ui"); + auto uishader = assets->get("ui"); uishader->use(); uishader->uniformMatrix("u_projview", uicamera->getProjView()); diff --git a/src/graphics/ui/elements/Image.cpp b/src/graphics/ui/elements/Image.cpp index d99c5a9d..e3b7243e 100644 --- a/src/graphics/ui/elements/Image.cpp +++ b/src/graphics/ui/elements/Image.cpp @@ -18,7 +18,7 @@ void Image::draw(const DrawContext* pctx, Assets* assets) { glm::vec2 pos = calcPos(); auto batch = pctx->getBatch2D(); - auto texture = assets->getTexture(this->texture); + auto texture = assets->get(this->texture); if (texture && autoresize) { setSize(glm::vec2(texture->getWidth(), texture->getHeight())); } diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index b364425f..92084c37 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -21,6 +21,7 @@ #include "../../core/Font.hpp" #include "../../core/DrawContext.hpp" #include "../../core/Shader.hpp" +#include "../../core/Texture.hpp" #include "../../render/BlocksPreview.hpp" #include "../GUI.hpp" @@ -155,7 +156,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { batch->setColor(glm::vec4(1.0f)); - auto previews = assets->getAtlas("block-previews"); + auto previews = assets->get("block-previews"); auto indices = content->getIndices(); ItemDef* item = indices->getItemDef(stack.getItemId()); @@ -177,10 +178,10 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { std::string name = item->icon.substr(index+1); UVRegion region(0.0f, 0.0, 1.0f, 1.0f); if (index == std::string::npos) { - batch->texture(assets->getTexture(name)); + batch->texture(assets->get(name)); } else { std::string atlasname = item->icon.substr(0, index); - Atlas* atlas = assets->getAtlas(atlasname); + auto atlas = assets->get(atlasname); if (atlas && atlas->has(name)) { region = atlas->get(name); batch->texture(atlas->getTexture()); @@ -194,7 +195,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { } if (stack.getCount() > 1) { - auto font = assets->getFont("normal"); + auto font = assets->get("normal"); std::wstring text = std::to_wstring(stack.getCount()); int x = pos.x+slotSize-text.length()*8; diff --git a/src/graphics/ui/elements/Label.cpp b/src/graphics/ui/elements/Label.cpp index d4a9019c..ac31e2a9 100644 --- a/src/graphics/ui/elements/Label.cpp +++ b/src/graphics/ui/elements/Label.cpp @@ -158,7 +158,7 @@ uint Label::getLinesNumber() const { void Label::draw(const DrawContext* pctx, Assets* assets) { auto batch = pctx->getBatch2D(); - auto font = assets->getFont(fontName); + auto font = assets->get(fontName); cache.prepare(font, static_cast(glm::abs(getSize().x))); if (supplier) { diff --git a/src/graphics/ui/elements/Plotter.cpp b/src/graphics/ui/elements/Plotter.cpp index bcb653d7..28057ae5 100644 --- a/src/graphics/ui/elements/Plotter.cpp +++ b/src/graphics/ui/elements/Plotter.cpp @@ -37,7 +37,7 @@ void Plotter::draw(const DrawContext* pctx, Assets* assets) { } int current_point = static_cast(points[index % dmwidth]); - auto font = assets->getFont("normal"); + auto font = assets->get("normal"); for (int y = 0; y < dmheight; y += labelsInterval) { std::wstring string; if (current_point/16 == y/labelsInterval) { diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index b531bf68..26eaae96 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -33,7 +33,7 @@ TextBox::TextBox(std::wstring placeholder, glm::vec4 padding) void TextBox::draw(const DrawContext* pctx, Assets* assets) { Panel::draw(pctx, assets); - font = assets->getFont(label->getFontName()); + font = assets->get(label->getFontName()); if (!isFocused()) { return; diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libaudio.cpp index bec44719..ec874971 100644 --- a/src/logic/scripting/lua/libaudio.cpp +++ b/src/logic/scripting/lua/libaudio.cpp @@ -32,7 +32,7 @@ inline audio::speakerid_t play_sound( return 0; } auto assets = scripting::engine->getAssets(); - auto sound = assets->getSound(name); + auto sound = assets->get(name); if (sound == nullptr) { return 0; } diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index 258af55a..7555f48d 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -27,7 +27,7 @@ struct DocumentNode { }; static DocumentNode getDocumentNode(lua::State*, const std::string& name, const std::string& nodeName) { - auto doc = engine->getAssets()->getLayout(name); + auto doc = engine->getAssets()->get(name); if (doc == nullptr) { throw std::runtime_error("document '"+name+"' not found"); } @@ -545,7 +545,7 @@ static int l_gui_setattr(lua::State* L) { static int l_gui_get_env(lua::State* L) { auto name = lua::require_string(L, 1); - auto doc = engine->getAssets()->getLayout(name); + auto doc = engine->getAssets()->get(name); if (doc == nullptr) { throw std::runtime_error("document '"+std::string(name)+"' not found"); } @@ -566,7 +566,7 @@ static int l_gui_str(lua::State* L) { static int l_gui_reindex(lua::State* L) { auto name = lua::require_string(L, 1); - auto doc = engine->getAssets()->getLayout(name); + auto doc = engine->getAssets()->get(name); if (doc == nullptr) { throw std::runtime_error("document '"+std::string(name)+"' not found"); } diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index f2bd5535..5395e7a1 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -51,7 +51,7 @@ static int l_hud_open_block(lua::State* L) { } auto def = content->getIndices()->getBlockDef(vox->id); auto assets = engine->getAssets(); - auto layout = assets->getLayout(def->uiLayout); + auto layout = assets->get(def->uiLayout); if (layout == nullptr) { throw std::runtime_error("block '"+def->name+"' has no ui layout"); } @@ -71,7 +71,7 @@ static int l_hud_show_overlay(lua::State* L) { bool playerInventory = lua::toboolean(L, 2); auto assets = engine->getAssets(); - auto layout = assets->getLayout(name); + auto layout = assets->get(name); if (layout == nullptr) { throw std::runtime_error("there is no ui layout "+util::quote(name)); } @@ -81,7 +81,7 @@ static int l_hud_show_overlay(lua::State* L) { static UiDocument* require_layout(const char* name) { auto assets = engine->getAssets(); - auto layout = assets->getLayout(name); + auto layout = assets->get(name); if (layout == nullptr) { throw std::runtime_error("layout '"+std::string(name)+"' is not found"); }