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

1333 lines
43 KiB

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