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

1230 lines
42 KiB

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