💿🐜 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.

1244 lines
40 KiB

1 year ago
  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 "animation/animation.hpp"
  20. #include "animation/animator.hpp"
  21. #include "animation/ease.hpp"
  22. #include "animation/screen-transition.hpp"
  23. #include "animation/timeline.hpp"
  24. #include "application.hpp"
  25. #include "color/color.hpp"
  26. #include "config.hpp"
  27. #include "debug/cli.hpp"
  28. #include "debug/log.hpp"
  29. #include "entity/commands.hpp"
  30. #include "game/context.hpp"
  31. #include "game/controls.hpp"
  32. #include "game/fonts.hpp"
  33. #include "game/graphics.hpp"
  34. #include "game/menu.hpp"
  35. #include "game/save.hpp"
  36. #include "game/state/boot.hpp"
  37. #include "game/state/splash.hpp"
  38. #include "game/system/astronomy.hpp"
  39. #include "game/system/atmosphere.hpp"
  40. #include "game/system/behavior.hpp"
  41. #include "game/system/blackbody.hpp"
  42. #include "game/system/camera.hpp"
  43. #include "game/system/collision.hpp"
  44. #include "game/system/constraint.hpp"
  45. #include "game/system/locomotion.hpp"
  46. #include "game/system/orbit.hpp"
  47. #include "game/system/render.hpp"
  48. #include "game/system/spatial.hpp"
  49. #include "game/system/spring.hpp"
  50. #include "game/system/steering.hpp"
  51. #include "game/system/subterrain.hpp"
  52. #include "game/system/terrain.hpp"
  53. #include "game/system/vegetation.hpp"
  54. #include "gl/framebuffer.hpp"
  55. #include "gl/pixel-format.hpp"
  56. #include "gl/pixel-type.hpp"
  57. #include "gl/rasterizer.hpp"
  58. #include "gl/texture-2d.hpp"
  59. #include "gl/texture-filter.hpp"
  60. #include "gl/texture-wrapping.hpp"
  61. #include "gl/vertex-array.hpp"
  62. #include "gl/vertex-attribute.hpp"
  63. #include "gl/vertex-buffer.hpp"
  64. #include "input/gamepad.hpp"
  65. #include "input/keyboard.hpp"
  66. #include "input/mapper.hpp"
  67. #include "input/mouse.hpp"
  68. #include "input/scancode.hpp"
  69. #include "render/compositor.hpp"
  70. #include "render/material-flags.hpp"
  71. #include "render/material-property.hpp"
  72. #include "render/passes/bloom-pass.hpp"
  73. #include "render/passes/clear-pass.hpp"
  74. #include "render/passes/final-pass.hpp"
  75. #include "render/passes/fxaa-pass.hpp"
  76. #include "render/passes/ground-pass.hpp"
  77. #include "render/passes/material-pass.hpp"
  78. #include "render/passes/outline-pass.hpp"
  79. #include "render/passes/resample-pass.hpp"
  80. #include "render/passes/shadow-map-pass.hpp"
  81. #include "render/passes/sky-pass.hpp"
  82. #include "render/renderer.hpp"
  83. #include "render/vertex-attribute.hpp"
  84. #include "resources/file-buffer.hpp"
  85. #include "resources/resource-manager.hpp"
  86. #include "scene/scene.hpp"
  87. #include "utility/paths.hpp"
  88. #include <algorithm>
  89. #include <cxxopts.hpp>
  90. #include <entt/entt.hpp>
  91. #include <execution>
  92. #include <filesystem>
  93. #include <functional>
  94. #include <string>
  95. #include <vector>
  96. namespace game {
  97. namespace state {
  98. boot::boot(game::context& ctx, int argc, char** argv):
  99. game::state::base(ctx)
  100. {
  101. // Boot process
  102. debug::log::trace("Booting up...");
  103. // Allocate application
  104. ctx.app = new application();
  105. // Parse command line options
  106. parse_options(argc, argv);
  107. setup_resources();
  108. load_config();
  109. load_strings();
  110. setup_window();
  111. setup_rendering();
  112. setup_audio();
  113. setup_scenes();
  114. setup_animation();
  115. setup_entities();
  116. setup_systems();
  117. setup_controls();
  118. setup_ui();
  119. setup_debugging();
  120. setup_loop();
  121. ctx.active_ecoregion = nullptr;
  122. debug::log::trace("Boot up complete");
  123. // Push splash state
  124. ctx.state_machine.emplace(new game::state::splash(ctx));
  125. // Enter main loop
  126. debug::log::trace("Entered main loop");
  127. loop();
  128. }
  129. boot::~boot()
  130. {
  131. debug::log::trace("Booting down...");
  132. shutdown_audio();
  133. // Close application
  134. delete ctx.app;
  135. ctx.app = nullptr;
  136. debug::log::trace("Boot down complete");
  137. }
  138. void boot::parse_options(int argc, char** argv)
  139. {
  140. debug::log::trace("Parsing {} command line arguments...", argc);
  141. try
  142. {
  143. cxxopts::Options options("Antkeeper", "Ant colony simulation game");
  144. options.add_options()
  145. ("c,continue", "Continues from the last save")
  146. ("d,data", "Sets the data package path", cxxopts::value<std::string>())
  147. ("f,fullscreen", "Starts in fullscreen mode")
  148. ("n,new-game", "Starts a new game")
  149. ("q,quick-start", "Skips to the main menu")
  150. ("r,reset", "Restores all settings to default")
  151. ("v,vsync", "Enables or disables v-sync", cxxopts::value<int>())
  152. ("w,windowed", "Starts in windowed mode");
  153. auto result = options.parse(argc, argv);
  154. // --continue
  155. if (result.count("continue"))
  156. option_continue = true;
  157. // --data
  158. if (result.count("data"))
  159. option_data = result["data"].as<std::string>();
  160. // --fullscreen
  161. if (result.count("fullscreen"))
  162. option_fullscreen = true;
  163. // --new-game
  164. if (result.count("new-game"))
  165. option_new_game = true;
  166. // --quick-start
  167. if (result.count("quick-start"))
  168. option_quick_start = true;
  169. // --reset
  170. if (result.count("reset"))
  171. option_reset = true;
  172. // --vsync
  173. if (result.count("vsync"))
  174. option_v_sync = (result["vsync"].as<int>()) ? true : false;
  175. // --windowed
  176. if (result.count("windowed"))
  177. option_windowed = true;
  178. debug::log::trace("Parsed {} command line arguments", argc);
  179. }
  180. catch (const std::exception& e)
  181. {
  182. debug::log::warning("Exception caught while parsing command line arguments: {}", e.what());
  183. }
  184. }
  185. void boot::setup_resources()
  186. {
  187. // Setup resource manager
  188. ctx.resource_manager = new resource_manager();
  189. // Detect paths
  190. ctx.data_path = get_data_path(config::application_name);
  191. ctx.config_path = get_config_path(config::application_name);
  192. ctx.mods_path = ctx.config_path / "mods";
  193. ctx.saves_path = ctx.config_path / "saves";
  194. ctx.screenshots_path = ctx.config_path / "gallery";
  195. ctx.controls_path = ctx.config_path / "controls";
  196. // Log resource paths
  197. debug::log::info("Data path: \"{}\"", ctx.data_path.string());
  198. debug::log::info("Config path: \"{}\"", ctx.config_path.string());
  199. // Create nonexistent config directories
  200. std::vector<std::filesystem::path> config_paths;
  201. config_paths.push_back(ctx.config_path);
  202. config_paths.push_back(ctx.mods_path);
  203. config_paths.push_back(ctx.saves_path);
  204. config_paths.push_back(ctx.screenshots_path);
  205. config_paths.push_back(ctx.controls_path);
  206. for (const std::filesystem::path& path: config_paths)
  207. {
  208. if (!std::filesystem::exists(path))
  209. {
  210. const std::string path_string = path.string();
  211. debug::log::trace("Creating directory \"{}\"...", path_string);
  212. if (std::filesystem::create_directories(path))
  213. {
  214. debug::log::trace("Created directory \"{}\"", path_string);
  215. }
  216. else
  217. {
  218. debug::log::error("Failed to create directory \"{}\"", path_string);
  219. }
  220. }
  221. }
  222. // Scan for mods
  223. std::vector<std::filesystem::path> mod_paths;
  224. for (const auto& directory_entry: std::filesystem::directory_iterator(ctx.mods_path))
  225. {
  226. if (directory_entry.is_directory())
  227. mod_paths.push_back(directory_entry.path());
  228. }
  229. // Determine data package path
  230. if (option_data.has_value())
  231. {
  232. ctx.data_package_path = std::filesystem::path(option_data.value());
  233. if (ctx.data_package_path.is_relative())
  234. ctx.data_package_path = ctx.data_path / ctx.data_package_path;
  235. }
  236. else
  237. {
  238. ctx.data_package_path = ctx.data_path / "antkeeper.dat";
  239. }
  240. // Mount mods
  241. for (const std::filesystem::path& mod_path: mod_paths)
  242. ctx.resource_manager->mount(ctx.mods_path / mod_path);
  243. // Mount config path
  244. ctx.resource_manager->mount(ctx.config_path);
  245. // Mount data package
  246. ctx.resource_manager->mount(ctx.data_package_path);
  247. // Include resource search paths in order of priority
  248. ctx.resource_manager->include("/controls/");
  249. ctx.resource_manager->include("/");
  250. }
  251. void boot::load_config()
  252. {
  253. debug::log::trace("Loading config...");
  254. // Load config file
  255. ctx.config = ctx.resource_manager->load<json>("config.json");
  256. if (ctx.config)
  257. {
  258. debug::log::trace("Loaded config");
  259. }
  260. else
  261. {
  262. debug::log::error("Failed to load config");
  263. }
  264. }
  265. void boot::load_strings()
  266. {
  267. debug::log::trace("Loading strings...");
  268. ctx.string_table = ctx.resource_manager->load<string_table>("strings.csv");
  269. build_string_table_map(&ctx.string_table_map, *ctx.string_table);
  270. ctx.language_code = (*ctx.config)["language"].get<std::string>();
  271. ctx.language_index = -1;
  272. for (int i = 2; i < (*ctx.string_table)[0].size(); ++i)
  273. {
  274. if ((*ctx.string_table)[0][i] == ctx.language_code)
  275. ctx.language_index = i - 2;
  276. }
  277. ctx.language_count = (*ctx.string_table)[0].size() - 2;
  278. debug::log::info("Languages available: {}", ctx.language_count);
  279. debug::log::info("Language index: {}", ctx.language_index);
  280. debug::log::info("Language code: {}", ctx.language_code);
  281. ctx.strings = &ctx.string_table_map[ctx.language_code];
  282. debug::log::trace("Loaded strings");
  283. }
  284. void boot::setup_window()
  285. {
  286. debug::log::trace("Setting up window...");
  287. application* app = ctx.app;
  288. json* config = ctx.config;
  289. // Set fullscreen or windowed mode
  290. bool fullscreen = true;
  291. if (option_fullscreen.has_value())
  292. fullscreen = true;
  293. else if (option_windowed.has_value())
  294. fullscreen = false;
  295. else if (config->contains("fullscreen"))
  296. fullscreen = (*config)["fullscreen"].get<bool>();
  297. app->set_fullscreen(fullscreen);
  298. // Set resolution
  299. const auto& display_dimensions = ctx.app->get_display_dimensions();
  300. int2 resolution = {display_dimensions[0], display_dimensions[1]};
  301. if (fullscreen)
  302. {
  303. if (config->contains("fullscreen_resolution"))
  304. {
  305. resolution.x() = (*config)["fullscreen_resolution"][0].get<int>();
  306. resolution.y() = (*config)["fullscreen_resolution"][1].get<int>();
  307. }
  308. }
  309. else
  310. {
  311. if (config->contains("windowed_resolution"))
  312. {
  313. resolution.x() = (*config)["windowed_resolution"][0].get<int>();
  314. resolution.y() = (*config)["windowed_resolution"][1].get<int>();
  315. }
  316. }
  317. app->resize_window(resolution.x(), resolution.y());
  318. // Set v-sync
  319. bool v_sync = true;
  320. if (option_v_sync.has_value())
  321. v_sync = (option_v_sync.value() != 0);
  322. else if (config->contains("v_sync"))
  323. v_sync = (*config)["v_sync"].get<bool>();
  324. app->set_v_sync(v_sync);
  325. // Set title
  326. app->set_title((*ctx.strings)["application_title"]);
  327. // Show window
  328. ctx.app->get_rasterizer()->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  329. ctx.app->get_rasterizer()->clear_framebuffer(true, false, false);
  330. app->show_window();
  331. ctx.app->swap_buffers();
  332. debug::log::trace("Set up window");
  333. }
  334. void boot::setup_rendering()
  335. {
  336. debug::log::trace("Setting up rendering...");
  337. // Get rasterizer from application
  338. ctx.rasterizer = ctx.app->get_rasterizer();
  339. // Create framebuffers
  340. game::graphics::create_framebuffers(ctx);
  341. // Load blue noise texture
  342. gl::texture_2d* blue_noise_map = ctx.resource_manager->load<gl::texture_2d>("blue-noise.tex");
  343. // Load fallback material
  344. ctx.fallback_material = ctx.resource_manager->load<render::material>("fallback.mtl");
  345. // Setup common render passes
  346. {
  347. // Construct bloom pass
  348. ctx.bloom_pass = new render::bloom_pass(ctx.rasterizer, ctx.resource_manager);
  349. ctx.bloom_pass->set_source_texture(ctx.hdr_color_texture);
  350. ctx.bloom_pass->set_mip_chain_length(0);
  351. ctx.bloom_pass->set_filter_radius(0.005f);
  352. ctx.common_final_pass = new render::final_pass(ctx.rasterizer, ctx.ldr_framebuffer_a, ctx.resource_manager);
  353. ctx.common_final_pass->set_color_texture(ctx.hdr_color_texture);
  354. ctx.common_final_pass->set_bloom_texture(ctx.bloom_pass->get_bloom_texture());
  355. ctx.common_final_pass->set_bloom_weight(0.04f);
  356. ctx.common_final_pass->set_blue_noise_texture(blue_noise_map);
  357. ctx.fxaa_pass = new render::fxaa_pass(ctx.rasterizer, &ctx.rasterizer->get_default_framebuffer(), ctx.resource_manager);
  358. ctx.fxaa_pass->set_source_texture(ctx.ldr_color_texture_a);
  359. ctx.resample_pass = new render::resample_pass(ctx.rasterizer, &ctx.rasterizer->get_default_framebuffer(), ctx.resource_manager);
  360. ctx.resample_pass->set_source_texture(ctx.ldr_color_texture_b);
  361. ctx.resample_pass->set_enabled(false);
  362. // Toggle bloom according to settings
  363. ctx.bloom_enabled = true;
  364. if (ctx.config->contains("bloom_enabled"))
  365. ctx.bloom_enabled = (*ctx.config)["bloom_enabled"].get<bool>();
  366. graphics::toggle_bloom(ctx, ctx.bloom_enabled);
  367. // Configure anti-aliasing according to settings
  368. ctx.anti_aliasing_method = render::anti_aliasing_method::fxaa;
  369. if (ctx.config->contains("anti_aliasing_method"))
  370. {
  371. const std::string aa_method = (*ctx.config)["anti_aliasing_method"].get<std::string>();
  372. if (aa_method == "fxaa")
  373. {
  374. ctx.anti_aliasing_method = render::anti_aliasing_method::fxaa;
  375. }
  376. else
  377. {
  378. ctx.anti_aliasing_method = render::anti_aliasing_method::none;
  379. }
  380. }
  381. graphics::select_anti_aliasing_method(ctx, ctx.anti_aliasing_method);
  382. // Configure render scaling according to settings
  383. ctx.render_scale = 1.0f;
  384. if (ctx.config->contains("render_scale"))
  385. {
  386. ctx.render_scale = (*ctx.config)["render_scale"].get<float>();
  387. if (ctx.render_scale != 1.0f)
  388. {
  389. graphics::change_render_resolution(ctx, ctx.render_scale);
  390. }
  391. }
  392. }
  393. // Setup UI compositor
  394. {
  395. ctx.ui_clear_pass = new render::clear_pass(ctx.rasterizer, &ctx.rasterizer->get_default_framebuffer());
  396. ctx.ui_clear_pass->set_cleared_buffers(false, true, false);
  397. ctx.ui_clear_pass->set_clear_depth(-1.0f);
  398. ctx.ui_material_pass = new render::material_pass(ctx.rasterizer, &ctx.rasterizer->get_default_framebuffer(), ctx.resource_manager);
  399. ctx.ui_material_pass->set_fallback_material(ctx.fallback_material);
  400. ctx.ui_compositor = new render::compositor();
  401. ctx.ui_compositor->add_pass(ctx.ui_clear_pass);
  402. ctx.ui_compositor->add_pass(ctx.ui_material_pass);
  403. }
  404. // Setup underground compositor
  405. {
  406. ctx.underground_clear_pass = new render::clear_pass(ctx.rasterizer, ctx.hdr_framebuffer);
  407. ctx.underground_clear_pass->set_cleared_buffers(true, true, false);
  408. ctx.underground_clear_pass->set_clear_color({1, 0, 1, 0});
  409. ctx.underground_clear_pass->set_clear_depth(-1.0f);
  410. ctx.underground_material_pass = new render::material_pass(ctx.rasterizer, ctx.hdr_framebuffer, ctx.resource_manager);
  411. ctx.underground_material_pass->set_fallback_material(ctx.fallback_material);
  412. ctx.underground_compositor = new render::compositor();
  413. ctx.underground_compositor->add_pass(ctx.underground_clear_pass);
  414. ctx.underground_compositor->add_pass(ctx.underground_material_pass);
  415. ctx.underground_compositor->add_pass(ctx.bloom_pass);
  416. ctx.underground_compositor->add_pass(ctx.common_final_pass);
  417. ctx.underground_compositor->add_pass(ctx.fxaa_pass);
  418. ctx.underground_compositor->add_pass(ctx.resample_pass);
  419. }
  420. // Setup surface compositor
  421. {
  422. ctx.surface_shadow_map_clear_pass = new render::clear_pass(ctx.rasterizer, ctx.shadow_map_framebuffer);
  423. ctx.surface_shadow_map_clear_pass->set_cleared_buffers(false, true, false);
  424. ctx.surface_shadow_map_clear_pass->set_clear_depth(1.0f);
  425. ctx.surface_shadow_map_pass = new render::shadow_map_pass(ctx.rasterizer, ctx.resource_manager);
  426. ctx.surface_clear_pass = new render::clear_pass(ctx.rasterizer, ctx.hdr_framebuffer);
  427. ctx.surface_clear_pass->set_cleared_buffers(false, true, true);
  428. ctx.surface_clear_pass->set_clear_depth(-1.0f);
  429. ctx.sky_pass = new render::sky_pass(ctx.rasterizer, ctx.hdr_framebuffer, ctx.resource_manager);
  430. ctx.sky_pass->set_enabled(false);
  431. ctx.sky_pass->set_magnification(3.0f);
  432. ctx.ground_pass = new render::ground_pass(ctx.rasterizer, ctx.hdr_framebuffer, ctx.resource_manager);
  433. ctx.ground_pass->set_enabled(false);
  434. ctx.surface_material_pass = new render::material_pass(ctx.rasterizer, ctx.hdr_framebuffer, ctx.resource_manager);
  435. ctx.surface_material_pass->set_fallback_material(ctx.fallback_material);
  436. ctx.surface_outline_pass = new render::outline_pass(ctx.rasterizer, ctx.hdr_framebuffer, ctx.resource_manager);
  437. ctx.surface_outline_pass->set_outline_width(0.25f);
  438. ctx.surface_outline_pass->set_outline_color(float4{1.0f, 1.0f, 1.0f, 1.0f});
  439. ctx.surface_compositor = new render::compositor();
  440. ctx.surface_compositor->add_pass(ctx.surface_shadow_map_clear_pass);
  441. ctx.surface_compositor->add_pass(ctx.surface_shadow_map_pass);
  442. ctx.surface_compositor->add_pass(ctx.surface_clear_pass);
  443. ctx.surface_compositor->add_pass(ctx.sky_pass);
  444. ctx.surface_compositor->add_pass(ctx.ground_pass);
  445. ctx.surface_compositor->add_pass(ctx.surface_material_pass);
  446. //ctx.surface_compositor->add_pass(ctx.surface_outline_pass);
  447. ctx.surface_compositor->add_pass(ctx.bloom_pass);
  448. ctx.surface_compositor->add_pass(ctx.common_final_pass);
  449. ctx.surface_compositor->add_pass(ctx.fxaa_pass);
  450. ctx.surface_compositor->add_pass(ctx.resample_pass);
  451. }
  452. // Create billboard VAO
  453. {
  454. const float billboard_vertex_data[] =
  455. {
  456. -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  457. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  458. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  459. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  460. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  461. 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
  462. };
  463. std::size_t billboard_vertex_size = 8;
  464. std::size_t billboard_vertex_stride = sizeof(float) * billboard_vertex_size;
  465. std::size_t billboard_vertex_count = 6;
  466. ctx.billboard_vbo = new gl::vertex_buffer(sizeof(float) * billboard_vertex_size * billboard_vertex_count, billboard_vertex_data);
  467. ctx.billboard_vao = new gl::vertex_array();
  468. std::size_t attribute_offset = 0;
  469. // Define position vertex attribute
  470. gl::vertex_attribute position_attribute;
  471. position_attribute.buffer = ctx.billboard_vbo;
  472. position_attribute.offset = attribute_offset;
  473. position_attribute.stride = billboard_vertex_stride;
  474. position_attribute.type = gl::vertex_attribute_type::float_32;
  475. position_attribute.components = 3;
  476. attribute_offset += position_attribute.components * sizeof(float);
  477. // Define UV vertex attribute
  478. gl::vertex_attribute uv_attribute;
  479. uv_attribute.buffer = ctx.billboard_vbo;
  480. uv_attribute.offset = attribute_offset;
  481. uv_attribute.stride = billboard_vertex_stride;
  482. uv_attribute.type = gl::vertex_attribute_type::float_32;
  483. uv_attribute.components = 2;
  484. attribute_offset += uv_attribute.components * sizeof(float);
  485. // Define barycentric vertex attribute
  486. gl::vertex_attribute barycentric_attribute;
  487. barycentric_attribute.buffer = ctx.billboard_vbo;
  488. barycentric_attribute.offset = attribute_offset;
  489. barycentric_attribute.stride = billboard_vertex_stride;
  490. barycentric_attribute.type = gl::vertex_attribute_type::float_32;
  491. barycentric_attribute.components = 3;
  492. attribute_offset += barycentric_attribute.components * sizeof(float);
  493. // Bind vertex attributes to VAO
  494. ctx.billboard_vao->bind(render::vertex_attribute::position, position_attribute);
  495. ctx.billboard_vao->bind(render::vertex_attribute::uv, uv_attribute);
  496. ctx.billboard_vao->bind(render::vertex_attribute::barycentric, barycentric_attribute);
  497. }
  498. // Create renderer
  499. ctx.renderer = new render::renderer();
  500. ctx.renderer->set_billboard_vao(ctx.billboard_vao);
  501. debug::log::trace("Set up rendering");
  502. }
  503. void boot::setup_audio()
  504. {
  505. debug::log::trace("Setting up audio...");
  506. // Load master volume config
  507. ctx.master_volume = 1.0f;
  508. if (ctx.config->contains("master_volume"))
  509. ctx.master_volume = (*ctx.config)["master_volume"].get<float>();
  510. // Load ambience volume config
  511. ctx.ambience_volume = 1.0f;
  512. if (ctx.config->contains("ambience_volume"))
  513. ctx.ambience_volume = (*ctx.config)["ambience_volume"].get<float>();
  514. // Load effects volume config
  515. ctx.effects_volume = 1.0f;
  516. if (ctx.config->contains("effects_volume"))
  517. ctx.effects_volume = (*ctx.config)["effects_volume"].get<float>();
  518. // Load mono audio config
  519. ctx.mono_audio = false;
  520. if (ctx.config->contains("mono_audio"))
  521. ctx.mono_audio = (*ctx.config)["mono_audio"].get<bool>();
  522. // Load captions config
  523. ctx.captions = false;
  524. if (ctx.config->contains("captions"))
  525. ctx.captions = (*ctx.config)["captions"].get<bool>();
  526. // Load captions size config
  527. ctx.captions_size = 1.0f;
  528. if (ctx.config->contains("captions_size"))
  529. ctx.captions_size = (*ctx.config)["captions_size"].get<float>();
  530. // Open audio device
  531. debug::log::trace("Opening audio device...");
  532. ctx.alc_device = alcOpenDevice(nullptr);
  533. if (!ctx.alc_device)
  534. {
  535. debug::log::error("Failed to open audio device: AL error code {}", alGetError());
  536. return;
  537. }
  538. else
  539. {
  540. // Get audio device name
  541. const ALCchar* alc_device_name = nullptr;
  542. if (alcIsExtensionPresent(ctx.alc_device, "ALC_ENUMERATE_ALL_EXT"))
  543. alc_device_name = alcGetString(ctx.alc_device, ALC_ALL_DEVICES_SPECIFIER);
  544. if (alcGetError(ctx.alc_device) != AL_NO_ERROR || !alc_device_name)
  545. alc_device_name = alcGetString(ctx.alc_device, ALC_DEVICE_SPECIFIER);
  546. // Log audio device name
  547. debug::log::info("Opened audio device \"{}\"", alc_device_name);
  548. }
  549. // Create audio context
  550. debug::log::trace("Creating audio context...");
  551. ctx.alc_context = alcCreateContext(ctx.alc_device, nullptr);
  552. if (!ctx.alc_context)
  553. {
  554. debug::log::error("Failed to create audio context: ALC error code {}", alcGetError(ctx.alc_device));
  555. alcCloseDevice(ctx.alc_device);
  556. return;
  557. }
  558. else
  559. {
  560. debug::log::trace("Created audio context");
  561. }
  562. // Make audio context current
  563. debug::log::trace("Making audio context current...");
  564. if (alcMakeContextCurrent(ctx.alc_context) == ALC_FALSE)
  565. {
  566. debug::log::error("Failed to make audio context current: ALC error code {}", alcGetError(ctx.alc_device));
  567. if (ctx.alc_context != nullptr)
  568. {
  569. alcDestroyContext(ctx.alc_context);
  570. }
  571. alcCloseDevice(ctx.alc_device);
  572. return;
  573. }
  574. else
  575. {
  576. debug::log::trace("Made audio context current");
  577. }
  578. debug::log::trace("Set up audio");
  579. }
  580. void boot::setup_scenes()
  581. {
  582. debug::log::trace("Setting up scenes...");
  583. // Get default framebuffer
  584. const auto& viewport_dimensions = ctx.rasterizer->get_default_framebuffer().get_dimensions();
  585. const float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
  586. // Setup UI camera
  587. ctx.ui_camera = new scene::camera();
  588. ctx.ui_camera->set_compositor(ctx.ui_compositor);
  589. auto viewport = ctx.app->get_viewport_dimensions();
  590. float clip_left = -viewport[0] * 0.5f;
  591. float clip_right = viewport[0] * 0.5f;
  592. float clip_top = -viewport[1] * 0.5f;
  593. float clip_bottom = viewport[1] * 0.5f;
  594. float clip_near = 0.0f;
  595. float clip_far = 1000.0f;
  596. ctx.ui_camera->set_orthographic(clip_left, clip_right, clip_top, clip_bottom, clip_near, clip_far);
  597. ctx.ui_camera->look_at({0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f});
  598. ctx.ui_camera->update_tweens();
  599. // Setup underground camera
  600. ctx.underground_camera = new scene::camera();
  601. ctx.underground_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  602. ctx.underground_camera->set_compositor(ctx.underground_compositor);
  603. ctx.underground_camera->set_composite_index(0);
  604. ctx.underground_camera->set_active(false);
  605. // Setup surface camera
  606. ctx.surface_camera = new scene::camera();
  607. ctx.surface_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 5000.0f);
  608. ctx.surface_camera->set_compositor(ctx.surface_compositor);
  609. ctx.surface_camera->set_composite_index(0);
  610. ctx.surface_camera->set_active(false);
  611. // Setup UI scene
  612. {
  613. ctx.ui_scene = new scene::collection();
  614. // Menu BG billboard
  615. render::material* menu_bg_material = new render::material();
  616. menu_bg_material->set_shader_program(ctx.resource_manager->load<gl::shader_program>("ui-element-untextured.glsl"));
  617. auto menu_bg_tint = menu_bg_material->add_property<float4>("tint");
  618. menu_bg_tint->set_value(float4{0.0f, 0.0f, 0.0f, 0.5f});
  619. menu_bg_material->set_blend_mode(render::blend_mode::translucent);
  620. menu_bg_material->update_tweens();
  621. ctx.menu_bg_billboard = new scene::billboard();
  622. ctx.menu_bg_billboard->set_active(false);
  623. ctx.menu_bg_billboard->set_material(menu_bg_material);
  624. ctx.menu_bg_billboard->set_scale({(float)viewport_dimensions[0] * 0.5f, (float)viewport_dimensions[1] * 0.5f, 1.0f});
  625. ctx.menu_bg_billboard->set_translation({0.0f, 0.0f, -100.0f});
  626. ctx.menu_bg_billboard->update_tweens();
  627. // Create camera flash billboard
  628. render::material* flash_material = new render::material();
  629. flash_material->set_shader_program(ctx.resource_manager->load<gl::shader_program>("ui-element-untextured.glsl"));
  630. auto flash_tint = flash_material->add_property<float4>("tint");
  631. flash_tint->set_value(float4{1, 1, 1, 1});
  632. //flash_tint->set_tween_interpolator(ease<float4>::out_quad);
  633. flash_material->set_blend_mode(render::blend_mode::translucent);
  634. flash_material->update_tweens();
  635. ctx.camera_flash_billboard = new scene::billboard();
  636. ctx.camera_flash_billboard->set_material(flash_material);
  637. ctx.camera_flash_billboard->set_scale({(float)viewport_dimensions[0] * 0.5f, (float)viewport_dimensions[1] * 0.5f, 1.0f});
  638. ctx.camera_flash_billboard->set_translation({0.0f, 0.0f, 0.0f});
  639. ctx.camera_flash_billboard->update_tweens();
  640. // Create depth debug billboard
  641. /*
  642. material* depth_debug_material = new material();
  643. depth_debug_material->set_shader_program(ctx.resource_manager->load<gl::shader_program>("ui-element-textured.glsl"));
  644. depth_debug_material->add_property<const gl::texture_2d*>("background")->set_value(shadow_map_depth_texture);
  645. depth_debug_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  646. billboard* depth_debug_billboard = new billboard();
  647. depth_debug_billboard->set_material(depth_debug_material);
  648. depth_debug_billboard->set_scale({128, 128, 1});
  649. depth_debug_billboard->set_translation({-960 + 128, 1080 * 0.5f - 128, 0});
  650. depth_debug_billboard->update_tweens();
  651. ui_system->get_scene()->add_object(depth_debug_billboard);
  652. */
  653. ctx.ui_scene->add_object(ctx.ui_camera);
  654. }
  655. // Setup underground scene
  656. {
  657. ctx.underground_scene = new scene::collection();
  658. ctx.underground_scene->add_object(ctx.underground_camera);
  659. }
  660. // Setup surface scene
  661. {
  662. ctx.surface_scene = new scene::collection();
  663. ctx.surface_scene->add_object(ctx.surface_camera);
  664. }
  665. // Clear active scene
  666. ctx.active_scene = nullptr;
  667. debug::log::trace("Set up scenes");
  668. }
  669. void boot::setup_animation()
  670. {
  671. // Setup timeline system
  672. ctx.timeline = new timeline();
  673. ctx.timeline->set_autoremove(true);
  674. // Setup animator
  675. ctx.animator = new animator();
  676. // Create fade transition
  677. ctx.fade_transition = new screen_transition();
  678. ctx.fade_transition->get_material()->set_shader_program(ctx.resource_manager->load<gl::shader_program>("fade-transition.glsl"));
  679. ctx.fade_transition_color = ctx.fade_transition->get_material()->add_property<float3>("color");
  680. ctx.fade_transition_color->set_value({0, 0, 0});
  681. ctx.ui_scene->add_object(ctx.fade_transition->get_billboard());
  682. ctx.animator->add_animation(ctx.fade_transition->get_animation());
  683. // Create inner radial transition
  684. ctx.radial_transition_inner = new screen_transition();
  685. ctx.radial_transition_inner->get_material()->set_shader_program(ctx.resource_manager->load<gl::shader_program>("radial-transition-inner.glsl"));
  686. //ctx.ui_scene->add_object(ctx.radial_transition_inner->get_billboard());
  687. //ctx.animator->add_animation(ctx.radial_transition_inner->get_animation());
  688. // Create outer radial transition
  689. ctx.radial_transition_outer = new screen_transition();
  690. ctx.radial_transition_outer->get_material()->set_shader_program(ctx.resource_manager->load<gl::shader_program>("radial-transition-outer.glsl"));
  691. //ctx.ui_scene->add_object(ctx.radial_transition_outer->get_billboard());
  692. //ctx.animator->add_animation(ctx.radial_transition_outer->get_animation());
  693. // Menu BG animations
  694. {
  695. render::material_property<float4>* menu_bg_tint = static_cast<render::material_property<float4>*>(ctx.menu_bg_billboard->get_material()->get_property("tint"));
  696. auto menu_bg_frame_callback = [menu_bg_tint](int channel, const float& opacity)
  697. {
  698. menu_bg_tint->set_value(float4{0.0f, 0.0f, 0.0f, opacity});
  699. };
  700. // Create menu BG fade in animation
  701. ctx.menu_bg_fade_in_animation = new animation<float>();
  702. {
  703. ctx.menu_bg_fade_in_animation->set_interpolator(ease<float>::out_cubic);
  704. animation_channel<float>* channel = ctx.menu_bg_fade_in_animation->add_channel(0);
  705. channel->insert_keyframe({0.0f, 0.0f});
  706. channel->insert_keyframe({config::menu_fade_in_duration, config::menu_bg_opacity});
  707. ctx.menu_bg_fade_in_animation->set_frame_callback(menu_bg_frame_callback);
  708. ctx.menu_bg_fade_in_animation->set_start_callback
  709. (
  710. [&ctx = this->ctx, menu_bg_tint]()
  711. {
  712. ctx.ui_scene->add_object(ctx.menu_bg_billboard);
  713. menu_bg_tint->set_value(float4{0.0f, 0.0f, 0.0f, 0.0f});
  714. menu_bg_tint->update_tweens();
  715. ctx.menu_bg_billboard->set_active(true);
  716. }
  717. );
  718. }
  719. // Create menu BG fade out animation
  720. ctx.menu_bg_fade_out_animation = new animation<float>();
  721. {
  722. ctx.menu_bg_fade_out_animation->set_interpolator(ease<float>::out_cubic);
  723. animation_channel<float>* channel = ctx.menu_bg_fade_out_animation->add_channel(0);
  724. channel->insert_keyframe({0.0f, config::menu_bg_opacity});
  725. channel->insert_keyframe({config::menu_fade_out_duration, 0.0f});
  726. ctx.menu_bg_fade_out_animation->set_frame_callback(menu_bg_frame_callback);
  727. ctx.menu_bg_fade_out_animation->set_end_callback
  728. (
  729. [&ctx = this->ctx]()
  730. {
  731. ctx.ui_scene->remove_object(ctx.menu_bg_billboard);
  732. ctx.menu_bg_billboard->set_active(false);
  733. }
  734. );
  735. }
  736. ctx.animator->add_animation(ctx.menu_bg_fade_in_animation);
  737. ctx.animator->add_animation(ctx.menu_bg_fade_out_animation);
  738. }
  739. // Create camera flash animation
  740. ctx.camera_flash_animation = new animation<float>();
  741. {
  742. ctx.camera_flash_animation->set_interpolator(ease<float>::out_sine);
  743. const float duration = 0.5f;
  744. animation_channel<float>* channel = ctx.camera_flash_animation->add_channel(0);
  745. channel->insert_keyframe({0.0f, 1.0f});
  746. channel->insert_keyframe({duration, 0.0f});
  747. }
  748. }
  749. void boot::setup_entities()
  750. {
  751. // Create entity registry
  752. ctx.entity_registry = new entt::registry();
  753. }
  754. void boot::setup_systems()
  755. {
  756. const auto& viewport_dimensions = ctx.app->get_viewport_dimensions();
  757. float4 viewport = {0.0f, 0.0f, static_cast<float>(viewport_dimensions[0]), static_cast<float>(viewport_dimensions[1])};
  758. // Setup terrain system
  759. ctx.terrain_system = new game::system::terrain(*ctx.entity_registry);
  760. ctx.terrain_system->set_patch_side_length(31.0f);
  761. ctx.terrain_system->set_patch_subdivisions(31);
  762. ctx.terrain_system->set_scene_collection(ctx.surface_scene);
  763. // Setup vegetation system
  764. //ctx.vegetation_system = new game::system::vegetation(*ctx.entity_registry);
  765. //ctx.vegetation_system->set_terrain_patch_size(TERRAIN_PATCH_SIZE);
  766. //ctx.vegetation_system->set_vegetation_patch_resolution(VEGETATION_PATCH_RESOLUTION);
  767. //ctx.vegetation_system->set_vegetation_density(1.0f);
  768. //ctx.vegetation_system->set_vegetation_model(ctx.resource_manager->load<model>("grass-tuft.mdl"));
  769. //ctx.vegetation_system->set_scene(ctx.surface_scene);
  770. // Setup camera system
  771. ctx.camera_system = new game::system::camera(*ctx.entity_registry);
  772. ctx.camera_system->set_viewport(viewport);
  773. // Setup subterrain system
  774. ctx.subterrain_system = new game::system::subterrain(*ctx.entity_registry, ctx.resource_manager);
  775. ctx.subterrain_system->set_scene(ctx.underground_scene);
  776. // Setup collision system
  777. ctx.collision_system = new game::system::collision(*ctx.entity_registry);
  778. // Setup behavior system
  779. ctx.behavior_system = new game::system::behavior(*ctx.entity_registry);
  780. // Setup locomotion system
  781. ctx.locomotion_system = new game::system::locomotion(*ctx.entity_registry);
  782. // Setup steering system
  783. ctx.steering_system = new game::system::steering(*ctx.entity_registry);
  784. // Setup spring system
  785. ctx.spring_system = new game::system::spring(*ctx.entity_registry);
  786. // Setup spatial system
  787. ctx.spatial_system = new game::system::spatial(*ctx.entity_registry);
  788. // Setup constraint system
  789. ctx.constraint_system = new game::system::constraint(*ctx.entity_registry);
  790. // Setup orbit system
  791. ctx.orbit_system = new game::system::orbit(*ctx.entity_registry);
  792. // Setup blackbody system
  793. ctx.blackbody_system = new game::system::blackbody(*ctx.entity_registry);
  794. ctx.blackbody_system->set_illuminant(color::illuminant::deg2::d55<double>);
  795. // RGB wavelengths for atmospheric scatteering
  796. ctx.rgb_wavelengths = {680, 550, 440};
  797. // Setup atmosphere system
  798. ctx.atmosphere_system = new game::system::atmosphere(*ctx.entity_registry);
  799. ctx.atmosphere_system->set_rgb_wavelengths(ctx.rgb_wavelengths * 1e-9);
  800. ctx.atmosphere_system->set_sky_pass(ctx.sky_pass);
  801. // Setup astronomy system
  802. ctx.astronomy_system = new game::system::astronomy(*ctx.entity_registry);
  803. ctx.astronomy_system->set_transmittance_samples(16);
  804. ctx.astronomy_system->set_sky_pass(ctx.sky_pass);
  805. // Setup render system
  806. ctx.render_system = new game::system::render(*ctx.entity_registry);
  807. //ctx.render_system->add_layer(ctx.underground_scene);
  808. ctx.render_system->add_layer(ctx.surface_scene);
  809. ctx.render_system->add_layer(ctx.ui_scene);
  810. ctx.render_system->set_renderer(ctx.renderer);
  811. }
  812. void boot::setup_controls()
  813. {
  814. // Load SDL game controller mappings database
  815. debug::log::trace("Loading SDL game controller mappings...");
  816. file_buffer* game_controller_db = ctx.resource_manager->load<file_buffer>("gamecontrollerdb.txt");
  817. if (!game_controller_db)
  818. {
  819. debug::log::error("Failed to load SDL game controller mappings");
  820. }
  821. else
  822. {
  823. ctx.app->add_game_controller_mappings(game_controller_db->data(), game_controller_db->size());
  824. debug::log::trace("Loaded SDL game controller mappings");
  825. ctx.resource_manager->unload("gamecontrollerdb.txt");
  826. }
  827. // Load controls
  828. debug::log::trace("Loading controls...");
  829. try
  830. {
  831. // If a control profile is set in the config file
  832. if (ctx.config->contains("control_profile"))
  833. {
  834. // Load control profile
  835. json* profile = ctx.resource_manager->load<json>((*ctx.config)["control_profile"].get<std::string>());
  836. // Apply control profile
  837. if (profile)
  838. {
  839. game::apply_control_profile(ctx, *profile);
  840. }
  841. }
  842. // Calibrate gamepads
  843. // for (input::gamepad* gamepad: ctx.app->get_gamepads())
  844. // {
  845. // const std::string uuid_string = gamepad->get_uuid().to_string();
  846. // debug::log::push_task("Loading calibration for gamepad " + uuid_string);
  847. // json* calibration = game::load_gamepad_calibration(ctx, *gamepad);
  848. // if (!calibration)
  849. // {
  850. // debug::log::pop_task(EXIT_FAILURE);
  851. // debug::log::push_task("Generating default calibration for gamepad " + uuid_string);
  852. // json default_calibration = game::default_gamepad_calibration();
  853. // apply_gamepad_calibration(*gamepad, default_calibration);
  854. // if (!save_gamepad_calibration(ctx, *gamepad, default_calibration))
  855. // debug::log::pop_task(EXIT_FAILURE);
  856. // else
  857. // debug::log::pop_task(EXIT_SUCCESS);
  858. // }
  859. // else
  860. // {
  861. // debug::log::pop_task(EXIT_SUCCESS);
  862. // apply_gamepad_calibration(*gamepad, *calibration);
  863. // }
  864. // }
  865. // Setup fullscreen control
  866. ctx.control_subscriptions.emplace_front
  867. (
  868. ctx.fullscreen_control.get_activated_channel().subscribe
  869. (
  870. [&ctx = this->ctx](const auto& event)
  871. {
  872. bool fullscreen = !ctx.app->is_fullscreen();
  873. ctx.app->set_fullscreen(fullscreen);
  874. if (!fullscreen)
  875. {
  876. int2 resolution;
  877. resolution.x() = (*ctx.config)["windowed_resolution"][0].get<int>();
  878. resolution.y() = (*ctx.config)["windowed_resolution"][1].get<int>();
  879. ctx.app->resize_window(resolution.x(), resolution.y());
  880. }
  881. // Save display mode config
  882. (*ctx.config)["fullscreen"] = fullscreen;
  883. game::save::config(ctx);
  884. }
  885. )
  886. );
  887. // Setup screenshot control
  888. ctx.control_subscriptions.emplace_front
  889. (
  890. ctx.screenshot_control.get_activated_channel().subscribe
  891. (
  892. [&ctx = this->ctx](const auto& event)
  893. {
  894. game::graphics::save_screenshot(ctx);
  895. }
  896. )
  897. );
  898. // Map and enable window controls
  899. ctx.window_controls.add_mapping(ctx.fullscreen_control, input::key_mapping(nullptr, input::scancode::f11, false));
  900. ctx.window_controls.add_mapping(ctx.screenshot_control, input::key_mapping(nullptr, input::scancode::f12, false));
  901. ctx.window_controls.connect(ctx.app->get_device_manager().get_event_queue());
  902. // Set activation threshold for menu navigation controls to mitigate drifting gamepad axes
  903. auto menu_control_threshold = [](float x) -> bool
  904. {
  905. return x > 0.1f;
  906. };
  907. ctx.menu_up_control.set_threshold_function(menu_control_threshold);
  908. ctx.menu_down_control.set_threshold_function(menu_control_threshold);
  909. ctx.menu_left_control.set_threshold_function(menu_control_threshold);
  910. ctx.menu_right_control.set_threshold_function(menu_control_threshold);
  911. debug::log::trace("Loaded controls");
  912. }
  913. catch (...)
  914. {
  915. debug::log::error("Failed to load controls");
  916. }
  917. }
  918. void boot::setup_ui()
  919. {
  920. // Load font size config
  921. ctx.font_size = 1.0f;
  922. if (ctx.config->contains("font_size"))
  923. ctx.font_size = (*ctx.config)["font_size"].get<float>();
  924. // Load dyslexia font config
  925. ctx.dyslexia_font = false;
  926. if (ctx.config->contains("dyslexia_font"))
  927. ctx.dyslexia_font = (*ctx.config)["dyslexia_font"].get<bool>();
  928. // Load fonts
  929. debug::log::trace("Loading fonts...");
  930. try
  931. {
  932. game::load_fonts(ctx);
  933. debug::log::trace("Loaded fonts");
  934. }
  935. catch (...)
  936. {
  937. debug::log::error("Failed to load fonts");
  938. }
  939. // Setup UI resize handler
  940. ctx.ui_resize_subscription = ctx.app->get_window_resized_channel().subscribe
  941. (
  942. [&](const auto& event)
  943. {
  944. const float clip_left = static_cast<float>(event.viewport_width) * -0.5f;
  945. const float clip_right = static_cast<float>(event.viewport_width) * 0.5f;
  946. const float clip_top = static_cast<float>(event.viewport_height) * -0.5f;
  947. const float clip_bottom = static_cast<float>(event.viewport_height) * 0.5f;
  948. const float clip_near = ctx.ui_camera->get_clip_near();
  949. const float clip_far = ctx.ui_camera->get_clip_far();
  950. ctx.ui_camera->set_orthographic(clip_left, clip_right, clip_top, clip_bottom, clip_near, clip_far);
  951. }
  952. );
  953. }
  954. void boot::setup_debugging()
  955. {
  956. // Setup performance sampling
  957. ctx.performance_sampler.set_sample_size(15);
  958. ctx.cli = new debug::cli();
  959. //debug::log::info(ctx.cli->interpret("echo hi 123"));
  960. }
  961. void boot::setup_loop()
  962. {
  963. // Set update frequency
  964. if (ctx.config->contains("update_frequency"))
  965. {
  966. ctx.loop.set_update_frequency((*ctx.config)["update_frequency"].get<double>());
  967. }
  968. // Set update callback
  969. ctx.loop.set_update_callback
  970. (
  971. [&ctx = this->ctx](double t, double dt)
  972. {
  973. // Update tweens
  974. ctx.sky_pass->update_tweens();
  975. ctx.surface_scene->update_tweens();
  976. ctx.underground_scene->update_tweens();
  977. ctx.ui_scene->update_tweens();
  978. // Process events
  979. ctx.app->process_events();
  980. ctx.app->get_device_manager().get_event_queue().flush();
  981. // Process function queue
  982. while (!ctx.function_queue.empty())
  983. {
  984. ctx.function_queue.front()();
  985. ctx.function_queue.pop();
  986. }
  987. // Update processes
  988. std::for_each
  989. (
  990. std::execution::par,
  991. ctx.processes.begin(),
  992. ctx.processes.end(),
  993. [t, dt](const auto& process)
  994. {
  995. process.second(t, dt);
  996. }
  997. );
  998. // Advance timeline
  999. ctx.timeline->advance(dt);
  1000. // Update entity systems
  1001. ctx.terrain_system->update(t, dt);
  1002. //ctx.vegetation_system->update(t, dt);
  1003. ctx.subterrain_system->update(t, dt);
  1004. ctx.collision_system->update(t, dt);
  1005. ctx.behavior_system->update(t, dt);
  1006. ctx.steering_system->update(t, dt);
  1007. ctx.locomotion_system->update(t, dt);
  1008. ctx.camera_system->update(t, dt);
  1009. ctx.orbit_system->update(t, dt);
  1010. ctx.blackbody_system->update(t, dt);
  1011. ctx.atmosphere_system->update(t, dt);
  1012. ctx.astronomy_system->update(t, dt);
  1013. ctx.spring_system->update(t, dt);
  1014. ctx.spatial_system->update(t, dt);
  1015. ctx.constraint_system->update(t, dt);
  1016. ctx.animator->animate(dt);
  1017. ctx.render_system->update(t, dt);
  1018. }
  1019. );
  1020. // Set render callback
  1021. ctx.loop.set_render_callback
  1022. (
  1023. [&ctx = this->ctx](double alpha)
  1024. {
  1025. ctx.render_system->draw(alpha);
  1026. ctx.app->swap_buffers();
  1027. }
  1028. );
  1029. }
  1030. void boot::loop()
  1031. {
  1032. while (!ctx.app->was_closed())
  1033. {
  1034. // Execute main loop
  1035. ctx.loop.tick();
  1036. // Sample frame duration
  1037. ctx.performance_sampler.sample(ctx.loop.get_frame_duration());
  1038. }
  1039. // Exit all active game states
  1040. while (!ctx.state_machine.empty())
  1041. ctx.state_machine.pop();
  1042. }
  1043. void boot::shutdown_audio()
  1044. {
  1045. debug::log::trace("Shutting down audio...");
  1046. if (ctx.alc_context)
  1047. {
  1048. alcMakeContextCurrent(nullptr);
  1049. alcDestroyContext(ctx.alc_context);
  1050. }
  1051. if (ctx.alc_device)
  1052. {
  1053. alcCloseDevice(ctx.alc_device);
  1054. }
  1055. debug::log::trace("Shut down audio");
  1056. }
  1057. } // namespace state
  1058. } // namespace game