Skip to content

Commit

Permalink
Merge pull request #1726 from ZzzhHe/cli-window-parameters
Browse files Browse the repository at this point in the history
Add CLI Support for Window Parameters
  • Loading branch information
heinezen authored Dec 7, 2024
2 parents dc69f2e + 8094dc6 commit 8e07bba
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 23 deletions.
8 changes: 4 additions & 4 deletions libopenage/engine/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
// Copyright 2023-2024 the openage authors. See copying.md for legal info.

#include "engine.h"

Expand All @@ -16,7 +16,7 @@ namespace openage::engine {
Engine::Engine(mode mode,
const util::Path &root_dir,
const std::vector<std::string> &mods,
bool debug_graphics) :
const renderer::window_settings &window_settings) :
running{true},
run_mode{mode},
root_dir{root_dir},
Expand Down Expand Up @@ -55,8 +55,8 @@ Engine::Engine(mode mode,

// if presenter is used, run it in a separate thread
if (this->run_mode == mode::FULL) {
this->threads.emplace_back([&, debug_graphics]() {
this->presenter->run(debug_graphics);
this->threads.emplace_back([&]() {
this->presenter->run(window_settings);

// Make sure that the presenter gets destructed in the same thread
// otherwise OpenGL complains about missing contexts
Expand Down
6 changes: 4 additions & 2 deletions libopenage/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include <thread>
#include <vector>

#include "renderer/window.h"
#include "util/path.h"


// TODO: Remove custom jthread definition when clang/libc++ finally supports it
#if __llvm__
#if !__cpp_lib_jthread
Expand Down Expand Up @@ -71,12 +73,12 @@ class Engine {
* @param mode The run mode to use.
* @param root_dir openage root directory.
* @param mods The mods to load.
* @param debug_graphics If true, enable OpenGL debug logging.
* @param window_settings The settings to customize the display window (e.g. size, display mode, vsync).
*/
Engine(mode mode,
const util::Path &root_dir,
const std::vector<std::string> &mods,
bool debug_graphics = false);
const renderer::window_settings &window_settings = {});

// engine should not be copied or moved
Engine(const Engine &) = delete;
Expand Down
26 changes: 24 additions & 2 deletions libopenage/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

#include "main.h"

Expand Down Expand Up @@ -31,7 +31,29 @@ int run_game(const main_arguments &args) {
run_mode = openage::engine::Engine::mode::HEADLESS;
}

openage::engine::Engine engine{run_mode, args.root_path, args.mods, args.gl_debug};
// convert window arguments to window settings
renderer::window_settings win_settings = {};
win_settings.width = args.window_args.width;
win_settings.height = args.window_args.height;
win_settings.vsync = args.window_args.vsync;

renderer::window_mode wmode;
if (args.window_args.mode == "fullscreen") {
wmode = renderer::window_mode::FULLSCREEN;
}
else if (args.window_args.mode == "borderless") {
wmode = renderer::window_mode::BORDERLESS;
}
else if (args.window_args.mode == "windowed") {
wmode = renderer::window_mode::WINDOWED;
}
else {
throw Error(MSG(err) << "Invalid window mode: " << args.window_args.mode);
}
win_settings.mode = wmode;
win_settings.debug = args.gl_debug;

openage::engine::Engine engine{run_mode, args.root_path, args.mods, win_settings};

engine.loop();

Expand Down
22 changes: 21 additions & 1 deletion libopenage/main.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

#pragma once

Expand All @@ -16,6 +16,24 @@

namespace openage {

/**
* Window parameters struct.
*
* pxd:
*
* cppclass window_arguments:
* int width
* int height
* bool vsync
* string mode
*/
struct window_arguments {
int width;
int height;
bool vsync;
std::string mode;
};

/**
* Used for passing arguments to run_game.
*
Expand All @@ -26,12 +44,14 @@ namespace openage {
* bool gl_debug
* bool headless
* vector[string] mods
* window_arguments window_args
*/
struct main_arguments {
util::Path root_path;
bool gl_debug;
bool headless;
std::vector<std::string> mods;
window_arguments window_args;
};


Expand Down
13 changes: 4 additions & 9 deletions libopenage/presenter/presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "renderer/stages/skybox/render_stage.h"
#include "renderer/stages/terrain/render_stage.h"
#include "renderer/stages/world/render_stage.h"
#include "renderer/window.h"
#include "time/time_loop.h"
#include "util/path.h"

Expand All @@ -48,10 +47,10 @@ Presenter::Presenter(const util::Path &root_dir,
time_loop{time_loop} {}


void Presenter::run(bool debug_graphics) {
void Presenter::run(const renderer::window_settings window_settings) {
log::log(INFO << "Presenter: Launching subsystems...");

this->init_graphics(debug_graphics);
this->init_graphics(window_settings);

this->init_input();

Expand Down Expand Up @@ -93,18 +92,14 @@ std::shared_ptr<qtgui::GuiApplication> Presenter::init_window_system() {
return std::make_shared<renderer::gui::GuiApplicationWithLogger>();
}

void Presenter::init_graphics(bool debug) {
void Presenter::init_graphics(const renderer::window_settings &window_settings) {
log::log(INFO << "Presenter: Initializing graphics subsystems...");

// Start up rendering framework
this->gui_app = this->init_window_system();

// Window and renderer
renderer::window_settings settings;
settings.width = 1024;
settings.height = 768;
settings.debug = debug;
this->window = renderer::Window::create("openage presenter test", settings);
this->window = renderer::Window::create("openage presenter test", window_settings);
this->renderer = this->window->make_renderer();

// Asset mangement
Expand Down
8 changes: 5 additions & 3 deletions libopenage/presenter/presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
#include <memory>
#include <vector>

#include "renderer/window.h"
#include "util/path.h"


namespace qtgui {
class GuiApplication;
}
Expand Down Expand Up @@ -87,9 +89,9 @@ class Presenter {
/**
* Start the presenter and initialize subsystems.
*
* @param debug_graphics If true, enable OpenGL debug logging.
* @param window_settings The settings to customize the display window (e.g. size, display mode, vsync).
*/
void run(bool debug_graphics = false);
void run(const renderer::window_settings window_settings = {});

/**
* Set the game simulation controlled by this presenter.
Expand Down Expand Up @@ -120,7 +122,7 @@ class Presenter {
* - main renderer
* - component renderers (Terrain, Game Entities, GUI)
*/
void init_graphics(bool debug = false);
void init_graphics(const renderer::window_settings &window_settings = {});

/**
* Initialize the GUI.
Expand Down
17 changes: 17 additions & 0 deletions libopenage/renderer/opengl/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ GlWindow::GlWindow(const std::string &title,
this->window->setFormat(format);
this->window->create();

// set display mode
// Reset to a known state
this->window->setWindowState(Qt::WindowNoState);
switch (settings.mode) {
case window_mode::WINDOWED:
// nothing to do because it's the default
break;
case window_mode::BORDERLESS:
this->window->setFlags(this->window->flags() | Qt::FramelessWindowHint);
break;
case window_mode::FULLSCREEN:
this->window->setWindowState(Qt::WindowFullScreen);
break;
default:
throw Error{MSG(err) << "Invalid window mode."};
}

this->context = std::make_shared<GlContext>(this->window, settings.debug);
if (not this->context->get_raw_context()->isValid()) {
throw Error{MSG(err) << "Failed to create Qt OpenGL context."};
Expand Down
11 changes: 11 additions & 0 deletions libopenage/renderer/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ namespace openage::renderer {

class WindowEventHandler;

/**
* Modes for window display.
*/
enum class window_mode {
FULLSCREEN,
BORDERLESS,
WINDOWED
};

/**
* Settings for creating a window.
*/
Expand All @@ -35,6 +44,8 @@ struct window_settings {
bool vsync = true;
// If true, enable debug logging for the selected backend.
bool debug = false;
// Display mode for the window.
window_mode mode = window_mode::WINDOWED;
};


Expand Down
21 changes: 21 additions & 0 deletions openage/game/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ def init_subparser(cli: ArgumentParser) -> None:
help="Check if the assets are up to date"
)

cli.add_argument(
"--window-size", nargs=2, type=int, default=[1024, 768],
metavar=('WIDTH', 'HEIGHT'),
help="Initial window size in pixels")

cli.add_argument(
"--vsync", action='store_true',
help="Enable vertical synchronization")

cli.add_argument(
"--window-mode", choices=["fullscreen", "borderless", "windowed"], default="windowed",
help="Set the window mode")


def main(args, error):
"""
Expand Down Expand Up @@ -98,5 +111,13 @@ def main(args, error):
# encode modpacks as bytes for the C++ interface
args.modpacks = [modpack.encode('utf-8') for modpack in args.modpacks]

# Pass window parameters to engine
args.window_args = {
"width": args.window_size[0],
"height": args.window_size[1],
"vsync": args.vsync,
"window_mode": args.window_mode,
}

# start the game, continue in main_cpp.pyx!
return run_game(args, root)
8 changes: 7 additions & 1 deletion openage/game/main_cpp.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2023 the openage authors. See copying.md for legal info.
# Copyright 2015-2024 the openage authors. See copying.md for legal info.

from cpython.ref cimport PyObject
from libcpp.string cimport string
Expand Down Expand Up @@ -37,6 +37,12 @@ def run_game(args, root_path):
else:
args_cpp.mods = vector[string]()

# window
args_cpp.window_args.width = args.window_args["width"]
args_cpp.window_args.height = args.window_args["height"]
args_cpp.window_args.vsync = args.window_args["vsync"]
args_cpp.window_args.mode = args.window_args["window_mode"].encode('utf-8')

# run the game!
with nogil:
result = run_game_cpp(args_cpp)
Expand Down
21 changes: 21 additions & 0 deletions openage/main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def init_subparser(cli: ArgumentParser):
"--modpacks", nargs="+", type=str,
help="list of modpacks to load")

cli.add_argument(
"--window-size", nargs=2, type=int, default=[1024, 768],
metavar=('WIDTH', 'HEIGHT'),
help="Initial window size in pixels")

cli.add_argument(
"--vsync", action='store_true',
help="Enable vertical synchronization")

cli.add_argument(
"--window-mode", choices=["fullscreen", "borderless", "windowed"], default="windowed",
help="Set the window mode")


def main(args, error):
"""
Expand Down Expand Up @@ -106,5 +119,13 @@ def main(args, error):
else:
args.modpacks = [query_modpack(list(available_modpacks.keys())).encode("utf-8")]

# Pass window parameters to engine
args.window_args = {
"width": args.window_size[0],
"height": args.window_size[1],
"vsync": args.vsync,
"window_mode": args.window_mode,
}

# start the game, continue in main_cpp.pyx!
return run_game(args, root)
8 changes: 7 additions & 1 deletion openage/main/main_cpp.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015-2023 the openage authors. See copying.md for legal info.
# Copyright 2015-2024 the openage authors. See copying.md for legal info.

from cpython.ref cimport PyObject
from libcpp.string cimport string
Expand Down Expand Up @@ -37,6 +37,12 @@ def run_game(args, root_path):
else:
args_cpp.mods = vector[string]()

# window
args_cpp.window_args.width = args.window_args["width"]
args_cpp.window_args.height = args.window_args["height"]
args_cpp.window_args.vsync = args.window_args["vsync"]
args_cpp.window_args.mode = args.window_args["window_mode"].encode('utf-8')

# run the game!
with nogil:
result = run_game_cpp(args_cpp)
Expand Down

0 comments on commit 8e07bba

Please sign in to comment.