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

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