Skip to content

Commit 8e07bba

Browse files
authored
Merge pull request SFTtech#1726 from ZzzhHe/cli-window-parameters
Add CLI Support for Window Parameters
2 parents dc69f2e + 8094dc6 commit 8e07bba

File tree

12 files changed

+146
-23
lines changed

12 files changed

+146
-23
lines changed

libopenage/engine/engine.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2023-2024 the openage authors. See copying.md for legal info.
22

33
#include "engine.h"
44

@@ -16,7 +16,7 @@ namespace openage::engine {
1616
Engine::Engine(mode mode,
1717
const util::Path &root_dir,
1818
const std::vector<std::string> &mods,
19-
bool debug_graphics) :
19+
const renderer::window_settings &window_settings) :
2020
running{true},
2121
run_mode{mode},
2222
root_dir{root_dir},
@@ -55,8 +55,8 @@ Engine::Engine(mode mode,
5555

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

6161
// Make sure that the presenter gets destructed in the same thread
6262
// otherwise OpenGL complains about missing contexts

libopenage/engine/engine.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
#include <thread>
88
#include <vector>
99

10+
#include "renderer/window.h"
1011
#include "util/path.h"
1112

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

8183
// engine should not be copied or moved
8284
Engine(const Engine &) = delete;

libopenage/main.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
22

33
#include "main.h"
44

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

34-
openage::engine::Engine engine{run_mode, args.root_path, args.mods, args.gl_debug};
34+
// convert window arguments to window settings
35+
renderer::window_settings win_settings = {};
36+
win_settings.width = args.window_args.width;
37+
win_settings.height = args.window_args.height;
38+
win_settings.vsync = args.window_args.vsync;
39+
40+
renderer::window_mode wmode;
41+
if (args.window_args.mode == "fullscreen") {
42+
wmode = renderer::window_mode::FULLSCREEN;
43+
}
44+
else if (args.window_args.mode == "borderless") {
45+
wmode = renderer::window_mode::BORDERLESS;
46+
}
47+
else if (args.window_args.mode == "windowed") {
48+
wmode = renderer::window_mode::WINDOWED;
49+
}
50+
else {
51+
throw Error(MSG(err) << "Invalid window mode: " << args.window_args.mode);
52+
}
53+
win_settings.mode = wmode;
54+
win_settings.debug = args.gl_debug;
55+
56+
openage::engine::Engine engine{run_mode, args.root_path, args.mods, win_settings};
3557

3658
engine.loop();
3759

libopenage/main.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
1+
// Copyright 2015-2024 the openage authors. See copying.md for legal info.
22

33
#pragma once
44

@@ -16,6 +16,24 @@
1616

1717
namespace openage {
1818

19+
/**
20+
* Window parameters struct.
21+
*
22+
* pxd:
23+
*
24+
* cppclass window_arguments:
25+
* int width
26+
* int height
27+
* bool vsync
28+
* string mode
29+
*/
30+
struct window_arguments {
31+
int width;
32+
int height;
33+
bool vsync;
34+
std::string mode;
35+
};
36+
1937
/**
2038
* Used for passing arguments to run_game.
2139
*
@@ -26,12 +44,14 @@ namespace openage {
2644
* bool gl_debug
2745
* bool headless
2846
* vector[string] mods
47+
* window_arguments window_args
2948
*/
3049
struct main_arguments {
3150
util::Path root_path;
3251
bool gl_debug;
3352
bool headless;
3453
std::vector<std::string> mods;
54+
window_arguments window_args;
3555
};
3656

3757

libopenage/presenter/presenter.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "renderer/stages/skybox/render_stage.h"
3333
#include "renderer/stages/terrain/render_stage.h"
3434
#include "renderer/stages/world/render_stage.h"
35-
#include "renderer/window.h"
3635
#include "time/time_loop.h"
3736
#include "util/path.h"
3837

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

5049

51-
void Presenter::run(bool debug_graphics) {
50+
void Presenter::run(const renderer::window_settings window_settings) {
5251
log::log(INFO << "Presenter: Launching subsystems...");
5352

54-
this->init_graphics(debug_graphics);
53+
this->init_graphics(window_settings);
5554

5655
this->init_input();
5756

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

96-
void Presenter::init_graphics(bool debug) {
95+
void Presenter::init_graphics(const renderer::window_settings &window_settings) {
9796
log::log(INFO << "Presenter: Initializing graphics subsystems...");
9897

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

102101
// Window and renderer
103-
renderer::window_settings settings;
104-
settings.width = 1024;
105-
settings.height = 768;
106-
settings.debug = debug;
107-
this->window = renderer::Window::create("openage presenter test", settings);
102+
this->window = renderer::Window::create("openage presenter test", window_settings);
108103
this->renderer = this->window->make_renderer();
109104

110105
// Asset mangement

libopenage/presenter/presenter.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include <memory>
66
#include <vector>
77

8+
#include "renderer/window.h"
89
#include "util/path.h"
910

11+
1012
namespace qtgui {
1113
class GuiApplication;
1214
}
@@ -87,9 +89,9 @@ class Presenter {
8789
/**
8890
* Start the presenter and initialize subsystems.
8991
*
90-
* @param debug_graphics If true, enable OpenGL debug logging.
92+
* @param window_settings The settings to customize the display window (e.g. size, display mode, vsync).
9193
*/
92-
void run(bool debug_graphics = false);
94+
void run(const renderer::window_settings window_settings = {});
9395

9496
/**
9597
* Set the game simulation controlled by this presenter.
@@ -120,7 +122,7 @@ class Presenter {
120122
* - main renderer
121123
* - component renderers (Terrain, Game Entities, GUI)
122124
*/
123-
void init_graphics(bool debug = false);
125+
void init_graphics(const renderer::window_settings &window_settings = {});
124126

125127
/**
126128
* Initialize the GUI.

libopenage/renderer/opengl/window.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ GlWindow::GlWindow(const std::string &title,
5757
this->window->setFormat(format);
5858
this->window->create();
5959

60+
// set display mode
61+
// Reset to a known state
62+
this->window->setWindowState(Qt::WindowNoState);
63+
switch (settings.mode) {
64+
case window_mode::WINDOWED:
65+
// nothing to do because it's the default
66+
break;
67+
case window_mode::BORDERLESS:
68+
this->window->setFlags(this->window->flags() | Qt::FramelessWindowHint);
69+
break;
70+
case window_mode::FULLSCREEN:
71+
this->window->setWindowState(Qt::WindowFullScreen);
72+
break;
73+
default:
74+
throw Error{MSG(err) << "Invalid window mode."};
75+
}
76+
6077
this->context = std::make_shared<GlContext>(this->window, settings.debug);
6178
if (not this->context->get_raw_context()->isValid()) {
6279
throw Error{MSG(err) << "Failed to create Qt OpenGL context."};

libopenage/renderer/window.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ namespace openage::renderer {
2121

2222
class WindowEventHandler;
2323

24+
/**
25+
* Modes for window display.
26+
*/
27+
enum class window_mode {
28+
FULLSCREEN,
29+
BORDERLESS,
30+
WINDOWED
31+
};
32+
2433
/**
2534
* Settings for creating a window.
2635
*/
@@ -35,6 +44,8 @@ struct window_settings {
3544
bool vsync = true;
3645
// If true, enable debug logging for the selected backend.
3746
bool debug = false;
47+
// Display mode for the window.
48+
window_mode mode = window_mode::WINDOWED;
3849
};
3950

4051

openage/game/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ def init_subparser(cli: ArgumentParser) -> None:
3636
help="Check if the assets are up to date"
3737
)
3838

39+
cli.add_argument(
40+
"--window-size", nargs=2, type=int, default=[1024, 768],
41+
metavar=('WIDTH', 'HEIGHT'),
42+
help="Initial window size in pixels")
43+
44+
cli.add_argument(
45+
"--vsync", action='store_true',
46+
help="Enable vertical synchronization")
47+
48+
cli.add_argument(
49+
"--window-mode", choices=["fullscreen", "borderless", "windowed"], default="windowed",
50+
help="Set the window mode")
51+
3952

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

114+
# Pass window parameters to engine
115+
args.window_args = {
116+
"width": args.window_size[0],
117+
"height": args.window_size[1],
118+
"vsync": args.vsync,
119+
"window_mode": args.window_mode,
120+
}
121+
101122
# start the game, continue in main_cpp.pyx!
102123
return run_game(args, root)

openage/game/main_cpp.pyx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015-2023 the openage authors. See copying.md for legal info.
1+
# Copyright 2015-2024 the openage authors. See copying.md for legal info.
22

33
from cpython.ref cimport PyObject
44
from libcpp.string cimport string
@@ -37,6 +37,12 @@ def run_game(args, root_path):
3737
else:
3838
args_cpp.mods = vector[string]()
3939

40+
# window
41+
args_cpp.window_args.width = args.window_args["width"]
42+
args_cpp.window_args.height = args.window_args["height"]
43+
args_cpp.window_args.vsync = args.window_args["vsync"]
44+
args_cpp.window_args.mode = args.window_args["window_mode"].encode('utf-8')
45+
4046
# run the game!
4147
with nogil:
4248
result = run_game_cpp(args_cpp)

openage/main/main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ def init_subparser(cli: ArgumentParser):
2828
"--modpacks", nargs="+", type=str,
2929
help="list of modpacks to load")
3030

31+
cli.add_argument(
32+
"--window-size", nargs=2, type=int, default=[1024, 768],
33+
metavar=('WIDTH', 'HEIGHT'),
34+
help="Initial window size in pixels")
35+
36+
cli.add_argument(
37+
"--vsync", action='store_true',
38+
help="Enable vertical synchronization")
39+
40+
cli.add_argument(
41+
"--window-mode", choices=["fullscreen", "borderless", "windowed"], default="windowed",
42+
help="Set the window mode")
43+
3144

3245
def main(args, error):
3346
"""
@@ -106,5 +119,13 @@ def main(args, error):
106119
else:
107120
args.modpacks = [query_modpack(list(available_modpacks.keys())).encode("utf-8")]
108121

122+
# Pass window parameters to engine
123+
args.window_args = {
124+
"width": args.window_size[0],
125+
"height": args.window_size[1],
126+
"vsync": args.vsync,
127+
"window_mode": args.window_mode,
128+
}
129+
109130
# start the game, continue in main_cpp.pyx!
110131
return run_game(args, root)

openage/main/main_cpp.pyx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2015-2023 the openage authors. See copying.md for legal info.
1+
# Copyright 2015-2024 the openage authors. See copying.md for legal info.
22

33
from cpython.ref cimport PyObject
44
from libcpp.string cimport string
@@ -37,6 +37,12 @@ def run_game(args, root_path):
3737
else:
3838
args_cpp.mods = vector[string]()
3939

40+
# window
41+
args_cpp.window_args.width = args.window_args["width"]
42+
args_cpp.window_args.height = args.window_args["height"]
43+
args_cpp.window_args.vsync = args.window_args["vsync"]
44+
args_cpp.window_args.mode = args.window_args["window_mode"].encode('utf-8')
45+
4046
# run the game!
4147
with nogil:
4248
result = run_game_cpp(args_cpp)

0 commit comments

Comments
 (0)