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#define BS_THREAD_POOL_NATIVE_EXTENSIONS
9
10#include <format>
11
12#include <BS_thread_pool.hpp>
13#include <entt/signal/dispatcher.hpp>
14#include <glad/glad.h>
15#include <mimalloc.h>
16#include <SDL3/SDL.h>
17#include <sol/sol.hpp>
18
29#include "galaxy/lua/Lua.hpp"
33#include "galaxy/time/Time.hpp"
34
35#include "Application.hpp"
36
37using namespace std::chrono_literals;
38
39namespace galaxy
40{
41 App::App(const std::string& config_file)
42 {
43 SDL_SetMemoryFunctions(&mi_malloc, &mi_calloc, &mi_realloc, &mi_free);
44
47 setup_config(config_file);
49 setup_fs();
53 // setup_nuklear();
54 // setup_loader();
55 setup_meta();
58
59 // Load game assets.
60 // core::entt::locator<core::Loader>::ref().load_all();
61 }
62
64 {
65 entt::locator<VirtualFileSystem>::reset();
66 entt::locator<Config>::reset();
67
68 GALAXY_LOG(GALAXY_INFO, "Application closed.");
69 entt::locator<BS::priority_thread_pool>::value().wait();
70
71 entt::locator<Log>::reset();
72 entt::locator<BS::priority_thread_pool>::reset();
73
74 SDL_Quit();
75 }
76
77 /*void App::load()
78 {
79 const auto path = Settings::root_dir() / Settings::asset_pack();
80
81 auto& sm = entt::locator<scene::SceneManager>::value();
82 sm.load_app(path.string());
83 }*/
84
85 void App::run()
86 {
87 // https://stackoverflow.com/a/59446610
88 // We dont need 't' or 'alpha/render' sections.
89
90 auto& window = entt::locator<Window>::value();
91 auto& scenes = entt::locator<SceneManager>::value();
92
93 // The expression dt/1s simply converts the double-based chrono seconds
94 // into a double so it can participate in the physics computation.
95 constexpr const auto dt = std::chrono::duration<long long, std::ratio<1, 60>> {1};
96 time::dt(dt / 1.0s);
97
98 using clock = std::chrono::steady_clock;
99 using duration = decltype(clock::duration {} + dt);
100 using time_point = std::chrono::time_point<clock, duration>;
101
102 duration accum = 0s;
103 time_point prev = clock::now();
104 time_point now = clock::now();
105
106 auto updates = 0u;
107 auto frames = 0u;
108 duration perf = 0s;
109
110 if (!m_update)
111 {
112 m_update = [&](App* app) {
113 while (SDL_PollEvent(&m_events))
114 {
115 switch (m_events.type)
116 {
117 case SDL_EVENT_QUIT:
118 window.close();
119 break;
120
121 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
122 if (m_events.window.windowID == SDL_GetWindowID(window.handle()))
123 {
124 window.close();
125 }
126 break;
127
128 default:
129 scenes.on_event(m_events);
130 break;
131 }
132 }
133
134 scenes.update();
135 };
136 }
137
138 if (!m_render)
139 {
140 m_render = [&](App* app) {
141 scenes.render();
142 window.swap();
143 };
144 }
145
146 while (window.is_open())
147 {
148 now = clock::now();
149 auto elapsed = now - prev;
150
151 // 250ms is the limit put in place on the frame time to cope with the spiral of death.
152 // It doesn't have to be 250ms exactly but it should be sufficiently high enough to deal with spikes in load.
153 if (elapsed > 250ms)
154 {
155 elapsed = 250ms;
156 }
157
158 prev = now;
159 accum += elapsed;
160
161 while (accum >= dt)
162 {
163 perf += dt;
164 accum -= dt;
165
166 m_update(this);
167
168 updates++;
169 }
170
171 m_render(this);
172
173 frames++;
174
175 if (perf >= 1s)
176 {
177 window.append_title(std::format(" | UPS: {0}, FPS: {1}", updates, frames));
178
179 frames = 0;
180 updates = 0;
181 perf = 0s;
182 }
183 }
184 }
185
187 {
188 m_update = std::move(update);
189 }
190
192 {
193 m_render = std::move(render);
194 }
195
196 SDL_Event& App::events() noexcept
197 {
198 return m_events;
199 }
200
202 {
203 // Configure threadpool.
204
205 // Calc threads.
206 // We optimize for 6: 1 for audio, 1 for main, 4 for tasks.
207 auto system_cores = std::thread::hardware_concurrency();
208 if (system_cores < 6)
209 {
210 system_cores = std::thread::hardware_concurrency();
211 }
212
213 // Check for highest available priority.
214 BS::set_os_process_priority(BS::os_process_priority::high);
215 entt::locator<BS::priority_thread_pool>::emplace(system_cores, [](const std::size_t idx) {
216 BS::this_thread::set_os_thread_priority(BS::os_thread_priority::highest);
217 });
218 }
219
221 {
222 platform::configure_terminal();
223 if (!std::filesystem::exists(Settings::log_dir()))
224 {
225 std::filesystem::create_directory(Settings::log_dir());
226 }
227 entt::locator<Log>::emplace();
228
229 const auto path = std::format("{0}{1}{2}", Settings::log_dir(), std::format("{0:%d-%m-%Y-[%H-%M]}", time::now()), ".log");
231
233 GALAXY_LOG(GALAXY_INFO, "App started.");
234 }
235
236 void App::setup_config(std::string_view config_file)
237 {
238 auto& config = entt::locator<Config>::emplace(config_file);
241 }
242
244 {
246
247 platform::set_metadata(SDL_PROP_APP_METADATA_NAME_STRING, Settings::title().c_str());
248 platform::set_metadata(SDL_PROP_APP_METADATA_VERSION_STRING, Settings::version().c_str());
249 platform::set_metadata(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, Settings::identifier().c_str());
250 platform::set_metadata(SDL_PROP_APP_METADATA_CREATOR_STRING, Settings::creator().c_str());
251 platform::set_metadata(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, Settings::copyright().c_str());
252 platform::set_metadata(SDL_PROP_APP_METADATA_URL_STRING, Settings::website().c_str());
253 platform::set_metadata(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
254
255 platform::set_hint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "1");
256 platform::set_hint(SDL_HINT_ASSERT, "break");
257 platform::set_hint(SDL_HINT_AUDIO_CATEGORY, "playback");
258 platform::set_hint(SDL_HINT_AUDIO_CHANNELS, "2");
259 platform::set_hint(SDL_HINT_AUDIO_INCLUDE_MONITORS, "0");
260 platform::set_hint(SDL_HINT_AUTO_UPDATE_JOYSTICKS, "1");
261 platform::set_hint(SDL_HINT_AUTO_UPDATE_SENSORS, "1");
262 platform::set_hint(SDL_HINT_LOGGING, "warn");
263 platform::set_hint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0");
264 platform::set_hint(SDL_HINT_EVENT_LOGGING, "0");
265 platform::set_hint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "opengl");
266 platform::set_hint(SDL_HINT_GAMECONTROLLER_SENSOR_FUSION, "1");
267 platform::set_hint(SDL_HINT_GPU_DRIVER, "vulkan");
268 platform::set_hint(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, "1");
269 platform::set_hint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "0");
270 platform::set_hint(SDL_HINT_JOYSTICK_DIRECTINPUT, "1");
271 platform::set_hint(SDL_HINT_TV_REMOTE_AS_JOYSTICK, "1");
272 platform::set_hint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
273 platform::set_hint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
274 platform::set_hint(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER, "0");
275 platform::set_hint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
276 platform::set_hint(SDL_HINT_MOUSE_DPI_SCALE_CURSORS, "1");
277 platform::set_hint(SDL_HINT_PEN_MOUSE_EVENTS, "1");
278 platform::set_hint(SDL_HINT_PEN_TOUCH_EVENTS, "1");
279 platform::set_hint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "1");
280 platform::set_hint(SDL_HINT_RENDER_DRIVER, "opengl");
281 platform::set_hint(SDL_HINT_RENDER_GPU_DEBUG, "0");
282 platform::set_hint(SDL_HINT_RENDER_GPU_LOW_POWER, "0");
283 platform::set_hint(SDL_HINT_TOUCH_MOUSE_EVENTS, "1");
284 platform::set_hint(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY, "0");
285 platform::set_hint(SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL, "0");
286 platform::set_hint(SDL_HINT_XINPUT_ENABLED, "1");
287 platform::set_hint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "0");
288 platform::set_hint(SDL_HINT_VIDEO_DUMMY_SAVE_FRAMES, "0");
289 platform::set_hint(SDL_HINT_VIDEO_MATCH_EXCLUSIVE_MODE_ON_MOVE, "1");
290 platform::set_hint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
291 platform::set_hint(SDL_HINT_VIDEO_OFFSCREEN_SAVE_FRAMES, "0");
292 platform::set_hint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "0");
293 platform::set_hint(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, "1");
294 platform::set_hint(SDL_HINT_WINDOWS_CLOSE_ON_ALT_F4, "1");
295 platform::set_hint(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, "0");
296 platform::set_hint(SDL_HINT_WINDOWS_GAMEINPUT, "1");
297 platform::set_hint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "0");
298 // TODO: platform::set_hint(SDL_HINT_IME_IMPLEMENTED_UI, "0");
299
300 const auto vsync = Settings::vsync() ? "1" : "0";
301 platform::set_hint(SDL_HINT_RENDER_VSYNC, vsync);
302
303 const auto audio_freq = std::to_string(Settings::audio_freq());
304 platform::set_hint(SDL_HINT_AUDIO_FREQUENCY, audio_freq.c_str());
305
306 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_HAPTIC | SDL_INIT_SENSOR))
307 {
308 GALAXY_LOG(GALAXY_FATAL, SDL_GetError());
309 }
310 }
311
313 {
314 entt::locator<VirtualFileSystem>::emplace();
315 }
316
318 {
319 auto& window = entt::locator<Window>::emplace();
320
321 window.set_icon(Settings::window_icon());
322 window.show();
323
324 auto& sampler = entt::locator<Sampler>::emplace();
325
326 // Need to create our default texture sampler object.
327 if (Settings::mipmap())
328 {
329 switch (Settings::texture_filter())
330 {
332 sampler.set(GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
333 sampler.set(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
334 break;
336 sampler.set(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
337 sampler.set(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
338 break;
340 sampler.set(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
341 sampler.set(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
342 break;
343 }
344 }
345 else
346 {
348 {
349 sampler.set(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
350 sampler.set(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
351 }
352 else
353 {
354 sampler.set(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
355 sampler.set(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
356 }
357 }
358
359 auto ansio = std::clamp(Settings::ansiotrophy(), 1, 16);
360 if (ansio == 3)
361 {
362 ansio = 4;
363 }
364 else if (ansio > 4 && ansio < 8)
365 {
366 ansio = 8;
367 }
368 else
369 {
370 ansio = 16;
371 }
372 sampler.setf(GL_TEXTURE_MAX_ANISOTROPY, static_cast<float>(ansio));
373
374 sampler.set(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
375 sampler.set(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
376 sampler.set(GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
377 sampler.set(GL_TEXTURE_LOD_BIAS, GL_NONE);
378 }
379
381 {
382 SDL_zero(m_events);
383 }
384
386 {
387 auto& keyboard = entt::locator<Keyboard>::emplace();
388 auto& mouse = entt::locator<Mouse>::emplace();
389
390 mouse.set_cursor_custom(Settings::cursor_icon(), Settings::cursor_hotspot());
392 {
393 mouse.show_cursor();
394 }
395 else
396 {
397 mouse.hide_cursor();
398 }
399 }
400
401 // void App::setup_nuklear()
402 //{
403 // auto& nui = ServiceLocator<ui::NuklearUI>::make();
404 // }
405
406 // void App::setup_loader()
407 //{
408 // // entt::locator<Loader>::make();
409 // }
410
412 {
413 auto& sf = entt::locator<SystemFactory>::emplace();
414 // auto& ef = entt::locator<meta::EntityFactory>::emplace();
415
416 /*ef.register_component<components::Tag>("Tag");
417
418 em.register_component<components::Animated>("Animated");
419 em.register_component<components::Circle>("Circle");
420 em.register_component<components::Ellipse>("Ellipse");
421 em.register_component<components::Point>("Point");
422 em.register_component<components::Polygon>("Polygon");
423 em.register_component<components::Polyline>("Polyline");
424 em.register_component<components::RigidBody>("RigidBody");
425 em.register_component<components::Script>("Script");
426 em.register_component<components::Sprite>("Sprite");
427 em.register_component<components::Text>("Text");
428 em.register_component<components::TileMap>("TileMap");
429 em.register_component<components::Transform>("Transform");
430 em.register_component<flags::DenySerialization>("DenySerialization");
431 em.register_component<flags::Disabled>("Disabled");
432
433 em.register_dependencies<components::Animated, components::Sprite>();
434 em.register_dependencies<components::Circle, components::Transform>();
435 em.register_dependencies<components::Ellipse, components::Transform>();
436 em.register_dependencies<components::Point, components::Transform>();
437 em.register_dependencies<components::Polygon, components::Transform>();
438 em.register_dependencies<components::Polyline, components::Transform>();
439 em.register_dependencies<components::RigidBody, components::Transform>();
440 em.register_dependencies<components::Sprite, components::Transform>();
441 em.register_dependencies<components::Text, components::Transform>();
442 */
443 }
444
446 {
447 // entt::locator<media::SoundEngine>::make(listener_count);
448 // entt::locator<media::MusicEngine>::make(listener_count);
449 // entt::locator<media::VoiceEngine>::make(listener_count);
450 // entt::locator<resource::SoundCache>::make();
451 // entt::locator<resource::MusicCache>::make();
452 // entt::locator<resource::VoiceCache>::make();
453 // entt::locator<resource::VideoCache>::make();
454 // entt::locator<resource::Animations>::make();
455 // entt::locator<resource::Shaders>::make();
456 // entt::locator<resource::Fonts>::make();
457 // entt::locator<resource::Textures>::make();
458 // entt::locator<resource::Prefabs>::make();
459 // entt::locator<resource::Scripts>::make();
460 entt::locator<SceneManager>::emplace();
461 }
462
464 {
465 auto& lua = entt::locator<sol::state>::emplace();
466 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);
467
468 //
469 // Add external libraries to Lua.
470 // Inject all configured galaxy into Lua.
471 // Add engine services to lua.
472 //
473 Lua::inject();
474 }
475} // namespace galaxy
#define GALAXY_ADD_SINK(sink,...)
Definition Log.hpp:28
#define GALAXY_INFO
Log.hpp galaxy.
Definition Log.hpp:23
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_FATAL
Definition Log.hpp:26
Base level class for any galaxy app.
void setup_config(std::string_view config_file)
void run()
Loads the default appdata file.
SDL_Event & events() noexcept
Get event data.
App(const std::string &config_file="config.json")
Default constructor.
SDL_Event m_events
Core event data.
void setup_logging()
void set_render_func(LoopFunc &&render)
Use a custom rendering step in game loop.
void setup_async()
void setup_events()
void setup_input()
void setup_rendering()
void setup_meta()
std::move_only_function< void(App *app)> LoopFunc
Defines a callback for update() or render() loops in app.run().
void set_update_func(LoopFunc &&update)
Use a custom update step in game loop.
void setup_services()
void setup_scripting()
void setup_platform()
LoopFunc m_render
Render step in gameloop.
~App()
Destructor.
LoopFunc m_update
Update step in gameloop.
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
Application.hpp galaxy.
@ NEAREST
Nearest-neighbour.
static auto window_icon() noexcept -> const std::string &
Window icon file in vfs.
Definition Settings.cpp:139
static auto log_dir() noexcept -> const std::string &
Current root directory of application, unless it has been changed.
Definition Settings.cpp:244
static auto ansiotrophy() noexcept -> int
Ansiotropic filtering level.
Definition Settings.cpp:194
static auto copyright() noexcept -> const std::string &
Copyright message.
Definition Settings.cpp:234
static auto vsync() noexcept -> bool
Vsync control.
Definition Settings.cpp:154
static auto mipmap() noexcept -> bool
Mipmapping.
Definition Settings.cpp:199
static auto set_settings_from_config() -> void
Set all our settings using the provided config file.
Definition Settings.cpp:76
static auto identifier() noexcept -> const std::string &
Game identifier i.e. com.galaxy.app.
Definition Settings.cpp:224
static auto texture_filter() noexcept -> GLTextureFilter
Texture filtering type.
Definition Settings.cpp:204
static auto title() noexcept -> const std::string &
Game title.
Definition Settings.cpp:214
static auto cursor_show() noexcept -> bool
Is the mouse cursor visible or not.
Definition Settings.cpp:174
static auto cursor_hotspot() noexcept -> const glm::ivec2 &
Cursor selector point (hotspot).
Definition Settings.cpp:184
static auto version() noexcept -> const std::string &
Game semver.
Definition Settings.cpp:219
static auto audio_freq() noexcept -> int
Set audio frequency to use with SDL.
Definition Settings.cpp:189
static auto website() noexcept -> const std::string &
Website URL.
Definition Settings.cpp:239
static auto set_config_to_default() -> void
Restore all config settings to default.
Definition Settings.cpp:23
static auto cursor_icon() noexcept -> const std::string &
Cursor texture file in vfs.
Definition Settings.cpp:179
static auto creator() noexcept -> const std::string &
Owner.
Definition Settings.cpp:229