diff --git a/res/content/base/resources.json b/res/content/base/resources.json new file mode 100644 index 00000000..e1628cc3 --- /dev/null +++ b/res/content/base/resources.json @@ -0,0 +1,7 @@ +{ + "camera": [ + "first-person", + "third-person-front", + "third-person-back" + ] +} diff --git a/src/content/Content.hpp b/src/content/Content.hpp index f64e063e..121b8e16 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -1,11 +1,12 @@ #ifndef CONTENT_CONTENT_HPP_ #define CONTENT_CONTENT_HPP_ -#include "../typedefs.hpp" +#include "content_fwd.hpp" #include #include #include +#include #include #include #include @@ -18,18 +19,12 @@ class Block; struct BlockMaterial; struct ItemDef; struct EntityDef; -class Content; -class ContentPackRuntime; namespace rigging { class SkeletonConfig; } -enum class contenttype { - none, block, item, entity -}; - -inline const char* contenttype_name(contenttype type) { +constexpr const char* contenttype_name(contenttype type) { switch (type) { case contenttype::none: return "none"; case contenttype::block: return "block"; @@ -111,6 +106,44 @@ public: } }; +class ResourceIndices { + std::vector names; + std::unordered_map indices; +public: + ResourceIndices() {} + + static constexpr size_t MISSING = -1; + + void add(std::string name) { + indices[name] = names.size(); + names.push_back(name); + } + + size_t indexOf(const std::string& name) const { + const auto& found = indices.find(name); + if (found == indices.end()) { + return found->second; + } + return MISSING; + } +}; + +constexpr const char* to_string(ResourceType type) { + switch (type) { + case ResourceType::CAMERA: return "camera"; + default: return "unknown"; + } +} + +inline std::optional ResourceType_from(std::string_view str) { + if (str == "camera") { + return ResourceType::CAMERA; + } + return std::nullopt; +} + +using ResourceIndicesSet = ResourceIndices[static_cast(ResourceType::LAST)+1]; + /// @brief Content is a definitions repository class Content { std::unique_ptr indices; @@ -122,6 +155,7 @@ public: ContentUnitDefs items; ContentUnitDefs entities; std::unique_ptr const drawGroups; + ResourceIndicesSet resourceIndices {}; Content( std::unique_ptr indices, diff --git a/src/content/ContentBuilder.hpp b/src/content/ContentBuilder.hpp index 601b57e1..5c188ca3 100644 --- a/src/content/ContentBuilder.hpp +++ b/src/content/ContentBuilder.hpp @@ -57,6 +57,7 @@ public: ContentUnitBuilder blocks {allNames, contenttype::block}; ContentUnitBuilder items {allNames, contenttype::item}; ContentUnitBuilder entities {allNames, contenttype::entity}; + ResourceIndicesSet resourceIndices {}; ~ContentBuilder(); diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 2c5c691f..9ff3463b 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -478,9 +478,28 @@ void ContentLoader::load() { fs::path scriptfile = entry.path(); if (fs::is_regular_file(scriptfile)) { auto name = pack->id+":"+scriptfile.stem().u8string(); - std::cout << name << std::endl; scripting::load_entity_component(name, scriptfile); } } } + + fs::path resourcesFile = folder / fs::u8path("resources.json"); + if (fs::exists(resourcesFile)) { + auto resRoot = files::read_json(resourcesFile); + for (const auto& [key, _] : resRoot->values) { + if (auto resType = ResourceType_from(key)) { + if (auto arr = resRoot->list(key)) { + loadResources(*resType, arr.get()); + } + } else { + logger.warning() << "unknown resource type: " << key; + } + } + } +} + +void ContentLoader::loadResources(ResourceType type, dynamic::List* list) { + for (size_t i = 0; i < list->size(); i++) { + builder.resourceIndices[static_cast(type)].add(list->str(i)); + } } diff --git a/src/content/ContentLoader.hpp b/src/content/ContentLoader.hpp index 8d236d35..321f2e90 100644 --- a/src/content/ContentLoader.hpp +++ b/src/content/ContentLoader.hpp @@ -1,7 +1,7 @@ #ifndef CONTENT_CONTENT_LOADER_HPP_ #define CONTENT_CONTENT_LOADER_HPP_ -#include "../typedefs.hpp" +#include "content_fwd.hpp" #include #include @@ -19,6 +19,7 @@ struct ContentPackStats; namespace dynamic { class Map; + class List; } class ContentLoader { @@ -36,6 +37,7 @@ class ContentLoader { static void loadBlock(Block& def, const std::string& name, const fs::path& file); static void loadItem(ItemDef& def, const std::string& name, const fs::path& file); static void loadEntity(EntityDef& def, const std::string& name, const fs::path& file); + void loadResources(ResourceType type, dynamic::List* list); public: ContentLoader(ContentPack* pack, ContentBuilder& builder); diff --git a/src/content/content_fwd.hpp b/src/content/content_fwd.hpp new file mode 100644 index 00000000..ea4b3459 --- /dev/null +++ b/src/content/content_fwd.hpp @@ -0,0 +1,18 @@ +#ifndef CONTENT_CONTENT_FWD_HPP_ +#define CONTENT_CONTENT_FWD_HPP_ + +#include "../typedefs.hpp" + +class Content; +class ContentPackRuntime; + +enum class contenttype { + none, block, item, entity +}; + +enum class ResourceType : size_t { + CAMERA, + LAST=CAMERA +}; + +#endif // CONTENT_CONTENT_FWD_HPP_ diff --git a/src/engine.cpp b/src/engine.cpp index 10943f0c..354cb745 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/Content.hpp" #include "content/ContentBuilder.hpp" #include "content/ContentLoader.hpp" #include "core_defs.hpp" diff --git a/src/engine.hpp b/src/engine.hpp index 2b601743..de18b3e3 100644 --- a/src/engine.hpp +++ b/src/engine.hpp @@ -5,7 +5,7 @@ #include "typedefs.hpp" #include "assets/Assets.hpp" -#include "content/Content.hpp" +#include "content/content_fwd.hpp" #include "content/ContentPack.hpp" #include "content/PacksManager.hpp" #include "files/engine_paths.hpp" @@ -48,11 +48,11 @@ class Engine : public util::ObjectsKeeper { SettingsHandler& settingsHandler; EnginePaths* paths; - std::unique_ptr assets = nullptr; - std::shared_ptr screen = nullptr; + std::unique_ptr assets; + std::shared_ptr screen; std::vector contentPacks; - std::unique_ptr content = nullptr; - std::unique_ptr resPaths = nullptr; + std::unique_ptr content; + std::unique_ptr resPaths; std::queue postRunnables; std::recursive_mutex postRunnablesMutex; std::unique_ptr controller; diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index d6c8ae30..e6c44efb 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -2,6 +2,7 @@ #include "../delegates.hpp" #include "../engine.hpp" #include "../settings.hpp" +#include "../content/Content.hpp" #include "../graphics/core/Mesh.hpp" #include "../graphics/ui/elements/CheckBox.hpp" #include "../graphics/ui/elements/TextBox.hpp" diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index b803a12f..4c2474d7 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -8,6 +8,7 @@ #include "../../debug/Logger.hpp" #include "../../engine.hpp" #include "../../files/files.hpp" +#include "../../content/Content.hpp" #include "../../graphics/core/DrawContext.hpp" #include "../../graphics/core/ImageData.hpp" #include "../../graphics/core/PostProcessing.hpp" diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libpack.cpp index 256c6032..0c1805a0 100644 --- a/src/logic/scripting/lua/libpack.cpp +++ b/src/logic/scripting/lua/libpack.cpp @@ -1,6 +1,7 @@ #include "api_lua.hpp" #include "../../../engine.hpp" +#include "../../../content/Content.hpp" #include "../../../assets/AssetsLoader.hpp" #include "../../../files/engine_paths.hpp" #include "../../../files/WorldFiles.hpp" diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index bc159584..10a74a71 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -2,6 +2,7 @@ #include "lua/lua_engine.hpp" +#include "../../content/Content.hpp" #include "../../content/ContentPack.hpp" #include "../../debug/Logger.hpp" #include "../../engine.hpp" diff --git a/src/world/WorldGenerators.hpp b/src/world/WorldGenerators.hpp index 5143cc07..65202103 100644 --- a/src/world/WorldGenerators.hpp +++ b/src/world/WorldGenerators.hpp @@ -2,11 +2,12 @@ #define WORLD_WORLDGENERATORS_HPP_ #include "../voxels/WorldGenerator.hpp" -#include "../content/Content.hpp" #include #include #include +class Content; + typedef WorldGenerator* (*gen_constructor) (const Content*);