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

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