This commit is contained in:
MihailRis 2024-12-18 04:13:33 +03:00
parent 5e55e20ec4
commit d745a34657
33 changed files with 209 additions and 205 deletions

View File

@ -1,8 +1,6 @@
test.set_setting("chunks.load-distance", 3) test.set_setting("chunks.load-distance", 3)
test.set_setting("chunks.load-speed", 1) test.set_setting("chunks.load-speed", 1)
test.quit()
test.reconfig_packs({"base"}, {}) test.reconfig_packs({"base"}, {})
test.new_world("demo", "2019", "core:default") test.new_world("demo", "2019", "core:default")
local pid1 = player.create("Xerxes") local pid1 = player.create("Xerxes")

View File

@ -20,14 +20,14 @@ void Mainloop::run() {
// destroy LevelScreen and run quit callbacks // destroy LevelScreen and run quit callbacks
engine.setScreen(nullptr); engine.setScreen(nullptr);
// create and go to menu screen // create and go to menu screen
engine.setScreen(std::make_shared<MenuScreen>(&engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
} else { } else {
engine.setScreen(std::make_shared<LevelScreen>(&engine, std::move(level))); engine.setScreen(std::make_shared<LevelScreen>(engine, std::move(level)));
} }
}); });
logger.info() << "starting menu screen"; logger.info() << "starting menu screen";
engine.setScreen(std::make_shared<MenuScreen>(&engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
logger.info() << "main loop started"; logger.info() << "main loop started";
while (!Window::isShouldClose()){ while (!Window::isShouldClose()){

View File

@ -69,7 +69,7 @@ void ServerMainloop::run() {
void ServerMainloop::setLevel(std::unique_ptr<Level> level) { void ServerMainloop::setLevel(std::unique_ptr<Level> level) {
if (level == nullptr) { if (level == nullptr) {
controller->onWorldQuit(); controller->onWorldQuit();
engine.getPaths()->setCurrentWorldFolder(fs::path()); engine.getPaths().setCurrentWorldFolder(fs::path());
controller = nullptr; controller = nullptr;
} else { } else {
controller = std::make_unique<LevelController>( controller = std::make_unique<LevelController>(

View File

@ -87,7 +87,7 @@ Engine::Engine(CoreParameters coreParameters)
auto resdir = paths.getResourcesFolder(); auto resdir = paths.getResourcesFolder();
controller = std::make_unique<EngineController>(this); controller = std::make_unique<EngineController>(*this);
if (!params.headless) { if (!params.headless) {
if (Window::initialize(&settings.display)){ if (Window::initialize(&settings.display)){
throw initialize_error("could not initialize window"); throw initialize_error("could not initialize window");
@ -101,7 +101,7 @@ Engine::Engine(CoreParameters coreParameters)
gui = std::make_unique<gui::GUI>(); gui = std::make_unique<gui::GUI>();
if (ENGINE_DEBUG_BUILD) { if (ENGINE_DEBUG_BUILD) {
menus::create_version_label(this); menus::create_version_label(*this);
} }
} }
audio::initialize(settings.audio.enabled.get() && !params.headless); audio::initialize(settings.audio.enabled.get() && !params.headless);
@ -118,7 +118,7 @@ Engine::Engine(CoreParameters coreParameters)
paths.getResourcesFolder() paths.getResourcesFolder()
)); ));
} }
keepAlive(settings.ui.language.observe([=](auto lang) { keepAlive(settings.ui.language.observe([this](auto lang) {
setLanguage(lang); setLanguage(lang);
}, true)); }, true));
@ -447,7 +447,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);
if (gui) { if (gui) {
gui->getMenu()->setPageLoader(menus::create_page_loader(this)); gui->getMenu()->setPageLoader(menus::create_page_loader(*this));
} }
} }
@ -502,8 +502,8 @@ std::vector<std::string>& Engine::getBasePacks() {
return basePacks; return basePacks;
} }
EnginePaths* Engine::getPaths() { EnginePaths& Engine::getPaths() {
return &paths; return paths;
} }
ResPaths* Engine::getResPaths() { ResPaths* Engine::getResPaths() {

View File

@ -131,7 +131,7 @@ public:
EngineSettings& getSettings(); EngineSettings& getSettings();
/// @brief Get engine filesystem paths source /// @brief Get engine filesystem paths source
EnginePaths* getPaths(); EnginePaths& getPaths();
/// @brief Get engine resource paths controller /// @brief Get engine resource paths controller
ResPaths* getResPaths(); ResPaths* getResPaths();

View File

@ -132,7 +132,7 @@ std::filesystem::path EnginePaths::getSettingsFile() const {
return userFilesFolder / SETTINGS_FILE; return userFilesFolder / SETTINGS_FILE;
} }
std::vector<std::filesystem::path> EnginePaths::scanForWorlds() { std::vector<std::filesystem::path> EnginePaths::scanForWorlds() const {
std::vector<std::filesystem::path> folders; std::vector<std::filesystem::path> folders;
auto folder = getWorldsFolder(); auto folder = getWorldsFolder();
@ -189,7 +189,7 @@ std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view pat
std::filesystem::path EnginePaths::resolve( std::filesystem::path EnginePaths::resolve(
const std::string& path, bool throwErr const std::string& path, bool throwErr
) { ) const {
auto [prefix, filename] = EnginePaths::parsePath(path); auto [prefix, filename] = EnginePaths::parsePath(path);
if (prefix.empty()) { if (prefix.empty()) {
throw files_access_error("no entry point specified"); throw files_access_error("no entry point specified");

View File

@ -39,9 +39,9 @@ public:
void setContentPacks(std::vector<ContentPack>* contentPacks); void setContentPacks(std::vector<ContentPack>* contentPacks);
std::vector<std::filesystem::path> scanForWorlds(); std::vector<std::filesystem::path> scanForWorlds() const;
std::filesystem::path resolve(const std::string& path, bool throwErr = true); std::filesystem::path resolve(const std::string& path, bool throwErr = true) const;
static std::tuple<std::string, std::string> parsePath(std::string_view view); static std::tuple<std::string, std::string> parsePath(std::string_view view);

View File

@ -42,7 +42,7 @@ static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
// TODO: move to xml // TODO: move to xml
// TODO: move to xml finally // TODO: move to xml finally
std::shared_ptr<UINode> create_debug_panel( std::shared_ptr<UINode> create_debug_panel(
Engine* engine, Engine& engine,
Level& level, Level& level,
Player& player, Player& player,
bool allowDebugCheats bool allowDebugCheats
@ -56,8 +56,8 @@ std::shared_ptr<UINode> create_debug_panel(
static int fpsMax = fps; static int fpsMax = fps;
static std::wstring fpsString = L""; static std::wstring fpsString = L"";
panel->listenInterval(0.016f, [engine]() { panel->listenInterval(0.016f, [&engine]() {
fps = 1.0f / engine->getTime().getDelta(); fps = 1.0f / engine.getTime().getDelta();
fpsMin = std::min(fps, fpsMin); fpsMin = std::min(fps, fpsMin);
fpsMax = std::max(fps, fpsMax); fpsMax = std::max(fps, fpsMax);
}); });
@ -84,8 +84,8 @@ std::shared_ptr<UINode> create_debug_panel(
panel->add(create_label([]() { panel->add(create_label([]() {
return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack()); return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack());
})); }));
panel->add(create_label([=]() { panel->add(create_label([&engine]() {
auto& settings = engine->getSettings(); auto& settings = engine.getSettings();
bool culling = settings.graphics.frustumCulling.get(); bool culling = settings.graphics.frustumCulling.get();
return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off"); return L"frustum-culling: "+std::wstring(culling ? L"on" : L"off");
})); }));

View File

@ -59,8 +59,8 @@ using namespace gui;
bool Hud::showGeneratorMinimap = false; bool Hud::showGeneratorMinimap = false;
// implemented in debug_panel.cpp // implemented in debug_panel.cpp
extern std::shared_ptr<UINode> create_debug_panel( std::shared_ptr<UINode> create_debug_panel(
Engine* engine, Engine& engine,
Level& level, Level& level,
Player& player, Player& player,
bool allowDebugCheats bool allowDebugCheats
@ -148,10 +148,10 @@ std::shared_ptr<InventoryView> Hud::createHotbar() {
static constexpr uint WORLDGEN_IMG_SIZE = 128U; static constexpr uint WORLDGEN_IMG_SIZE = 128U;
Hud::Hud(Engine* engine, LevelFrontend& frontend, Player& player) Hud::Hud(Engine& engine, LevelFrontend& frontend, Player& player)
: engine(engine), : engine(engine),
assets(engine->getAssets()), assets(*engine.getAssets()),
gui(engine->getGUI()), gui(engine.getGUI()),
frontend(frontend), frontend(frontend),
player(player), player(player),
debugImgWorldGen(std::make_unique<ImageData>( debugImgWorldGen(std::make_unique<ImageData>(
@ -191,7 +191,7 @@ Hud::Hud(Engine* engine, LevelFrontend& frontend, Player& player)
dplotter->setInteractive(false); dplotter->setInteractive(false);
add(HudElement(hud_element_mode::permanent, nullptr, dplotter, true)); add(HudElement(hud_element_mode::permanent, nullptr, dplotter, true));
assets->store(Texture::from(debugImgWorldGen.get()), DEBUG_WORLDGEN_IMAGE); assets.store(Texture::from(debugImgWorldGen.get()), DEBUG_WORLDGEN_IMAGE);
debugMinimap = guiutil::create( debugMinimap = guiutil::create(
"<image src='"+DEBUG_WORLDGEN_IMAGE+ "<image src='"+DEBUG_WORLDGEN_IMAGE+
@ -312,8 +312,8 @@ void Hud::updateWorldGenDebugVisualization() {
data[(flippedZ * width + x) * 4 + 3] = 150; data[(flippedZ * width + x) * 4 + 3] = 150;
} }
} }
auto texture = assets->get<Texture>(DEBUG_WORLDGEN_IMAGE); auto& texture = assets.require<Texture>(DEBUG_WORLDGEN_IMAGE);
texture->reload(*debugImgWorldGen); texture.reload(*debugImgWorldGen);
} }
void Hud::update(bool visible) { void Hud::update(bool visible) {
@ -377,7 +377,7 @@ void Hud::openInventory() {
inventoryOpen = true; inventoryOpen = true;
auto inventory = player.getInventory(); auto inventory = player.getInventory();
auto inventoryDocument = assets->get<UiDocument>("core:inventory"); auto inventoryDocument = assets.get<UiDocument>("core:inventory");
inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot()); inventoryView = std::dynamic_pointer_cast<InventoryView>(inventoryDocument->getRoot());
inventoryView->bind(inventory, content); inventoryView->bind(inventory, content);
add(HudElement(hud_element_mode::inventory_bound, inventoryDocument, inventoryView, false)); add(HudElement(hud_element_mode::inventory_bound, inventoryDocument, inventoryView, false));
@ -569,15 +569,15 @@ void Hud::draw(const DrawContext& ctx){
auto batch = ctx.getBatch2D(); auto batch = ctx.getBatch2D();
batch->begin(); batch->begin();
auto uishader = assets->get<Shader>("ui"); auto& uishader = assets.require<Shader>("ui");
uishader->use(); uishader.use();
uishader->uniformMatrix("u_projview", uicamera->getProjView()); uishader.uniformMatrix("u_projview", uicamera->getProjView());
// Crosshair // Crosshair
if (!pause && !inventoryOpen && !player.debug) { if (!pause && !inventoryOpen && !player.debug) {
DrawContext chctx = ctx.sub(batch); DrawContext chctx = ctx.sub(batch);
chctx.setBlendMode(BlendMode::inversion); chctx.setBlendMode(BlendMode::inversion);
auto texture = assets->get<Texture>("gui/crosshair"); auto texture = assets.get<Texture>("gui/crosshair");
batch->texture(texture); batch->texture(texture);
int chsizex = texture != nullptr ? texture->getWidth() : 16; int chsizex = texture != nullptr ? texture->getWidth() : 16;
int chsizey = texture != nullptr ? texture->getHeight() : 16; int chsizey = texture != nullptr ? texture->getHeight() : 16;

View File

@ -70,8 +70,8 @@ public:
}; };
class Hud : public util::ObjectsKeeper { class Hud : public util::ObjectsKeeper {
Engine* engine; Engine& engine;
Assets* assets; Assets& assets;
std::unique_ptr<Camera> uicamera; std::unique_ptr<Camera> uicamera;
gui::GUI* gui; gui::GUI* gui;
LevelFrontend& frontend; LevelFrontend& frontend;
@ -131,7 +131,7 @@ class Hud : public util::ObjectsKeeper {
void showExchangeSlot(); void showExchangeSlot();
void updateWorldGenDebugVisualization(); void updateWorldGenDebugVisualization();
public: public:
Hud(Engine* engine, LevelFrontend& frontend, Player& player); Hud(Engine& engine, LevelFrontend& frontend, Player& player);
~Hud(); ~Hud();
void update(bool hudVisible); void update(bool hudVisible);

View File

@ -24,8 +24,8 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
using namespace gui; using namespace gui;
void menus::create_version_label(Engine* engine) { void menus::create_version_label(Engine& engine) {
auto gui = engine->getGUI(); auto gui = engine.getGUI();
auto text = ENGINE_VERSION_STRING+" debug build"; auto text = ENGINE_VERSION_STRING+" debug build";
gui->add(guiutil::create( gui->add(guiutil::create(
"<label z-index='1000' color='#FFFFFF80' gravity='top-right' margin='4'>" "<label z-index='1000' color='#FFFFFF80' gravity='top-right' margin='4'>"
@ -34,8 +34,8 @@ void menus::create_version_label(Engine* engine) {
)); ));
} }
gui::page_loader_func menus::create_page_loader(Engine* engine) { gui::page_loader_func menus::create_page_loader(Engine& engine) {
return [=](const std::string& query) { return [&](const std::string& query) {
std::vector<dv::value> args; std::vector<dv::value> args;
std::string name; std::string name;
@ -58,75 +58,79 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
name = query; name = query;
} }
auto file = engine->getResPaths()->find("layouts/pages/"+name+".xml"); auto file = engine.getResPaths()->find("layouts/pages/"+name+".xml");
auto fullname = "core:pages/"+name; auto fullname = "core:pages/"+name;
auto document_ptr = UiDocument::read( auto documentPtr = UiDocument::read(
scripting::get_root_environment(), scripting::get_root_environment(),
fullname, fullname,
file, file,
"core:layouts/pages/" + name "core:layouts/pages/" + name
); );
auto document = document_ptr.get(); auto document = documentPtr.get();
engine->getAssets()->store(std::move(document_ptr), fullname); engine.getAssets()->store(std::move(documentPtr), fullname);
scripting::on_ui_open(document, std::move(args)); scripting::on_ui_open(document, std::move(args));
return document->getRoot(); return document->getRoot();
}; };
} }
bool menus::call(Engine* engine, runnable func) { bool menus::call(Engine& engine, runnable func) {
if (engine.isHeadless()) {
throw std::runtime_error("menus::call(...) in headless mode");
}
auto gui = engine.getGUI();
try { try {
func(); func();
return true; return true;
} catch (const contentpack_error& error) { } catch (const contentpack_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
// could not to find or read pack // could not to find or read pack
guiutil::alert( guiutil::alert(
engine->getGUI(), langs::get(L"error.pack-not-found")+L": "+ gui, langs::get(L"error.pack-not-found")+L": "+
util::str2wstr_utf8(error.getPackId()) util::str2wstr_utf8(error.getPackId())
); );
return false; return false;
} catch (const assetload::error& error) { } catch (const assetload::error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert( guiutil::alert(
engine->getGUI(), langs::get(L"Assets Load Error", L"menu")+L":\n"+ gui, langs::get(L"Assets Load Error", L"menu")+L":\n"+
util::str2wstr_utf8(error.what()) util::str2wstr_utf8(error.what())
); );
return false; return false;
} catch (const parsing_error& error) { } catch (const parsing_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(engine->getGUI(), util::str2wstr_utf8(error.errorLog())); guiutil::alert(gui, util::str2wstr_utf8(error.errorLog()));
return false; return false;
} catch (const std::runtime_error& error) { } catch (const std::runtime_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert( guiutil::alert(
engine->getGUI(), langs::get(L"Content Error", L"menu")+L":\n"+ gui, langs::get(L"Content Error", L"menu")+L":\n"+
util::str2wstr_utf8(error.what()) util::str2wstr_utf8(error.what())
); );
return false; return false;
} }
} }
UiDocument* menus::show(Engine* engine, const std::string& name, std::vector<dv::value> args) { UiDocument* menus::show(Engine& engine, const std::string& name, std::vector<dv::value> args) {
auto menu = engine->getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
auto file = engine->getResPaths()->find("layouts/"+name+".xml"); auto file = engine.getResPaths()->find("layouts/"+name+".xml");
auto fullname = "core:layouts/"+name; auto fullname = "core:layouts/"+name;
auto document_ptr = UiDocument::read( auto documentPtr = UiDocument::read(
scripting::get_root_environment(), fullname, file, "core:layouts/"+name scripting::get_root_environment(), fullname, file, "core:layouts/"+name
); );
auto document = document_ptr.get(); auto document = documentPtr.get();
engine->getAssets()->store(std::move(document_ptr), fullname); engine.getAssets()->store(std::move(documentPtr), fullname);
scripting::on_ui_open(document, std::move(args)); scripting::on_ui_open(document, std::move(args));
menu->addPage(name, document->getRoot()); menu->addPage(name, document->getRoot());
menu->setPage(name); menu->setPage(name);
return document; return document;
} }
void menus::show_process_panel(Engine* engine, const std::shared_ptr<Task>& task, const std::wstring& text) { void menus::show_process_panel(Engine& engine, const std::shared_ptr<Task>& task, const std::wstring& text) {
uint initialWork = task->getWorkTotal(); uint initialWork = task->getWorkTotal();
auto menu = engine->getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
menu->reset(); menu->reset();
auto doc = menus::show(engine, "process", { auto doc = menus::show(engine, "process", {
util::wstr2str_utf8(langs::get(text)) util::wstr2str_utf8(langs::get(text))

View File

@ -14,17 +14,17 @@ class UiDocument;
namespace menus { namespace menus {
/// @brief Create development version label at the top-right screen corner /// @brief Create development version label at the top-right screen corner
void create_version_label(Engine* engine); void create_version_label(Engine& engine);
gui::page_loader_func create_page_loader(Engine* engine); gui::page_loader_func create_page_loader(Engine& engine);
UiDocument* show( UiDocument* show(
Engine* engine, Engine& engine,
const std::string& name, const std::string& name,
std::vector<dv::value> args std::vector<dv::value> args
); );
void show_process_panel(Engine* engine, const std::shared_ptr<Task>& task, const std::wstring& text=L""); void show_process_panel(Engine& engine, const std::shared_ptr<Task>& task, const std::wstring& text=L"");
bool call(Engine* engine, runnable func); bool call(Engine& engine, runnable func);
} }

View File

@ -34,19 +34,19 @@
static debug::Logger logger("level-screen"); static debug::Logger logger("level-screen");
LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr) LevelScreen::LevelScreen(Engine& engine, std::unique_ptr<Level> levelPtr)
: Screen(engine), postProcessing(std::make_unique<PostProcessing>()) : Screen(engine), postProcessing(std::make_unique<PostProcessing>())
{ {
Level* level = levelPtr.get(); Level* level = levelPtr.get();
auto& settings = engine->getSettings(); auto& settings = engine.getSettings();
auto& assets = *engine->getAssets(); auto& assets = *engine.getAssets();
auto menu = engine->getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
menu->reset(); menu->reset();
auto player = level->players->get(0); auto player = level->players->get(0);
controller = controller =
std::make_unique<LevelController>(engine, std::move(levelPtr), player); std::make_unique<LevelController>(&engine, std::move(levelPtr), player);
playerController = std::make_unique<PlayerController>( playerController = std::make_unique<PlayerController>(
settings, settings,
level, level,
@ -58,12 +58,12 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
player, controller.get(), assets player, controller.get(), assets
); );
worldRenderer = std::make_unique<WorldRenderer>( worldRenderer = std::make_unique<WorldRenderer>(
engine, *frontend, player engine, *frontend, *player
); );
hud = std::make_unique<Hud>(engine, *frontend, *player); hud = std::make_unique<Hud>(engine, *frontend, *player);
decorator = std::make_unique<Decorator>( decorator = std::make_unique<Decorator>(
*engine, *controller, *worldRenderer, assets, *player engine, *controller, *worldRenderer, assets, *player
); );
keepAlive(settings.graphics.backlight.observe([=](bool) { keepAlive(settings.graphics.backlight.observe([=](bool) {
@ -111,15 +111,15 @@ LevelScreen::~LevelScreen() {
// unblock all bindings // unblock all bindings
Events::enableBindings(); Events::enableBindings();
controller->onWorldQuit(); controller->onWorldQuit();
engine->getPaths()->setCurrentWorldFolder(fs::path()); engine.getPaths().setCurrentWorldFolder(fs::path());
} }
void LevelScreen::saveWorldPreview() { void LevelScreen::saveWorldPreview() {
try { try {
logger.info() << "saving world preview"; logger.info() << "saving world preview";
auto paths = engine->getPaths(); const auto& paths = engine.getPaths();
auto player = playerController->getPlayer(); auto player = playerController->getPlayer();
auto& settings = engine->getSettings(); auto& settings = engine.getSettings();
int previewSize = settings.ui.worldPreviewSize.get(); int previewSize = settings.ui.worldPreviewSize.get();
// camera special copy for world preview // camera special copy for world preview
@ -134,7 +134,7 @@ void LevelScreen::saveWorldPreview() {
worldRenderer->draw(ctx, camera, false, true, 0.0f, postProcessing.get()); worldRenderer->draw(ctx, camera, false, true, 0.0f, postProcessing.get());
auto image = postProcessing->toImage(); auto image = postProcessing->toImage();
image->flipY(); image->flipY();
imageio::write(paths->resolve("world:preview.png").u8string(), image.get()); imageio::write(paths.resolve("world:preview.png").u8string(), image.get());
} catch (const std::exception& err) { } catch (const std::exception& err) {
logger.error() << err.what(); logger.error() << err.what();
} }
@ -142,7 +142,7 @@ void LevelScreen::saveWorldPreview() {
void LevelScreen::updateHotkeys() { void LevelScreen::updateHotkeys() {
auto player = playerController->getPlayer(); auto player = playerController->getPlayer();
auto& settings = engine->getSettings(); auto& settings = engine.getSettings();
if (Events::jpressed(keycode::O)) { if (Events::jpressed(keycode::O)) {
settings.graphics.frustumCulling.toggle(); settings.graphics.frustumCulling.toggle();
} }
@ -155,7 +155,7 @@ void LevelScreen::updateHotkeys() {
} }
void LevelScreen::update(float delta) { void LevelScreen::update(float delta) {
gui::GUI* gui = engine->getGUI(); gui::GUI* gui = engine.getGUI();
bool inputLocked = hud->isPause() || bool inputLocked = hud->isPause() ||
hud->isInventoryOpen() || hud->isInventoryOpen() ||
@ -181,7 +181,7 @@ void LevelScreen::update(float delta) {
glm::vec3(0, 1, 0) glm::vec3(0, 1, 0)
); );
auto level = controller->getLevel(); auto level = controller->getLevel();
const auto& settings = engine->getSettings(); const auto& settings = engine.getSettings();
if (!hud->isPause()) { if (!hud->isPause()) {
level->getWorld()->updateTimers(delta); level->getWorld()->updateTimers(delta);
@ -204,7 +204,7 @@ void LevelScreen::draw(float delta) {
DrawContext ctx(nullptr, viewport, batch.get()); DrawContext ctx(nullptr, viewport, batch.get());
if (!hud->isPause()) { if (!hud->isPause()) {
scripting::on_entities_render(engine->getTime().getDelta()); scripting::on_entities_render(engine.getTime().getDelta());
} }
worldRenderer->draw( worldRenderer->draw(
ctx, *camera, hudVisible, hud->isPause(), delta, postProcessing.get() ctx, *camera, hudVisible, hud->isPause(), delta, postProcessing.get()

View File

@ -33,7 +33,7 @@ class LevelScreen : public Screen {
void initializeContent(); void initializeContent();
void initializePack(ContentPackRuntime* pack); void initializePack(ContentPackRuntime* pack);
public: public:
LevelScreen(Engine* engine, std::unique_ptr<Level> level); LevelScreen(Engine& engine, std::unique_ptr<Level> level);
~LevelScreen(); ~LevelScreen();
void update(float delta) override; void update(float delta) override;

View File

@ -10,10 +10,10 @@
#include "window/Camera.hpp" #include "window/Camera.hpp"
#include "engine.hpp" #include "engine.hpp"
MenuScreen::MenuScreen(Engine* engine) : Screen(engine) { MenuScreen::MenuScreen(Engine& engine) : Screen(engine) {
engine->resetContent(); engine.resetContent();
auto menu = engine->getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
menu->reset(); menu->reset();
menu->setPage("main"); menu->setPage("main");
@ -29,7 +29,7 @@ void MenuScreen::update(float delta) {
} }
void MenuScreen::draw(float delta) { void MenuScreen::draw(float delta) {
auto assets = engine->getAssets(); auto assets = engine.getAssets();
Window::clear(); Window::clear();
Window::setBgColor(glm::vec3(0.2f)); Window::setBgColor(glm::vec3(0.2f));

View File

@ -10,7 +10,7 @@ class Engine;
class MenuScreen : public Screen { class MenuScreen : public Screen {
std::unique_ptr<Camera> uicamera; std::unique_ptr<Camera> uicamera;
public: public:
MenuScreen(Engine* engine); MenuScreen(Engine& engine);
~MenuScreen(); ~MenuScreen();
void update(float delta) override; void update(float delta) override;

View File

@ -3,7 +3,7 @@
#include "graphics/core/Batch2D.hpp" #include "graphics/core/Batch2D.hpp"
#include "engine.hpp" #include "engine.hpp"
Screen::Screen(Engine* engine) Screen::Screen(Engine& engine)
: engine(engine), : engine(engine),
batch(std::make_unique<Batch2D>(1024)) { batch(std::make_unique<Batch2D>(1024)) {
} }

View File

@ -8,10 +8,10 @@ class Batch2D;
/// @brief Screen is a mainloop state /// @brief Screen is a mainloop state
class Screen : public util::ObjectsKeeper { class Screen : public util::ObjectsKeeper {
protected: protected:
Engine* engine; Engine& engine;
std::unique_ptr<Batch2D> batch; std::unique_ptr<Batch2D> batch;
public: public:
Screen(Engine* engine); Screen(Engine& engine);
virtual ~Screen(); virtual ~Screen();
virtual void update(float delta) = 0; virtual void update(float delta) = 0;
virtual void draw(float delta) = 0; virtual void draw(float delta) = 0;

View File

@ -13,6 +13,7 @@
#include "content/Content.hpp" #include "content/Content.hpp"
#include "engine.hpp" #include "engine.hpp"
#include "frontend/LevelFrontend.hpp" #include "frontend/LevelFrontend.hpp"
#include "frontend/ContentGfxCache.hpp"
#include "items/Inventory.hpp" #include "items/Inventory.hpp"
#include "items/ItemDef.hpp" #include "items/ItemDef.hpp"
#include "items/ItemStack.hpp" #include "items/ItemStack.hpp"
@ -58,40 +59,40 @@ bool WorldRenderer::showChunkBorders = false;
bool WorldRenderer::showEntitiesDebug = false; bool WorldRenderer::showEntitiesDebug = false;
WorldRenderer::WorldRenderer( WorldRenderer::WorldRenderer(
Engine* engine, LevelFrontend& frontend, Player* player Engine& engine, LevelFrontend& frontend, Player& player
) )
: engine(engine), : engine(engine),
level(frontend.getLevel()), level(frontend.getLevel()),
player(player), player(player),
assets(*engine->getAssets()), assets(*engine.getAssets()),
frustumCulling(std::make_unique<Frustum>()), frustumCulling(std::make_unique<Frustum>()),
lineBatch(std::make_unique<LineBatch>()), lineBatch(std::make_unique<LineBatch>()),
batch3d(std::make_unique<Batch3D>(BATCH3D_CAPACITY)), batch3d(std::make_unique<Batch3D>(BATCH3D_CAPACITY)),
modelBatch(std::make_unique<ModelBatch>( modelBatch(std::make_unique<ModelBatch>(
MODEL_BATCH_CAPACITY, assets, *player->chunks, engine->getSettings() MODEL_BATCH_CAPACITY, assets, *player.chunks, engine.getSettings()
)), )),
particles(std::make_unique<ParticlesRenderer>( particles(std::make_unique<ParticlesRenderer>(
assets, level, *player->chunks, &engine->getSettings().graphics assets, level, *player.chunks, &engine.getSettings().graphics
)), )),
texts(std::make_unique<TextsRenderer>(*batch3d, assets, *frustumCulling)), texts(std::make_unique<TextsRenderer>(*batch3d, assets, *frustumCulling)),
guides(std::make_unique<GuidesRenderer>()), guides(std::make_unique<GuidesRenderer>()),
chunks(std::make_unique<ChunksRenderer>( chunks(std::make_unique<ChunksRenderer>(
&level, &level,
*player->chunks, *player.chunks,
assets, assets,
*frustumCulling, *frustumCulling,
frontend.getContentGfxCache(), frontend.getContentGfxCache(),
engine->getSettings() engine.getSettings()
)), )),
blockWraps( blockWraps(
std::make_unique<BlockWrapsRenderer>(assets, level, *player->chunks) std::make_unique<BlockWrapsRenderer>(assets, level, *player.chunks)
) { ) {
auto& settings = engine->getSettings(); auto& settings = engine.getSettings();
level.events->listen( level.events->listen(
EVT_CHUNK_HIDDEN, EVT_CHUNK_HIDDEN,
[this](LevelEventType, Chunk* chunk) { chunks->unload(chunk); } [this](LevelEventType, Chunk* chunk) { chunks->unload(chunk); }
); );
auto assets = engine->getAssets(); auto assets = engine.getAssets();
skybox = std::make_unique<Skybox>( skybox = std::make_unique<Skybox>(
settings.graphics.skyboxResolution.get(), settings.graphics.skyboxResolution.get(),
assets->require<Shader>("skybox_gen") assets->require<Shader>("skybox_gen")
@ -122,8 +123,8 @@ void WorldRenderer::setupWorldShader(
auto indices = level.content->getIndices(); auto indices = level.content->getIndices();
// Light emission when an emissive item is chosen // Light emission when an emissive item is chosen
{ {
auto inventory = player->getInventory(); auto inventory = player.getInventory();
ItemStack& stack = inventory->getSlot(player->getChosenSlot()); ItemStack& stack = inventory->getSlot(player.getChosenSlot());
auto& item = indices->items.require(stack.getItemId()); auto& item = indices->items.require(stack.getItemId());
float multiplier = 0.5f; float multiplier = 0.5f;
shader.uniform3f( shader.uniform3f(
@ -146,7 +147,7 @@ void WorldRenderer::renderLevel(
) { ) {
texts->render(ctx, camera, settings, hudVisible, false); texts->render(ctx, camera, settings, hudVisible, false);
bool culling = engine->getSettings().graphics.frustumCulling.get(); bool culling = engine.getSettings().graphics.frustumCulling.get();
float fogFactor = float fogFactor =
15.0f / static_cast<float>(settings.chunks.loadDistance.get() - 2); 15.0f / static_cast<float>(settings.chunks.loadDistance.get() - 2);
@ -175,7 +176,7 @@ void WorldRenderer::renderLevel(
setupWorldShader(shader, camera, settings, fogFactor); setupWorldShader(shader, camera, settings, fogFactor);
chunks->drawChunks(camera, shader); chunks->drawChunks(camera, shader);
blockWraps->draw(ctx, *player); blockWraps->draw(ctx, player);
if (hudVisible) { if (hudVisible) {
renderLines(camera, linesShader, ctx); renderLines(camera, linesShader, ctx);
@ -191,11 +192,11 @@ void WorldRenderer::renderLevel(
} }
void WorldRenderer::renderBlockSelection() { void WorldRenderer::renderBlockSelection() {
const auto& selection = player->selection; const auto& selection = player.selection;
auto indices = level.content->getIndices(); auto indices = level.content->getIndices();
blockid_t id = selection.vox.id; blockid_t id = selection.vox.id;
auto& block = indices->blocks.require(id); auto& block = indices->blocks.require(id);
const glm::ivec3 pos = player->selection.position; const glm::ivec3 pos = player.selection.position;
const glm::vec3 point = selection.hitPosition; const glm::vec3 point = selection.hitPosition;
const glm::vec3 norm = selection.normal; const glm::vec3 norm = selection.normal;
@ -210,7 +211,7 @@ void WorldRenderer::renderBlockSelection() {
lineBatch->box( lineBatch->box(
center, size + glm::vec3(0.01), glm::vec4(0.f, 0.f, 0.f, 0.5f) center, size + glm::vec3(0.01), glm::vec4(0.f, 0.f, 0.f, 0.5f)
); );
if (player->debug) { if (player.debug) {
lineBatch->line( lineBatch->line(
point, point + norm * 0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f) point, point + norm * 0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f)
); );
@ -224,12 +225,12 @@ void WorldRenderer::renderLines(
) { ) {
linesShader.use(); linesShader.use();
linesShader.uniformMatrix("u_projview", camera.getProjView()); linesShader.uniformMatrix("u_projview", camera.getProjView());
if (player->selection.vox.id != BLOCK_VOID) { if (player.selection.vox.id != BLOCK_VOID) {
renderBlockSelection(); renderBlockSelection();
} }
if (player->debug && showEntitiesDebug) { if (player.debug && showEntitiesDebug) {
auto ctx = pctx.sub(lineBatch.get()); auto ctx = pctx.sub(lineBatch.get());
bool culling = engine->getSettings().graphics.frustumCulling.get(); bool culling = engine.getSettings().graphics.frustumCulling.get();
level.entities->renderDebug( level.entities->renderDebug(
*lineBatch, culling ? frustumCulling.get() : nullptr, ctx *lineBatch, culling ? frustumCulling.get() : nullptr, ctx
); );
@ -243,8 +244,8 @@ void WorldRenderer::renderHands(
auto indices = level.content->getIndices(); auto indices = level.content->getIndices();
// get current chosen item // get current chosen item
const auto& inventory = player->getInventory(); const auto& inventory = player.getInventory();
int slot = player->getChosenSlot(); int slot = player.getChosenSlot();
const ItemStack& stack = inventory->getSlot(slot); const ItemStack& stack = inventory->getSlot(slot);
const auto& def = indices->items.require(stack.getItemId()); const auto& def = indices->items.require(stack.getItemId());
@ -273,8 +274,8 @@ void WorldRenderer::renderHands(
glm::mat4(1.0f), -glm::pi<float>() * 0.5f, glm::vec3(0, 1, 0) glm::mat4(1.0f), -glm::pi<float>() * 0.5f, glm::vec3(0, 1, 0)
); );
prevRotation = rotation; prevRotation = rotation;
auto offset = -(camera.position - player->getPosition()); auto offset = -(camera.position - player.getPosition());
float angle = glm::radians(player->cam.x - 90); float angle = glm::radians(player.cam.x - 90);
float cos = glm::cos(angle); float cos = glm::cos(angle);
float sin = glm::sin(angle); float sin = glm::sin(angle);
@ -292,7 +293,7 @@ void WorldRenderer::renderHands(
nullptr nullptr
); );
Window::clearDepth(); Window::clearDepth();
setupWorldShader(entityShader, hudcam, engine->getSettings(), 0.0f); setupWorldShader(entityShader, hudcam, engine.getSettings(), 0.0f);
skybox->bind(); skybox->bind();
modelBatch->render(); modelBatch->render();
modelBatch->setLightsOffset(glm::vec3()); modelBatch->setLightsOffset(glm::vec3());
@ -312,12 +313,12 @@ void WorldRenderer::draw(
const Viewport& vp = pctx.getViewport(); const Viewport& vp = pctx.getViewport();
camera.aspect = vp.getWidth() / static_cast<float>(vp.getHeight()); camera.aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
const auto& settings = engine->getSettings(); const auto& settings = engine.getSettings();
const auto& worldInfo = world->getInfo(); const auto& worldInfo = world->getInfo();
skybox->refresh(pctx, worldInfo.daytime, 1.0f + worldInfo.fog * 2.0f, 4); skybox->refresh(pctx, worldInfo.daytime, 1.0f + worldInfo.fog * 2.0f, 4);
const auto& assets = *engine->getAssets(); const auto& assets = *engine.getAssets();
auto& linesShader = assets.require<Shader>("lines"); auto& linesShader = assets.require<Shader>("lines");
/* World render scope with diegetic HUD included */ { /* World render scope with diegetic HUD included */ {
@ -336,12 +337,12 @@ void WorldRenderer::draw(
renderLevel(ctx, camera, settings, delta, pause, hudVisible); renderLevel(ctx, camera, settings, delta, pause, hudVisible);
// Debug lines // Debug lines
if (hudVisible) { if (hudVisible) {
if (player->debug) { if (player.debug) {
guides->renderDebugLines( guides->renderDebugLines(
ctx, camera, *lineBatch, linesShader, showChunkBorders ctx, camera, *lineBatch, linesShader, showChunkBorders
); );
} }
if (player->currentCamera == player->fpCamera) { if (player.currentCamera == player.fpCamera) {
renderHands(camera, delta * !pause); renderHands(camera, delta * !pause);
} }
} }
@ -362,10 +363,10 @@ void WorldRenderer::draw(
} }
void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) { void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
int x = std::floor(player->currentCamera->position.x); int x = std::floor(player.currentCamera->position.x);
int y = std::floor(player->currentCamera->position.y); int y = std::floor(player.currentCamera->position.y);
int z = std::floor(player->currentCamera->position.z); int z = std::floor(player.currentCamera->position.z);
auto block = player->chunks->get(x, y, z); auto block = player.chunks->get(x, y, z);
if (block && block->id) { if (block && block->id) {
const auto& def = const auto& def =
level.content->getIndices()->blocks.require(block->id); level.content->getIndices()->blocks.require(block->id);
@ -384,7 +385,7 @@ void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) {
batch3d->begin(); batch3d->begin();
shader.uniformMatrix("u_projview", glm::mat4(1.0f)); shader.uniformMatrix("u_projview", glm::mat4(1.0f));
shader.uniformMatrix("u_apply", glm::mat4(1.0f)); shader.uniformMatrix("u_apply", glm::mat4(1.0f));
auto light = player->chunks->getLight(x, y, z); auto light = player.chunks->getLight(x, y, z);
float s = Lightmap::extract(light, 3) / 15.0f; float s = Lightmap::extract(light, 3) / 15.0f;
glm::vec4 tint( glm::vec4 tint(
glm::min(1.0f, Lightmap::extract(light, 0) / 15.0f + s), glm::min(1.0f, Lightmap::extract(light, 0) / 15.0f + s),

View File

@ -32,9 +32,9 @@ class Assets;
struct EngineSettings; struct EngineSettings;
class WorldRenderer { class WorldRenderer {
Engine* engine; Engine& engine;
const Level& level; const Level& level;
Player* player; Player& player;
const Assets& assets; const Assets& assets;
std::unique_ptr<Frustum> frustumCulling; std::unique_ptr<Frustum> frustumCulling;
std::unique_ptr<LineBatch> lineBatch; std::unique_ptr<LineBatch> lineBatch;
@ -74,7 +74,7 @@ public:
static bool showChunkBorders; static bool showChunkBorders;
static bool showEntitiesDebug; static bool showEntitiesDebug;
WorldRenderer(Engine* engine, LevelFrontend& frontend, Player* player); WorldRenderer(Engine& engine, LevelFrontend& frontend, Player& player);
~WorldRenderer(); ~WorldRenderer();
void draw( void draw(

View File

@ -612,7 +612,7 @@ static std::shared_ptr<UINode> readInventory(UiXmlReader& reader, const xml::xml
static std::shared_ptr<UINode> readPageBox(UiXmlReader& reader, const xml::xmlelement& element) { static std::shared_ptr<UINode> readPageBox(UiXmlReader& reader, const xml::xmlelement& element) {
auto menu = std::make_shared<Menu>(); auto menu = std::make_shared<Menu>();
// fixme // fixme
menu->setPageLoader(menus::create_page_loader(scripting::engine)); menu->setPageLoader(menus::create_page_loader(*scripting::engine));
_readContainer(reader, element, *menu); _readContainer(reader, element, *menu);
return menu; return menu;

View File

@ -27,13 +27,13 @@ namespace fs = std::filesystem;
static debug::Logger logger("engine-control"); static debug::Logger logger("engine-control");
EngineController::EngineController(Engine* engine) : engine(engine) { EngineController::EngineController(Engine& engine) : engine(engine) {
} }
void EngineController::deleteWorld(const std::string& name) { void EngineController::deleteWorld(const std::string& name) {
fs::path folder = engine->getPaths()->getWorldFolderByName(name); fs::path folder = engine.getPaths().getWorldFolderByName(name);
guiutil::confirm( guiutil::confirm(
engine->getGUI(), engine.getGUI(),
langs::get(L"delete-confirm", L"world") + L" (" + langs::get(L"delete-confirm", L"world") + L" (" +
util::str2wstr_utf8(folder.u8string()) + L")", util::str2wstr_utf8(folder.u8string()) + L")",
[=]() { [=]() {
@ -44,7 +44,7 @@ void EngineController::deleteWorld(const std::string& name) {
} }
std::shared_ptr<Task> create_converter( std::shared_ptr<Task> create_converter(
Engine* engine, Engine& engine,
const std::shared_ptr<WorldFiles>& worldFiles, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
const std::shared_ptr<ContentReport>& report, const std::shared_ptr<ContentReport>& report,
@ -62,25 +62,25 @@ std::shared_ptr<Task> create_converter(
worldFiles, worldFiles,
content, content,
report, report,
[=]() { [&engine, postRunnable]() {
auto menu = engine->getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
menu->reset(); menu->reset();
menu->setPage("main", false); menu->setPage("main", false);
engine->getGUI()->postRunnable([=]() { postRunnable(); }); engine.getGUI()->postRunnable([=]() { postRunnable(); });
}, },
mode, mode,
true true
); );
} }
void show_convert_request( static void show_convert_request(
Engine* engine, Engine& engine,
const Content* content, const Content* content,
const std::shared_ptr<ContentReport>& report, const std::shared_ptr<ContentReport>& report,
const std::shared_ptr<WorldFiles>& worldFiles, const std::shared_ptr<WorldFiles>& worldFiles,
const runnable& postRunnable const runnable& postRunnable
) { ) {
auto on_confirm = [=]() { auto on_confirm = [&engine, worldFiles, content, report, postRunnable]() {
auto converter = auto converter =
create_converter(engine, worldFiles, content, report, postRunnable); create_converter(engine, worldFiles, content, report, postRunnable);
menus::show_process_panel( menus::show_process_panel(
@ -101,7 +101,7 @@ void show_convert_request(
text += util::str2wstr_utf8(line) + L"\n"; text += util::str2wstr_utf8(line) + L"\n";
} }
guiutil::confirmWithMemo( guiutil::confirmWithMemo(
engine->getGUI(), engine.getGUI(),
langs::get(message), langs::get(message),
text, text,
on_confirm, on_confirm,
@ -111,7 +111,7 @@ void show_convert_request(
return; return;
} }
guiutil::confirm( guiutil::confirm(
engine->getGUI(), engine.getGUI(),
langs::get(message), langs::get(message),
on_confirm, on_confirm,
L"", L"",
@ -120,7 +120,7 @@ void show_convert_request(
} }
static void show_content_missing( static void show_content_missing(
Engine* engine, const std::shared_ptr<ContentReport>& report Engine& engine, const std::shared_ptr<ContentReport>& report
) { ) {
auto root = dv::object(); auto root = dv::object();
auto& contentEntries = root.list("content"); auto& contentEntries = root.list("content");
@ -133,28 +133,30 @@ static void show_content_missing(
menus::show(engine, "reports/missing_content", {std::move(root)}); menus::show(engine, "reports/missing_content", {std::move(root)});
} }
static bool load_world_content(Engine* engine, const fs::path& folder) { static bool load_world_content(Engine& engine, const fs::path& folder) {
if (engine->isHeadless()) { if (engine.isHeadless()) {
engine->loadWorldContent(folder); engine.loadWorldContent(folder);
return true; return true;
} else { } else {
return menus::call(engine, [engine, folder]() { return menus::call(engine, [&engine, folder]() {
engine->loadWorldContent(folder); engine.loadWorldContent(folder);
}); });
} }
} }
static void load_world(Engine* engine, const std::shared_ptr<WorldFiles>& worldFiles) { static void load_world(
Engine& engine, const std::shared_ptr<WorldFiles>& worldFiles
) {
try { try {
auto content = engine->getContent(); auto content = engine.getContent();
auto& packs = engine->getContentPacks(); auto& packs = engine.getContentPacks();
auto& settings = engine->getSettings(); auto& settings = engine.getSettings();
auto level = World::load(worldFiles, settings, content, packs); auto level = World::load(worldFiles, settings, content, packs);
engine->onWorldOpen(std::move(level)); engine.onWorldOpen(std::move(level));
} catch (const world_load_error& error) { } catch (const world_load_error& error) {
guiutil::alert( guiutil::alert(
engine->getGUI(), engine.getGUI(),
langs::get(L"Error") + L": " + util::str2wstr_utf8(error.what()) langs::get(L"Error") + L": " + util::str2wstr_utf8(error.what())
); );
return; return;
@ -162,8 +164,8 @@ static void load_world(Engine* engine, const std::shared_ptr<WorldFiles>& worldF
} }
void EngineController::openWorld(const std::string& name, bool confirmConvert) { void EngineController::openWorld(const std::string& name, bool confirmConvert) {
auto paths = engine->getPaths(); auto& paths = engine.getPaths();
auto folder = paths->getWorldsFolder() / fs::u8path(name); auto folder = paths.getWorldsFolder() / fs::u8path(name);
auto worldFile = folder / fs::u8path("world.json"); auto worldFile = folder / fs::u8path("world.json");
if (!fs::exists(worldFile)) { if (!fs::exists(worldFile)) {
throw std::runtime_error(worldFile.u8string() + " does not exists"); throw std::runtime_error(worldFile.u8string() + " does not exists");
@ -173,12 +175,12 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
return; return;
} }
auto* content = engine->getContent(); const Content* content = engine.getContent();
auto worldFiles = std::make_shared<WorldFiles>( auto worldFiles = std::make_shared<WorldFiles>(
folder, engine->getSettings().debug); folder, engine.getSettings().debug);
if (auto report = World::checkIndices(worldFiles, content)) { if (auto report = World::checkIndices(worldFiles, content)) {
if (report->hasMissingContent()) { if (report->hasMissingContent()) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
show_content_missing(engine, report); show_content_missing(engine, report);
} else { } else {
if (confirmConvert) { if (confirmConvert) {
@ -225,15 +227,15 @@ void EngineController::createWorld(
) { ) {
uint64_t seed = str2seed(seedstr); uint64_t seed = str2seed(seedstr);
EnginePaths* paths = engine->getPaths(); EnginePaths& paths = engine.getPaths();
auto folder = paths->getWorldsFolder() / fs::u8path(name); auto folder = paths.getWorldsFolder() / fs::u8path(name);
if (engine->isHeadless()) { if (engine.isHeadless()) {
engine->loadContent(); engine.loadContent();
paths->setCurrentWorldFolder(folder); paths.setCurrentWorldFolder(folder);
} else if (!menus::call(engine, [this, paths, folder]() { } else if (!menus::call(engine, [this, &paths, folder]() {
engine->loadContent(); engine.loadContent();
paths->setCurrentWorldFolder(folder); paths.setCurrentWorldFolder(folder);
})) { })) {
return; return;
} }
@ -242,20 +244,20 @@ void EngineController::createWorld(
generatorID, generatorID,
folder, folder,
seed, seed,
engine->getSettings(), engine.getSettings(),
engine->getContent(), engine.getContent(),
engine->getContentPacks() engine.getContentPacks()
); );
if (!engine->isHeadless()) { if (!engine.isHeadless()) {
level->players->create(); level->players->create();
} }
engine->onWorldOpen(std::move(level)); engine.onWorldOpen(std::move(level));
} }
void EngineController::reopenWorld(World* world) { void EngineController::reopenWorld(World* world) {
std::string wname = world->wfile->getFolder().filename().u8string(); std::string wname = world->wfile->getFolder().filename().u8string();
engine->setScreen(nullptr); engine.setScreen(nullptr);
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine.setScreen(std::make_shared<MenuScreen>(engine));
openWorld(wname, true); openWorld(wname, true);
} }
@ -264,7 +266,7 @@ void EngineController::reconfigPacks(
const std::vector<std::string>& packsToAdd, const std::vector<std::string>& packsToAdd,
const std::vector<std::string>& packsToRemove const std::vector<std::string>& packsToRemove
) { ) {
auto content = engine->getContent(); auto content = engine.getContent();
bool hasIndices = false; bool hasIndices = false;
std::stringstream ss; std::stringstream ss;
@ -281,13 +283,12 @@ void EngineController::reconfigPacks(
} }
} }
runnable removeFunc = [=]() { runnable removeFunc = [this, controller, packsToAdd, packsToRemove]() {
if (controller == nullptr) { if (controller == nullptr) {
try { try {
auto manager = engine->createPacksManager(fs::path("")); auto manager = engine.createPacksManager(fs::path(""));
manager.scan(); manager.scan();
std::vector<std::string> names = auto names = PacksManager::getNames(engine.getContentPacks());
PacksManager::getNames(engine->getContentPacks());
for (const auto& id : packsToAdd) { for (const auto& id : packsToAdd) {
names.push_back(id); names.push_back(id);
} }
@ -296,7 +297,7 @@ void EngineController::reconfigPacks(
names.erase(std::find(names.begin(), names.end(), id)); names.erase(std::find(names.begin(), names.end(), id));
} }
names = manager.assembly(names); names = manager.assembly(names);
engine->getContentPacks() = manager.getAll(names); engine.getContentPacks() = manager.getAll(names);
} catch (const contentpack_error& err) { } catch (const contentpack_error& err) {
throw std::runtime_error( throw std::runtime_error(
std::string(err.what()) + " [" + err.getPackId() + "]" std::string(err.what()) + " [" + err.getPackId() + "]"
@ -304,9 +305,9 @@ void EngineController::reconfigPacks(
} }
} else { } else {
auto world = controller->getLevel()->getWorld(); auto world = controller->getLevel()->getWorld();
auto wfile = world->wfile.get(); auto& wfile = *world->wfile;
controller->saveWorld(); controller->saveWorld();
auto manager = engine->createPacksManager(wfile->getFolder()); auto manager = engine.createPacksManager(wfile.getFolder());
manager.scan(); manager.scan();
auto names = PacksManager::getNames(world->getPacks()); auto names = PacksManager::getNames(world->getPacks());
@ -317,15 +318,15 @@ void EngineController::reconfigPacks(
manager.exclude(id); manager.exclude(id);
names.erase(std::find(names.begin(), names.end(), id)); names.erase(std::find(names.begin(), names.end(), id));
} }
wfile->removeIndices(packsToRemove); wfile.removeIndices(packsToRemove);
wfile->writePacks(manager.getAll(names)); wfile.writePacks(manager.getAll(names));
reopenWorld(world); reopenWorld(world);
} }
}; };
if (hasIndices) { if (hasIndices) {
guiutil::confirm( guiutil::confirm(
engine->getGUI(), engine.getGUI(),
langs::get(L"remove-confirm", L"pack") + L" (" + langs::get(L"remove-confirm", L"pack") + L" (" +
util::str2wstr_utf8(ss.str()) + L")", util::str2wstr_utf8(ss.str()) + L")",
[=]() { removeFunc(); } [=]() { removeFunc(); }

View File

@ -8,9 +8,9 @@ class World;
class LevelController; class LevelController;
class EngineController { class EngineController {
Engine* engine; Engine& engine;
public: public:
EngineController(Engine* engine); EngineController(Engine& engine);
/// @brief Load world, convert if required and set to LevelScreen. /// @brief Load world, convert if required and set to LevelScreen.
/// @param name world name /// @param name world name

View File

@ -225,7 +225,7 @@ static int l_load_texture(lua::State* L) {
} }
static int l_open_folder(lua::State* L) { static int l_open_folder(lua::State* L) {
auto path = engine->getPaths()->resolve(lua::require_string(L, 1)); auto path = engine->getPaths().resolve(lua::require_string(L, 1));
platform::open_folder(path); platform::open_folder(path);
return 0; return 0;
} }

View File

@ -14,14 +14,14 @@ namespace fs = std::filesystem;
using namespace scripting; using namespace scripting;
static fs::path resolve_path(const std::string& path) { static fs::path resolve_path(const std::string& path) {
return engine->getPaths()->resolve(path); return engine->getPaths().resolve(path);
} }
static fs::path resolve_path_soft(const std::string& path) { static fs::path resolve_path_soft(const std::string& path) {
if (path.find(':') == std::string::npos) { if (path.find(':') == std::string::npos) {
return fs::u8path(""); return fs::u8path("");
} }
return engine->getPaths()->resolve(path, false); return engine->getPaths().resolve(path, false);
} }
static int l_find(lua::State* L) { static int l_find(lua::State* L) {

View File

@ -12,9 +12,9 @@
using namespace scripting; using namespace scripting;
static int l_save_fragment(lua::State* L) { static int l_save_fragment(lua::State* L) {
auto paths = engine->getPaths(); const auto& paths = engine->getPaths();
auto fragment = lua::touserdata<lua::LuaVoxelFragment>(L, 1); auto fragment = lua::touserdata<lua::LuaVoxelFragment>(L, 1);
auto file = paths->resolve(lua::require_string(L, 2), true); auto file = paths.resolve(lua::require_string(L, 2), true);
auto map = fragment->getFragment()->serialize(); auto map = fragment->getFragment()->serialize();
auto bytes = json::to_binary(map, true); auto bytes = json::to_binary(map, true);
files::write_bytes(file, bytes.data(), bytes.size()); files::write_bytes(file, bytes.data(), bytes.size());
@ -35,9 +35,9 @@ static int l_create_fragment(lua::State* L) {
} }
static int l_load_fragment(lua::State* L) { static int l_load_fragment(lua::State* L) {
auto paths = engine->getPaths(); const auto& paths = engine->getPaths();
auto filename = lua::require_string(L, 1); auto filename = lua::require_string(L, 1);
auto path = paths->resolve(filename); auto path = paths.resolve(filename);
if (!std::filesystem::exists(path)) { if (!std::filesystem::exists(path)) {
throw std::runtime_error("file "+path.u8string()+" does not exist"); throw std::runtime_error("file "+path.u8string()+" does not exist");
} }

View File

@ -125,7 +125,7 @@ static void resetPackBindings(fs::path& packFolder) {
} }
static int l_reset_bindings(lua::State*) { static int l_reset_bindings(lua::State*) {
auto resFolder = engine->getPaths()->getResourcesFolder(); auto resFolder = engine->getPaths().getResourcesFolder();
resetPackBindings(resFolder); resetPackBindings(resFolder);
for (auto& pack : engine->getContentPacks()) { for (auto& pack : engine->getContentPacks()) {
resetPackBindings(pack.folder); resetPackBindings(pack.folder);

View File

@ -34,8 +34,8 @@ static int l_is_open(lua::State* L) {
} }
static int l_get_list(lua::State* L) { static int l_get_list(lua::State* L) {
auto paths = engine->getPaths(); const auto& paths = engine->getPaths();
auto worlds = paths->scanForWorlds(); auto worlds = paths.scanForWorlds();
lua::createtable(L, worlds.size(), 0); lua::createtable(L, worlds.size(), 0);
for (size_t i = 0; i < worlds.size(); i++) { for (size_t i = 0; i < worlds.size(); i++) {
@ -104,7 +104,7 @@ static int l_get_seed(lua::State* L) {
static int l_exists(lua::State* L) { static int l_exists(lua::State* L) {
auto name = lua::require_string(L, 1); auto name = lua::require_string(L, 1);
auto worldsDir = engine->getPaths()->getWorldFolderByName(name); auto worldsDir = engine->getPaths().getWorldFolderByName(name);
return lua::pushboolean(L, fs::is_directory(worldsDir)); return lua::pushboolean(L, fs::is_directory(worldsDir));
} }

View File

@ -50,9 +50,9 @@ const float* LuaHeightmap::getValues() const {
} }
static int l_dump(lua::State* L) { static int l_dump(lua::State* L) {
auto paths = scripting::engine->getPaths(); const auto& paths = scripting::engine->getPaths();
if (auto heightmap = touserdata<LuaHeightmap>(L, 1)) { if (auto heightmap = touserdata<LuaHeightmap>(L, 1)) {
auto file = paths->resolve(require_string(L, 2)); auto file = paths.resolve(require_string(L, 2));
uint w = heightmap->getWidth(); uint w = heightmap->getWidth();
uint h = heightmap->getHeight(); uint h = heightmap->getHeight();
ImageData image(ImageFormat::rgb888, w, h); ImageData image(ImageFormat::rgb888, w, h);

View File

@ -41,8 +41,8 @@ BlocksController* scripting::blocks = nullptr;
LevelController* scripting::controller = nullptr; LevelController* scripting::controller = nullptr;
void scripting::load_script(const fs::path& name, bool throwable) { void scripting::load_script(const fs::path& name, bool throwable) {
auto paths = scripting::engine->getPaths(); const auto& paths = scripting::engine->getPaths();
fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name; fs::path file = paths.getResourcesFolder() / fs::path("scripts") / name;
std::string src = files::read_string(file); std::string src = files::read_string(file);
auto L = lua::get_main_state(); auto L = lua::get_main_state();
lua::loadbuffer(L, 0, src, "core:scripts/"+name.u8string()); lua::loadbuffer(L, 0, src, "core:scripts/"+name.u8string());
@ -66,7 +66,7 @@ int scripting::load_script(
void scripting::initialize(Engine* engine) { void scripting::initialize(Engine* engine) {
scripting::engine = engine; scripting::engine = engine;
lua::initialize(*engine->getPaths(), engine->getCoreParameters()); lua::initialize(engine->getPaths(), engine->getCoreParameters());
load_script(fs::path("stdlib.lua"), true); load_script(fs::path("stdlib.lua"), true);
load_script(fs::path("classes.lua"), true); load_script(fs::path("classes.lua"), true);

View File

@ -18,7 +18,7 @@ Hud* scripting::hud = nullptr;
WorldRenderer* scripting::renderer = nullptr; WorldRenderer* scripting::renderer = nullptr;
static void load_script(const std::string& name) { static void load_script(const std::string& name) {
auto file = engine->getPaths()->getResourcesFolder() / "scripts" / name; auto file = engine->getPaths().getResourcesFolder() / "scripts" / name;
std::string src = files::read_string(file); std::string src = files::read_string(file);
logger.info() << "loading script " << file.u8string(); logger.info() << "loading script " << file.u8string();

View File

@ -255,7 +255,7 @@ std::unique_ptr<GeneratorScript> scripting::load_generator(
const fs::path& file, const fs::path& file,
const std::string& dirPath const std::string& dirPath
) { ) {
auto L = create_state(*engine->getPaths(), StateType::GENERATOR); auto L = create_state(engine->getPaths(), StateType::GENERATOR);
return std::make_unique<LuaGeneratorScript>(L, def, file, dirPath); return std::make_unique<LuaGeneratorScript>(L, def, file, dirPath);
} }

View File

@ -199,7 +199,7 @@ int Window::initialize(DisplaySettings* settings) {
observers_keeper = util::ObjectsKeeper(); observers_keeper = util::ObjectsKeeper();
observers_keeper.keepAlive(settings->fullscreen.observe( observers_keeper.keepAlive(settings->fullscreen.observe(
[=](bool value) { [](bool value) {
if (value != isFullscreen()) { if (value != isFullscreen()) {
toggleFullscreen(); toggleFullscreen();
} }