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