This commit is contained in:
MihailRis 2024-12-07 00:04:56 +03:00
parent 61b73bfabd
commit 3e01a399f9
8 changed files with 43 additions and 52 deletions

View File

@ -13,9 +13,9 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
ContentPack ContentPack::createCore(const EnginePaths* paths) { ContentPack ContentPack::createCore(const EnginePaths& paths) {
return ContentPack { return ContentPack {
"core", "Core", ENGINE_VERSION_STRING, "", "", paths->getResourcesFolder(), {} "core", "Core", ENGINE_VERSION_STRING, "", "", paths.getResourcesFolder(), {}
}; };
} }

View File

@ -71,7 +71,7 @@ struct ContentPack {
const std::string& name const std::string& name
); );
static ContentPack createCore(const EnginePaths*); static ContentPack createCore(const EnginePaths&);
static inline fs::path getFolderFor(ContentType type) { static inline fs::path getFolderFor(ContentType type) {
switch (type) { switch (type) {

View File

@ -11,9 +11,9 @@
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
// All in-game definitions (blocks, items, etc..) // All in-game definitions (blocks, items, etc..)
void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) { void corecontent::setup(const EnginePaths& paths, ContentBuilder& builder) {
{ {
Block& block = builder->blocks.create(CORE_AIR); Block& block = builder.blocks.create(CORE_AIR);
block.replaceable = true; block.replaceable = true;
block.drawGroup = 1; block.drawGroup = 1;
block.lightPassing = true; block.lightPassing = true;
@ -24,11 +24,11 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
block.pickingItem = CORE_EMPTY; block.pickingItem = CORE_EMPTY;
} }
{ {
ItemDef& item = builder->items.create(CORE_EMPTY); ItemDef& item = builder.items.create(CORE_EMPTY);
item.iconType = ItemIconType::NONE; item.iconType = ItemIconType::NONE;
} }
auto bindsFile = paths->getResourcesFolder()/fs::path("bindings.toml"); auto bindsFile = paths.getResourcesFolder()/fs::path("bindings.toml");
if (fs::is_regular_file(bindsFile)) { if (fs::is_regular_file(bindsFile)) {
Events::loadBindings( Events::loadBindings(
bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND bindsFile.u8string(), files::read_string(bindsFile), BindType::BIND
@ -36,20 +36,20 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
} }
{ {
Block& block = builder->blocks.create(CORE_OBSTACLE); Block& block = builder.blocks.create(CORE_OBSTACLE);
for (uint i = 0; i < 6; i++) { for (uint i = 0; i < 6; i++) {
block.textureFaces[i] = "obstacle"; block.textureFaces[i] = "obstacle";
} }
block.hitboxes = {AABB()}; block.hitboxes = {AABB()};
block.breakable = false; block.breakable = false;
ItemDef& item = builder->items.create(CORE_OBSTACLE+".item"); ItemDef& item = builder.items.create(CORE_OBSTACLE+".item");
item.iconType = ItemIconType::BLOCK; item.iconType = ItemIconType::BLOCK;
item.icon = CORE_OBSTACLE; item.icon = CORE_OBSTACLE;
item.placingBlock = CORE_OBSTACLE; item.placingBlock = CORE_OBSTACLE;
item.caption = block.caption; item.caption = block.caption;
} }
{ {
Block& block = builder->blocks.create(CORE_STRUCT_AIR); Block& block = builder.blocks.create(CORE_STRUCT_AIR);
for (uint i = 0; i < 6; i++) { for (uint i = 0; i < 6; i++) {
block.textureFaces[i] = "struct_air"; block.textureFaces[i] = "struct_air";
} }
@ -58,7 +58,7 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
block.lightPassing = true; block.lightPassing = true;
block.hitboxes = {AABB()}; block.hitboxes = {AABB()};
block.obstacle = false; block.obstacle = false;
ItemDef& item = builder->items.create(CORE_STRUCT_AIR+".item"); ItemDef& item = builder.items.create(CORE_STRUCT_AIR+".item");
item.iconType = ItemIconType::BLOCK; item.iconType = ItemIconType::BLOCK;
item.icon = CORE_STRUCT_AIR; item.icon = CORE_STRUCT_AIR;
item.placingBlock = CORE_STRUCT_AIR; item.placingBlock = CORE_STRUCT_AIR;

View File

@ -35,5 +35,5 @@ class EnginePaths;
class ContentBuilder; class ContentBuilder;
namespace corecontent { namespace corecontent {
void setup(EnginePaths* paths, ContentBuilder* builder); void setup(const EnginePaths& paths, ContentBuilder& builder);
} }

View File

@ -15,7 +15,6 @@
#include "content/ContentLoader.hpp" #include "content/ContentLoader.hpp"
#include "core_defs.hpp" #include "core_defs.hpp"
#include "files/files.hpp" #include "files/files.hpp"
#include "files/settings_io.hpp"
#include "frontend/locale.hpp" #include "frontend/locale.hpp"
#include "frontend/menu.hpp" #include "frontend/menu.hpp"
#include "frontend/screens/Screen.hpp" #include "frontend/screens/Screen.hpp"
@ -37,7 +36,6 @@
#include "window/Events.hpp" #include "window/Events.hpp"
#include "window/input.hpp" #include "window/input.hpp"
#include "window/Window.hpp" #include "window/Window.hpp"
#include "settings.hpp"
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
@ -71,15 +69,15 @@ static std::unique_ptr<ImageData> load_icon(const fs::path& resdir) {
return nullptr; return nullptr;
} }
Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, EnginePaths* paths) Engine::Engine(EnginePaths& paths)
: settings(settings), settingsHandler(settingsHandler), paths(paths), : settings(), settingsHandler({settings}), paths(paths),
interpreter(std::make_unique<cmd::CommandsInterpreter>()), interpreter(std::make_unique<cmd::CommandsInterpreter>()),
network(network::Network::create(settings.network)) network(network::Network::create(settings.network))
{ {
paths->prepare(); paths.prepare();
loadSettings(); loadSettings();
auto resdir = paths->getResourcesFolder(); auto resdir = paths.getResourcesFolder();
controller = std::make_unique<EngineController>(this); controller = std::make_unique<EngineController>(this);
if (Window::initialize(&this->settings.display)){ if (Window::initialize(&this->settings.display)){
@ -101,7 +99,7 @@ Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, Engin
if (settings.ui.language.get() == "auto") { if (settings.ui.language.get() == "auto") {
settings.ui.language.set(langs::locale_by_envlocale( settings.ui.language.set(langs::locale_by_envlocale(
platform::detect_locale(), platform::detect_locale(),
paths->getResourcesFolder() paths.getResourcesFolder()
)); ));
} }
if (ENGINE_DEBUG_BUILD) { if (ENGINE_DEBUG_BUILD) {
@ -116,7 +114,7 @@ Engine::Engine(EngineSettings& settings, SettingsHandler& settingsHandler, Engin
} }
void Engine::loadSettings() { void Engine::loadSettings() {
fs::path settings_file = paths->getSettingsFile(); fs::path settings_file = paths.getSettingsFile();
if (fs::is_regular_file(settings_file)) { if (fs::is_regular_file(settings_file)) {
logger.info() << "loading settings"; logger.info() << "loading settings";
std::string text = files::read_string(settings_file); std::string text = files::read_string(settings_file);
@ -130,7 +128,7 @@ void Engine::loadSettings() {
} }
void Engine::loadControls() { void Engine::loadControls() {
fs::path controls_file = paths->getControlsFile(); fs::path controls_file = paths.getControlsFile();
if (fs::is_regular_file(controls_file)) { if (fs::is_regular_file(controls_file)) {
logger.info() << "loading controls"; logger.info() << "loading controls";
std::string text = files::read_string(controls_file); std::string text = files::read_string(controls_file);
@ -162,7 +160,7 @@ void Engine::updateHotkeys() {
void Engine::saveScreenshot() { void Engine::saveScreenshot() {
auto image = Window::takeScreenshot(); auto image = Window::takeScreenshot();
image->flipY(); image->flipY();
fs::path filename = paths->getNewScreenshotFile("png"); fs::path filename = paths.getNewScreenshotFile("png");
imageio::write(filename.string(), image.get()); imageio::write(filename.string(), image.get());
logger.info() << "saved screenshot as " << filename.u8string(); logger.info() << "saved screenshot as " << filename.u8string();
} }
@ -220,9 +218,9 @@ void Engine::processPostRunnables() {
void Engine::saveSettings() { void Engine::saveSettings() {
logger.info() << "saving settings"; logger.info() << "saving settings";
files::write_string(paths->getSettingsFile(), toml::stringify(settingsHandler)); files::write_string(paths.getSettingsFile(), toml::stringify(settingsHandler));
logger.info() << "saving bindings"; logger.info() << "saving bindings";
files::write_string(paths->getControlsFile(), Events::writeBindings()); files::write_string(paths.getControlsFile(), Events::writeBindings());
} }
Engine::~Engine() { Engine::~Engine() {
@ -257,8 +255,8 @@ PacksManager Engine::createPacksManager(const fs::path& worldFolder) {
PacksManager manager; PacksManager manager;
manager.setSources({ manager.setSources({
worldFolder/fs::path("content"), worldFolder/fs::path("content"),
paths->getUserFilesFolder()/fs::path("content"), paths.getUserFilesFolder()/fs::path("content"),
paths->getResourcesFolder()/fs::path("content") paths.getResourcesFolder()/fs::path("content")
}); });
return manager; return manager;
} }
@ -329,7 +327,7 @@ static void load_configs(const fs::path& root) {
void Engine::loadContent() { void Engine::loadContent() {
scripting::cleanup(); scripting::cleanup();
auto resdir = paths->getResourcesFolder(); auto resdir = paths.getResourcesFolder();
std::vector<std::string> names; std::vector<std::string> names;
for (auto& pack : contentPacks) { for (auto& pack : contentPacks) {
@ -337,10 +335,10 @@ void Engine::loadContent() {
} }
ContentBuilder contentBuilder; ContentBuilder contentBuilder;
corecontent::setup(paths, &contentBuilder); corecontent::setup(paths, contentBuilder);
paths->setContentPacks(&contentPacks); paths.setContentPacks(&contentPacks);
PacksManager manager = createPacksManager(paths->getCurrentWorldFolder()); PacksManager manager = createPacksManager(paths.getCurrentWorldFolder());
manager.scan(); manager.scan();
names = manager.assembly(names); names = manager.assembly(names);
contentPacks = manager.getAll(names); contentPacks = manager.getAll(names);
@ -378,7 +376,7 @@ void Engine::loadContent() {
void Engine::resetContent() { void Engine::resetContent() {
scripting::cleanup(); scripting::cleanup();
auto resdir = paths->getResourcesFolder(); auto resdir = paths.getResourcesFolder();
std::vector<PathsRoot> resRoots; std::vector<PathsRoot> resRoots;
{ {
auto pack = ContentPack::createCore(paths); auto pack = ContentPack::createCore(paths);
@ -407,17 +405,17 @@ void Engine::loadWorldContent(const fs::path& folder) {
PacksManager manager; PacksManager manager;
manager.setSources({ manager.setSources({
folder/fs::path("content"), folder/fs::path("content"),
paths->getUserFilesFolder()/fs::path("content"), paths.getUserFilesFolder()/fs::path("content"),
paths->getResourcesFolder()/fs::path("content") paths.getResourcesFolder()/fs::path("content")
}); });
manager.scan(); manager.scan();
contentPacks = manager.getAll(manager.assembly(packNames)); contentPacks = manager.getAll(manager.assembly(packNames));
paths->setCurrentWorldFolder(folder); paths.setCurrentWorldFolder(folder);
loadContent(); loadContent();
} }
void Engine::loadAllPacks() { void Engine::loadAllPacks() {
PacksManager manager = createPacksManager(paths->getCurrentWorldFolder()); PacksManager manager = createPacksManager(paths.getCurrentWorldFolder());
manager.scan(); manager.scan();
auto allnames = manager.getAllNames(); auto allnames = manager.getAllNames();
contentPacks = manager.getAll(manager.assembly(allnames)); contentPacks = manager.getAll(manager.assembly(allnames));
@ -435,7 +433,7 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
} }
void Engine::setLanguage(std::string locale) { void Engine::setLanguage(std::string locale) {
langs::setup(paths->getResourcesFolder(), std::move(locale), contentPacks); langs::setup(paths.getResourcesFolder(), std::move(locale), contentPacks);
gui->getMenu()->setPageLoader(menus::create_page_loader(this)); gui->getMenu()->setPageLoader(menus::create_page_loader(this));
} }
@ -470,7 +468,7 @@ std::vector<std::string>& Engine::getBasePacks() {
} }
EnginePaths* Engine::getPaths() { EnginePaths* Engine::getPaths() {
return paths; return &paths;
} }
ResPaths* Engine::getResPaths() { ResPaths* Engine::getResPaths() {

View File

@ -2,12 +2,14 @@
#include "delegates.hpp" #include "delegates.hpp"
#include "typedefs.hpp" #include "typedefs.hpp"
#include "settings.hpp"
#include "assets/Assets.hpp" #include "assets/Assets.hpp"
#include "content/content_fwd.hpp" #include "content/content_fwd.hpp"
#include "content/ContentPack.hpp" #include "content/ContentPack.hpp"
#include "content/PacksManager.hpp" #include "content/PacksManager.hpp"
#include "files/engine_paths.hpp" #include "files/engine_paths.hpp"
#include "files/settings_io.hpp"
#include "util/ObjectsKeeper.hpp" #include "util/ObjectsKeeper.hpp"
#include <filesystem> #include <filesystem>
@ -26,8 +28,6 @@ class EngineController;
class SettingsHandler; class SettingsHandler;
struct EngineSettings; struct EngineSettings;
namespace fs = std::filesystem;
namespace gui { namespace gui {
class GUI; class GUI;
} }
@ -46,9 +46,9 @@ public:
}; };
class Engine : public util::ObjectsKeeper { class Engine : public util::ObjectsKeeper {
EngineSettings& settings; EngineSettings settings;
SettingsHandler& settingsHandler; SettingsHandler settingsHandler;
EnginePaths* paths; EnginePaths& paths;
std::unique_ptr<Assets> assets; std::unique_ptr<Assets> assets;
std::shared_ptr<Screen> screen; std::shared_ptr<Screen> screen;
@ -77,7 +77,7 @@ class Engine : public util::ObjectsKeeper {
void processPostRunnables(); void processPostRunnables();
void loadAssets(); void loadAssets();
public: public:
Engine(EngineSettings& settings, SettingsHandler& settingsHandler, EnginePaths* paths); Engine(EnginePaths& paths);
~Engine(); ~Engine();
/// @brief Start main engine input/update/render loop. /// @brief Start main engine input/update/render loop.

View File

@ -40,7 +40,7 @@ public:
} }
}; };
bool perform_keyword( static bool perform_keyword(
ArgsReader& reader, const std::string& keyword, EnginePaths& paths ArgsReader& reader, const std::string& keyword, EnginePaths& paths
) { ) {
if (keyword == "--res") { if (keyword == "--res") {

View File

@ -1,6 +1,4 @@
#include "engine.hpp" #include "engine.hpp"
#include "settings.hpp"
#include "files/settings_io.hpp"
#include "files/engine_paths.hpp" #include "files/engine_paths.hpp"
#include "util/platform.hpp" #include "util/platform.hpp"
#include "util/command_line.hpp" #include "util/command_line.hpp"
@ -19,12 +17,7 @@ int main(int argc, char** argv) {
platform::configure_encoding(); platform::configure_encoding();
try { try {
EngineSettings settings; Engine(paths).mainloop();
SettingsHandler handler(settings);
Engine engine(settings, handler, &paths);
engine.mainloop();
} }
catch (const initialize_error& err) { catch (const initialize_error& err) {
logger.error() << "could not to initialize engine\n" << err.what(); logger.error() << "could not to initialize engine\n" << err.what();