From c6d2266026f405d6e242dc989ef6c2a648f22569 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 14 Dec 2023 00:20:40 +0300 Subject: [PATCH] Refactor --- src/content/ContentPack.cpp | 16 ---------------- src/content/ContentPack.h | 12 +++--------- src/engine.cpp | 28 ++++++++++++++++++++-------- src/engine.h | 6 +++--- src/frontend/locale/langs.cpp | 4 ++-- src/voxel_engine.cpp | 24 +++++------------------- 6 files changed, 33 insertions(+), 57 deletions(-) diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index 5c7c85ed..8318e5c8 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -1,17 +1 @@ #include "ContentPack.h" - -using std::string; -using std::filesystem::path; - -ContentPack::ContentPack(const string id, - const path folder) - : id(id), folder(folder) { - -} -const string& ContentPack::getId() const { - return id; -} - -const path& ContentPack::getFolder() const { - return folder; -} diff --git a/src/content/ContentPack.h b/src/content/ContentPack.h index df440a2b..08ab5b81 100644 --- a/src/content/ContentPack.h +++ b/src/content/ContentPack.h @@ -4,15 +4,9 @@ #include #include -class ContentPack { - const std::string id; - const std::filesystem::path folder; -public: - ContentPack(const std::string id, - const std::filesystem::path folder); - - const std::string& getId() const; - const std::filesystem::path& getFolder() const; +struct ContentPack { + std::string id; + std::filesystem::path folder; }; #endif // CONTENT_CONTENT_PACK_H_ diff --git a/src/engine.cpp b/src/engine.cpp index c679e7f1..f4369c10 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -29,9 +29,13 @@ #include "files/files.h" #include "files/engine_paths.h" +#include "content/Content.h" #include "content/ContentPack.h" +#include "content/ContentLoader.h" #include "frontend/locale/langs.h" +#include "definitions.h" + using std::unique_ptr; using std::shared_ptr; using std::string; @@ -40,12 +44,24 @@ using std::filesystem::path; using glm::vec3; using gui::GUI; -Engine::Engine(EngineSettings& settings, EnginePaths* paths, Content* content) - : settings(settings), content(content), paths(paths) { +Engine::Engine(EngineSettings& settings, EnginePaths* paths) + : settings(settings), paths(paths) { if (Window::initialize(settings.display)){ throw initialize_error("could not initialize window"); } - Shader::preprocessor->setLibFolder(paths->getResources()/path("shaders/lib")); + + auto resdir = paths->getResources(); + contentPacks.push_back({"base", resdir/path("content/base")}); + { + ContentBuilder contentBuilder; + setup_definitions(&contentBuilder); + for (auto& pack : contentPacks) { + ContentLoader loader(pack.folder); + loader.load(&contentBuilder); + } + content.reset(contentBuilder.build()); + } + Shader::preprocessor->setLibFolder(paths->getResources()/path("shaders/lib")); assets = new Assets(); std::cout << "-- loading assets" << std::endl; @@ -61,10 +77,6 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths, Content* content) } Audio::initialize(); gui = new GUI(); - - auto resdir = paths->getResources(); - contentPacks.push_back(ContentPack("base", resdir/path("content/base"))); - if (settings.ui.language == "auto") { settings.ui.language = platform::detect_locale(); } @@ -145,7 +157,7 @@ void Engine::setScreen(shared_ptr screen) { } const Content* Engine::getContent() const { - return content; + return content.get(); } vector& Engine::getContentPacks() { diff --git a/src/engine.h b/src/engine.h index e04abd77..b2f9d199 100644 --- a/src/engine.h +++ b/src/engine.h @@ -8,12 +8,12 @@ #include "typedefs.h" #include "settings.h" +#include "content/Content.h" #include "content/ContentPack.h" class Assets; class Level; class Screen; -class Content; class EnginePaths; namespace gui { @@ -30,7 +30,7 @@ class Engine { std::shared_ptr screen = nullptr; std::vector contentPacks; EngineSettings& settings; - Content* content; + std::unique_ptr content = nullptr; EnginePaths* paths; uint64_t frame = 0; @@ -39,7 +39,7 @@ class Engine { gui::GUI* gui; public: - Engine(EngineSettings& settings, EnginePaths* paths, Content* content); + Engine(EngineSettings& settings, EnginePaths* paths); ~Engine(); void updateTimers(); diff --git a/src/frontend/locale/langs.cpp b/src/frontend/locale/langs.cpp index 483fbe25..762e2756 100644 --- a/src/frontend/locale/langs.cpp +++ b/src/frontend/locale/langs.cpp @@ -105,11 +105,11 @@ void langs::load(const path& resdir, reader.read(lang, ""); } for (auto pack : packs) { - path file = pack.getFolder()/filename; + path file = pack.folder/filename; if (fs::is_regular_file(file)) { string text = files::read_string(file); Reader reader(file.string(), text); - reader.read(lang, pack.getId()+":"); + reader.read(lang, pack.id+":"); } } } diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index a16be120..fa1fbdb4 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -12,13 +12,6 @@ #include "files/files.h" #include "files/settings_io.h" #include "files/engine_paths.h" -#include "content/Content.h" -#include "content/ContentLoader.h" - -#include "coders/png.h" -#include "graphics/Atlas.h" -#include "graphics/ImageData.h" - #include "util/command_line.h" using std::filesystem::path; @@ -29,13 +22,6 @@ int main(int argc, char** argv) { return EXIT_SUCCESS; platform::configure_encoding(); - ContentBuilder contentBuilder; - setup_definitions(&contentBuilder); - // TODO: implement worlds indexing - ContentLoader loader(paths.getResources()/path("content/base")); - loader.load(&contentBuilder); - - std::unique_ptr content(contentBuilder.build()); try { EngineSettings settings; toml::Wrapper wrapper = create_wrapper(settings); @@ -44,16 +30,16 @@ int main(int argc, char** argv) { path controls_file = platform::get_controls_file(); if (std::filesystem::is_regular_file(settings_file)) { std::cout << "-- loading settings" << std::endl; - std::string content = files::read_string(settings_file); - toml::Reader reader(&wrapper, settings_file.string(), content); + std::string text = files::read_string(settings_file); + toml::Reader reader(&wrapper, settings_file.string(), text); reader.read(); } - Engine engine(settings, &paths, content.get()); + Engine engine(settings, &paths); setup_bindings(); if (std::filesystem::is_regular_file(controls_file)) { std::cout << "-- loading controls" << std::endl; - std::string content = files::read_string(controls_file); - load_controls(controls_file.string(), content); + std::string text = files::read_string(controls_file); + load_controls(controls_file.string(), text); } engine.mainloop();