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 m_update = [&](entt::dispatcher& dispatcher, Window& window, World& world) {
56 // nui.begin_input();
57 window.process_events(dispatcher);
58 // nui.end_input();
59 world.update();
60 };
61
62 m_render = [&](entt::dispatcher& dispatcher, Window& window, World& world) {
63 // graphics::renderer::begin();
64 world.render();
65 // graphics::renderer::end();
66 };
67 }
68
70 {
71 entt::locator<VirtualFileSystem>::reset();
72 entt::locator<Config>::reset();
73 entt::locator<BS::light_thread_pool>::reset();
74
75 GALAXY_LOG(GALAXY_INFO, "Application closed.");
76 entt::locator<Log>::reset();
77
78 SDL_Quit();
79 }
80
81 /*void App::load()
82 {
83 const auto path = Settings::root_dir() / Settings::asset_pack();
84
85 auto& sm = entt::locator<scene::SceneManager>::value();
86 sm.load_app(path.string());
87 }*/
88
89 void App::run()
90 {
91 // https://stackoverflow.com/a/59446610
92 // We dont need 't' or 'alpha/render' sections.
93
94 auto& dispatcher = entt::locator<entt::dispatcher>::value();
95 auto& window = entt::locator<Window>::value();
96 auto& world = entt::locator<World>::value();
97
98 // The expression dt/1s simply converts the double-based chrono seconds
99 // into a double so it can participate in the physics computation.
100 constexpr const auto dt = std::chrono::duration<long long, std::ratio<1, 60>> {1};
101 time::dt(dt / 1.0s);
102
103 using clock = std::chrono::steady_clock;
104 using duration = decltype(clock::duration {} + dt);
105 using time_point = std::chrono::time_point<clock, duration>;
106
107 duration accum = 0s;
108 time_point prev = clock::now();
109 time_point now = clock::now();
110
111 auto updates = 0u;
112 auto frames = 0u;
113 duration perf = 0s;
114
115 while (window.is_open())
116 {
117 now = clock::now();
118 auto elapsed = now - prev;
119
120 // 250ms is the limit put in place on the frame time to cope with the spiral of death.
121 // It doesn't have to be 250ms exactly but it should be sufficiently high enough to deal with spikes in load.
122 if (elapsed > 250ms)
123 {
124 elapsed = 250ms;
125 }
126
127 prev = now;
128 accum += elapsed;
129
130 while (accum >= dt)
131 {
132 perf += dt;
133 accum -= dt;
134
135 m_update(dispatcher, window, world);
136 updates++;
137 }
138
139 m_render(dispatcher, window, world);
140 frames++;
141
142 if (perf >= 1s)
143 {
144 window.append_title(std::format(" | UPS: {0}, FPS: {1}", updates, frames));
145
146 frames = 0;
147 updates = 0;
148 perf = 0s;
149 }
150 }
151 }
152
154 {
155 m_update = std::move(update);
156 }
157
159 {
160 m_render = std::move(render);
161 }
162
164 {
165 platform::configure_terminal();
166 if (!std::filesystem::exists(Settings::log_dir()))
167 {
168 std::filesystem::create_directory(Settings::log_dir());
169 }
170 entt::locator<Log>::emplace();
171
172 const auto path = std::format("{0}{1}{2}", Settings::log_dir(), std::format("{0:%d-%m-%Y-[%H-%M]}", time::now()), ".log");
174
176 GALAXY_LOG(GALAXY_INFO, "App started.");
177 }
178
180 {
181 // Configure threadpool.
182 // Use half of available cores minus 2 for audio and main thread.
183 const auto system_cores = std::thread::hardware_concurrency();
184 if (system_cores < 4)
185 {
186 GALAXY_LOG(GALAXY_WARN, "Total cores are less than 4, this is not optimal.");
187 }
188
189 const auto cores = static_cast<int>(std::floor(system_cores / 2.0) - 2);
190 entt::locator<BS::light_thread_pool>::emplace(cores);
191 }
192
193 void App::setup_config(std::string_view config_file)
194 {
195 auto& config = entt::locator<Config>::emplace(config_file);
198 }
199
201 {
203
204 platform::set_metadata(SDL_PROP_APP_METADATA_NAME_STRING, Settings::title().c_str());
205 platform::set_metadata(SDL_PROP_APP_METADATA_VERSION_STRING, Settings::version().c_str());
206 platform::set_metadata(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, Settings::identifier().c_str());
207 platform::set_metadata(SDL_PROP_APP_METADATA_CREATOR_STRING, Settings::creator().c_str());
208 platform::set_metadata(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, Settings::copyright().c_str());
209 platform::set_metadata(SDL_PROP_APP_METADATA_URL_STRING, Settings::website().c_str());
210 platform::set_metadata(SDL_PROP_APP_METADATA_TYPE_STRING, "game");
211
212 platform::set_hint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "1");
213 platform::set_hint(SDL_HINT_ASSERT, "break");
214 platform::set_hint(SDL_HINT_AUDIO_CATEGORY, "playback");
215 platform::set_hint(SDL_HINT_AUDIO_CHANNELS, "2");
216 platform::set_hint(SDL_HINT_AUDIO_INCLUDE_MONITORS, "0");
217 platform::set_hint(SDL_HINT_AUTO_UPDATE_JOYSTICKS, "1");
218 platform::set_hint(SDL_HINT_AUTO_UPDATE_SENSORS, "1");
219 // platform::set_hint(SDL_HINT_DEBUG_LOGGING, "1");
220 platform::set_hint(SDL_HINT_ENABLE_SCREEN_KEYBOARD, "0");
221 platform::set_hint(SDL_HINT_EVENT_LOGGING, "0");
222 platform::set_hint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "opengl");
223 platform::set_hint(SDL_HINT_GAMECONTROLLER_SENSOR_FUSION, "1");
224 platform::set_hint(SDL_HINT_GPU_DRIVER, "vulkan");
225 platform::set_hint(SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS, "1");
226 platform::set_hint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "0");
227 platform::set_hint(SDL_HINT_JOYSTICK_DIRECTINPUT, "1");
228 platform::set_hint(SDL_HINT_TV_REMOTE_AS_JOYSTICK, "1");
229 // platform::set_hint(SDL_HINT_LOG_BACKENDS, "1");
230 platform::set_hint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
231 platform::set_hint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
232 platform::set_hint(SDL_HINT_MOUSE_RELATIVE_MODE_CENTER, "0");
233 platform::set_hint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
234 platform::set_hint(SDL_HINT_PEN_MOUSE_EVENTS, "1");
235 platform::set_hint(SDL_HINT_PEN_TOUCH_EVENTS, "1");
236 platform::set_hint(SDL_HINT_QUIT_ON_LAST_WINDOW_CLOSE, "1");
237 platform::set_hint(SDL_HINT_RENDER_DRIVER, "opengl");
238 platform::set_hint(SDL_HINT_RENDER_GPU_DEBUG, "0");
239 platform::set_hint(SDL_HINT_RENDER_GPU_LOW_POWER, "0");
240 platform::set_hint(SDL_HINT_TOUCH_MOUSE_EVENTS, "1");
241 platform::set_hint(SDL_HINT_TRACKPAD_IS_TOUCH_ONLY, "0");
242 platform::set_hint(SDL_HINT_THREAD_FORCE_REALTIME_TIME_CRITICAL, "0");
243 platform::set_hint(SDL_HINT_XINPUT_ENABLED, "1");
244 platform::set_hint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "0");
245 platform::set_hint(SDL_HINT_VIDEO_DUMMY_SAVE_FRAMES, "0");
246 platform::set_hint(SDL_HINT_VIDEO_MATCH_EXCLUSIVE_MODE_ON_MOVE, "1");
247 platform::set_hint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
248 platform::set_hint(SDL_HINT_VIDEO_OFFSCREEN_SAVE_FRAMES, "0");
249 platform::set_hint(SDL_HINT_VIDEO_SYNC_WINDOW_OPERATIONS, "0");
250 platform::set_hint(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, "1");
251 platform::set_hint(SDL_HINT_WINDOWS_CLOSE_ON_ALT_F4, "1");
252 platform::set_hint(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, "0");
253 platform::set_hint(SDL_HINT_WINDOWS_GAMEINPUT, "1");
254 platform::set_hint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "0");
255 // platform::set_hint(SDL_HINT_IME_IMPLEMENTED_UI, "0");
256
257 const auto vsync = Settings::vsync() ? "1" : "0";
258 platform::set_hint(SDL_HINT_RENDER_VSYNC, vsync);
259
260 const auto audio_freq = std::to_string(Settings::audio_freq());
261 platform::set_hint(SDL_HINT_AUDIO_FREQUENCY, audio_freq.c_str());
262
263 if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS | SDL_INIT_HAPTIC | SDL_INIT_SENSOR))
264 {
265 GALAXY_LOG(GALAXY_FATAL, SDL_GetError());
266 }
267 }
268
270 {
271 entt::locator<VirtualFileSystem>::emplace();
272 }
273
275 {
276 auto& window = entt::locator<Window>::emplace();
277
278 window.set_icon(Settings::window_icon());
279 window.show();
280 }
281
283 {
284 entt::locator<entt::dispatcher>::emplace();
285 }
286
287 // void App::setup_nuklear()
288 //{
289 // auto& nui = ServiceLocator<ui::NuklearUI>::make();
290 // }
291
292 // void App::setup_loader()
293 //{
294 // // entt::locator<Loader>::make();
295 // }
296
298 {
299 auto& sf = entt::locator<SystemFactory>::emplace();
300 // auto& ef = entt::locator<meta::EntityFactory>::emplace();
301
302 /*ef.register_component<components::Tag>("Tag");
303
304 em.register_component<components::Animated>("Animated");
305 em.register_component<components::Circle>("Circle");
306 em.register_component<components::Ellipse>("Ellipse");
307 em.register_component<components::Point>("Point");
308 em.register_component<components::Polygon>("Polygon");
309 em.register_component<components::Polyline>("Polyline");
310 em.register_component<components::RigidBody>("RigidBody");
311 em.register_component<components::Script>("Script");
312 em.register_component<components::Sprite>("Sprite");
313 em.register_component<components::Text>("Text");
314 em.register_component<components::TileMap>("TileMap");
315 em.register_component<components::Transform>("Transform");
316 em.register_component<flags::DenySerialization>("DenySerialization");
317 em.register_component<flags::Disabled>("Disabled");
318
319 em.register_dependencies<components::Animated, components::Sprite>();
320 em.register_dependencies<components::Circle, components::Transform>();
321 em.register_dependencies<components::Ellipse, components::Transform>();
322 em.register_dependencies<components::Point, components::Transform>();
323 em.register_dependencies<components::Polygon, components::Transform>();
324 em.register_dependencies<components::Polyline, components::Transform>();
325 em.register_dependencies<components::RigidBody, components::Transform>();
326 em.register_dependencies<components::Sprite, components::Transform>();
327 em.register_dependencies<components::Text, components::Transform>();
328 */
329 }
330
332 {
333 // entt::locator<media::SoundEngine>::make(listener_count);
334 // entt::locator<media::MusicEngine>::make(listener_count);
335 // entt::locator<media::VoiceEngine>::make(listener_count);
336 // entt::locator<resource::SoundCache>::make();
337 // entt::locator<resource::MusicCache>::make();
338 // entt::locator<resource::VoiceCache>::make();
339 // entt::locator<resource::VideoCache>::make();
340 // entt::locator<resource::Animations>::make();
341 // entt::locator<resource::Shaders>::make();
342 // entt::locator<resource::Fonts>::make();
343 // entt::locator<resource::Textures>::make();
344 // entt::locator<resource::Prefabs>::make();
345 // entt::locator<resource::Scripts>::make();
346 entt::locator<World>::emplace();
347 }
348
350 {
351 auto& lua = entt::locator<sol::state>::emplace();
352 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);
353
354 //
355 // Add external libraries to Lua.
356 // Inject all configured galaxy into Lua.
357 // Add engine services to lua.
358 //
359 Lua::inject();
360 }
361} // 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
std::move_only_function< void(entt::dispatcher &, Window &, World &)> LoopFunc
Defines a callback for update() or render() loops in app.run().
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 set_render_func(LoopFunc &&render)
Use a custom rendering step in game loop.
void setup_async()
void setup_events()
void setup_meta()
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.
void setup_window()
~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
RAII Window. Handles events, input & display.
Definition Window.hpp:33
Scene, Entity and global game management.
Definition World.hpp:20
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: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 copyright() noexcept -> const std::string &
Copyright message.
Definition Settings.cpp:234
static auto vsync() noexcept -> bool
Vsync control.
Definition Settings.cpp:154
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 title() noexcept -> const std::string &
Game title.
Definition Settings.cpp:214
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 creator() noexcept -> const std::string &
Owner.
Definition Settings.cpp:229