diff --git a/res/scripts/hud_classes.lua b/res/scripts/hud_classes.lua new file mode 100644 index 00000000..dc373c6e --- /dev/null +++ b/res/scripts/hud_classes.lua @@ -0,0 +1,18 @@ +local Text3D = {__index={ + hide=function(self) return gfx.text3d.hide(self.id) end, + get_pos=function(self) return gfx.text3d.get_pos(self.id) end, + set_pos=function(self, v) return gfx.text3d.set_pos(self.id, v) end, + get_axis_x=function(self) return gfx.text3d.get_axis_x(self.id) end, + set_axis_x=function(self, v) return gfx.text3d.set_axis_x(self.id, v) end, + get_axis_y=function(self) return gfx.text3d.get_axis_y(self.id) end, + set_axis_y=function(self, v) return gfx.text3d.set_axis_y(self.id, v) end, + set_rotation=function(self, m) return gfx.text3d.set_rotation(self.id, m) end, + get_text=function(self) return gfx.text3d.get_text(self.id) end, + set_text=function(self, s) return gfx.text3d.set_text(self.id, s) end, + update_settings=function(self, t) return gfx.text3d.update_settings(self.id, t) end, +}} + +gfx.text3d.new = function(pos, text, preset, extension) + local id = gfx.text3d.show(pos, text, preset, extension) + return setmetatable({id=id}, Text3D) +end diff --git a/src/assets/assetload_funcs.cpp b/src/assets/assetload_funcs.cpp index a7d258ec..b73a3882 100644 --- a/src/assets/assetload_funcs.cpp +++ b/src/assets/assetload_funcs.cpp @@ -32,7 +32,7 @@ static debug::Logger logger("assetload-funcs"); namespace fs = std::filesystem; -static bool animation( +static bool load_animation( Assets* assets, const ResPaths* paths, const std::string& atlasName, @@ -102,8 +102,13 @@ static bool append_atlas(AtlasBuilder& atlas, const fs::path& file) { return true; } -assetload::postfunc assetload:: - atlas(AssetsLoader*, const ResPaths* paths, const std::string& directory, const std::string& name, const std::shared_ptr&) { +assetload::postfunc assetload::atlas( + AssetsLoader*, + const ResPaths* paths, + const std::string& directory, + const std::string& name, + const std::shared_ptr& +) { AtlasBuilder builder; for (const auto& file : paths->listdir(directory)) { if (!imageio::is_read_supported(file.extension().u8string())) continue; @@ -115,24 +120,41 @@ assetload::postfunc assetload:: atlas->prepare(); assets->store(std::unique_ptr(atlas), name); for (const auto& file : names) { - animation(assets, paths, name, directory, file, atlas); + load_animation(assets, paths, name, directory, file, atlas); } }; } -assetload::postfunc assetload:: - font(AssetsLoader*, const ResPaths* paths, const std::string& filename, const std::string& name, const std::shared_ptr&) { +assetload::postfunc assetload::font( + AssetsLoader*, + const ResPaths* paths, + const std::string& filename, + const std::string& name, + const std::shared_ptr& +) { auto pages = std::make_shared>>(); - for (size_t i = 0; i <= 4; i++) { + for (size_t i = 0; i <= 1024; i++) { std::string pagefile = filename + "_" + std::to_string(i) + ".png"; - pagefile = paths->find(pagefile).string(); - pages->push_back(imageio::read(pagefile)); + auto file = paths->find(pagefile); + if (fs::exists(file)) { + pages->push_back(imageio::read(file.u8string())); + } else if (i == 0) { + throw std::runtime_error("font must have page 0"); + } else { + pages->push_back(nullptr); + } } return [=](auto assets) { int res = pages->at(0)->getHeight() / 16; std::vector> textures; for (auto& page : *pages) { - textures.emplace_back(Texture::from(page.get())); + if (page == nullptr) { + textures.emplace_back(nullptr); + } else { + auto texture = Texture::from(page.get()); + texture->setMipMapping(false); + textures.emplace_back(std::move(texture)); + } } assets->store( std::make_unique(std::move(textures), res, 4), name @@ -316,11 +338,9 @@ static TextureAnimation create_animation( if (elem.second > 0) { frame.duration = static_cast(elem.second) / 1000.0f; } - frame.srcPos = - glm::ivec2( - region.u1 * srcWidth, srcHeight - region.v2 * srcHeight - ) - - extension; + frame.srcPos = glm::ivec2( + region.u1 * srcWidth, srcHeight - region.v2 * srcHeight + ) - extension; animation.addFrame(frame); } return animation; @@ -338,7 +358,7 @@ inline bool contains( return false; } -static bool animation( +static bool load_animation( Assets* assets, const ResPaths* paths, const std::string& atlasName, 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 b3d6aca0..9043f5b9 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -11,6 +11,7 @@ #include "graphics/ui/elements/InputBindBox.hpp" #include "graphics/render/WorldRenderer.hpp" #include "graphics/render/ParticlesRenderer.hpp" +#include "graphics/render/ChunksRenderer.hpp" #include "logic/scripting/scripting.hpp" #include "objects/Player.hpp" #include "objects/Entities.hpp" @@ -41,8 +42,8 @@ static std::shared_ptr