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

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