move window-related code from Engine.cpp to WindowControl
This commit is contained in:
+7
-56
@@ -8,8 +8,6 @@
|
|||||||
#include "assets/AssetsLoader.hpp"
|
#include "assets/AssetsLoader.hpp"
|
||||||
#include "audio/audio.hpp"
|
#include "audio/audio.hpp"
|
||||||
#include "coders/GLSLExtension.hpp"
|
#include "coders/GLSLExtension.hpp"
|
||||||
#include "coders/imageio.hpp"
|
|
||||||
#include "coders/json.hpp"
|
|
||||||
#include "coders/toml.hpp"
|
#include "coders/toml.hpp"
|
||||||
#include "coders/commons.hpp"
|
#include "coders/commons.hpp"
|
||||||
#include "devtools/Editor.hpp"
|
#include "devtools/Editor.hpp"
|
||||||
@@ -23,23 +21,21 @@
|
|||||||
#include "frontend/screens/Screen.hpp"
|
#include "frontend/screens/Screen.hpp"
|
||||||
#include "graphics/render/ModelsGenerator.hpp"
|
#include "graphics/render/ModelsGenerator.hpp"
|
||||||
#include "graphics/core/DrawContext.hpp"
|
#include "graphics/core/DrawContext.hpp"
|
||||||
#include "graphics/core/ImageData.hpp"
|
|
||||||
#include "graphics/core/Shader.hpp"
|
#include "graphics/core/Shader.hpp"
|
||||||
#include "graphics/ui/GUI.hpp"
|
#include "graphics/ui/GUI.hpp"
|
||||||
#include "graphics/ui/elements/Menu.hpp"
|
#include "graphics/ui/elements/Menu.hpp"
|
||||||
#include "objects/rigging.hpp"
|
|
||||||
#include "logic/EngineController.hpp"
|
#include "logic/EngineController.hpp"
|
||||||
#include "logic/CommandsInterpreter.hpp"
|
#include "logic/CommandsInterpreter.hpp"
|
||||||
#include "logic/scripting/scripting.hpp"
|
#include "logic/scripting/scripting.hpp"
|
||||||
#include "logic/scripting/scripting_hud.hpp"
|
#include "logic/scripting/scripting_hud.hpp"
|
||||||
#include "network/Network.hpp"
|
#include "network/Network.hpp"
|
||||||
#include "util/platform.hpp"
|
#include "util/platform.hpp"
|
||||||
#include "window/Camera.hpp"
|
|
||||||
#include "window/input.hpp"
|
#include "window/input.hpp"
|
||||||
#include "window/Window.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "Mainloop.hpp"
|
#include "Mainloop.hpp"
|
||||||
#include "ServerMainloop.hpp"
|
#include "ServerMainloop.hpp"
|
||||||
|
#include "WindowControl.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -50,18 +46,6 @@
|
|||||||
|
|
||||||
static debug::Logger logger("engine");
|
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() {
|
static std::unique_ptr<scripting::IClientProjectScript> load_project_client_script() {
|
||||||
io::path scriptFile = "project:project_client.lua";
|
io::path scriptFile = "project:project_client.lua";
|
||||||
if (io::exists(scriptFile)) {
|
if (io::exists(scriptFile)) {
|
||||||
@@ -119,29 +103,9 @@ void Engine::onContentLoad() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::initializeClient() {
|
void Engine::initializeClient() {
|
||||||
std::string title = project->title;
|
windowControl = std::make_unique<WindowControl>(*this);
|
||||||
if (title.empty()) {
|
auto [window, input] = windowControl->initialize();
|
||||||
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());
|
|
||||||
|
|
||||||
time.set(window->time());
|
|
||||||
if (auto icon = load_icon()) {
|
|
||||||
icon->flipY();
|
|
||||||
window->setIcon(icon.get());
|
|
||||||
}
|
|
||||||
this->window = std::move(window);
|
this->window = std::move(window);
|
||||||
this->input = std::move(input);
|
this->input = std::move(input);
|
||||||
|
|
||||||
@@ -270,7 +234,7 @@ void Engine::loadControls() {
|
|||||||
|
|
||||||
void Engine::updateHotkeys() {
|
void Engine::updateHotkeys() {
|
||||||
if (input->jpressed(Keycode::F2)) {
|
if (input->jpressed(Keycode::F2)) {
|
||||||
saveScreenshot();
|
windowControl->saveScreenshot();
|
||||||
}
|
}
|
||||||
if (input->pressed(Keycode::LEFT_CONTROL) && input->pressed(Keycode::F3) &&
|
if (input->pressed(Keycode::LEFT_CONTROL) && input->pressed(Keycode::F3) &&
|
||||||
input->jpressed(Keycode::U)) {
|
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() {
|
void Engine::run() {
|
||||||
if (params.headless) {
|
if (params.headless) {
|
||||||
ServerMainloop(*this).run();
|
ServerMainloop(*this).run();
|
||||||
@@ -331,13 +287,7 @@ void Engine::updateFrontend() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::nextFrame() {
|
void Engine::nextFrame() {
|
||||||
window->setFramerate(
|
windowControl->nextFrame();
|
||||||
window->isIconified() && settings.display.limitFpsIconified.get()
|
|
||||||
? 20
|
|
||||||
: settings.display.framerate.get()
|
|
||||||
);
|
|
||||||
window->swapBuffers();
|
|
||||||
input->pollEvents();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::startPauseLoop() {
|
void Engine::startPauseLoop() {
|
||||||
@@ -437,7 +387,8 @@ void Engine::loadAssets() {
|
|||||||
|
|
||||||
// no need
|
// no need
|
||||||
// correct log messages order is more useful
|
// 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) {
|
if (threading) {
|
||||||
auto task = loader.startTask([=](){});
|
auto task = loader.startTask([=](){});
|
||||||
task->waitForEnd();
|
task->waitForEnd();
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Window;
|
class Window;
|
||||||
|
class WindowControl;
|
||||||
class Assets;
|
class Assets;
|
||||||
class Level;
|
class Level;
|
||||||
class Screen;
|
class Screen;
|
||||||
@@ -75,6 +76,7 @@ class Engine : public util::ObjectsKeeper {
|
|||||||
std::unique_ptr<gui::GUI> gui;
|
std::unique_ptr<gui::GUI> gui;
|
||||||
std::unique_ptr<devtools::Editor> editor;
|
std::unique_ptr<devtools::Editor> editor;
|
||||||
std::unique_ptr<devtools::DebuggingServer> debuggingServer;
|
std::unique_ptr<devtools::DebuggingServer> debuggingServer;
|
||||||
|
std::unique_ptr<WindowControl> windowControl;
|
||||||
PostRunnables postRunnables;
|
PostRunnables postRunnables;
|
||||||
Time time;
|
Time time;
|
||||||
OnWorldOpen levelConsumer;
|
OnWorldOpen levelConsumer;
|
||||||
@@ -144,8 +146,6 @@ public:
|
|||||||
postRunnables.postRunnable(callback);
|
postRunnables.postRunnable(callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveScreenshot();
|
|
||||||
|
|
||||||
EngineController* getController();
|
EngineController* getController();
|
||||||
|
|
||||||
void setLevelConsumer(OnWorldOpen levelConsumer);
|
void setLevelConsumer(OnWorldOpen levelConsumer);
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -67,7 +67,7 @@ const std::filesystem::path& EnginePaths::getResourcesFolder() const {
|
|||||||
return resourcesFolder;
|
return resourcesFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
|
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) const {
|
||||||
auto folder = SCREENSHOTS_FOLDER;
|
auto folder = SCREENSHOTS_FOLDER;
|
||||||
if (!io::is_directory(folder)) {
|
if (!io::is_directory(folder)) {
|
||||||
io::create_directories(folder);
|
io::create_directories(folder);
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ public:
|
|||||||
|
|
||||||
void setCurrentWorldFolder(io::path folder);
|
void setCurrentWorldFolder(io::path folder);
|
||||||
io::path getCurrentWorldFolder();
|
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);
|
std::string mount(const io::path& file);
|
||||||
void unmount(const std::string& name);
|
void unmount(const std::string& name);
|
||||||
|
|||||||
Reference in New Issue
Block a user