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

1184 lines
39 KiB

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