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-speed", 1)
test.quit()
test.reconfig_packs({"base"}, {})
test.new_world("demo", "2019", "core:default")
local pid1 = player.create("Xerxes")

View File

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

View File

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

View File

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

View File

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

View File

@ -132,7 +132,7 @@ std::filesystem::path EnginePaths::getSettingsFile() const {
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;
auto folder = getWorldsFolder();
@ -189,7 +189,7 @@ std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view pat
std::filesystem::path EnginePaths::resolve(
const std::string& path, bool throwErr
) {
) const {
auto [prefix, filename] = EnginePaths::parsePath(path);
if (prefix.empty()) {
throw files_access_error("no entry point specified");

View File

@ -39,9 +39,9 @@ public:
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);

View File

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

View File

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

View File

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

View File

@ -24,8 +24,8 @@
namespace fs = std::filesystem;
using namespace gui;
void menus::create_version_label(Engine* engine) {
auto gui = engine->getGUI();
void menus::create_version_label(Engine& engine) {
auto gui = engine.getGUI();
auto text = ENGINE_VERSION_STRING+" debug build";
gui->add(guiutil::create(
"<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) {
return [=](const std::string& query) {
gui::page_loader_func menus::create_page_loader(Engine& engine) {
return [&](const std::string& query) {
std::vector<dv::value> args;
std::string name;
@ -58,75 +58,79 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) {
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 document_ptr = UiDocument::read(
auto documentPtr = UiDocument::read(
scripting::get_root_environment(),
fullname,
file,
"core:layouts/pages/" + name
);
auto document = document_ptr.get();
engine->getAssets()->store(std::move(document_ptr), fullname);
auto document = documentPtr.get();
engine.getAssets()->store(std::move(documentPtr), fullname);
scripting::on_ui_open(document, std::move(args));
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 {
func();
return true;
} 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
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())
);
return false;
} catch (const assetload::error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
engine.setScreen(std::make_shared<MenuScreen>(engine));
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())
);
return false;
} catch (const parsing_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(engine->getGUI(), util::str2wstr_utf8(error.errorLog()));
engine.setScreen(std::make_shared<MenuScreen>(engine));
guiutil::alert(gui, util::str2wstr_utf8(error.errorLog()));
return false;
} catch (const std::runtime_error& error) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
engine.setScreen(std::make_shared<MenuScreen>(engine));
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())
);
return false;
}
}
UiDocument* menus::show(Engine* engine, const std::string& name, std::vector<dv::value> args) {
auto menu = engine->getGUI()->getMenu();
auto file = engine->getResPaths()->find("layouts/"+name+".xml");
UiDocument* menus::show(Engine& engine, const std::string& name, std::vector<dv::value> args) {
auto menu = engine.getGUI()->getMenu();
auto file = engine.getResPaths()->find("layouts/"+name+".xml");
auto fullname = "core:layouts/"+name;
auto document_ptr = UiDocument::read(
auto documentPtr = UiDocument::read(
scripting::get_root_environment(), fullname, file, "core:layouts/"+name
);
auto document = document_ptr.get();
engine->getAssets()->store(std::move(document_ptr), fullname);
auto document = documentPtr.get();
engine.getAssets()->store(std::move(documentPtr), fullname);
scripting::on_ui_open(document, std::move(args));
menu->addPage(name, document->getRoot());
menu->setPage(name);
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();
auto menu = engine->getGUI()->getMenu();
auto menu = engine.getGUI()->getMenu();
menu->reset();
auto doc = menus::show(engine, "process", {
util::wstr2str_utf8(langs::get(text))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -32,9 +32,9 @@ class Assets;
struct EngineSettings;
class WorldRenderer {
Engine* engine;
Engine& engine;
const Level& level;
Player* player;
Player& player;
const Assets& assets;
std::unique_ptr<Frustum> frustumCulling;
std::unique_ptr<LineBatch> lineBatch;
@ -74,7 +74,7 @@ public:
static bool showChunkBorders;
static bool showEntitiesDebug;
WorldRenderer(Engine* engine, LevelFrontend& frontend, Player* player);
WorldRenderer(Engine& engine, LevelFrontend& frontend, Player& player);
~WorldRenderer();
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) {
auto menu = std::make_shared<Menu>();
// fixme
menu->setPageLoader(menus::create_page_loader(scripting::engine));
menu->setPageLoader(menus::create_page_loader(*scripting::engine));
_readContainer(reader, element, *menu);
return menu;

View File

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

View File

@ -8,9 +8,9 @@ class World;
class LevelController;
class EngineController {
Engine* engine;
Engine& engine;
public:
EngineController(Engine* engine);
EngineController(Engine& engine);
/// @brief Load world, convert if required and set to LevelScreen.
/// @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) {
auto path = engine->getPaths()->resolve(lua::require_string(L, 1));
auto path = engine->getPaths().resolve(lua::require_string(L, 1));
platform::open_folder(path);
return 0;
}

View File

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

View File

@ -12,9 +12,9 @@
using namespace scripting;
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 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 bytes = json::to_binary(map, true);
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) {
auto paths = engine->getPaths();
const auto& paths = engine->getPaths();
auto filename = lua::require_string(L, 1);
auto path = paths->resolve(filename);
auto path = paths.resolve(filename);
if (!std::filesystem::exists(path)) {
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*) {
auto resFolder = engine->getPaths()->getResourcesFolder();
auto resFolder = engine->getPaths().getResourcesFolder();
resetPackBindings(resFolder);
for (auto& pack : engine->getContentPacks()) {
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) {
auto paths = engine->getPaths();
auto worlds = paths->scanForWorlds();
const auto& paths = engine->getPaths();
auto worlds = paths.scanForWorlds();
lua::createtable(L, worlds.size(), 0);
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) {
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));
}

View File

@ -50,9 +50,9 @@ const float* LuaHeightmap::getValues() const {
}
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)) {
auto file = paths->resolve(require_string(L, 2));
auto file = paths.resolve(require_string(L, 2));
uint w = heightmap->getWidth();
uint h = heightmap->getHeight();
ImageData image(ImageFormat::rgb888, w, h);

View File

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

View File

@ -18,7 +18,7 @@ Hud* scripting::hud = nullptr;
WorldRenderer* scripting::renderer = nullptr;
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);
logger.info() << "loading script " << file.u8string();

View File

@ -255,7 +255,7 @@ std::unique_ptr<GeneratorScript> scripting::load_generator(
const fs::path& file,
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);
}

View File

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