diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 8d9119ee..b9fcc1e3 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -11,6 +11,7 @@ #include "../files/engine_paths.hpp" #include "../content/Content.hpp" #include "../content/ContentPack.hpp" +#include "../voxels/Block.hpp" #include "../graphics/core/Texture.hpp" #include "../logic/scripting/scripting.hpp" @@ -187,7 +188,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) { loader.processPreloadConfigs(content); for (auto& entry : content->getBlockMaterials()) { - auto& material = entry.second; + auto& material = *entry.second; loader.tryAddSound(material.stepsSound); loader.tryAddSound(material.placeSound); loader.tryAddSound(material.breakSound); diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index bf5f7f13..939aa4ad 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -389,7 +389,7 @@ std::unique_ptr ALAudio::openStream(std::shared_ptr stream, b return std::make_unique(this, stream, keepSource); } -ALAudio* ALAudio::create() { +std::unique_ptr ALAudio::create() { ALCdevice* device = alcOpenDevice(nullptr); if (device == nullptr) return nullptr; @@ -400,7 +400,7 @@ ALAudio* ALAudio::create() { } AL_CHECK(); logger.info() << "initialized"; - return new ALAudio(device, context); + return std::make_unique(device, context); } uint ALAudio::getFreeSource(){ diff --git a/src/audio/AL/ALAudio.hpp b/src/audio/AL/ALAudio.hpp index 294b0224..f0a22dde 100644 --- a/src/audio/AL/ALAudio.hpp +++ b/src/audio/AL/ALAudio.hpp @@ -136,9 +136,8 @@ namespace audio { std::vector freebuffers; uint maxSources = 256; - - ALAudio(ALCdevice* device, ALCcontext* context); public: + ALAudio(ALCdevice* device, ALCcontext* context); ~ALAudio(); uint getFreeSource(); @@ -164,7 +163,7 @@ namespace audio { return false; } - static ALAudio* create(); + static std::unique_ptr create(); }; } diff --git a/src/audio/NoAudio.cpp b/src/audio/NoAudio.cpp index 3000b891..04dd2b77 100644 --- a/src/audio/NoAudio.cpp +++ b/src/audio/NoAudio.cpp @@ -17,6 +17,6 @@ std::unique_ptr NoAudio::openStream(std::shared_ptr stream, b return std::make_unique(stream, keepSource); } -NoAudio* NoAudio::create() { - return new NoAudio(); +std::unique_ptr NoAudio::create() { + return std::make_unique(); } diff --git a/src/audio/NoAudio.hpp b/src/audio/NoAudio.hpp index e24fb5c6..bc105139 100644 --- a/src/audio/NoAudio.hpp +++ b/src/audio/NoAudio.hpp @@ -81,7 +81,7 @@ namespace audio { return true; } - static NoAudio* create(); + static std::unique_ptr create(); }; } diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 3829c16a..9fe62de0 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -147,11 +147,11 @@ public: void audio::initialize(bool enabled) { if (enabled) { - backend = ALAudio::create(); + backend = ALAudio::create().release(); } if (backend == nullptr) { std::cerr << "could not to initialize audio" << std::endl; - backend = NoAudio::create(); + backend = NoAudio::create().release(); } create_channel("master"); } @@ -333,7 +333,7 @@ int audio::create_channel(const std::string& name) { if (index != -1) { return index; } - channels.emplace_back(new Channel(name)); + channels.emplace_back(std::make_unique(name)); return channels.size()-1; } diff --git a/src/content/Content.cpp b/src/content/Content.cpp index 4ba19fe3..4de7367c 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -10,127 +10,6 @@ #include "ContentPack.hpp" #include "../logic/scripting/scripting.hpp" -ContentBuilder::~ContentBuilder() {} - -void ContentBuilder::add(Block* def) { - checkIdentifier(def->name); - blockDefs[def->name] = def; - blockIds.push_back(def->name); -} - -void ContentBuilder::add(ItemDef* def) { - checkIdentifier(def->name); - itemDefs[def->name] = def; - itemIds.push_back(def->name); -} - -void ContentBuilder::add(ContentPackRuntime* pack) { - packs.emplace(pack->getId(), pack); -} - -void ContentBuilder::add(BlockMaterial material) { - blockMaterials.emplace(material.name, material); -} - -Block& ContentBuilder::createBlock(std::string id) { - auto found = blockDefs.find(id); - if (found != blockDefs.end()) { - return *found->second; - // throw namereuse_error("name "+id+" is already used", contenttype::item); - } - Block* block = new Block(id); - add(block); - return *block; -} - -ItemDef& ContentBuilder::createItem(std::string id) { - auto found = itemDefs.find(id); - if (found != itemDefs.end()) { - // if (found->second->generated) { - return *found->second; - // } - // throw namereuse_error("name "+id+" is already used", contenttype::item); - } - ItemDef* item = new ItemDef(id); - add(item); - return *item; -} - -void ContentBuilder::checkIdentifier(std::string id) { - contenttype result; - if (((result = checkContentType(id)) != contenttype::none)) { - throw namereuse_error("name "+id+" is already used", result); - } -} - -contenttype ContentBuilder::checkContentType(std::string id) { - if (blockDefs.find(id) != blockDefs.end()) { - return contenttype::block; - } - if (itemDefs.find(id) != itemDefs.end()) { - return contenttype::item; - } - return contenttype::none; -} - -Content* ContentBuilder::build() { - std::vector blockDefsIndices; - auto groups = std::make_unique(); - for (const std::string& name : blockIds) { - Block* def = blockDefs[name]; - - // Generating runtime info - def->rt.id = blockDefsIndices.size(); - def->rt.emissive = *reinterpret_cast(def->emission); - def->rt.solid = def->model == BlockModel::block; - - if (def->rotatable) { - for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) { - def->rt.hitboxes[i].reserve(def->hitboxes.size()); - for (AABB aabb : def->hitboxes) { - def->rotations.variants[i].transform(aabb); - def->rt.hitboxes[i].push_back(aabb); - } - } - } - - blockDefsIndices.push_back(def); - groups->insert(def->drawGroup); - } - - std::vector itemDefsIndices; - for (const std::string& name : itemIds) { - ItemDef* def = itemDefs[name]; - - // Generating runtime info - def->rt.id = itemDefsIndices.size(); - def->rt.emissive = *reinterpret_cast(def->emission); - itemDefsIndices.push_back(def); - } - - auto indices = new ContentIndices(blockDefsIndices, itemDefsIndices); - - auto content = std::make_unique( - indices, - std::move(groups), - blockDefs, - itemDefs, - std::move(packs), - std::move(blockMaterials) - ); - - // Now, it's time to resolve foreign keys - for (Block* def : blockDefsIndices) { - def->rt.pickingItem = content->requireItem(def->pickingItem).rt.id; - } - - for (ItemDef* def : itemDefsIndices) { - def->rt.placingBlock = content->requireBlock(def->placingBlock).rt.id; - } - - return content.release(); -} - ContentIndices::ContentIndices( std::vector blockDefs, std::vector itemDefs @@ -139,27 +18,21 @@ ContentIndices::ContentIndices( {} Content::Content( - ContentIndices* indices, + std::unique_ptr indices, std::unique_ptr drawGroups, - std::unordered_map blockDefs, - std::unordered_map itemDefs, + std::unordered_map> blockDefs, + std::unordered_map> itemDefs, std::unordered_map> packs, - std::unordered_map blockMaterials -) : blockDefs(blockDefs), - itemDefs(itemDefs), - indices(indices), + std::unordered_map> blockMaterials +) : blockDefs(std::move(blockDefs)), + itemDefs(std::move(itemDefs)), + indices(std::move(indices)), packs(std::move(packs)), blockMaterials(std::move(blockMaterials)), drawGroups(std::move(drawGroups)) {} Content::~Content() { - for (auto& entry : blockDefs) { - delete entry.second; - } - for (auto& entry : itemDefs) { - delete entry.second; - } } Block* Content::findBlock(std::string id) const { @@ -167,7 +40,7 @@ Block* Content::findBlock(std::string id) const { if (found == blockDefs.end()) { return nullptr; } - return found->second; + return found->second.get(); } Block& Content::requireBlock(std::string id) const { @@ -183,7 +56,7 @@ ItemDef* Content::findItem(std::string id) const { if (found == itemDefs.end()) { return nullptr; } - return found->second; + return found->second.get(); } ItemDef& Content::requireItem(std::string id) const { @@ -199,7 +72,7 @@ const BlockMaterial* Content::findBlockMaterial(std::string id) const { if (found == blockMaterials.end()) { return nullptr; } - return &found->second; + return found->second.get(); } const ContentPackRuntime* Content::getPackRuntime(std::string id) const { @@ -210,7 +83,7 @@ const ContentPackRuntime* Content::getPackRuntime(std::string id) const { return found->second.get(); } -const std::unordered_map& Content::getBlockMaterials() const { +const std::unordered_map>& Content::getBlockMaterials() const { return blockMaterials; } diff --git a/src/content/Content.hpp b/src/content/Content.hpp index dcf3c56f..c9df8417 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -1,17 +1,19 @@ #ifndef CONTENT_CONTENT_HPP_ #define CONTENT_CONTENT_HPP_ +#include "../typedefs.hpp" + #include #include #include #include #include #include -#include "../typedefs.hpp" -#include "../voxels/Block.hpp" using DrawGroups = std::set; +class Block; +struct BlockMaterial; class ItemDef; class Content; class ContentPackRuntime; @@ -41,33 +43,6 @@ public: } }; -class ContentBuilder { - std::unordered_map blockDefs; - std::vector blockIds; - - std::unordered_map itemDefs; - std::vector itemIds; - - std::unordered_map blockMaterials; - - std::unordered_map> packs; -public: - ~ContentBuilder(); - - void add(Block* def); - void add(ItemDef* def); - void add(ContentPackRuntime* pack); - void add(BlockMaterial material); - - Block& createBlock(std::string id); - ItemDef& createItem(std::string id); - - void checkIdentifier(std::string id); - contenttype checkContentType(std::string id); - - Content* build(); -}; - /// @brief Runtime defs cache: indices class ContentIndices { std::vector blockDefs; @@ -110,21 +85,21 @@ public: /* Content is a definitions repository */ class Content { - std::unordered_map blockDefs; - std::unordered_map itemDefs; + std::unordered_map> blockDefs; + std::unordered_map> itemDefs; std::unique_ptr indices; std::unordered_map> packs; - std::unordered_map blockMaterials; + std::unordered_map> blockMaterials; public: std::unique_ptr const drawGroups; Content( - ContentIndices* indices, + std::unique_ptr indices, std::unique_ptr drawGroups, - std::unordered_map blockDefs, - std::unordered_map itemDefs, + std::unordered_map> blockDefs, + std::unordered_map> itemDefs, std::unordered_map> packs, - std::unordered_map blockMaterials + std::unordered_map> blockMaterials ); ~Content(); @@ -142,7 +117,7 @@ public: const ContentPackRuntime* getPackRuntime(std::string id) const; - const std::unordered_map& getBlockMaterials() const; + const std::unordered_map>& getBlockMaterials() const; const std::unordered_map>& getPacks() const; }; diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp new file mode 100644 index 00000000..16ec0a85 --- /dev/null +++ b/src/content/ContentBuilder.cpp @@ -0,0 +1,109 @@ +#include "ContentBuilder.hpp" + +ContentBuilder::~ContentBuilder() {} + +void ContentBuilder::add(std::unique_ptr pack) { + packs[pack->getId()] = std::move(pack); +} + +Block& ContentBuilder::createBlock(std::string id) { + auto found = blockDefs.find(id); + if (found != blockDefs.end()) { + return *found->second; + } + checkIdentifier(id); + blockIds.push_back(id); + blockDefs[id] = std::make_unique(id); + return *blockDefs[id]; +} + +ItemDef& ContentBuilder::createItem(std::string id) { + auto found = itemDefs.find(id); + if (found != itemDefs.end()) { + return *found->second; + } + checkIdentifier(id); + itemIds.push_back(id); + itemDefs[id] = std::make_unique(id); + return *itemDefs[id]; +} + +BlockMaterial& ContentBuilder::createBlockMaterial(std::string id) { + blockMaterials[id] = std::make_unique(); + auto& material = *blockMaterials[id]; + material.name = id; + return material; +} + +void ContentBuilder::checkIdentifier(std::string id) { + contenttype result; + if (((result = checkContentType(id)) != contenttype::none)) { + throw namereuse_error("name "+id+" is already used", result); + } +} + +contenttype ContentBuilder::checkContentType(std::string id) { + if (blockDefs.find(id) != blockDefs.end()) { + return contenttype::block; + } + if (itemDefs.find(id) != itemDefs.end()) { + return contenttype::item; + } + return contenttype::none; +} + +std::unique_ptr ContentBuilder::build() { + std::vector blockDefsIndices; + auto groups = std::make_unique(); + for (const std::string& name : blockIds) { + Block& def = *blockDefs[name]; + + // Generating runtime info + def.rt.id = blockDefsIndices.size(); + def.rt.emissive = *reinterpret_cast(def.emission); + def.rt.solid = def.model == BlockModel::block; + + if (def.rotatable) { + for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) { + def.rt.hitboxes[i].reserve(def.hitboxes.size()); + for (AABB& aabb : def.hitboxes) { + def.rotations.variants[i].transform(aabb); + def.rt.hitboxes[i].push_back(aabb); + } + } + } + + blockDefsIndices.push_back(&def); + groups->insert(def.drawGroup); + } + + std::vector itemDefsIndices; + for (const std::string& name : itemIds) { + ItemDef& def = *itemDefs[name]; + + // Generating runtime info + def.rt.id = itemDefsIndices.size(); + def.rt.emissive = *reinterpret_cast(def.emission); + itemDefsIndices.push_back(&def); + } + + auto content = std::make_unique( + std::make_unique(blockDefsIndices, itemDefsIndices), + std::move(groups), + std::move(blockDefs), + std::move(itemDefs), + std::move(packs), + std::move(blockMaterials) + ); + + // Now, it's time to resolve foreign keys + for (Block* def : blockDefsIndices) { + def->rt.pickingItem = content->requireItem(def->pickingItem).rt.id; + } + + for (ItemDef* def : itemDefsIndices) { + def->rt.placingBlock = content->requireBlock(def->placingBlock).rt.id; + } + + return content; +} diff --git a/src/content/ContentBuilder.hpp b/src/content/ContentBuilder.hpp new file mode 100644 index 00000000..f6bcbd5e --- /dev/null +++ b/src/content/ContentBuilder.hpp @@ -0,0 +1,37 @@ +#ifndef CONTENT_CONTENT_BUILDER_HPP_ +#define CONTENT_CONTENT_BUILDER_HPP_ + +#include "../items/ItemDef.hpp" +#include "../voxels/Block.hpp" +#include "../content/Content.hpp" +#include "../content/ContentPack.hpp" + +#include +#include +#include + +class ContentBuilder { + std::unordered_map> blockDefs; + std::vector blockIds; + + std::unordered_map> itemDefs; + std::vector itemIds; + + std::unordered_map> blockMaterials; + std::unordered_map> packs; +public: + ~ContentBuilder(); + + void add(std::unique_ptr pack); + + Block& createBlock(std::string id); + ItemDef& createItem(std::string id); + BlockMaterial& createBlockMaterial(std::string id); + + void checkIdentifier(std::string id); + contenttype checkContentType(std::string id); + + std::unique_ptr build(); +}; + +#endif // CONTENT_CONTENT_BUILDER_HPP_ diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 9878d9ef..36a7efa8 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -2,6 +2,7 @@ #include "Content.hpp" #include "ContentPack.hpp" +#include "ContentBuilder.hpp" #include "../coders/json.hpp" #include "../core_defs.hpp" #include "../data/dynamic.hpp" @@ -26,9 +27,11 @@ static debug::Logger logger("content-loader"); ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) { } -bool ContentLoader::fixPackIndices(fs::path folder, - dynamic::Map* indicesRoot, - std::string contentSection) { +bool ContentLoader::fixPackIndices( + fs::path folder, + dynamic::Map* indicesRoot, + std::string contentSection +) { std::vector detected; std::vector indexed; if (fs::is_directory(folder)) { @@ -312,22 +315,22 @@ void ContentLoader::loadItem(ItemDef& def, std::string full, std::string name) { } } -BlockMaterial ContentLoader::loadBlockMaterial(fs::path file, std::string full) { +void ContentLoader::loadBlockMaterial(BlockMaterial& def, fs::path file) { auto root = files::read_json(file); - BlockMaterial material {full}; - root->str("steps-sound", material.stepsSound); - root->str("place-sound", material.placeSound); - root->str("break-sound", material.breakSound); - return material; + root->str("steps-sound", def.stepsSound); + root->str("place-sound", def.placeSound); + root->str("break-sound", def.breakSound); } void ContentLoader::load(ContentBuilder& builder) { logger.info() << "loading pack [" << pack->id << "]"; - auto runtime = new ContentPackRuntime(*pack, scripting::create_pack_environment(*pack)); - builder.add(runtime); + auto runtime = std::make_unique( + *pack, scripting::create_pack_environment(*pack) + ); env = runtime->getEnvironment(); ContentPackStats& stats = runtime->getStatsWriteable(); + builder.add(std::move(runtime)); fixPackIndices(); @@ -388,7 +391,7 @@ void ContentLoader::load(ContentBuilder& builder) { for (auto entry : fs::directory_iterator(materialsDir)) { fs::path file = entry.path(); std::string name = pack->id+":"+file.stem().u8string(); - builder.add(loadBlockMaterial(file, name)); + loadBlockMaterial(builder.createBlockMaterial(name), file); } } } diff --git a/src/content/ContentLoader.hpp b/src/content/ContentLoader.hpp index d709f0d0..515d742a 100644 --- a/src/content/ContentLoader.hpp +++ b/src/content/ContentLoader.hpp @@ -1,13 +1,15 @@ #ifndef CONTENT_CONTENT_LOADER_HPP_ #define CONTENT_CONTENT_LOADER_HPP_ -#include "../voxels/Block.hpp" +#include "../typedefs.hpp" #include #include namespace fs = std::filesystem; +class Block; +struct BlockMaterial; class ItemDef; struct ContentPack; class ContentBuilder; @@ -23,7 +25,7 @@ class ContentLoader { void loadBlock(Block& def, std::string full, std::string name); void loadCustomBlockModel(Block& def, dynamic::Map* primitives); void loadItem(ItemDef& def, std::string full, std::string name); - BlockMaterial loadBlockMaterial(fs::path file, std::string full); + void loadBlockMaterial(BlockMaterial& def, fs::path file); public: ContentLoader(ContentPack* pack); diff --git a/src/content/ContentPack.hpp b/src/content/ContentPack.hpp index d8790b8b..e58fbd84 100644 --- a/src/content/ContentPack.hpp +++ b/src/content/ContentPack.hpp @@ -12,17 +12,11 @@ class EnginePaths; namespace fs = std::filesystem; -namespace scripting { - class Environment; -} - class contentpack_error : public std::runtime_error { std::string packId; fs::path folder; public: - contentpack_error(std::string packId, - fs::path folder, - std::string message); + contentpack_error(std::string packId, fs::path folder, std::string message); std::string getPackId() const; fs::path getFolder() const; diff --git a/src/core_defs.cpp b/src/core_defs.cpp index 12e840b4..e0a4e6b0 100644 --- a/src/core_defs.cpp +++ b/src/core_defs.cpp @@ -2,6 +2,7 @@ #include "items/ItemDef.hpp" #include "content/Content.hpp" +#include "content/ContentBuilder.hpp" #include "window/Window.hpp" #include "window/Events.hpp" #include "window/input.hpp" diff --git a/src/engine.cpp b/src/engine.cpp index a903453e..a4b9f5bf 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -9,6 +9,7 @@ #include "coders/imageio.hpp" #include "coders/json.hpp" #include "coders/toml.hpp" +#include "content/ContentBuilder.hpp" #include "content/ContentLoader.hpp" #include "core_defs.hpp" #include "files/files.hpp" @@ -273,7 +274,7 @@ void Engine::loadContent() { ContentLoader loader(&pack); loader.load(contentBuilder); } - content.reset(contentBuilder.build()); + content = contentBuilder.build(); resPaths = std::make_unique(resdir, resRoots); langs::setup(resdir, langs::current->getId(), contentPacks); diff --git a/src/frontend/screens/MenuScreen.cpp b/src/frontend/screens/MenuScreen.cpp index 08d4f8a4..96b7656c 100644 --- a/src/frontend/screens/MenuScreen.cpp +++ b/src/frontend/screens/MenuScreen.cpp @@ -5,6 +5,7 @@ #include "../../graphics/core/Batch2D.hpp" #include "../../graphics/core/Shader.hpp" #include "../../graphics/core/Texture.hpp" +#include "../../maths/UVRegion.hpp" #include "../../window/Window.hpp" #include "../../window/Camera.hpp" #include "../../engine.hpp"