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/Core/Context.h>
20#include <RmlUi/Core/ElementDocument.h>
21#include <RmlUi/Core/EventListener.h>
22#include <RmlUi/Core/EventListenerInstancer.h>
23#include <RmlUi/Core/RenderInterface.h>
24#include <glad/glad.h>
25#include <spdlog/spdlog.h>
26
27#include <map>
28#include <memory>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include "common/game.h"
37#include "engine/engine.h"
38#include "engine/gamestate.h"
40#include "engine/gui.h"
41#include "engine/scene.h"
42#include "engine/scenemanager.h"
44#include "engine/userinput.h"
45#include "engine/window.h"
46
47namespace cqsp::engine {
49 public:
50 Application(int _argc, char* _argv[]);
51
52 /*
53 * Runs the entire application.
54 */
55 void run();
56
58
59 int GetWindowHeight() const { return m_window->GetWindowHeight(); }
60
61 int GetWindowWidth() const { return m_window->GetWindowWidth(); }
62
63 double GetDeltaTime() const { return deltaTime; }
64 double GetFps() const { return fps; }
65
67
68 bool ShouldExit();
69
70 void ExitApplication();
71
72 /*
73 * Sets the next scene which will be run in the next frame.
74 */
75 template <class T>
76 void SetScene() {
77 std::unique_ptr<Scene> ptr = std::make_unique<T>(*this);
78 m_scene_manager.SetScene(std::move(ptr));
79 }
80
82
84
85 bool ButtonIsHeld(int btn) { return m_window->ButtonIsHeld(btn); }
86 bool ButtonIsHeld(KeyInput btn) { return ButtonIsHeld(GetGlfwKey(btn)); }
87
88 bool ButtonIsReleased(int btn) { return m_window->ButtonIsReleased(btn); }
90
91 bool ButtonIsPressed(int btn) { return m_window->ButtonIsPressed(btn); }
93
96
98
99 double GetMouseX() { return m_window->GetMouseX(); }
100 double GetMouseY() { return m_window->GetMouseY(); }
101
102 bool MouseButtonIsHeld(int btn) { return m_window->MouseButtonIsHeld(btn); }
104 bool MouseButtonIsPressed(int btn) { return m_window->MouseButtonIsPressed(btn); }
105
106 Rml::ElementDocument* LoadDocument(const std::string& path);
107 void CloseDocument(const std::string& path);
108
120 Rml::ElementDocument* ReloadDocument(const std::string& path);
121
122 Window* GetWindow() { return m_window; }
123
125 void DrawText(const std::string& text, float x, float y);
126 void DrawText(const std::string& text, const glm::vec3& color, float x, float y);
127 void DrawText(const std::string& text, float x, float y, float size);
128 void DrawText(const std::string& text, const glm::vec3& color, float x, float y, float size);
129 // Draw text based on normalized device coordinates
130 void DrawTextNormalized(const std::string& text, float x, float y);
131
132 void SetFont(cqsp::asset::Font* font) { m_font = font; }
134
139 double GetTime();
140
141 bool MouseDragged() { return m_window->MouseDragged(); }
142
143 void SetWindowDimensions(int width, int height);
144 void SetFullScreen(bool screen);
145
146 glm::mat4 Get2DProj() { return two_dim_projection; }
147 glm::mat4 Get3DProj() { return three_dim_projection; }
148 glm::mat4 GetRmlUiProj() { return rmlui_projection; }
149
150 std::vector<std::string>& GetCmdLineArgs() { return cmd_line_args; }
151
152 bool HasCmdLineArgs(const std::string& arg) {
153 return (std::find(GetCmdLineArgs().begin(), GetCmdLineArgs().end(), arg) != GetCmdLineArgs().end());
154 }
161 bool Screenshot(const char* path = NULL);
162
163 Rml::Context* GetRmlUiContext() { return rml_context; }
164
165 class CqspEventInstancer : public Rml::EventListenerInstancer {
166 public:
169
171 Rml::EventListener* InstanceEventListener(const Rml::String& value, Rml::Element* element) override;
172 };
173
174 typedef void (*EventListener)(Rml::Event&);
175
177 public:
178 explicit CqspEventListener(const std::string& name) : name(name) {}
180 void ProcessEvent(Rml::Event& event);
181 std::string name;
182 };
183
184 template <class T>
185 void InitGame() {
186 m_game = std::make_unique<T>();
187 }
188
189 private:
190 void InitFonts();
191
192 void SetIcon();
193
194 bool GlInit();
195 void LoggerInit();
196 void LogInfo();
197
198 void InitRmlUi();
199 void InitImgui();
201 void InitAudio();
202
203 /*
204 * Intializes glfw and imgui.
205 */
206 int init();
207
208 void ResetGame() { m_game.reset(); }
209
210 /*
211 * Releases all data.
212 */
213 int destroy();
214
216
217 std::vector<std::string> cmd_line_args;
220 std::string icon_path;
221
222 Rml::Context* rml_context;
223 std::unique_ptr<SystemInterface_GLFW> m_system_interface;
224 std::unique_ptr<Rml::RenderInterface> m_render_interface;
225 ax::NodeEditor::EditorContext* m_ne_context = nullptr;
226
227 std::unique_ptr<CqspEventInstancer> m_event_instancer;
228
230
232
233 double fps;
234
236
237 std::string locale;
238
240
241 std::unique_ptr<cqsp::engine::GameState> m_game;
242
245
246 std::map<std::string, std::string> properties;
247
249
250 std::map<std::string, Rml::ElementDocument*> loaded_documents;
251
256
257 bool draw_fps = false;
258};
259} // namespace cqsp::engine
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:563
void ProcessEvent(Rml::Event &event)
Definition: application.cpp:571
CqspEventListener(const std::string &name)
Definition: application.h:178
std::string name
Definition: application.h:181
Definition: application.h:48
int init()
Definition: application.cpp:60
void SetWindowDimensions(int width, int height)
Definition: application.cpp:555
void InitRmlUi()
Definition: application.cpp:173
bool to_halt
Definition: application.h:255
Rml::ElementDocument * ReloadDocument(const std::string &path)
Reloads the document from the file How to hotload the a document.
Definition: application.cpp:406
bool ButtonIsPressed(KeyInput btn)
Definition: application.h:92
Window * GetWindow()
Definition: application.h:122
bool ButtonIsHeld(int btn)
Definition: application.h:85
double GetTime()
Time since glfw was initialized in seconds
Definition: application.cpp:459
int GetWindowWidth() const
Definition: application.h:61
void SetFontShader(cqsp::asset::ShaderProgram *shader)
Definition: application.h:133
Rml::ElementDocument * LoadDocument(const std::string &path)
Definition: application.cpp:383
bool draw_fps
Definition: application.h:257
cqsp::engine::SceneManager m_scene_manager
Definition: application.h:229
bool MouseButtonIsHeld(int btn)
Definition: application.h:102
bool ButtonIsHeld(KeyInput btn)
Definition: application.h:86
bool ButtonIsReleased(int btn)
Definition: application.h:88
glm::mat4 two_dim_projection
Definition: application.h:252
std::map< std::string, std::string > properties
Definition: application.h:246
void LogInfo()
Definition: application.cpp:537
void InitImgui()
Definition: application.cpp:85
void run()
Definition: application.cpp:288
void SetFont(cqsp::asset::Font *font)
Definition: application.h:132
bool MouseButtonIsPressed(int btn)
Definition: application.h:104
void SetScene()
Definition: application.h:76
void CalculateProjections()
Definition: application.cpp:259
double MouseButtonLastReleased(int btn)
Definition: application.h:94
Application(int _argc, char *_argv[])
Definition: application.cpp:270
glm::mat4 Get3DProj()
Definition: application.h:147
double GetScrollAmount()
Definition: application.h:97
bool ButtonIsPressed(int btn)
Definition: application.h:91
glm::mat4 three_dim_projection
Definition: application.h:253
ax::NodeEditor::EditorContext * m_ne_context
Definition: application.h:225
void SetIcon()
Definition: application.cpp:509
bool ButtonIsReleased(KeyInput btn)
Definition: application.h:89
glm::mat4 rmlui_projection
Definition: application.h:254
cqsp::engine::audio::IAudioInterface & GetAudioInterface()
Definition: application.h:83
cqsp::asset::AssetManager & GetAssetManager()
Definition: application.h:66
std::unique_ptr< SystemInterface_GLFW > m_system_interface
Definition: application.h:223
void SetFullScreen(bool screen)
Definition: application.cpp:557
cqsp::asset::Font * m_font
Definition: application.h:243
std::unique_ptr< CqspEventInstancer > m_event_instancer
Definition: application.h:227
cqsp::asset::ShaderProgram * fontShader
Definition: application.h:244
std::map< std::string, Rml::ElementDocument * > loaded_documents
Definition: application.h:250
int destroy()
Definition: application.cpp:214
void InitFonts()
Definition: application.cpp:495
double fps
Definition: application.h:233
void ResetGame()
Definition: application.h:208
std::vector< std::string > & GetCmdLineArgs()
Definition: application.h:150
bool MouseDragged()
Definition: application.h:141
std::string icon_path
Definition: application.h:220
void LoggerInit()
Definition: application.cpp:529
void(* EventListener)(Rml::Event &)
Definition: application.h:174
void ProcessRmlUiUserInput()
Definition: application.cpp:104
std::vector< std::string > cmd_line_args
Definition: application.h:217
void DrawText(const std::string &text, float x, float y)
Definition: application.cpp:424
void InitGame()
Definition: application.h:185
Rml::Context * rml_context
Definition: application.h:222
cqsp::asset::Font *& GetFont()
Definition: application.h:124
void CloseDocument(const std::string &path)
Definition: application.cpp:397
bool GlInit()
Definition: application.cpp:511
bool HasCmdLineArgs(const std::string &arg)
Definition: application.h:152
bool MouseButtonDoubleClicked(int btn)
Definition: application.h:95
bool Screenshot(const char *path=NULL)
Screenshots the current framebuffer to the filename
Definition: application.cpp:461
client::ClientOptions & GetClientOptions()
Definition: application.h:57
double lastFrame
Definition: application.h:235
std::string locale
Definition: application.h:237
Rml::Context * GetRmlUiContext()
Definition: application.h:163
double GetMouseX()
Definition: application.h:99
bool full_screen
Definition: application.h:218
double deltaTime
Definition: application.h:235
std::unique_ptr< cqsp::engine::GameState > m_game
Definition: application.h:241
std::unique_ptr< Rml::RenderInterface > m_render_interface
Definition: application.h:224
double GetFps() const
Definition: application.h:64
cqsp::engine::audio::IAudioInterface * m_audio_interface
Definition: application.h:248
glm::mat4 Get2DProj()
Definition: application.h:146
Window * m_window
Definition: application.h:219
double GetDeltaTime() const
Definition: application.h:63
void ExitApplication()
Definition: application.cpp:381
double GetMouseY()
Definition: application.h:100
client::ClientOptions m_client_options
Definition: application.h:231
void InitAudio()
Definition: application.cpp:164
cqsp::engine::GameState * GetGame()
Definition: application.h:81
void DrawTextNormalized(const std::string &text, float x, float y)
Definition: application.cpp:452
cqsp::asset::AssetManager manager
Definition: application.h:239
int GetWindowHeight() const
Definition: application.h:59
bool ShouldExit()
Definition: application.cpp:379
glm::mat4 GetRmlUiProj()
Definition: application.h:148
bool MouseButtonIsReleased(int btn)
Definition: application.h:103
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 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 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
Definition: iaudiointerface.h:27
Definition: application.cpp:55
int GetGlfwKey(KeyInput key)
Definition: userinput.cpp:444
KeyInput
Definition: userinput.h:24
EventListener(common::Universe *universe)
Definition: turnsavewindow.h:1