Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
application.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <RmlUi/Config/Config.h>
20#include <RmlUi/Core/Context.h>
21#include <RmlUi/Core/Element.h>
22#include <RmlUi/Core/ElementDocument.h>
23#include <RmlUi/Core/ElementInstancer.h>
24#include <RmlUi/Core/Event.h>
25#include <RmlUi/Core/EventListener.h>
26#include <RmlUi/Core/EventListenerInstancer.h>
27#include <RmlUi/Core/FileInterface.h>
28#include <RmlUi/Core/RenderInterface.h>
29#include <RmlUi/Core/Types.h>
30#include <glad/glad.h>
31#include <imgui_node_editor.h>
32#include <spdlog/spdlog.h>
33
34#include <algorithm>
35#include <map>
36#include <memory>
37#include <string>
38#include <utility>
39#include <vector>
40
41#include <glm/fwd.hpp>
42
43#include "core/game.h"
48#include "engine/engine.h"
49#include "engine/gamestate.h"
51#include "engine/gui.h"
52#include "engine/scene.h"
53#include "engine/scenemanager.h"
56#include "engine/userinput.h"
57#include "engine/window.h"
58#include "graphics/shader.h"
59
60namespace cqsp::engine {
62 public:
63 Application(int _argc, char* _argv[]);
64
65 /*
66 * Runs the entire application.
67 */
68 void run();
69
71
72 int GetWindowHeight() const { return m_window->GetWindowHeight(); }
73
74 int GetWindowWidth() const { return m_window->GetWindowWidth(); }
75
76 double GetDeltaTime() const { return deltaTime; }
77 double GetFps() const { return fps; }
78
80
81 bool ShouldExit();
82
83 void ExitApplication();
84
85 /*
86 * Sets the next scene which will be run in the next frame.
87 */
88 template <class T>
89 void SetScene() {
90 std::unique_ptr<Scene> ptr = std::make_unique<T>(*this);
91 m_scene_manager.SetScene(std::move(ptr));
92 }
93
94 template <class T, typename... Args>
95 void SetScene(Args&&... args) {
96 std::unique_ptr<Scene> ptr = std::make_unique<T>(*this, std::forward<Args>(args)...);
97 m_scene_manager.SetScene(std::move(ptr));
98 }
99
100 GameState* GetGame() { return m_game.get(); }
101
103
104 bool ButtonIsHeld(int btn) { return m_window->ButtonIsHeld(btn); }
105 bool ButtonIsHeld(KeyInput btn) { return ButtonIsHeld(GetGlfwKey(btn)); }
106
107 bool ButtonIsReleased(int btn) { return m_window->ButtonIsReleased(btn); }
109
110 bool ButtonIsPressed(int btn) { return m_window->ButtonIsPressed(btn); }
112
115
117
118 double GetMouseX() { return m_window->GetMouseX(); }
119 double GetMouseY() { return m_window->GetMouseY(); }
120
121 bool MouseButtonIsHeld(int btn) { return m_window->MouseButtonIsHeld(btn); }
123 bool MouseButtonIsPressed(int btn) { return m_window->MouseButtonIsPressed(btn); }
124
125 Rml::ElementDocument* LoadDocument(const std::string& path);
126 void CloseDocument(const std::string& path);
127 bool DocumentIsLoaded(const std::string& path) { return loaded_documents.contains(path); }
128
140 Rml::ElementDocument* ReloadDocument(const std::string& path);
141
142 Window* GetWindow() { return m_window; }
143
144 asset::Font*& GetFont() { return m_font; }
145 void DrawText(const std::string& text, float x, float y);
146 void DrawText(const std::string& text, const glm::vec3& color, float x, float y);
147 void DrawText(const std::string& text, float x, float y, float size);
148 void DrawText(const std::string& text, const glm::vec3& color, float x, float y, float size);
149 // Draw text based on normalized device coordinates
150 void DrawTextNormalized(const std::string& text, float x, float y);
151
152 void SetFont(asset::Font* font) { m_font = font; }
154
159 double GetTime();
160
161 bool MouseDragged() { return m_window->MouseDragged(); }
162 bool MouseMoved() { return m_window->MouseMoved(); }
163
164 void SetWindowDimensions(int width, int height);
165 void SetFullScreen(bool screen);
166
168
169 glm::mat4 Get2DProj() { return two_dim_projection; }
170 glm::mat4 Get3DProj() { return three_dim_projection; }
171 glm::mat4 GetRmlUiProj() { return rmlui_projection; }
172
173 std::vector<std::string>& GetCmdLineArgs() { return cmd_line_args; }
174
175 bool HasCmdLineArgs(const std::string& arg) {
176 return (std::find(GetCmdLineArgs().begin(), GetCmdLineArgs().end(), arg) != GetCmdLineArgs().end());
177 }
184 bool Screenshot(const char* path = NULL);
185
186 Rml::Context* GetRmlUiContext() { return rml_context; }
187
188 class CqspEventInstancer : public Rml::EventListenerInstancer {
189 public:
192
194 Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* element) override;
195 };
196
197 typedef void (*EventListener)(Rml::Event&);
198
200 public:
201 explicit CqspEventListener(const std::string& name) : name(name) {}
203 void ProcessEvent(Rml::Event& event);
204 std::string name;
205 };
206
207 template <class T>
208 void InitGame() {
209 m_game = std::make_unique<T>();
210 }
211
212 sol::state& GetSolState() { return lua_state; }
213
218
219 private:
220 void InitFonts();
221
222 void SetIcon();
223
224 bool GlInit();
225 void LoggerInit();
226 void LogInfo();
227
228 void InitRmlUi();
229 void InitImgui();
230
231 void InitAudio();
232
233 void UpdateScene();
234 void ComputeFramerate();
235 void UpdateFonts();
236
237 /*
238 * Intializes glfw and imgui.
239 */
240 int init();
241
242 void ResetGame() { m_game.reset(); }
243
244 /*
245 * Releases all data.
246 */
247 int destroy();
248
250
251 class RmlFileInterface : public Rml::FileInterface {
252 public:
255 Rml::FileHandle Open(const Rml::String& path);
256 // Closes a previously opened file.
257 void Close(Rml::FileHandle file);
258 // Reads data from a previously opened file.
259 size_t Read(void* buffer, size_t size, Rml::FileHandle file);
260
261 // Seeks to a point in a previously opened file.
262 bool Seek(Rml::FileHandle file, long offset, int origin);
263
264 // Returns the current position of the file pointer.
265 size_t Tell(Rml::FileHandle file);
266 std::string root;
267 };
268
269 std::vector<std::string> cmd_line_args;
272 std::string icon_path;
273
274 Rml::Context* rml_context;
275 std::unique_ptr<SystemInterface_GLFW> m_system_interface;
276 std::unique_ptr<Rml::FileInterface> m_file_interface;
277 std::unique_ptr<Rml::RenderInterface> m_render_interface;
278 ax::NodeEditor::EditorContext* m_ne_context = nullptr;
279
280 std::unique_ptr<CqspEventInstancer> m_event_instancer;
281
283
285
286 double fps;
287
289
290 std::string locale;
291
293
294 std::unique_ptr<GameState> m_game;
295
296 asset::Font* m_font = nullptr;
298
299 std::map<std::string, std::string> properties;
300
302
303 std::map<std::string, Rml::ElementDocument*> loaded_documents;
304
312 sol::state lua_state;
314
315 bool draw_fps = false;
316
317 ax::NodeEditor::Config node_editor_config;
318};
319} // namespace cqsp::engine
Definition: DataView.h:21
Definition: assetmanager.h:46
Definition: text.h:34
A shader program.
Definition: shader.h:68
Definition: clientoptions.h:26
Rml::EventListener * InstanceEventListener(const Rml::String &value, Rml::Element *element) override
Instances a new event handle for Invaders.
Definition: application.cpp:511
void ProcessEvent(Rml::Event &event)
Definition: application.cpp:519
CqspEventListener(const std::string &name)
Definition: application.h:201
std::string name
Definition: application.h:204
Definition: application.h:251
RmlFileInterface()
Definition: application.cpp:526
Rml::FileHandle Open(const Rml::String &path)
Definition: application.cpp:530
void Close(Rml::FileHandle file)
Definition: application.cpp:545
std::string root
Definition: application.h:266
bool Seek(Rml::FileHandle file, long offset, int origin)
Definition: application.cpp:551
size_t Read(void *buffer, size_t size, Rml::FileHandle file)
Definition: application.cpp:548
size_t Tell(Rml::FileHandle file)
Definition: application.cpp:554
Definition: application.h:61
int init()
Definition: application.cpp:63
void SetFontShader(asset::ShaderProgram *shader)
Definition: application.h:153
void SetWindowDimensions(int width, int height)
Definition: application.cpp:503
void ComputeFramerate()
Definition: application.cpp:131
void InitRmlUi()
Definition: application.cpp:147
bool to_halt
Definition: application.h:308
Rml::ElementDocument * ReloadDocument(const std::string &path)
Reloads the document from the file How to hotload the a document.
Definition: application.cpp:355
bool ButtonIsPressed(KeyInput btn)
Definition: application.h:111
Rml::DataViewInstancerDefault< ui::DataViewNumber > instancer
Definition: application.h:313
Window * GetWindow()
Definition: application.h:142
void UpdateFonts()
Definition: application.cpp:139
bool ButtonIsHeld(int btn)
Definition: application.h:104
audio::IAudioInterface & GetAudioInterface()
Definition: application.h:102
ax::NodeEditor::Config node_editor_config
Definition: application.h:317
double GetTime()
Time since glfw was initialized in seconds
Definition: application.cpp:407
asset::Font *& GetFont()
Definition: application.h:144
int GetWindowWidth() const
Definition: application.h:74
std::unique_ptr< Rml::FileInterface > m_file_interface
Definition: application.h:276
Rml::ElementDocument * LoadDocument(const std::string &path)
Definition: application.cpp:334
bool draw_fps
Definition: application.h:315
std::unique_ptr< GameState > m_game
Definition: application.h:294
bool MouseButtonIsHeld(int btn)
Definition: application.h:121
bool ButtonIsHeld(KeyInput btn)
Definition: application.h:105
asset::AssetManager manager
Definition: application.h:292
bool ButtonIsReleased(int btn)
Definition: application.h:107
glm::mat4 two_dim_projection
Definition: application.h:305
std::map< std::string, std::string > properties
Definition: application.h:299
SceneManager m_scene_manager
Definition: application.h:282
void LogInfo()
Definition: application.cpp:485
bool HoveringOnRmluiComponent()
Definition: application.cpp:521
void InitImgui()
Definition: application.cpp:88
void run()
Definition: application.cpp:264
sol::state & GetSolState()
Definition: application.h:212
bool MouseButtonIsPressed(int btn)
Definition: application.h:123
void SetScene()
Definition: application.h:89
void CalculateProjections()
Definition: application.cpp:235
double MouseButtonLastReleased(int btn)
Definition: application.h:113
Application(int _argc, char *_argv[])
Definition: application.cpp:246
glm::mat4 Get3DProj()
Definition: application.h:170
double GetScrollAmount()
Definition: application.h:116
bool ButtonIsPressed(int btn)
Definition: application.h:110
glm::mat4 three_dim_projection
Definition: application.h:306
ax::NodeEditor::EditorContext * m_ne_context
Definition: application.h:278
GameState * GetGame()
Definition: application.h:100
void SetIcon()
Definition: application.cpp:457
bool KeyboardInteractingWithUi()
Definition: application.cpp:127
asset::ShaderProgram * fontShader
Definition: application.h:297
bool ButtonIsReleased(KeyInput btn)
Definition: application.h:108
glm::mat4 rmlui_projection
Definition: application.h:307
std::unique_ptr< SystemInterface_GLFW > m_system_interface
Definition: application.h:275
void SetFullScreen(bool screen)
Definition: application.cpp:505
std::unique_ptr< CqspEventInstancer > m_event_instancer
Definition: application.h:280
std::map< std::string, Rml::ElementDocument * > loaded_documents
Definition: application.h:303
void UpdateScene()
Definition: application.cpp:117
int destroy()
Definition: application.cpp:188
void InitFonts()
Definition: application.cpp:443
bool RmlUiMouseProcessed()
Definition: application.h:215
double fps
Definition: application.h:286
void ResetGame()
Definition: application.h:242
std::vector< std::string > & GetCmdLineArgs()
Definition: application.h:173
bool MouseDragged()
Definition: application.h:161
std::string icon_path
Definition: application.h:272
void LoggerInit()
Definition: application.cpp:477
void(* EventListener)(Rml::Event &)
Definition: application.h:197
void SetScene(Args &&... args)
Definition: application.h:95
std::vector< std::string > cmd_line_args
Definition: application.h:269
void DrawText(const std::string &text, float x, float y)
Definition: application.cpp:372
void InitGame()
Definition: application.h:208
Rml::Context * rml_context
Definition: application.h:274
void CloseDocument(const std::string &path)
Definition: application.cpp:347
bool GlInit()
Definition: application.cpp:459
bool HasCmdLineArgs(const std::string &arg)
Definition: application.h:175
bool MouseButtonDoubleClicked(int btn)
Definition: application.h:114
bool Screenshot(const char *path=NULL)
Screenshots the current framebuffer to the filename
Definition: application.cpp:409
client::ClientOptions & GetClientOptions()
Definition: application.h:70
double lastFrame
Definition: application.h:288
std::string locale
Definition: application.h:290
Rml::Context * GetRmlUiContext()
Definition: application.h:186
bool MouseMoved()
Definition: application.h:162
double GetMouseX()
Definition: application.h:118
bool full_screen
Definition: application.h:270
bool RmlUiKeyboardProcessed()
Definition: application.h:214
bool DocumentIsLoaded(const std::string &path)
Definition: application.h:127
bool RmlUiTextProcessed()
Definition: application.h:216
double deltaTime
Definition: application.h:288
std::unique_ptr< Rml::RenderInterface > m_render_interface
Definition: application.h:277
double GetFps() const
Definition: application.h:77
void SetFont(asset::Font *font)
Definition: application.h:152
glm::mat4 Get2DProj()
Definition: application.h:169
Window * m_window
Definition: application.h:271
double GetDeltaTime() const
Definition: application.h:76
void ExitApplication()
Definition: application.cpp:332
double GetMouseY()
Definition: application.h:119
client::ClientOptions m_client_options
Definition: application.h:284
asset::AssetManager & GetAssetManager()
Definition: application.h:79
void InitAudio()
Definition: application.cpp:108
void DrawTextNormalized(const std::string &text, float x, float y)
Definition: application.cpp:400
audio::IAudioInterface * m_audio_interface
Definition: application.h:301
asset::Font * m_font
Definition: application.h:296
sol::state lua_state
Definition: application.h:312
int GetWindowHeight() const
Definition: application.h:72
bool ShouldExit()
Definition: application.cpp:330
glm::mat4 GetRmlUiProj()
Definition: application.h:171
bool MouseButtonIsReleased(int btn)
Definition: application.h:122
Definition: gamestate.h:20
Manages scenes for the application.
Definition: scenemanager.h:27
void SetScene(std::unique_ptr< Scene > scene)
Sets the next scene, and the scene will be switched when SwitchScene is executed.
Definition: scenemanager.cpp:31
The window handles the initialization of the callbacks, and all the input and output....
Definition: window.h:24
virtual bool RmlUiCapturedKeyboardInput() const =0
virtual bool RmlUiCapturedMouseInput() const =0
virtual int GetWindowHeight() const =0
virtual bool MouseButtonIsHeld(int btn) const =0
virtual bool ButtonIsPressed(int btn) const =0
virtual double MouseButtonLastReleased(int btn) const =0
virtual bool RmlUiCapturedTextInput() const =0
virtual bool MouseDragged() const =0
virtual int GetScrollAmount() const =0
virtual bool MouseButtonDoubleClicked(int btn) const =0
virtual double GetMouseX() const =0
virtual bool ButtonIsReleased(int btn) const =0
virtual bool ButtonIsHeld(int btn) const =0
virtual double GetMouseY() const =0
virtual bool MouseButtonIsPressed(int btn) const =0
virtual bool MouseButtonIsReleased(int btn) const =0
virtual int GetWindowWidth() const =0
virtual bool MouseMoved() const =0
Definition: iaudiointerface.h:27
EventListener(core::Universe &universe)
Definition: explorationwindow.h:1
Definition: application.cpp:56
int GetGlfwKey(KeyInput key)
Definition: userinput.cpp:445
KeyInput
Definition: userinput.h:24
std::string file
Definition: recipeviewer.cpp:41