move window-related code from Engine.cpp to WindowControl

This commit is contained in:
MihailRis 2025-11-09 19:09:35 +03:00
parent 87ff77a73f
commit 4bafad708e
6 changed files with 116 additions and 60 deletions

View File

@ -8,8 +8,6 @@
#include "assets/AssetsLoader.hpp"
#include "audio/audio.hpp"
#include "coders/GLSLExtension.hpp"
#include "coders/imageio.hpp"
#include "coders/json.hpp"
#include "coders/toml.hpp"
#include "coders/commons.hpp"
#include "devtools/Editor.hpp"
@ -23,23 +21,21 @@
#include "frontend/screens/Screen.hpp"
#include "graphics/render/ModelsGenerator.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/ImageData.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/ui/GUI.hpp"
#include "graphics/ui/elements/Menu.hpp"
#include "objects/rigging.hpp"
#include "logic/EngineController.hpp"
#include "logic/CommandsInterpreter.hpp"
#include "logic/scripting/scripting.hpp"
#include "logic/scripting/scripting_hud.hpp"
#include "network/Network.hpp"
#include "util/platform.hpp"
#include "window/Camera.hpp"
#include "window/input.hpp"
#include "window/Window.hpp"
#include "world/Level.hpp"
#include "Mainloop.hpp"
#include "ServerMainloop.hpp"
#include "WindowControl.hpp"
#include <iostream>
#include <assert.h>
@ -50,18 +46,6 @@
static debug::Logger logger("engine");
static std::unique_ptr<ImageData> load_icon() {
try {
auto file = "res:textures/misc/icon.png";
if (io::exists(file)) {
return imageio::read(file);
}
} catch (const std::exception& err) {
logger.error() << "could not load window icon: " << err.what();
}
return nullptr;
}
static std::unique_ptr<scripting::IClientProjectScript> load_project_client_script() {
io::path scriptFile = "project:project_client.lua";
if (io::exists(scriptFile)) {
@ -119,29 +103,9 @@ void Engine::onContentLoad() {
}
void Engine::initializeClient() {
std::string title = project->title;
if (title.empty()) {
title = "VoxelCore v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
}
if (ENGINE_DEBUG_BUILD) {
title += " [debug]";
}
if (debuggingServer) {
title = "[debugging] " + title;
}
auto [window, input] = Window::initialize(&settings.display, title);
if (!window || !input){
throw initialize_error("could not initialize window");
}
window->setFramerate(settings.display.framerate.get());
windowControl = std::make_unique<WindowControl>(*this);
auto [window, input] = windowControl->initialize();
time.set(window->time());
if (auto icon = load_icon()) {
icon->flipY();
window->setIcon(icon.get());
}
this->window = std::move(window);
this->input = std::move(input);
@ -270,7 +234,7 @@ void Engine::loadControls() {
void Engine::updateHotkeys() {
if (input->jpressed(Keycode::F2)) {
saveScreenshot();
windowControl->saveScreenshot();
}
if (input->pressed(Keycode::LEFT_CONTROL) && input->pressed(Keycode::F3) &&
input->jpressed(Keycode::U)) {
@ -285,14 +249,6 @@ void Engine::updateHotkeys() {
}
}
void Engine::saveScreenshot() {
auto image = window->takeScreenshot();
image->flipY();
io::path filename = paths.getNewScreenshotFile("png");
imageio::write(filename.string(), image.get());
logger.info() << "saved screenshot as " << filename.string();
}
void Engine::run() {
if (params.headless) {
ServerMainloop(*this).run();
@ -331,13 +287,7 @@ void Engine::updateFrontend() {
}
void Engine::nextFrame() {
window->setFramerate(
window->isIconified() && settings.display.limitFpsIconified.get()
? 20
: settings.display.framerate.get()
);
window->swapBuffers();
input->pollEvents();
windowControl->nextFrame();
}
void Engine::startPauseLoop() {
@ -437,7 +387,8 @@ void Engine::loadAssets() {
// no need
// correct log messages order is more useful
bool threading = false; // look at two upper lines
// todo: before setting to true, check if GLSLExtension thread safe
bool threading = false; // look at three upper lines
if (threading) {
auto task = loader.startTask([=](){});
task->waitForEnd();

View File

@ -14,6 +14,7 @@
#include <string>
class Window;
class WindowControl;
class Assets;
class Level;
class Screen;
@ -75,6 +76,7 @@ class Engine : public util::ObjectsKeeper {
std::unique_ptr<gui::GUI> gui;
std::unique_ptr<devtools::Editor> editor;
std::unique_ptr<devtools::DebuggingServer> debuggingServer;
std::unique_ptr<WindowControl> windowControl;
PostRunnables postRunnables;
Time time;
OnWorldOpen levelConsumer;
@ -144,8 +146,6 @@ public:
postRunnables.postRunnable(callback);
}
void saveScreenshot();
EngineController* getController();
void setLevelConsumer(OnWorldOpen levelConsumer);

View File

@ -0,0 +1,81 @@
#include "WindowControl.hpp"
#include "Engine.hpp"
#include "devtools/Project.hpp"
#include "coders/imageio.hpp"
#include "window/Window.hpp"
#include "window/input.hpp"
#include "debug/Logger.hpp"
#include "graphics/core/ImageData.hpp"
static debug::Logger logger("window-control");
namespace {
static std::unique_ptr<ImageData> load_icon() {
try {
auto file = "res:textures/misc/icon.png";
if (io::exists(file)) {
return imageio::read(file);
}
} catch (const std::exception& err) {
logger.error() << "could not load window icon: " << err.what();
}
return nullptr;
}
}
WindowControl::WindowControl(Engine& engine) : engine(engine) {}
WindowControl::Result WindowControl::initialize() {
const auto& project = engine.getProject();
auto& settings = engine.getSettings();
std::string title = project.title;
if (title.empty()) {
title = "VoxelCore v" +
std::to_string(ENGINE_VERSION_MAJOR) + "." +
std::to_string(ENGINE_VERSION_MINOR);
}
if (ENGINE_DEBUG_BUILD) {
title += " [debug]";
}
if (engine.getDebuggingServer()) {
title = "[debugging] " + title;
}
auto [window, input] = Window::initialize(&settings.display, title);
if (!window || !input){
throw initialize_error("could not initialize window");
}
window->setFramerate(settings.display.framerate.get());
if (auto icon = load_icon()) {
icon->flipY();
window->setIcon(icon.get());
}
return Result {std::move(window), std::move(input)};
}
void WindowControl::saveScreenshot() {
auto& window = engine.getWindow();
const auto& paths = engine.getPaths();
auto image = window.takeScreenshot();
image->flipY();
io::path filename = paths.getNewScreenshotFile("png");
imageio::write(filename.string(), image.get());
logger.info() << "saved screenshot as " << filename.string();
}
void WindowControl::nextFrame() {
const auto& settings = engine.getSettings();
auto& window = engine.getWindow();
auto& input = engine.getInput();
window.setFramerate(
window.isIconified() && settings.display.limitFpsIconified.get()
? 20
: settings.display.framerate.get()
);
window.swapBuffers();
input.pollEvents();
}

View File

@ -0,0 +1,24 @@
#pragma once
#include <memory>
class Window;
class Input;
class Engine;
class WindowControl {
public:
struct Result {
std::unique_ptr<Window> window;
std::unique_ptr<Input> input;
};
WindowControl(Engine& engine);
Result initialize();
void nextFrame();
void saveScreenshot();
private:
Engine& engine;
};

View File

@ -67,7 +67,7 @@ const std::filesystem::path& EnginePaths::getResourcesFolder() const {
return resourcesFolder;
}
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) const {
auto folder = SCREENSHOTS_FOLDER;
if (!io::is_directory(folder)) {
io::create_directories(folder);

View File

@ -63,7 +63,7 @@ public:
void setCurrentWorldFolder(io::path folder);
io::path getCurrentWorldFolder();
io::path getNewScreenshotFile(const std::string& ext);
io::path getNewScreenshotFile(const std::string& ext) const;
std::string mount(const io::path& file);
void unmount(const std::string& name);