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