galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Application.cpp
Go to the documentation of this file.
1
7
8#include <format>
9
10#include <BS_thread_pool.hpp>
11#include <entt/signal/dispatcher.hpp>
12#include <mimalloc.h>
13#include <SDL3/SDL.h>
14#include <sol/sol.hpp>
15
23#include "galaxy/lua/Lua.hpp"
27#include "galaxy/time/Time.hpp"
28
29#include "Application.hpp"
30
31using namespace std::chrono_literals;
32
33namespace galaxy
34{
35 App::App(const std::string& config_file)
36 {
37 SDL_SetMemoryFunctions(&mi_malloc, &mi_calloc, &mi_realloc, &mi_free);
38
41 setup_config(config_file);
43 setup_fs();
46 // setup_nuklear();
47 // setup_loader();
48 setup_meta();
51
52 // Load game assets.
53 // core::entt::locator<core::Loader>::ref().load_all();
54 }
55
57 {
58 entt::locator<VirtualFileSystem>::reset();
59 entt::locator<Config>::reset();
60 entt::locator<BS::light_thread_pool>::reset();
61
62 GALAXY_LOG(GALAXY_INFO, "Application closed.");
63 entt::locator<Log>::reset();
64
65 SDL_Quit();
66 }
67
68 /*void App::load()
69 {
70 const auto path = Settings::root_dir() / Settings::asset_pack();
71
72 auto& sm = entt::locator<scene::SceneManager>::value();
73 sm.load_app(path.string());
74 }*/
75
76 void App::run()
77 {
78 // https://stackoverflow.com/a/59446610
79 // We dont need 't' or 'alpha/render' sections.
80
81 auto& window = entt::locator<Window>::value();
82 auto& dispatcher = entt::locator<entt::dispatcher>::value();
83 auto& world = entt::locator<World>::value();
84
85 // The expression dt/1s simply converts the double-based chrono seconds
86 // into a double so it can participate in the physics computation.
87 constexpr const auto dt = std::chrono::duration<long long, std::ratio<1, 60>> {1};
88 time::dt(dt / 1.0s);
89
90 using clock = std::chrono::steady_clock;
91 using duration = decltype(clock::duration {} + dt);
92 using time_point = std::chrono::time_point<clock, duration>;
93
94 duration accum = 0s;
95 time_point prev = clock::now();
96 time_point now = clock::now();
97
98 auto updates = 0u;
99 auto frames = 0u;
100 duration perf = 0s;
101
102 while (window.is_open())
103 {
104 now = clock::now();
105 auto elapsed = now - prev;
106
107 // 250ms is the limit put in place on the frame time to cope with the spiral of death.
108 // It doesn't have to be 250ms exactly but it should be sufficiently high enough to deal with spikes in load.
109 if (elapsed > 250ms)
110 {
111 elapsed = 250ms;
112 }
113
114 prev = now;
115 accum += elapsed;
116
117 while (accum >= dt)
118 {
119 perf += dt;
120 accum -= dt;
121
122 // nui.begin_input();
123 window.process_events(dispatcher);
124 // nui.end_input();
125 world.update();
126
127 updates++;
128 }
129
130 // graphics::renderer::begin();
131 world.render();
132 // graphics::renderer::end();
133
134 frames++;
135
136 if (perf >= 1s)
137 {
138 window.append_title(std::format(" | UPS: {0}, FPS: {1}", updates, frames));
139
140 frames = 0;
141 updates = 0;
142 perf = 0s;
143 }
144 }
145 }
146
148 {
149 platform::configure_terminal();
150 if (!std::filesystem::exists(Settings::log_dir()))
151 {
152 std::filesystem::create_directory(Settings::log_dir());
153 }
154 entt::locator<Log>::emplace();
155
156 const auto path = std::format("{0}{1}{2}", Settings::log_dir(), std::format("{0:%d-%m-%Y-[%H-%M]}", time::now()), ".log");
158
160 GALAXY_LOG(GALAXY_INFO, "App started.");
161 }
162
164 {
165 // Configure threadpool.
166 // Use half of available cores minus 2 for audio and main thread.
167 const auto system_cores = std::thread::hardware_concurrency();
168 if (system_cores < 4)
169 {
170 GALAXY_LOG(GALAXY_WARN, "Total cores are less than 4, this is not optimal.");
171 }
172
173 const auto cores = static_cast<int>(std::floor(system_cores / 2.0) - 2);
174 entt::locator<BS::light_thread_pool>::emplace(cores);
175 }
176
177 void App::setup_config(std::string_view config_file)
178 {
179 auto& config = entt::locator<Config>::emplace(config_file);
182 }
183
185 {
187
188 platform::set_metadata(SDL_PROP_APP_METADATA_NAME_STRING, Settings::title().c_str());
189 platform::set_metadata(SDL_PROP_APP_METADATA_VERSION_STRING, Settings::version().c_str());
190 platform::set_metadata(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, Settings::identifier().c_str());
191 platform::set_metadata(SDL_PROP_APP_METADATA_CREATOR_STRING, Settings::creator().c_str());
192 platform::set_metadata(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, Settings::copyright().c_str());
193 platform::set_metadata(SDL_PROP_APP_METADATA_URL_STRING, Settings::website().c_str());
194 platform::set_metadata(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
195
196 platform::set_hint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "1");
197 platform::set_hint(SDL_HINT_ASSERT, "break");
198 platform::set_hint(SDL_HINT_AUDIO_CATEGORY, "playback");
199 platform::set_hint(SDL_HINT_AUDIO_CHANNELS, "2");
200 platform::set_hint(SDL_HINT_AUDIO_INCLUDE_MONITORS, "0");
201 platform::set_hint(SDL_HINT_AUTO_UPDATE_JOYSTICKS, "1");
202 platform::set_hint(SDL_HINT_AUTO_UPDATE_SENSORS, "1");
203 platform::set_hint(SDL_HINT_DEBUG_LOGGING, "1");
204 platform::set_hint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0");
205 platform::set_hint(SDL_HINT_EVENT_LOGGING, "0");
206 platform::set_hint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "opengl");
207 platform::set_hint(SDL_HINT_GAMECONTROLLER_SENSOR_FUSION, "1");
208 platform::set_hint(SDL_HINT_GPU_DRIVER, "vulkan");
209 platform::set_hint(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, "1");
210 platform::set_hint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "0");
211 platform::set_hint(SDL_HINT_JOYSTICK_DIRECTINPUT, "1");
212 platform::set_hint(SDL_HINT_TV_REMOTE_AS_JOYSTICK, "1");
213 // platform::set_hint(SDL_HINT_LOG_BACKENDS, "1");
214 platform::set_hint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
215 platform::set_hint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
216 platform::set_hint(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER, "0");
217 platform::set_hint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
218 platform::set_hint(SDL_HINT_PEN_MOUSE_EVENTS, "1");
219 platform::set_hint(SDL_HINT_PEN_TOUCH_EVENTS, "1");
220 platform::set_hint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "1");
221 platform::set_hint(SDL_HINT_RENDER_DRIVER, "opengl");
222 platform::set_hint(SDL_HINT_RENDER_GPU_DEBUG, "0");
223 platform::set_hint(SDL_HINT_RENDER_GPU_LOW_POWER, "0");
224 platform::set_hint(SDL_HINT_TOUCH_MOUSE_EVENTS, "1");
225 platform::set_hint(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY, "0");
226 platform::set_hint(SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL, "0");
227 platform::set_hint(SDL_HINT_XINPUT_ENABLED, "1");
228 platform::set_hint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "0");
229 platform::set_hint(SDL_HINT_VIDEO_DUMMY_SAVE_FRAMES, "0");
230 platform::set_hint(SDL_HINT_VIDEO_MATCH_EXCLUSIVE_MODE_ON_MOVE, "1");
231 platform::set_hint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
232 platform::set_hint(SDL_HINT_VIDEO_OFFSCREEN_SAVE_FRAMES, "0");
233 platform::set_hint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "0");
234 platform::set_hint(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, "1");
235 platform::set_hint(SDL_HINT_WINDOWS_CLOSE_ON_ALT_F4, "1");
236 platform::set_hint(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, "0");
237 platform::set_hint(SDL_HINT_WINDOWS_GAMEINPUT, "1");
238 platform::set_hint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "0");
239 // platform::set_hint(SDL_HINT_IME_IMPLEMENTED_UI, "0");
240
241 const auto vsync = Settings::vsync() ? "1" : "0";
242 platform::set_hint(SDL_HINT_RENDER_VSYNC, vsync);
243
244 const auto audio_freq = std::to_string(Settings::audio_freq());
245 platform::set_hint(SDL_HINT_AUDIO_FREQUENCY, audio_freq.c_str());
246
247 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_HAPTIC | SDL_INIT_SENSOR))
248 {
249 GALAXY_LOG(GALAXY_FATAL, SDL_GetError());
250 }
251 }
252
254 {
255 entt::locator<VirtualFileSystem>::emplace();
256 }
257
259 {
260 auto& window = entt::locator<Window>::emplace();
261
262 window.set_icon(Settings::window_icon());
263 window.show();
264 }
265
267 {
268 entt::locator<entt::dispatcher>::emplace();
269 }
270
271 // void App::setup_nuklear()
272 //{
273 // }
274
275 // void App::setup_loader()
276 //{
277 // // entt::locator<Loader>::make();
278 // }
279
281 {
282 auto& sf = entt::locator<SystemFactory>::emplace();
283 // auto& ef = entt::locator<meta::EntityFactory>::emplace();
284
285 /*ef.register_component<components::Tag>("Tag");
286
287 em.register_component<components::Animated>("Animated");
288 em.register_component<components::Circle>("Circle");
289 em.register_component<components::Ellipse>("Ellipse");
290 em.register_component<components::Point>("Point");
291 em.register_component<components::Polygon>("Polygon");
292 em.register_component<components::Polyline>("Polyline");
293 em.register_component<components::RigidBody>("RigidBody");
294 em.register_component<components::Script>("Script");
295 em.register_component<components::Sprite>("Sprite");
296 em.register_component<components::Text>("Text");
297 em.register_component<components::TileMap>("TileMap");
298 em.register_component<components::Transform>("Transform");
299 em.register_component<flags::DenySerialization>("DenySerialization");
300 em.register_component<flags::Disabled>("Disabled");
301
302 em.register_dependencies<components::Animated, components::Sprite>();
303 em.register_dependencies<components::Circle, components::Transform>();
304 em.register_dependencies<components::Ellipse, components::Transform>();
305 em.register_dependencies<components::Point, components::Transform>();
306 em.register_dependencies<components::Polygon, components::Transform>();
307 em.register_dependencies<components::Polyline, components::Transform>();
308 em.register_dependencies<components::RigidBody, components::Transform>();
309 em.register_dependencies<components::Sprite, components::Transform>();
310 em.register_dependencies<components::Text, components::Transform>();
311 */
312 }
313
315 {
316 // entt::locator<media::SoundEngine>::make(listener_count);
317 // entt::locator<media::MusicEngine>::make(listener_count);
318 // entt::locator<media::VoiceEngine>::make(listener_count);
319 // entt::locator<resource::SoundCache>::make();
320 // entt::locator<resource::MusicCache>::make();
321 // entt::locator<resource::VoiceCache>::make();
322 // entt::locator<resource::VideoCache>::make();
323 // entt::locator<resource::Animations>::make();
324 // entt::locator<resource::Shaders>::make();
325 // entt::locator<resource::Fonts>::make();
326 // entt::locator<resource::Textures>::make();
327 // entt::locator<resource::Prefabs>::make();
328 // entt::locator<resource::Scripts>::make();
329 entt::locator<World>::emplace();
330 }
331
333 {
334 auto& lua = entt::locator<sol::state>::emplace();
335 lua.open_libraries(sol::lib::base, sol::lib::package, sol::lib::coroutine, sol::lib::string, sol::lib::os, sol::lib::math, sol::lib::table, sol::lib::io, sol::lib::utf8);
336
337 //
338 // Add external libraries to Lua.
339 // Inject all configured galaxy into Lua.
340 // Add engine services to lua.
341 //
342 Lua::inject();
343 }
344} // namespace galaxy
#define GALAXY_ADD_SINK(sink,...)
Definition Log.hpp:27
#define GALAXY_INFO
Log.hpp galaxy.
Definition Log.hpp:22
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_WARN
Definition Log.hpp:23
#define GALAXY_FATAL
Definition Log.hpp:25
void setup_config(std::string_view config_file)
void run()
Loads the default appdata file.
App(const std::string &config_file="config.json")
Default constructor.
void setup_logging()
void setup_async()
void setup_events()
void setup_meta()
void setup_services()
void setup_scripting()
void setup_platform()
void setup_window()
~App()
Destructor.
Logs a message to the console.
static void inject() noexcept
Inject everything into Lua.
Definition Lua.cpp:12
void seed_random() noexcept
Seed the cstdlib rng algos.
Definition Platform.cpp:20
void set_metadata(const char *type, const char *value) noexcept
Sets metadata.
Definition Platform.cpp:25
void set_hint(const char *hint, const char *value) noexcept
Sets SDL hints.
Definition Platform.cpp:30
double dt() noexcept
Get galaxy delta time.
Definition Time.cpp:26
auto now() noexcept -> std::chrono::local_time< std::chrono::system_clock::duration >
Current local time.
Definition Time.cpp:16
Animated.cpp galaxy.
Definition Animated.cpp:16
static auto window_icon() noexcept -> const std::string &
Window icon file in vfs.
Definition Settings.cpp:137
static auto log_dir() noexcept -> const std::string &
Current root directory of application, unless it has been changed.
Definition Settings.cpp:237
static auto copyright() noexcept -> const std::string &
Copyright message.
Definition Settings.cpp:227
static auto vsync() noexcept -> bool
Vsync control.
Definition Settings.cpp:152
static auto set_settings_from_config() -> void
Set all our settings using the provided config file.
Definition Settings.cpp:75
static auto identifier() noexcept -> const std::string &
Game identifier i.e. com.galaxy.app.
Definition Settings.cpp:217
static auto title() noexcept -> const std::string &
Game title.
Definition Settings.cpp:207
static auto version() noexcept -> const std::string &
Game semver.
Definition Settings.cpp:212
static auto audio_freq() noexcept -> int
Set audio frequency to use with SDL.
Definition Settings.cpp:187
static auto website() noexcept -> const std::string &
Website URL.
Definition Settings.cpp:232
static auto set_config_to_default() -> void
Restore all config settings to default.
Definition Settings.cpp:23
static auto creator() noexcept -> const std::string &
Owner.
Definition Settings.cpp:222