💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1354 lines
43 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/game.hpp"
  20. #include "game/commands/commands.hpp"
  21. #include "game/control-profile.hpp"
  22. #include "game/controls.hpp"
  23. #include "game/fonts.hpp"
  24. #include "game/graphics.hpp"
  25. #include "game/menu.hpp"
  26. #include "game/settings.hpp"
  27. #include "game/states/main-menu-state.hpp"
  28. #include "game/states/splash-state.hpp"
  29. #include "game/strings.hpp"
  30. #include "game/systems/astronomy-system.hpp"
  31. #include "game/systems/atmosphere-system.hpp"
  32. #include "game/systems/behavior-system.hpp"
  33. #include "game/systems/blackbody-system.hpp"
  34. #include "game/systems/camera-system.hpp"
  35. #include "game/systems/collision-system.hpp"
  36. #include "game/systems/constraint-system.hpp"
  37. #include "game/systems/locomotion-system.hpp"
  38. #include "game/systems/ik-system.hpp"
  39. #include "game/systems/animation-system.hpp"
  40. #include "game/systems/orbit-system.hpp"
  41. #include "game/systems/render-system.hpp"
  42. #include "game/systems/spatial-system.hpp"
  43. #include "game/systems/steering-system.hpp"
  44. #include "game/systems/physics-system.hpp"
  45. #include "game/systems/subterrain-system.hpp"
  46. #include "game/systems/terrain-system.hpp"
  47. #include "game/systems/reproductive-system.hpp"
  48. #include "game/systems/metabolic-system.hpp"
  49. #include "game/systems/metamorphosis-system.hpp"
  50. #include <algorithm>
  51. #include <cctype>
  52. #include <engine/animation/animation.hpp>
  53. #include <engine/animation/animator.hpp>
  54. #include <engine/animation/ease.hpp>
  55. #include <engine/animation/screen-transition.hpp>
  56. #include <engine/animation/timeline.hpp>
  57. #include <engine/color/color.hpp>
  58. #include <engine/config.hpp>
  59. #include <engine/debug/cli.hpp>
  60. #include <engine/debug/log.hpp>
  61. #include <engine/gl/framebuffer.hpp>
  62. #include <engine/gl/pixel-format.hpp>
  63. #include <engine/gl/pixel-type.hpp>
  64. #include <engine/gl/rasterizer.hpp>
  65. #include <engine/gl/texture-2d.hpp>
  66. #include <engine/gl/texture-filter.hpp>
  67. #include <engine/gl/texture-wrapping.hpp>
  68. #include <engine/gl/vertex-array.hpp>
  69. #include <engine/gl/vertex-attribute.hpp>
  70. #include <engine/gl/vertex-buffer.hpp>
  71. #include <engine/input/application-events.hpp>
  72. #include <engine/input/gamepad.hpp>
  73. #include <engine/input/keyboard.hpp>
  74. #include <engine/input/mapper.hpp>
  75. #include <engine/input/mouse.hpp>
  76. #include <engine/input/scancode.hpp>
  77. #include <engine/render/compositor.hpp>
  78. #include <engine/render/material-flags.hpp>
  79. #include <engine/render/material-variable.hpp>
  80. #include <engine/render/passes/bloom-pass.hpp>
  81. #include <engine/render/passes/clear-pass.hpp>
  82. #include <engine/render/passes/final-pass.hpp>
  83. #include <engine/render/passes/fxaa-pass.hpp>
  84. #include <engine/render/passes/ground-pass.hpp>
  85. #include <engine/render/passes/material-pass.hpp>
  86. #include <engine/render/passes/outline-pass.hpp>
  87. #include <engine/render/passes/resample-pass.hpp>
  88. #include <engine/render/passes/shadow-map-pass.hpp>
  89. #include <engine/render/passes/sky-pass.hpp>
  90. #include <engine/render/renderer.hpp>
  91. #include <engine/render/vertex-attribute.hpp>
  92. #include <engine/resources/resource-manager.hpp>
  93. #include <engine/scene/scene.hpp>
  94. #include <engine/utility/dict.hpp>
  95. #include <engine/utility/hash/fnv1a.hpp>
  96. #include <engine/utility/paths.hpp>
  97. #include <entt/entt.hpp>
  98. #include <execution>
  99. #include <filesystem>
  100. #include <functional>
  101. #include <string>
  102. #include <vector>
  103. #include <chrono>
  104. // Prevent cxxopts from using RTTI
  105. #define CXXOPTS_NO_RTTI
  106. #include <cxxopts.hpp>
  107. using namespace hash::literals;
  108. game::game(int argc, const char* const* argv)
  109. {
  110. // Boot process
  111. debug::log::trace("Booting up...");
  112. // Profile boot duration
  113. #if !defined(NDEBUG)
  114. auto boot_t0 = std::chrono::high_resolution_clock::now();
  115. #endif
  116. parse_options(argc, argv);
  117. setup_resources();
  118. load_settings();
  119. setup_window();
  120. setup_audio();
  121. setup_input();
  122. load_strings();
  123. setup_rendering();
  124. setup_scenes();
  125. setup_animation();
  126. setup_ui();
  127. setup_rng();
  128. setup_entities();
  129. setup_systems();
  130. setup_controls();
  131. setup_debugging();
  132. setup_timing();
  133. active_ecoregion = nullptr;
  134. closed = false;
  135. // Profile boot duration
  136. #if !defined(NDEBUG)
  137. auto boot_t1 = std::chrono::high_resolution_clock::now();
  138. #endif
  139. debug::log::trace("Boot up complete");
  140. // Print boot duration
  141. #if !defined(NDEBUG)
  142. debug::log::info("Boot duration: {}", std::chrono::duration_cast<std::chrono::duration<double>>(boot_t1 - boot_t0));
  143. #endif
  144. }
  145. game::~game()
  146. {
  147. debug::log::trace("Booting down...");
  148. // Exit all active game states
  149. while (!state_machine.empty())
  150. {
  151. state_machine.pop();
  152. }
  153. // Update window settings
  154. const auto& windowed_position = window->get_windowed_position();
  155. const auto& windowed_size = window->get_windowed_size();
  156. const bool maximized = window->is_maximized();
  157. const bool fullscreen = window->is_fullscreen();
  158. (*settings)["window_x"] = windowed_position.x();
  159. (*settings)["window_y"] = windowed_position.y();
  160. (*settings)["window_w"] = windowed_size.x();
  161. (*settings)["window_h"] = windowed_size.y();
  162. (*settings)["maximized"] = maximized;
  163. (*settings)["fullscreen"] = fullscreen;
  164. // Destruct window
  165. window.reset();
  166. // Save settings
  167. resource_manager->set_write_path(shared_config_path);
  168. resource_manager->save(*settings, "settings.cfg");
  169. // Destruct input and window managers
  170. input_manager.reset();
  171. window_manager.reset();
  172. // Shut down audio
  173. shutdown_audio();
  174. debug::log::trace("Boot down complete");
  175. }
  176. void game::parse_options(int argc, const char* const* argv)
  177. {
  178. if (argc <= 1)
  179. {
  180. // No command-line options specified
  181. return;
  182. }
  183. debug::log::trace("Parsing command-line options...");
  184. // Parse command-line options with cxxopts
  185. try
  186. {
  187. cxxopts::Options options(config::application_name, config::application_name);
  188. options.add_options()
  189. ("c,continue", "Continues from the last save")
  190. ("d,data", "Sets the data package path", cxxopts::value<std::string>())
  191. ("f,fullscreen", "Starts in fullscreen mode")
  192. ("n,new-game", "Starts a new game")
  193. ("q,quick-start", "Skips to the main menu")
  194. ("r,reset", "Resets all settings to default")
  195. ("v,v-sync", "Enables or disables v-sync", cxxopts::value<int>())
  196. ("w,windowed", "Starts in windowed mode");
  197. auto result = options.parse(argc, argv);
  198. // --continue
  199. if (result.count("continue"))
  200. {
  201. option_continue = true;
  202. }
  203. // --data
  204. if (result.count("data"))
  205. {
  206. option_data = result["data"].as<std::string>();
  207. }
  208. // --fullscreen
  209. if (result.count("fullscreen"))
  210. {
  211. option_fullscreen = true;
  212. }
  213. // --new-game
  214. if (result.count("new-game"))
  215. {
  216. option_new_game = true;
  217. }
  218. // --quick-start
  219. if (result.count("quick-start"))
  220. {
  221. option_quick_start = true;
  222. }
  223. // --reset
  224. if (result.count("reset"))
  225. {
  226. option_reset = true;
  227. }
  228. // --v-sync
  229. if (result.count("v-sync"))
  230. {
  231. option_v_sync = result["v-sync"].as<int>();
  232. }
  233. // --windowed
  234. if (result.count("windowed"))
  235. {
  236. option_windowed = true;
  237. }
  238. debug::log::info("Parsed {} command-line options", argc);
  239. }
  240. catch (const std::exception& e)
  241. {
  242. debug::log::error("An error occurred while parsing command-line options: {}", e.what());
  243. }
  244. }
  245. void game::setup_resources()
  246. {
  247. // Allocate resource manager
  248. resource_manager = std::make_unique<::resource_manager>();
  249. // Get executable data path
  250. const auto data_path = get_executable_data_path();
  251. // Determine data package path
  252. if (option_data)
  253. {
  254. // Handle command-line data path option
  255. data_package_path = option_data.value();
  256. if (data_package_path.is_relative())
  257. {
  258. data_package_path = data_path / data_package_path;
  259. }
  260. }
  261. else
  262. {
  263. data_package_path = data_path / (config::application_slug + std::string("-data.zip"));
  264. }
  265. // Determine mods path
  266. mods_path = data_path / "mods";
  267. // Determine config paths
  268. local_config_path = get_local_config_path() / config::application_name;
  269. shared_config_path = get_shared_config_path() / config::application_name;
  270. saves_path = shared_config_path / "saves";
  271. screenshots_path = shared_config_path / "gallery";
  272. controls_path = shared_config_path / "controls";
  273. // Log paths
  274. debug::log::info("Data package path: \"{}\"", data_package_path.string());
  275. debug::log::info("Local config path: \"{}\"", local_config_path.string());
  276. debug::log::info("Shared config path: \"{}\"", shared_config_path.string());
  277. debug::log::info("Mods path: \"{}\"", mods_path.string());
  278. // Create nonexistent config directories
  279. std::vector<std::filesystem::path> config_paths;
  280. config_paths.push_back(local_config_path);
  281. config_paths.push_back(shared_config_path);
  282. config_paths.push_back(saves_path);
  283. config_paths.push_back(screenshots_path);
  284. config_paths.push_back(controls_path);
  285. for (const auto& path: config_paths)
  286. {
  287. try
  288. {
  289. if (std::filesystem::create_directories(path))
  290. {
  291. debug::log::info("Created directory \"{}\"", path.string());
  292. }
  293. }
  294. catch (const std::filesystem::filesystem_error& e)
  295. {
  296. debug::log::error("Failed to create directory \"{}\": {}", path.string(), e.what());
  297. }
  298. }
  299. // Scan for mods
  300. std::vector<std::filesystem::path> mod_paths;
  301. if (std::filesystem::is_directory(mods_path))
  302. {
  303. for (const auto& entry: std::filesystem::directory_iterator{mods_path})
  304. {
  305. if (entry.is_directory() || (entry.is_regular_file() && entry.path().extension() == ".zip"))
  306. {
  307. mod_paths.push_back(entry.path());
  308. debug::log::info("Found mod \"{}\"", entry.path().filename().string());
  309. }
  310. }
  311. }
  312. // Mount mod paths
  313. for (const std::filesystem::path& mod_path: mod_paths)
  314. {
  315. resource_manager->mount(mods_path / mod_path);
  316. }
  317. // Mount config path
  318. resource_manager->mount(local_config_path);
  319. resource_manager->mount(shared_config_path);
  320. // Mount data package path
  321. resource_manager->mount(data_package_path);
  322. // Mount controls path
  323. resource_manager->mount(shared_config_path / "controls");
  324. }
  325. void game::load_settings()
  326. {
  327. if (option_reset)
  328. {
  329. // Command-line reset option found, reset settings
  330. settings = std::make_shared<dict<hash::fnv1a32_t>>();
  331. resource_manager->set_write_path(shared_config_path);
  332. resource_manager->save(*settings, "settings.cfg");
  333. debug::log::info("Settings reset");
  334. }
  335. else
  336. {
  337. settings = resource_manager->load<dict<hash::fnv1a32_t>>("settings.cfg");
  338. if (!settings)
  339. {
  340. debug::log::info("Settings not found");
  341. settings = std::make_shared<dict<hash::fnv1a32_t>>();
  342. }
  343. }
  344. }
  345. void game::setup_window()
  346. {
  347. // Construct window manager
  348. window_manager = app::window_manager::instance();
  349. // Default window settings
  350. std::string window_title = config::application_name;
  351. int window_x = -1;
  352. int window_y = -1;
  353. int window_w = -1;
  354. int window_h = -1;
  355. bool maximized = true;
  356. bool fullscreen = true;
  357. bool v_sync = true;
  358. // Read window settings
  359. bool resize = false;
  360. read_or_write_setting(*this, "window_title", window_title);
  361. read_or_write_setting(*this, "window_x", window_x);
  362. read_or_write_setting(*this, "window_y", window_y);
  363. if (!read_or_write_setting(*this, "window_w", window_w) ||
  364. !read_or_write_setting(*this, "window_h", window_h))
  365. {
  366. resize = true;
  367. }
  368. read_or_write_setting(*this, "maximized", maximized);
  369. read_or_write_setting(*this, "fullscreen", fullscreen);
  370. read_or_write_setting(*this, "v_sync", v_sync);
  371. // If window size not set, resize and reposition relative to default display
  372. if (resize)
  373. {
  374. const app::display& display = window_manager->get_display(0);
  375. const auto& usable_bounds = display.get_usable_bounds();
  376. const auto usable_bounds_center = usable_bounds.center();
  377. const float default_windowed_scale = 1.0f / 1.2f;
  378. window_w = static_cast<int>((usable_bounds.max.x() - usable_bounds.min.x()) * default_windowed_scale);
  379. window_h = static_cast<int>((usable_bounds.max.y() - usable_bounds.min.y()) * default_windowed_scale);
  380. window_x = usable_bounds_center.x() - window_w / 2;
  381. window_y = usable_bounds_center.y() - window_h / 2;
  382. }
  383. // Handle window-related command-line options
  384. if (option_windowed)
  385. {
  386. // Start in windowed mode
  387. maximized = false;
  388. fullscreen = false;
  389. }
  390. if (option_fullscreen)
  391. {
  392. // Start in fullscreen mode
  393. fullscreen = true;
  394. }
  395. if (option_v_sync)
  396. {
  397. v_sync = option_v_sync.value();
  398. }
  399. // Construct window
  400. window = window_manager->create_window
  401. (
  402. window_title,
  403. {window_x, window_y},
  404. {window_w, window_h},
  405. maximized,
  406. fullscreen,
  407. v_sync
  408. );
  409. // Restrict window size
  410. window->set_minimum_size({160, 144});
  411. // Setup window closed callback
  412. window_closed_subscription = window->get_closed_channel().subscribe
  413. (
  414. [&](const auto& event)
  415. {
  416. closed = true;
  417. }
  418. );
  419. }
  420. void game::setup_audio()
  421. {
  422. debug::log::trace("Setting up audio...");
  423. // Default audio settings
  424. master_volume = 1.0f;
  425. ambience_volume = 1.0f;
  426. effects_volume = 1.0f;
  427. mono_audio = false;
  428. captions = false;
  429. captions_size = 1.0f;
  430. // Read audio settings
  431. read_or_write_setting(*this, "master_volume", master_volume);
  432. read_or_write_setting(*this, "ambience_volume", ambience_volume);
  433. read_or_write_setting(*this, "effects_volume", effects_volume);
  434. read_or_write_setting(*this, "mono_audio", mono_audio);
  435. read_or_write_setting(*this, "captions", captions);
  436. read_or_write_setting(*this, "captions_size", captions_size);
  437. // Open audio device
  438. debug::log::trace("Opening audio device...");
  439. alc_device = alcOpenDevice(nullptr);
  440. if (!alc_device)
  441. {
  442. debug::log::error("Failed to open audio device: AL error code {}", alGetError());
  443. return;
  444. }
  445. else
  446. {
  447. // Get audio device name
  448. const ALCchar* alc_device_name = nullptr;
  449. if (alcIsExtensionPresent(alc_device, "ALC_ENUMERATE_ALL_EXT"))
  450. {
  451. alc_device_name = alcGetString(alc_device, ALC_ALL_DEVICES_SPECIFIER);
  452. }
  453. if (alcGetError(alc_device) != AL_NO_ERROR || !alc_device_name)
  454. {
  455. alc_device_name = alcGetString(alc_device, ALC_DEVICE_SPECIFIER);
  456. }
  457. // Log audio device name
  458. debug::log::info("Opened audio device \"{}\"", alc_device_name);
  459. }
  460. // Create audio context
  461. debug::log::trace("Creating audio context...");
  462. alc_context = alcCreateContext(alc_device, nullptr);
  463. if (!alc_context)
  464. {
  465. debug::log::error("Failed to create audio context: ALC error code {}", alcGetError(alc_device));
  466. alcCloseDevice(alc_device);
  467. return;
  468. }
  469. else
  470. {
  471. debug::log::trace("Created audio context");
  472. }
  473. // Make audio context current
  474. debug::log::trace("Making audio context current...");
  475. if (alcMakeContextCurrent(alc_context) == ALC_FALSE)
  476. {
  477. debug::log::error("Failed to make audio context current: ALC error code {}", alcGetError(alc_device));
  478. if (alc_context != nullptr)
  479. {
  480. alcDestroyContext(alc_context);
  481. }
  482. alcCloseDevice(alc_device);
  483. return;
  484. }
  485. else
  486. {
  487. debug::log::trace("Made audio context current");
  488. }
  489. debug::log::trace("Set up audio");
  490. }
  491. void game::setup_input()
  492. {
  493. // Construct input manager
  494. input_manager = app::input_manager::instance();
  495. // Process initial input events, such as connecting gamepads
  496. input_manager->update();
  497. // Setup application quit callback
  498. application_quit_subscription = input_manager->get_event_dispatcher().subscribe<input::application_quit_event>
  499. (
  500. [&](const auto& event)
  501. {
  502. closed = true;
  503. }
  504. );
  505. /*
  506. // Gamepad deactivation function
  507. auto deactivate_gamepad = [&](const auto& event)
  508. {
  509. if (gamepad_active)
  510. {
  511. gamepad_active = false;
  512. // WARNING: huge source of lag
  513. input_manager->set_cursor_visible(true);
  514. }
  515. };
  516. // Setup gamepad activation callbacks
  517. gamepad_axis_moved_subscription = input_manager->get_event_dispatcher().subscribe<input::gamepad_axis_moved_event>
  518. (
  519. [&](const auto& event)
  520. {
  521. if (!gamepad_active && std::abs(event.position) > 0.5f)
  522. {
  523. gamepad_active = true;
  524. input_manager->set_cursor_visible(false);
  525. }
  526. }
  527. );
  528. gamepad_button_pressed_subscription = input_manager->get_event_dispatcher().subscribe<input::gamepad_button_pressed_event>
  529. (
  530. [&](const auto& event)
  531. {
  532. if (!gamepad_active)
  533. {
  534. gamepad_active = true;
  535. input_manager->set_cursor_visible(false);
  536. }
  537. }
  538. );
  539. // Setup gamepad deactivation callbacks
  540. mouse_button_pressed_subscription = input_manager->get_event_dispatcher().subscribe<input::mouse_button_pressed_event>
  541. (
  542. deactivate_gamepad
  543. );
  544. mouse_moved_subscription = input_manager->get_event_dispatcher().subscribe<input::mouse_moved_event>
  545. (
  546. deactivate_gamepad
  547. );
  548. mouse_scrolled_subscription = input_manager->get_event_dispatcher().subscribe<input::mouse_scrolled_event>
  549. (
  550. deactivate_gamepad
  551. );
  552. // Activate gamepad if one is connected
  553. if (!input_manager->get_gamepads().empty())
  554. {
  555. gamepad_active = true;
  556. input_manager->set_cursor_visible(false);
  557. }
  558. else
  559. {
  560. gamepad_active = false;
  561. }
  562. */
  563. }
  564. void game::load_strings()
  565. {
  566. debug::log::trace("Loading strings...");
  567. // Default strings settings
  568. language_tag = "en";
  569. // Read strings settings
  570. read_or_write_setting(*this, "language_tag", language_tag);
  571. // Slugify language tag
  572. std::string language_slug = language_tag;
  573. std::transform
  574. (
  575. language_slug.begin(),
  576. language_slug.end(),
  577. language_slug.begin(),
  578. [](unsigned char c)
  579. {
  580. return std::tolower(c);
  581. }
  582. );
  583. // Load string map
  584. string_map = resource_manager->load<i18n::string_map>(language_slug + ".str");
  585. // Log language info
  586. debug::log::info("Language tag: {}", language_tag);
  587. // Change window title
  588. const std::string window_title = get_string(*this, "window_title");
  589. window->set_title(window_title);
  590. // Update window title setting
  591. (*settings)["window_title"] = window_title;
  592. debug::log::trace("Loaded strings");
  593. }
  594. void game::setup_rendering()
  595. {
  596. debug::log::trace("Setting up rendering...");
  597. // Default rendering settings
  598. render_scale = 1.0f;
  599. anti_aliasing_method = render::anti_aliasing_method::none;
  600. shadow_map_resolution = 4096;
  601. // Read rendering settings
  602. read_or_write_setting(*this, "render_scale", render_scale);
  603. read_or_write_setting(*this, "anti_aliasing_method", *reinterpret_cast<std::underlying_type_t<render::anti_aliasing_method>*>(&anti_aliasing_method));
  604. read_or_write_setting(*this, "shadow_map_resolution", shadow_map_resolution);
  605. // Create framebuffers
  606. ::graphics::create_framebuffers(*this);
  607. // Load fallback material
  608. auto fallback_material = resource_manager->load<render::material>("fallback.mtl");
  609. // Setup common render passes
  610. {
  611. // Construct bloom pass
  612. bloom_pass = std::make_unique<render::bloom_pass>(window->get_rasterizer(), resource_manager.get());
  613. bloom_pass->set_source_texture(hdr_color_texture.get());
  614. bloom_pass->set_mip_chain_length(5);
  615. //bloom_pass->set_mip_chain_length(0);
  616. bloom_pass->set_filter_radius(0.005f);
  617. common_final_pass = std::make_unique<render::final_pass>(window->get_rasterizer(), ldr_framebuffer_a.get(), resource_manager.get());
  618. common_final_pass->set_color_texture(hdr_color_texture.get());
  619. common_final_pass->set_bloom_texture(bloom_pass->get_bloom_texture());
  620. common_final_pass->set_bloom_weight(0.04f);
  621. //common_final_pass->set_bloom_weight(0.0f);
  622. common_final_pass->set_blue_noise_texture(resource_manager->load<gl::texture_2d>("blue-noise.tex"));
  623. fxaa_pass = std::make_unique<render::fxaa_pass>(window->get_rasterizer(), &window->get_rasterizer()->get_default_framebuffer(), resource_manager.get());
  624. fxaa_pass->set_source_texture(ldr_color_texture_a.get());
  625. resample_pass = std::make_unique<render::resample_pass>(window->get_rasterizer(), &window->get_rasterizer()->get_default_framebuffer(), resource_manager.get());
  626. resample_pass->set_source_texture(ldr_color_texture_b.get());
  627. resample_pass->set_enabled(false);
  628. // Configure anti-aliasing according to settings
  629. graphics::select_anti_aliasing_method(*this, anti_aliasing_method);
  630. // Configure render scaling according to settings
  631. graphics::change_render_resolution(*this, render_scale);
  632. }
  633. // Setup UI compositor
  634. {
  635. ui_clear_pass = std::make_unique<render::clear_pass>(window->get_rasterizer(), &window->get_rasterizer()->get_default_framebuffer());
  636. ui_clear_pass->set_cleared_buffers(false, true, false);
  637. ui_clear_pass->set_clear_depth(-1.0f);
  638. ui_material_pass = std::make_unique<render::material_pass>(window->get_rasterizer(), &window->get_rasterizer()->get_default_framebuffer(), resource_manager.get());
  639. ui_material_pass->set_fallback_material(fallback_material);
  640. ui_compositor = std::make_unique<render::compositor>();
  641. ui_compositor->add_pass(ui_clear_pass.get());
  642. ui_compositor->add_pass(ui_material_pass.get());
  643. }
  644. // Setup surface compositor
  645. {
  646. surface_shadow_map_clear_pass = std::make_unique<render::clear_pass>(window->get_rasterizer(), shadow_map_framebuffer.get());
  647. surface_shadow_map_clear_pass->set_cleared_buffers(false, true, false);
  648. surface_shadow_map_clear_pass->set_clear_depth(1.0f);
  649. surface_shadow_map_pass = std::make_unique<render::shadow_map_pass>(window->get_rasterizer(), resource_manager.get());
  650. surface_clear_pass = std::make_unique<render::clear_pass>(window->get_rasterizer(), hdr_framebuffer.get());
  651. surface_clear_pass->set_clear_color({0.0f, 0.0f, 0.0f, 1.0f});
  652. surface_clear_pass->set_clear_depth(-1.0f);
  653. surface_clear_pass->set_clear_stencil(0);
  654. surface_clear_pass->set_cleared_buffers(true, true, true);
  655. sky_pass = std::make_unique<render::sky_pass>(window->get_rasterizer(), hdr_framebuffer.get(), resource_manager.get());
  656. // sky_pass->set_magnification(3.0f);
  657. ground_pass = std::make_unique<render::ground_pass>(window->get_rasterizer(), hdr_framebuffer.get(), resource_manager.get());
  658. surface_material_pass = std::make_unique<render::material_pass>(window->get_rasterizer(), hdr_framebuffer.get(), resource_manager.get());
  659. surface_material_pass->set_fallback_material(fallback_material);
  660. surface_outline_pass = std::make_unique<render::outline_pass>(window->get_rasterizer(), hdr_framebuffer.get(), resource_manager.get());
  661. surface_outline_pass->set_outline_width(0.25f);
  662. surface_outline_pass->set_outline_color(math::fvec4{1.0f, 1.0f, 1.0f, 1.0f});
  663. surface_compositor = std::make_unique<render::compositor>();
  664. surface_compositor->add_pass(surface_shadow_map_clear_pass.get());
  665. surface_compositor->add_pass(surface_shadow_map_pass.get());
  666. surface_compositor->add_pass(surface_clear_pass.get());
  667. surface_compositor->add_pass(sky_pass.get());
  668. //surface_compositor->add_pass(ground_pass.get());
  669. surface_compositor->add_pass(surface_material_pass.get());
  670. //surface_compositor->add_pass(surface_outline_pass.get());
  671. surface_compositor->add_pass(bloom_pass.get());
  672. surface_compositor->add_pass(common_final_pass.get());
  673. surface_compositor->add_pass(fxaa_pass.get());
  674. surface_compositor->add_pass(resample_pass.get());
  675. }
  676. // Setup underground compositor
  677. {
  678. underground_clear_pass = std::make_unique<render::clear_pass>(window->get_rasterizer(), hdr_framebuffer.get());
  679. underground_clear_pass->set_cleared_buffers(true, true, false);
  680. underground_clear_pass->set_clear_color({0, 0, 0, 1});
  681. underground_clear_pass->set_clear_depth(-1.0f);
  682. underground_material_pass = std::make_unique<render::material_pass>(window->get_rasterizer(), hdr_framebuffer.get(), resource_manager.get());
  683. underground_material_pass->set_fallback_material(fallback_material);
  684. underground_compositor = std::make_unique<render::compositor>();
  685. underground_compositor->add_pass(underground_clear_pass.get());
  686. underground_compositor->add_pass(underground_material_pass.get());
  687. underground_compositor->add_pass(bloom_pass.get());
  688. underground_compositor->add_pass(common_final_pass.get());
  689. underground_compositor->add_pass(fxaa_pass.get());
  690. underground_compositor->add_pass(resample_pass.get());
  691. }
  692. // Create renderer
  693. renderer = std::make_unique<render::renderer>(*window->get_rasterizer(), *resource_manager);
  694. debug::log::trace("Set up rendering");
  695. }
  696. void game::setup_scenes()
  697. {
  698. debug::log::trace("Setting up scenes...");
  699. // Ratio of meters to scene units.
  700. constexpr float scene_scale = 1.0f / 100.0f;
  701. // Get default framebuffer
  702. const auto& viewport_size = window->get_viewport_size();
  703. const float viewport_aspect_ratio = static_cast<float>(viewport_size[0]) / static_cast<float>(viewport_size[1]);
  704. // Allocate and init surface scene
  705. surface_scene = std::make_unique<scene::collection>();
  706. surface_scene->set_scale(scene_scale);
  707. // Allocate and init surface camera
  708. surface_camera = std::make_shared<scene::camera>();
  709. surface_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 5000.0f);
  710. surface_camera->set_compositor(surface_compositor.get());
  711. surface_camera->set_composite_index(0);
  712. // Allocate and init underground scene
  713. underground_scene = std::make_unique<scene::collection>();
  714. underground_scene->set_scale(scene_scale);
  715. // Allocate and init underground camera
  716. underground_camera = std::make_shared<scene::camera>();
  717. underground_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 200.0f);
  718. underground_camera->set_compositor(underground_compositor.get());
  719. underground_camera->set_composite_index(0);
  720. // Clear active scene
  721. active_scene = nullptr;
  722. debug::log::trace("Set up scenes");
  723. }
  724. void game::setup_animation()
  725. {
  726. // Setup timeline system
  727. timeline = std::make_unique<::timeline>();
  728. timeline->set_autoremove(true);
  729. // Setup animator
  730. animator = std::make_unique<::animator>();
  731. }
  732. void game::setup_ui()
  733. {
  734. // Default UI settings
  735. font_scale = 1.0f;
  736. debug_font_size_pt = 10.0f;
  737. menu_font_size_pt = 22.0f;
  738. title_font_size_pt = 80.0f;
  739. dyslexia_font = false;
  740. // Read UI settings
  741. read_or_write_setting(*this, "font_scale", font_scale);
  742. read_or_write_setting(*this, "debug_font_size_pt", debug_font_size_pt);
  743. read_or_write_setting(*this, "menu_font_size_pt", menu_font_size_pt);
  744. read_or_write_setting(*this, "title_font_size_pt", title_font_size_pt);
  745. read_or_write_setting(*this, "dyslexia_font", dyslexia_font);
  746. // Allocate font materials
  747. debug_font_material = std::make_shared<render::material>();
  748. menu_font_material = std::make_shared<render::material>();
  749. title_font_material = std::make_shared<render::material>();
  750. // Load fonts
  751. debug::log::trace("Loading fonts...");
  752. try
  753. {
  754. ::load_fonts(*this);
  755. debug::log::trace("Loaded fonts");
  756. }
  757. catch (...)
  758. {
  759. debug::log::error("Failed to load fonts");
  760. }
  761. // Get default framebuffer
  762. const auto& viewport_size = window->get_viewport_size();
  763. const float viewport_aspect_ratio = static_cast<float>(viewport_size[0]) / static_cast<float>(viewport_size[1]);
  764. // Setup UI scene
  765. ui_scene = std::make_unique<scene::collection>();
  766. // Setup UI camera
  767. ui_camera = std::make_unique<scene::camera>();
  768. ui_camera->set_compositor(ui_compositor.get());
  769. const float clip_left = 0.0f;
  770. const float clip_right = static_cast<float>(viewport_size.x());
  771. const float clip_top = 0.0f;
  772. const float clip_bottom = static_cast<float>(viewport_size.y());
  773. const float clip_near = -100.0f;
  774. const float clip_far = 100.0f;
  775. ui_camera->set_orthographic(clip_left, clip_right, clip_top, clip_bottom, clip_near, clip_far);
  776. ui_camera->look_at({0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f});
  777. // Menu BG material
  778. menu_bg_material = std::make_shared<render::material>();
  779. menu_bg_material->set_shader_template(resource_manager->load<gl::shader_template>("ui-element-untextured.glsl"));
  780. std::shared_ptr<render::matvar_fvec4> menu_bg_tint = std::make_shared<render::matvar_fvec4>(1, math::fvec4{0.0f, 0.0f, 0.0f, 0.5f});
  781. menu_bg_material->set_variable("tint", menu_bg_tint);
  782. menu_bg_material->set_blend_mode(render::material_blend_mode::translucent);
  783. // Menu BG billboard
  784. menu_bg_billboard = std::make_unique<scene::billboard>();
  785. menu_bg_billboard->set_material(menu_bg_material);
  786. menu_bg_billboard->set_scale({std::ceil(viewport_size.x() * 0.5f), std::ceil(viewport_size.y() * 0.5f), 1.0f});
  787. menu_bg_billboard->set_translation({std::floor(viewport_size.x() * 0.5f), std::floor(viewport_size.y() * 0.5f), -100.0f});
  788. // Create fade transition
  789. fade_transition = std::make_unique<screen_transition>();
  790. fade_transition->get_material()->set_shader_template(resource_manager->load<gl::shader_template>("fade-transition.glsl"));
  791. fade_transition_color = std::make_shared<render::matvar_fvec3>(1, math::fvec3{0, 0, 0});
  792. fade_transition->get_material()->set_variable("color", fade_transition_color);
  793. fade_transition->get_billboard()->set_translation({0, 0, 98});
  794. // Create inner radial transition
  795. radial_transition_inner = std::make_unique<screen_transition>();
  796. radial_transition_inner->get_material()->set_shader_template(resource_manager->load<gl::shader_template>("radial-transition-inner.glsl"));
  797. // Create outer radial transition
  798. radial_transition_outer = std::make_unique<screen_transition>();
  799. radial_transition_outer->get_material()->set_shader_template(resource_manager->load<gl::shader_template>("radial-transition-outer.glsl"));
  800. // Menu BG animations
  801. {
  802. auto menu_bg_frame_callback = [menu_bg_tint](int channel, const float& opacity)
  803. {
  804. menu_bg_tint->set(math::fvec4{0.0f, 0.0f, 0.0f, opacity});
  805. };
  806. // Menu BG fade in animation
  807. menu_bg_fade_in_animation = std::make_unique<animation<float>>();
  808. {
  809. menu_bg_fade_in_animation->set_interpolator(ease<float>::out_cubic);
  810. animation_channel<float>* channel = menu_bg_fade_in_animation->add_channel(0);
  811. channel->insert_keyframe({0.0f, 0.0f});
  812. channel->insert_keyframe({config::menu_fade_in_duration, config::menu_bg_opacity});
  813. menu_bg_fade_in_animation->set_frame_callback(menu_bg_frame_callback);
  814. menu_bg_fade_in_animation->set_start_callback
  815. (
  816. [&, menu_bg_tint]()
  817. {
  818. ui_scene->add_object(*menu_bg_billboard);
  819. menu_bg_tint->set(math::fvec4{0.0f, 0.0f, 0.0f, 0.0f});
  820. //menu_bg_billboard->set_active(true);
  821. }
  822. );
  823. }
  824. // Menu BG fade out animation
  825. menu_bg_fade_out_animation = std::make_unique<animation<float>>();
  826. {
  827. menu_bg_fade_out_animation->set_interpolator(ease<float>::out_cubic);
  828. animation_channel<float>* channel = menu_bg_fade_out_animation->add_channel(0);
  829. channel->insert_keyframe({0.0f, config::menu_bg_opacity});
  830. channel->insert_keyframe({config::menu_fade_out_duration, 0.0f});
  831. menu_bg_fade_out_animation->set_frame_callback(menu_bg_frame_callback);
  832. menu_bg_fade_out_animation->set_end_callback
  833. (
  834. [&]()
  835. {
  836. ui_scene->remove_object(*menu_bg_billboard);
  837. //menu_bg_billboard->set_active(false);
  838. }
  839. );
  840. }
  841. }
  842. // Add UI scene objects to UI scene
  843. ui_scene->add_object(*ui_camera);
  844. ui_scene->add_object(*fade_transition->get_billboard());
  845. // Add UI animations to animator
  846. animator->add_animation(fade_transition->get_animation());
  847. animator->add_animation(menu_bg_fade_in_animation.get());
  848. animator->add_animation(menu_bg_fade_out_animation.get());
  849. // Setup window resized callback
  850. window_resized_subscription = window->get_resized_channel().subscribe
  851. (
  852. [&](const auto& event)
  853. {
  854. const auto& viewport_size = event.window->get_viewport_size();
  855. const float viewport_aspect_ratio = static_cast<float>(viewport_size.x()) / static_cast<float>(viewport_size.y());
  856. // Resize framebuffers
  857. ::graphics::change_render_resolution(*this, render_scale);
  858. // Update camera projection matrix
  859. surface_camera->set_perspective
  860. (
  861. surface_camera->get_vertical_fov(),
  862. viewport_aspect_ratio,
  863. surface_camera->get_clip_near(),
  864. surface_camera->get_clip_far()
  865. );
  866. // Update UI camera projection matrix
  867. ui_camera->set_orthographic
  868. (
  869. 0.0f,
  870. static_cast<float>(viewport_size.x()),
  871. 0.0f,
  872. static_cast<float>(viewport_size.y()),
  873. ui_camera->get_clip_near(),
  874. ui_camera->get_clip_far()
  875. );
  876. // Resize menu BG billboard
  877. menu_bg_billboard->set_scale({std::ceil(viewport_size.x() * 0.5f), std::ceil(viewport_size.y() * 0.5f), 1.0f});
  878. menu_bg_billboard->set_translation({std::floor(viewport_size.x() * 0.5f), std::floor(viewport_size.y() * 0.5f), -100.0f});
  879. // Re-align debug text
  880. frame_time_text->set_translation({std::round(0.0f), std::round(viewport_size.y() - debug_font.get_font_metrics().size), 99.0f});
  881. // Re-align menu text
  882. ::menu::align_text(*this);
  883. }
  884. );
  885. }
  886. void game::setup_rng()
  887. {
  888. std::random_device rd;
  889. rng.seed(rd());
  890. }
  891. void game::setup_entities()
  892. {
  893. // Create entity registry
  894. entity_registry = std::make_unique<entt::registry>();
  895. }
  896. void game::setup_systems()
  897. {
  898. const auto& viewport_size = window->get_viewport_size();
  899. math::fvec4 viewport = {0.0f, 0.0f, static_cast<float>(viewport_size[0]), static_cast<float>(viewport_size[1])};
  900. // Setup terrain system
  901. terrain_system = std::make_unique<::terrain_system>(*entity_registry);
  902. // Setup camera system
  903. camera_system = std::make_unique<::camera_system>(*entity_registry);
  904. camera_system->set_viewport(viewport);
  905. // Setup subterrain system
  906. subterrain_system = std::make_unique<::subterrain_system>(*entity_registry, resource_manager.get());
  907. // Setup collision system
  908. collision_system = std::make_unique<::collision_system>(*entity_registry);
  909. // Setup behavior system
  910. behavior_system = std::make_unique<::behavior_system>(*entity_registry);
  911. // Setup steering system
  912. steering_system = std::make_unique<::steering_system>(*entity_registry);
  913. // Setup locomotion system
  914. locomotion_system = std::make_unique<::locomotion_system>(*entity_registry);
  915. // Setup IK system
  916. ik_system = std::make_unique<::ik_system>(*entity_registry);
  917. // Setup metabolic system
  918. metabolic_system = std::make_unique<::metabolic_system>(*entity_registry);
  919. // Setup metamorphosis system
  920. metamorphosis_system = std::make_unique<::metamorphosis_system>(*entity_registry);
  921. // Setup animation system
  922. animation_system = std::make_unique<::animation_system>(*entity_registry);
  923. // Setup physics system
  924. physics_system = std::make_unique<::physics_system>(*entity_registry);
  925. physics_system->set_gravity({0.0f, -9.80665f * 100.0f, 0.0f});
  926. // Setup reproductive system
  927. reproductive_system = std::make_unique<::reproductive_system>(*entity_registry);
  928. reproductive_system->set_physics_system(physics_system.get());
  929. // Setup spatial system
  930. spatial_system = std::make_unique<::spatial_system>(*entity_registry);
  931. // Setup constraint system
  932. constraint_system = std::make_unique<::constraint_system>(*entity_registry);
  933. // Setup orbit system
  934. orbit_system = std::make_unique<::orbit_system>(*entity_registry);
  935. // Setup blackbody system
  936. blackbody_system = std::make_unique<::blackbody_system>(*entity_registry);
  937. blackbody_system->set_illuminant(color::illuminant::deg2::d55<double>);
  938. // Setup atmosphere system
  939. atmosphere_system = std::make_unique<::atmosphere_system>(*entity_registry);
  940. atmosphere_system->set_sky_pass(sky_pass.get());
  941. // Setup astronomy system
  942. astronomy_system = std::make_unique<::astronomy_system>(*entity_registry);
  943. astronomy_system->set_transmittance_samples(16);
  944. astronomy_system->set_sky_pass(sky_pass.get());
  945. // Setup render system
  946. render_system = std::make_unique<::render_system>(*entity_registry);
  947. render_system->set_renderer(renderer.get());
  948. render_system->add_layer(surface_scene.get());
  949. render_system->add_layer(underground_scene.get());
  950. render_system->add_layer(ui_scene.get());
  951. }
  952. void game::setup_controls()
  953. {
  954. debug::log::trace("Setting up controls...");
  955. // Load SDL game controller mappings database
  956. // debug::log::trace("Loading SDL game controller mappings...");
  957. // file_buffer* game_controller_db = resource_manager->load<file_buffer>("gamecontrollerdb.txt");
  958. // if (!game_controller_db)
  959. // {
  960. // debug::log::error("Failed to load SDL game controller mappings");
  961. // }
  962. // else
  963. // {
  964. // app->add_game_controller_mappings(game_controller_db->data(), game_controller_db->size());
  965. // debug::log::trace("Loaded SDL game controller mappings");
  966. // resource_manager->unload("gamecontrollerdb.txt");
  967. // }
  968. // Pass input event queue to action maps
  969. event::dispatcher* input_event_dispatcher = &input_manager->get_event_dispatcher();
  970. window_action_map.set_event_dispatcher(input_event_dispatcher);
  971. menu_action_map.set_event_dispatcher(input_event_dispatcher);
  972. movement_action_map.set_event_dispatcher(input_event_dispatcher);
  973. camera_action_map.set_event_dispatcher(input_event_dispatcher);
  974. ant_action_map.set_event_dispatcher(input_event_dispatcher);
  975. debug_action_map.set_event_dispatcher(input_event_dispatcher);
  976. // Default control profile settings
  977. control_profile_filename = "controls.cfg";
  978. // Read control profile settings
  979. if (read_or_write_setting(*this, "control_profile", control_profile_filename))
  980. {
  981. control_profile = resource_manager->load<::control_profile>(control_profile_filename);
  982. }
  983. if (!control_profile)
  984. {
  985. // Allocate control profile
  986. control_profile = std::make_shared<::control_profile>();
  987. // Reset control profile to default settings.
  988. ::reset_control_profile(*control_profile);
  989. // Save control profile
  990. resource_manager->set_write_path(controls_path);
  991. resource_manager->save(*control_profile, control_profile_filename);
  992. }
  993. // Apply control profile
  994. ::apply_control_profile(*this, *control_profile);
  995. // Setup mouse sensitivity
  996. mouse_pan_factor = mouse_radians_per_pixel * mouse_pan_sensitivity * (mouse_invert_pan ? -1.0 : 1.0);
  997. mouse_tilt_factor = mouse_radians_per_pixel * mouse_tilt_sensitivity * (mouse_invert_tilt ? -1.0 : 1.0);
  998. // Setup gamepad sensitivity
  999. gamepad_pan_factor = gamepad_radians_per_second * gamepad_pan_sensitivity * (gamepad_invert_pan ? -1.0 : 1.0);
  1000. gamepad_tilt_factor = gamepad_radians_per_second * gamepad_tilt_sensitivity * (gamepad_invert_tilt ? -1.0 : 1.0);
  1001. // Setup action callbacks
  1002. setup_window_controls(*this);
  1003. setup_menu_controls(*this);
  1004. setup_camera_controls(*this);
  1005. setup_game_controls(*this);
  1006. setup_ant_controls(*this);
  1007. // Enable window controls
  1008. enable_window_controls(*this);
  1009. #if defined(DEBUG)
  1010. // Setup and enable debug controls
  1011. setup_debug_controls(*this);
  1012. enable_debug_controls(*this);
  1013. #endif
  1014. debug::log::trace("Set up controls");
  1015. }
  1016. void game::setup_debugging()
  1017. {
  1018. cli = std::make_unique<debug::cli>();
  1019. const auto& viewport_size = window->get_viewport_size();
  1020. frame_time_text = std::make_unique<scene::text>();
  1021. frame_time_text->set_material(debug_font_material);
  1022. frame_time_text->set_color({1.0f, 1.0f, 0.0f, 1.0f});
  1023. frame_time_text->set_font(&debug_font);
  1024. frame_time_text->set_translation({std::round(0.0f), std::round(viewport_size.y() - debug_font.get_font_metrics().size), 99.0f});
  1025. #if defined(DEBUG)
  1026. ui_scene->add_object(*frame_time_text);
  1027. debug_ui_visible = true;
  1028. #endif
  1029. }
  1030. void game::setup_timing()
  1031. {
  1032. // Init default settings
  1033. max_frame_rate = static_cast<float>(window_manager->get_display(0).get_refresh_rate() * 2);
  1034. // Read settings
  1035. read_or_write_setting(*this, "fixed_update_rate", fixed_update_rate);
  1036. read_or_write_setting(*this, "max_frame_rate", max_frame_rate);
  1037. read_or_write_setting(*this, "limit_frame_rate", limit_frame_rate);
  1038. const auto fixed_update_interval = std::chrono::duration_cast<::frame_scheduler::duration_type>(std::chrono::duration<double>(1.0 / fixed_update_rate));
  1039. const auto min_frame_duration = (limit_frame_rate) ? std::chrono::duration_cast<::frame_scheduler::duration_type>(std::chrono::duration<double>(1.0 / max_frame_rate)) : frame_scheduler::duration_type::zero();
  1040. const auto max_frame_duration = fixed_update_interval * 15;
  1041. // Configure frame scheduler
  1042. frame_scheduler.set_fixed_update_interval(fixed_update_interval);
  1043. frame_scheduler.set_min_frame_duration(min_frame_duration);
  1044. frame_scheduler.set_max_frame_duration(max_frame_duration);
  1045. frame_scheduler.set_fixed_update_callback(std::bind_front(&game::fixed_update, this));
  1046. frame_scheduler.set_variable_update_callback(std::bind_front(&game::variable_update, this));
  1047. // Init frame duration average
  1048. average_frame_duration.reserve(15);
  1049. }
  1050. void game::shutdown_audio()
  1051. {
  1052. debug::log::trace("Shutting down audio...");
  1053. if (alc_context)
  1054. {
  1055. alcMakeContextCurrent(nullptr);
  1056. alcDestroyContext(alc_context);
  1057. }
  1058. if (alc_device)
  1059. {
  1060. alcCloseDevice(alc_device);
  1061. }
  1062. debug::log::trace("Shut down audio");
  1063. }
  1064. void game::fixed_update(::frame_scheduler::duration_type fixed_update_time, ::frame_scheduler::duration_type fixed_update_interval)
  1065. {
  1066. const float t = std::chrono::duration<float>(fixed_update_time).count();
  1067. const float dt = std::chrono::duration<float>(fixed_update_interval).count();
  1068. // Update tweens
  1069. sky_pass->update_tweens();
  1070. // Process window events
  1071. window_manager->update();
  1072. // Process function queue
  1073. while (!function_queue.empty())
  1074. {
  1075. function_queue.front()();
  1076. function_queue.pop();
  1077. }
  1078. // Advance tline
  1079. //timeline->advance(dt);
  1080. // Update entity systems
  1081. animation_system->update(t, dt);
  1082. physics_system->update(t, dt);
  1083. //terrain_system->update(t, dt);
  1084. //subterrain_system->update(t, dt);
  1085. collision_system->update(t, dt);
  1086. behavior_system->update(t, dt);
  1087. steering_system->update(t, dt);
  1088. locomotion_system->update(t, dt);
  1089. ik_system->update(t, dt);
  1090. reproductive_system->update(t, dt);
  1091. metabolic_system->update(t, dt);
  1092. metamorphosis_system->update(t, dt);
  1093. // physics_system->update(t, dt);
  1094. orbit_system->update(t, dt);
  1095. blackbody_system->update(t, dt);
  1096. atmosphere_system->update(t, dt);
  1097. astronomy_system->update(t, dt);
  1098. spatial_system->update(t, dt);
  1099. constraint_system->update(t, dt);
  1100. animator->animate(dt);
  1101. camera_system->update(t, dt);
  1102. render_system->update(t, dt);
  1103. }
  1104. void game::variable_update(::frame_scheduler::duration_type fixed_update_time, ::frame_scheduler::duration_type fixed_update_interval, ::frame_scheduler::duration_type accumulated_time)
  1105. {
  1106. // Calculate subframe interpolation factor (`alpha`)
  1107. const float alpha = static_cast<float>(std::chrono::duration<double, ::frame_scheduler::duration_type::period>{accumulated_time} / fixed_update_interval);
  1108. // Sample average frame duration
  1109. const float average_frame_ms = average_frame_duration(std::chrono::duration<float, std::milli>(frame_scheduler.get_frame_duration()).count());
  1110. const float average_frame_fps = 1000.0f / average_frame_ms;
  1111. // Update frame rate display
  1112. frame_time_text->set_content(std::format("{:5.02f}ms / {:5.02f} FPS", average_frame_ms, average_frame_fps));
  1113. // Process input events
  1114. input_manager->update();
  1115. // Interpolate physics
  1116. physics_system->interpolate(alpha);
  1117. // Interpolate animation
  1118. animation_system->interpolate(alpha);
  1119. // Render
  1120. camera_system->interpolate(alpha);
  1121. render_system->draw(alpha);
  1122. window->swap_buffers();
  1123. }
  1124. void game::execute()
  1125. {
  1126. // Change to initial state
  1127. state_machine.emplace(std::make_unique<main_menu_state>(*this, true));
  1128. debug::log::trace("Entered main loop");
  1129. frame_scheduler.refresh();
  1130. while (!closed)
  1131. {
  1132. frame_scheduler.tick();
  1133. }
  1134. debug::log::trace("Exited main loop");
  1135. // Exit all active game states
  1136. while (!state_machine.empty())
  1137. {
  1138. state_machine.pop();
  1139. }
  1140. }