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

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