-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpresenter.cpp
More file actions
342 lines (280 loc) Β· 11.6 KB
/
Copy pathpresenter.cpp
File metadata and controls
342 lines (280 loc) Β· 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2019-2025 the openage authors. See copying.md for legal info.
#include "presenter.h"
#include <eigen3/Eigen/Dense>
#include <iostream>
#include <string>
#include <vector>
#include "gamestate/simulation.h"
#include "input/controller/camera/binding_context.h"
#include "input/controller/camera/controller.h"
#include "input/controller/game/binding_context.h"
#include "input/controller/game/controller.h"
#include "input/controller/hud/binding_context.h"
#include "input/controller/hud/controller.h"
#include "input/input_context.h"
#include "input/input_manager.h"
#include "log/log.h"
#include "renderer/camera/camera.h"
#include "renderer/gui/gui.h"
#include "renderer/gui/integration/public/gui_application_with_logger.h"
#include "renderer/render_factory.h"
#include "renderer/render_pass.h"
#include "renderer/render_target.h"
#include "renderer/resources/assets/asset_manager.h"
#include "renderer/resources/shader_source.h"
#include "renderer/resources/texture_info.h"
#include "renderer/stages/camera/manager.h"
#include "renderer/stages/hud/render_stage.h"
#include "renderer/stages/screen/render_stage.h"
#include "renderer/stages/skybox/render_stage.h"
#include "renderer/stages/terrain/render_stage.h"
#include "renderer/stages/world/render_stage.h"
#include "time/time_loop.h"
#include "util/path.h"
namespace openage::presenter {
Presenter::Presenter(const util::Path &root_dir,
const std::shared_ptr<gamestate::GameSimulation> &simulation,
const std::shared_ptr<time::TimeLoop> &time_loop) :
root_dir{root_dir},
render_passes{},
simulation{simulation},
time_loop{time_loop} {}
void Presenter::run(const renderer::window_settings window_settings) {
log::log(INFO << "Presenter: Launching subsystems...");
this->init_graphics(window_settings);
this->init_input();
while (not this->window->should_close()) {
this->gui_app->process_events();
// TODO: pass button presses and events from GUI to controller
this->render();
this->renderer->check_error();
this->window->update();
}
log::log(MSG(info) << "Presenter: Draw loop exited");
if (this->simulation) {
this->simulation->stop();
}
if (this->time_loop) {
this->time_loop->stop();
}
this->window->close();
}
void Presenter::set_simulation(const std::shared_ptr<gamestate::GameSimulation> &simulation) {
this->simulation = simulation;
auto render_factory = std::make_shared<renderer::RenderFactory>(this->terrain_renderer, this->world_renderer);
this->simulation->attach_renderer(render_factory);
}
void Presenter::set_time_loop(const std::shared_ptr<time::TimeLoop> &time_loop) {
this->time_loop = time_loop;
}
std::shared_ptr<qtgui::GuiApplication> Presenter::init_window_system() {
return std::make_shared<renderer::gui::GuiApplicationWithLogger>();
}
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
this->window = renderer::Window::create("openage presenter test", window_settings);
this->renderer = this->window->make_renderer();
// Asset mangement
this->asset_manager = std::make_shared<renderer::resources::AssetManager>(
this->renderer,
this->root_dir / "assets" / "converted");
auto missing_tex = this->root_dir / "assets" / "test" / "textures" / "test_missing.sprite";
this->asset_manager->set_placeholder_animation(missing_tex);
// Camera
this->camera = std::make_shared<renderer::camera::Camera>(this->renderer, this->window->get_size());
this->camera->look_at_coord(coord::scene3{10.0, 10.0, 0}); // Center camera on the map
this->window->add_resize_callback([this](size_t w, size_t h, double /*scale*/) {
this->camera->resize(w, h);
});
// Camera manager
this->camera_manager = std::make_shared<renderer::camera::CameraManager>(this->camera);
// TODO: Make boundaries dynamic based on map size.
this->camera_manager->set_camera_boundaries(
renderer::camera::CameraBoundaries{
renderer::camera::X_BOUND_MIN,
renderer::camera::X_BOUND_MAX,
renderer::camera::Y_BOUND_MIN,
renderer::camera::Y_BOUND_MAX,
renderer::camera::Z_BOUND_MIN,
renderer::camera::Z_BOUND_MAX});
// Skybox
this->skybox_renderer = std::make_shared<renderer::skybox::SkyboxRenderStage>(
this->window,
this->renderer,
this->root_dir["assets"]["shaders"]);
this->skybox_renderer->set_color(1.0f, 0.5f, 0.0f, 1.0f);
this->render_passes.push_back(this->skybox_renderer->get_render_pass());
// Terrain
this->terrain_renderer = std::make_shared<renderer::terrain::TerrainRenderStage>(
this->window,
this->renderer,
this->camera,
this->root_dir["assets"]["shaders"],
this->asset_manager,
this->time_loop->get_clock());
this->render_passes.push_back(this->terrain_renderer->get_render_pass());
// Units/buildings
this->world_renderer = std::make_shared<renderer::world::WorldRenderStage>(
this->window,
this->renderer,
this->camera,
this->root_dir["assets"]["shaders"],
this->asset_manager,
this->time_loop->get_clock());
this->render_passes.push_back(this->world_renderer->get_render_pass());
// HUD
this->hud_renderer = std::make_shared<renderer::hud::HudRenderStage>(
this->window,
this->renderer,
this->camera,
this->root_dir["assets"]["shaders"],
this->asset_manager,
this->time_loop->get_clock());
this->render_passes.push_back(this->hud_renderer->get_render_pass());
for (auto &render_pass : render_passes) {
render_pass->set_stencil_state(renderer::StencilState::USE_STENCIL_TEST);
}
this->init_gui();
this->init_final_render_pass();
// set passes indices
this->index_gui_stencil_pass = this->render_passes.size() - 3;
this->index_gui_render_pass = this->render_passes.size() - 2;
this->index_final_render_pass = this->render_passes.size() - 1;
if (this->simulation) {
auto render_factory = std::make_shared<renderer::RenderFactory>(this->terrain_renderer, this->world_renderer);
this->simulation->attach_renderer(render_factory);
}
log::log(INFO << "Presenter: Graphics subsystems initialized");
}
void Presenter::init_gui() {
log::log(INFO << "Presenter: Initializing GUI with Qt backend");
//// -- gui initialization
// TODO: Do not use test GUI
util::Path qml_root = this->root_dir / "assets" / "test" / "qml";
log::log(INFO << "Presenter: Setting QML root to " << qml_root.resolve_native_path());
if (not qml_root.is_dir()) {
throw Error{ERR << "could not find qml root folder " << qml_root};
}
util::Path qml_assets = this->root_dir / "assets";
log::log(INFO << "Presenter: Setting QML asset path to " << qml_assets.resolve_native_path());
if (not qml_assets.is_dir()) {
throw Error{ERR << "could not find asset root folder " << qml_assets};
}
util::Path qml_root_file = qml_root / "main.qml";
log::log(INFO << "Presenter: Setting QML root file to " << qml_root_file.resolve_native_path());
if (not qml_root_file.is_file()) {
throw Error{ERR << "could not find main.qml file " << qml_root_file};
}
// TODO: in order to support qml-mods, the fslike and filelike
// library has to be integrated into qt. For now,
// figure out the absolute paths here and pass them in.
this->gui = std::make_shared<renderer::gui::GUI>(
this->gui_app, // Qt application wrapper
this->window, // window for the gui
qml_root_file, // entry qml file, absolute path.
qml_root, // directory to watch for qml file changes
qml_assets, // qml data: Engine *, the data directory, ...
this->renderer // openage renderer
);
auto stencil_pass = this->gui->get_stencil_pass();
this->render_passes.push_back(stencil_pass);
auto gui_pass = this->gui->get_render_pass();
this->render_passes.push_back(gui_pass);
}
void Presenter::init_input() {
log::log(INFO << "Presenter: Initializing input subsystem...");
this->input_manager = std::make_shared<input::InputManager>();
this->window->add_key_callback([&](const QKeyEvent &ev) {
this->input_manager->process(ev);
});
this->window->add_mouse_button_callback([&](const QMouseEvent &ev) {
this->input_manager->process(ev);
});
this->window->add_mouse_move_callback([&](const QMouseEvent &ev) {
this->input_manager->set_mouse(ev.position().x(), ev.position().y());
this->input_manager->process(ev);
});
this->window->add_mouse_wheel_callback([&](const QWheelEvent &ev) {
this->input_manager->process(ev);
});
auto input_ctx = this->input_manager->get_global_context();
input::setup_defaults(input_ctx);
// setup simulation controls
if (this->simulation) {
log::log(INFO << "Loading game simulation controls");
// TODO: Remove hardcoding
auto game_controller = std::make_shared<input::game::Controller>(
std::unordered_set<size_t>{0, 1, 2, 3}, 0);
auto engine_context = std::make_shared<input::game::BindingContext>();
input::game::setup_defaults(engine_context, this->time_loop, this->simulation, this->camera);
this->input_manager->set_game_controller(game_controller);
input_ctx->set_game_bindings(engine_context);
}
// attach GUI if it's initialized
if (this->gui) {
log::log(INFO << "Loading GUI controls");
this->input_manager->set_gui(this->gui->get_input_handler());
}
// setup camera controls
if (this->camera) {
log::log(INFO << "Loading camera controls");
auto camera_controller = std::make_shared<input::camera::Controller>();
auto camera_context = std::make_shared<input::camera::BindingContext>();
input::camera::setup_defaults(camera_context, this->camera, this->camera_manager);
this->input_manager->set_camera_controller(camera_controller);
input_ctx->set_camera_bindings(camera_context);
}
// setup HUD controls
if (this->hud_renderer) {
log::log(INFO << "Loading HUD controls");
auto hud_controller = std::make_shared<input::hud::Controller>();
auto hud_context = std::make_shared<input::hud::BindingContext>();
input::hud::setup_defaults(hud_context, this->hud_renderer);
this->input_manager->set_hud_controller(hud_controller);
input_ctx->set_hud_bindings(hud_context);
}
log::log(INFO << "Presenter: Input subsystem initialized");
}
void Presenter::init_final_render_pass() {
// Final output to window
this->screen_renderer = std::make_shared<renderer::screen::ScreenRenderStage>(
this->window,
this->renderer,
this->root_dir["assets"]["shaders"]);
std::vector<std::shared_ptr<renderer::RenderTarget>> targets{};
for (auto pass : this->render_passes) {
targets.push_back(pass->get_target());
}
this->screen_renderer->set_render_targets(targets);
this->render_passes.push_back(this->screen_renderer->get_render_pass());
// Update final render pass if the textures are reassigned on resize
// TODO: This REQUIRES that all other render passes have already been
// resized
this->window->add_resize_callback([this](size_t, size_t, double /*scale*/) {
// Acquire the render targets for all previous passes
std::vector<std::shared_ptr<renderer::RenderTarget>> targets{};
for (size_t i = 0; i < this->render_passes.size() - 1; ++i) {
targets.push_back(this->render_passes[i]->get_target());
}
this->screen_renderer->set_render_targets(targets);
});
}
void Presenter::render() {
// TODO: Pass current time to update() instead of fetching it in renderer
this->camera_manager->update();
this->terrain_renderer->update();
this->world_renderer->update();
this->hud_renderer->update();
this->gui->render();
this->renderer->render(this->render_passes[this->index_gui_stencil_pass]);
for (size_t i = 0; i < this->index_gui_stencil_pass; ++i) {
this->renderer->render(this->render_passes[i]);
}
this->gui->render();
this->renderer->render(this->render_passes[this->index_gui_render_pass]);
this->renderer->render(this->render_passes[this->index_final_render_pass]);
}
} // namespace openage::presenter