From 01f41715d75f42476c967148144fd07b759c7c43 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 19 Apr 2024 05:39:28 +0300 Subject: [PATCH] minor refactor --- src/frontend/menu/menu_create_world.cpp | 90 ------------------------- src/frontend/menu/menu_pause.cpp | 71 ++++++++++++++++++- 2 files changed, 69 insertions(+), 92 deletions(-) delete mode 100644 src/frontend/menu/menu_create_world.cpp diff --git a/src/frontend/menu/menu_create_world.cpp b/src/frontend/menu/menu_create_world.cpp deleted file mode 100644 index be29e879..00000000 --- a/src/frontend/menu/menu_create_world.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#ifndef FRONTEND_MENU_MENU_CREATE_WORLD_H_ -#define FRONTEND_MENU_MENU_CREATE_WORLD_H_ - -#include "../../engine.h" -#include "../../files/WorldFiles.h" -#include "../../content/PacksManager.h" -#include "../../graphics/ui/elements/containers.h" -#include "../../graphics/ui/elements/controls.h" -#include "../../graphics/ui/gui_util.h" -#include "../../util/stringutil.h" -#include "../../world/Level.h" -#include "../../world/World.h" -#include "../../world/WorldGenerators.h" -#include "../locale/langs.h" -#include "../screens.h" -#include "menu_commons.h" -#include "menu.h" - -#include - -using namespace gui; - -inline uint64_t randU64() { - srand(time(NULL)); - return rand() ^ (rand() << 8) ^ - (rand() << 16) ^ (rand() << 24) ^ - ((uint64_t)rand() << 32) ^ - ((uint64_t)rand() << 40) ^ - ((uint64_t)rand() << 56); -} - -inline uint64_t str2seed(const std::string& seedstr) { - if (util::is_integer(seedstr)) { - try { - return std::stoull(seedstr); - } catch (const std::out_of_range& err) { - std::hash hash; - return hash(seedstr); - } - } else { - std::hash hash; - return hash(seedstr); - } -} - -void menus::create_world( - Engine* engine, - const std::string& name, - const std::string& seedstr, - const std::string& generatorID -) { - uint64_t seed = str2seed(seedstr); - - EnginePaths* paths = engine->getPaths(); - auto folder = paths->getWorldsFolder()/fs::u8path(name); - try { - engine->loadAllPacks(); - engine->loadContent(); - paths->setWorldFolder(folder); - } catch (const contentpack_error& error) { - guiutil::alert( - engine->getGUI(), - langs::get(L"Content Error", L"menu")+L":\n"+ - util::str2wstr_utf8( - std::string(error.what())+ - "\npack '"+error.getPackId()+"' from "+ - error.getFolder().u8string() - ) - ); - return; - } catch (const std::runtime_error& error) { - guiutil::alert( - engine->getGUI(), - langs::get(L"Content Error", L"menu")+ - L": "+util::str2wstr_utf8(error.what()) - ); - return; - } - - Level* level = World::create( - name, generatorID, folder, seed, - engine->getSettings(), - engine->getContent(), - engine->getContentPacks() - ); - level->getWorld()->wfile->createDirectories(); - engine->setScreen(std::make_shared(engine, level)); -} - -#endif // FRONTEND_MENU_MENU_CREATE_WORLD_H_ diff --git a/src/frontend/menu/menu_pause.cpp b/src/frontend/menu/menu_pause.cpp index 116f25a4..b22262e7 100644 --- a/src/frontend/menu/menu_pause.cpp +++ b/src/frontend/menu/menu_pause.cpp @@ -1,12 +1,12 @@ #include "menu.h" #include "menu_commons.h" -#include "../../coders/imageio.h" +// TODO: move functions to EngineController + #include "../../content/PacksManager.h" #include "../../content/ContentLUT.h" #include "../../engine.h" #include "../../files/WorldFiles.h" -#include "../../graphics/core/Texture.h" #include "../../graphics/ui/gui_util.h" #include "../../logic/LevelController.h" #include "../../util/stringutil.h" @@ -19,6 +19,73 @@ using namespace gui; +inline uint64_t randU64() { + srand(time(NULL)); + return rand() ^ (rand() << 8) ^ + (rand() << 16) ^ (rand() << 24) ^ + ((uint64_t)rand() << 32) ^ + ((uint64_t)rand() << 40) ^ + ((uint64_t)rand() << 56); +} + +inline uint64_t str2seed(const std::string& seedstr) { + if (util::is_integer(seedstr)) { + try { + return std::stoull(seedstr); + } catch (const std::out_of_range& err) { + std::hash hash; + return hash(seedstr); + } + } else { + std::hash hash; + return hash(seedstr); + } +} + +void menus::create_world( + Engine* engine, + const std::string& name, + const std::string& seedstr, + const std::string& generatorID +) { + uint64_t seed = str2seed(seedstr); + + EnginePaths* paths = engine->getPaths(); + auto folder = paths->getWorldsFolder()/fs::u8path(name); + try { + engine->loadAllPacks(); + engine->loadContent(); + paths->setWorldFolder(folder); + } catch (const contentpack_error& error) { + guiutil::alert( + engine->getGUI(), + langs::get(L"Content Error", L"menu")+L":\n"+ + util::str2wstr_utf8( + std::string(error.what())+ + "\npack '"+error.getPackId()+"' from "+ + error.getFolder().u8string() + ) + ); + return; + } catch (const std::runtime_error& error) { + guiutil::alert( + engine->getGUI(), + langs::get(L"Content Error", L"menu")+ + L": "+util::str2wstr_utf8(error.what()) + ); + return; + } + + Level* level = World::create( + name, generatorID, folder, seed, + engine->getSettings(), + engine->getContent(), + engine->getContentPacks() + ); + level->getWorld()->wfile->createDirectories(); + engine->setScreen(std::make_shared(engine, level)); +} + static void reopen_world(Engine* engine, World* world) { std::string wname = world->wfile->getFolder().filename().u8string(); engine->setScreen(nullptr);