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;
ContentPack ContentPack::createCore(const EnginePaths* paths) {
ContentPack ContentPack::createCore(const EnginePaths& paths) {
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
);
static ContentPack createCore(const EnginePaths*);
static ContentPack createCore(const EnginePaths&);
static inline fs::path getFolderFor(ContentType type) {
switch (type) {

View File

@ -11,9 +11,9 @@
#include "voxels/Block.hpp"
// 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.drawGroup = 1;
block.lightPassing = true;
@ -24,11 +24,11 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
block.pickingItem = CORE_EMPTY;
}
{
ItemDef& item = builder->items.create(CORE_EMPTY);
ItemDef& item = builder.items.create(CORE_EMPTY);
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)) {
Events::loadBindings(
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++) {
block.textureFaces[i] = "obstacle";
}
block.hitboxes = {AABB()};
block.breakable = false;
ItemDef& item = builder->items.create(CORE_OBSTACLE+".item");
ItemDef& item = builder.items.create(CORE_OBSTACLE+".item");
item.iconType = ItemIconType::BLOCK;
item.icon = CORE_OBSTACLE;
item.placingBlock = CORE_OBSTACLE;
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++) {
block.textureFaces[i] = "struct_air";
}
@ -58,7 +58,7 @@ void corecontent::setup(EnginePaths* paths, ContentBuilder* builder) {
block.lightPassing = true;
block.hitboxes = {AABB()};
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.icon = CORE_STRUCT_AIR;
item.placingBlock = CORE_STRUCT_AIR;

View File

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

View File

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

View File

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