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

1127 lines
45 KiB

  1. /*
  2. * Copyright (C) 2020 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 "debug/cli.hpp"
  26. #include "game/console-commands.hpp"
  27. #include "debug/logger.hpp"
  28. #include "game/game-context.hpp"
  29. #include "rasterizer/framebuffer.hpp"
  30. #include "rasterizer/pixel-format.hpp"
  31. #include "rasterizer/pixel-type.hpp"
  32. #include "rasterizer/rasterizer.hpp"
  33. #include "rasterizer/texture-2d.hpp"
  34. #include "rasterizer/texture-filter.hpp"
  35. #include "rasterizer/texture-wrapping.hpp"
  36. #include "rasterizer/vertex-array.hpp"
  37. #include "rasterizer/vertex-attribute-type.hpp"
  38. #include "rasterizer/vertex-buffer.hpp"
  39. #include "renderer/material-flags.hpp"
  40. #include "renderer/material-property.hpp"
  41. #include "renderer/passes/bloom-pass.hpp"
  42. #include "renderer/passes/clear-pass.hpp"
  43. #include "renderer/passes/final-pass.hpp"
  44. #include "renderer/passes/material-pass.hpp"
  45. #include "renderer/passes/outline-pass.hpp"
  46. #include "renderer/passes/shadow-map-pass.hpp"
  47. #include "renderer/passes/sky-pass.hpp"
  48. #include "renderer/simple-render-pass.hpp"
  49. #include "renderer/vertex-attributes.hpp"
  50. #include "renderer/compositor.hpp"
  51. #include "renderer/renderer.hpp"
  52. #include "resources/config-file.hpp"
  53. #include "resources/resource-manager.hpp"
  54. #include "resources/resource-manager.hpp"
  55. #include "scene/billboard.hpp"
  56. #include "scene/model-instance.hpp"
  57. #include "scene/point-light.hpp"
  58. #include "scene/directional-light.hpp"
  59. #include "scene/ambient-light.hpp"
  60. #include "scene/spotlight.hpp"
  61. #include "game/states/game-states.hpp"
  62. #include "game/systems/behavior-system.hpp"
  63. #include "game/systems/camera-system.hpp"
  64. #include "game/systems/collision-system.hpp"
  65. #include "game/systems/constraint-system.hpp"
  66. #include "game/systems/control-system.hpp"
  67. #include "game/systems/locomotion-system.hpp"
  68. #include "game/systems/nest-system.hpp"
  69. #include "game/systems/snapping-system.hpp"
  70. #include "game/systems/render-system.hpp"
  71. #include "game/systems/samara-system.hpp"
  72. #include "game/systems/subterrain-system.hpp"
  73. #include "game/systems/terrain-system.hpp"
  74. #include "game/systems/tool-system.hpp"
  75. #include "game/systems/ui-system.hpp"
  76. #include "game/systems/vegetation-system.hpp"
  77. #include "game/entity-commands.hpp"
  78. #include "utility/paths.hpp"
  79. #include "event/event-dispatcher.hpp"
  80. #include "input/input-event-router.hpp"
  81. #include "input/input-mapper.hpp"
  82. #include "input/input-listener.hpp"
  83. #include "input/game-controller.hpp"
  84. #include "input/mouse.hpp"
  85. #include "input/keyboard.hpp"
  86. #include "pheromone-matrix.hpp"
  87. #include "configuration.hpp"
  88. #include "input/scancode.hpp"
  89. #include <cxxopts.hpp>
  90. #include <dirent.h>
  91. #include <entt/entt.hpp>
  92. #include <filesystem>
  93. #include <functional>
  94. #include <string>
  95. #include <vector>
  96. #include "utility/timestamp.hpp"
  97. static void parse_options(game_context* ctx, int argc, char** argv);
  98. static void setup_resources(game_context* ctx);
  99. static void load_config(game_context* ctx);
  100. static void load_strings(game_context* ctx);
  101. static void setup_window(game_context* ctx);
  102. static void setup_rendering(game_context* ctx);
  103. static void setup_scenes(game_context* ctx);
  104. static void setup_animation(game_context* ctx);
  105. static void setup_entities(game_context* ctx);
  106. static void setup_systems(game_context* ctx);
  107. static void setup_controls(game_context* ctx);
  108. static void setup_cli(game_context* ctx);
  109. static void setup_callbacks(game_context* ctx);
  110. int bootloader(application* app, int argc, char** argv)
  111. {
  112. // Get application logger
  113. logger* logger = app->get_logger();
  114. logger->push_task("Running application bootloader");
  115. // Allocate game context
  116. game_context* ctx = new game_context();
  117. ctx->app = app;
  118. ctx->logger = logger;
  119. // Init game context
  120. try
  121. {
  122. parse_options(ctx, argc, argv);
  123. setup_resources(ctx);
  124. load_config(ctx);
  125. load_strings(ctx);
  126. setup_window(ctx);
  127. setup_rendering(ctx);
  128. setup_scenes(ctx);
  129. setup_animation(ctx);
  130. setup_entities(ctx);
  131. setup_systems(ctx);
  132. setup_controls(ctx);
  133. setup_cli(ctx);
  134. setup_callbacks(ctx);
  135. }
  136. catch (const std::exception& e)
  137. {
  138. logger->error("Caught exception: \"" + std::string(e.what()) + "\"");
  139. logger->pop_task(EXIT_FAILURE);
  140. return EXIT_FAILURE;
  141. }
  142. logger->pop_task(EXIT_SUCCESS);
  143. // Change state
  144. if (ctx->option_quick_start.has_value())
  145. app->change_state({std::bind(play_state_enter, ctx), std::bind(play_state_exit, ctx)});
  146. else
  147. app->change_state({std::bind(splash_state_enter, ctx), std::bind(splash_state_exit, ctx)});
  148. return EXIT_SUCCESS;
  149. }
  150. void parse_options(game_context* ctx, int argc, char** argv)
  151. {
  152. logger* logger = ctx->logger;
  153. logger->push_task("Parsing command line options");
  154. try
  155. {
  156. cxxopts::Options options("Antkeeper", "Ant colony simulation game");
  157. options.add_options()
  158. ("c,continue", "Continues from the last save")
  159. ("d,data", "Sets the data package path", cxxopts::value<std::string>())
  160. ("f,fullscreen", "Starts in fullscreen mode")
  161. ("n,new-game", "Starts a new game")
  162. ("q,quick-start", "Skips to the main menu")
  163. ("r,reset", "Restores all settings to default")
  164. ("v,vsync", "Enables or disables v-sync", cxxopts::value<int>())
  165. ("w,windowed", "Starts in windowed mode");
  166. auto result = options.parse(argc, argv);
  167. // --continue
  168. if (result.count("continue"))
  169. ctx->option_continue = true;
  170. // --data
  171. if (result.count("data"))
  172. ctx->option_data = result["data"].as<std::string>();
  173. // --fullscreen
  174. if (result.count("fullscreen"))
  175. ctx->option_fullscreen = true;
  176. // --new-game
  177. if (result.count("new-game"))
  178. ctx->option_new_game = true;
  179. // --quick-start
  180. if (result.count("quick-start"))
  181. ctx->option_quick_start = true;
  182. // --reset
  183. if (result.count("reset"))
  184. ctx->option_reset = true;
  185. // --vsync
  186. if (result.count("vsync"))
  187. ctx->option_vsync = (result["vsync"].as<int>()) ? true : false;
  188. // --windowed
  189. if (result.count("windowed"))
  190. ctx->option_windowed = true;
  191. }
  192. catch (const std::exception& e)
  193. {
  194. logger->error("Exception caught: \"" + std::string(e.what()) + "\"");
  195. logger->pop_task(EXIT_FAILURE);
  196. return;
  197. }
  198. logger->pop_task(EXIT_SUCCESS);
  199. }
  200. void setup_resources(game_context* ctx)
  201. {
  202. logger* logger = ctx->logger;
  203. // Setup resource manager
  204. ctx->resource_manager = new resource_manager(logger);
  205. // Determine application name
  206. std::string application_name;
  207. #if defined(_WIN32) || defined(__APPLE__)
  208. application_name = "Antkeeper";
  209. #else
  210. application_name = "antkeeper";
  211. #endif
  212. // Detect paths
  213. ctx->data_path = get_data_path(application_name);
  214. ctx->config_path = get_config_path(application_name);
  215. ctx->mods_path = ctx->config_path + "mods/";
  216. ctx->saves_path = ctx->config_path + "saves/";
  217. ctx->screenshots_path = ctx->config_path + "screenshots/";
  218. // Log resource paths
  219. logger->log("Detected data path as \"" + ctx->data_path + "\"");
  220. logger->log("Detected config path as \"" + ctx->config_path + "\"");
  221. // Create nonexistent config directories
  222. std::vector<std::string> config_paths;
  223. config_paths.push_back(ctx->config_path);
  224. config_paths.push_back(ctx->mods_path);
  225. config_paths.push_back(ctx->saves_path);
  226. config_paths.push_back(ctx->screenshots_path);
  227. for (const std::string& path: config_paths)
  228. {
  229. if (!path_exists(path))
  230. {
  231. logger->push_task("Creating directory \"" + path + "\"");
  232. if (create_directory(path))
  233. {
  234. logger->pop_task(EXIT_SUCCESS);
  235. }
  236. else
  237. {
  238. logger->pop_task(EXIT_FAILURE);
  239. }
  240. }
  241. }
  242. // Redirect logger output to log file on non-debug builds
  243. #if defined(NDEBUG)
  244. std::string log_filename = config_path + "log.txt";
  245. ctx->log_filestream.open(log_filename.c_str());
  246. ctx->log_filestream << logger->get_history();
  247. logger->redirect(&log_filestream);
  248. #endif
  249. // Scan for mods
  250. std::vector<std::string> mods;
  251. struct dirent** files = nullptr;
  252. if (int n = scandir(ctx->mods_path.c_str(), &files, NULL, alphasort); n >= 0)
  253. {
  254. for (int i = 0; i < n; ++i)
  255. {
  256. struct dirent* file = files[i];
  257. switch (file->d_type)
  258. {
  259. case DT_REG:
  260. case DT_DIR:
  261. {
  262. std::string mod_name = file->d_name;
  263. // Skip hidden files and directories
  264. if (mod_name.front() == '.')
  265. break;
  266. mods.push_back(mod_name);
  267. }
  268. default:
  269. break;
  270. }
  271. }
  272. }
  273. // Determine data package path
  274. if (ctx->option_data.has_value())
  275. {
  276. ctx->data_package_path = ctx->option_data.value();
  277. if (std::filesystem::path(ctx->data_package_path).is_relative())
  278. ctx->data_package_path = ctx->data_path + ctx->data_package_path;
  279. }
  280. else
  281. {
  282. ctx->data_package_path = ctx->data_path + "data.zip";
  283. }
  284. // Mount mods
  285. for (const std::string& mod_name: mods)
  286. ctx->resource_manager->mount(ctx->mods_path + mod_name);
  287. // Mount config path
  288. ctx->resource_manager->mount(ctx->config_path);
  289. // Mount data package
  290. ctx->resource_manager->mount(ctx->data_package_path);
  291. // Include resource search paths in order of priority
  292. ctx->resource_manager->include("/shaders/");
  293. ctx->resource_manager->include("/models/");
  294. ctx->resource_manager->include("/textures/");
  295. ctx->resource_manager->include("/materials/");
  296. ctx->resource_manager->include("/entities/");
  297. ctx->resource_manager->include("/behaviors/");
  298. ctx->resource_manager->include("/controls/");
  299. ctx->resource_manager->include("/localization/");
  300. ctx->resource_manager->include("/");
  301. }
  302. void load_config(game_context* ctx)
  303. {
  304. logger* logger = ctx->logger;
  305. logger->push_task("Loading config");
  306. // Load config file
  307. ctx->config = ctx->resource_manager->load<config_file>("config.txt");
  308. if (!ctx->config)
  309. {
  310. logger->pop_task(EXIT_FAILURE);
  311. return;
  312. }
  313. logger->pop_task(EXIT_SUCCESS);
  314. }
  315. void load_strings(game_context* ctx)
  316. {
  317. logger* logger = ctx->logger;
  318. logger->push_task("Loading strings");
  319. ctx->string_table = ctx->resource_manager->load<string_table>("strings.csv");
  320. build_string_table_map(&ctx->string_table_map, *ctx->string_table);
  321. ctx->language_code = ctx->config->get<std::string>("language");
  322. ctx->language_index = -1;
  323. for (int i = 2; i < (*ctx->string_table)[0].size(); ++i)
  324. {
  325. if ((*ctx->string_table)[0][i] == ctx->language_code)
  326. ctx->language_index = i;
  327. }
  328. logger->log("lang index: " + std::to_string(ctx->language_index));
  329. ctx->strings = &ctx->string_table_map[ctx->language_code];
  330. logger->pop_task(EXIT_SUCCESS);
  331. }
  332. void setup_window(game_context* ctx)
  333. {
  334. logger* logger = ctx->logger;
  335. logger->push_task("Setting up window");
  336. application* app = ctx->app;
  337. config_file* config = ctx->config;
  338. // Set fullscreen or windowed mode
  339. bool fullscreen = true;
  340. if (ctx->option_fullscreen.has_value())
  341. fullscreen = true;
  342. else if (ctx->option_windowed.has_value())
  343. fullscreen = false;
  344. else if (config->has("fullscreen"))
  345. fullscreen = (config->get<int>("fullscreen") != 0);
  346. app->set_fullscreen(fullscreen);
  347. // Set resolution
  348. const auto& display_dimensions = ctx->app->get_display_dimensions();
  349. int2 resolution = {display_dimensions[0], display_dimensions[1]};
  350. if (fullscreen)
  351. {
  352. if (config->has("fullscreen_resolution"))
  353. resolution = config->get<int2>("fullscreen_resolution");
  354. }
  355. else
  356. {
  357. if (config->has("windowed_resolution"))
  358. resolution = config->get<int2>("windowed_resolution");
  359. }
  360. app->resize_window(resolution.x, resolution.y);
  361. // Set v-sync
  362. bool vsync = true;
  363. if (ctx->option_vsync.has_value())
  364. vsync = (ctx->option_vsync.value() != 0);
  365. else if (config->has("vsync"))
  366. vsync = (config->get<int>("vsync") != 0);
  367. app->set_vsync(vsync);
  368. // Set title
  369. app->set_title((*ctx->strings)["title"]);
  370. logger->pop_task(EXIT_SUCCESS);
  371. }
  372. void setup_rendering(game_context* ctx)
  373. {
  374. logger* logger = ctx->logger;
  375. logger->push_task("Setting up rendering");
  376. // Get rasterizer from application
  377. ctx->rasterizer = ctx->app->get_rasterizer();
  378. // Get default framebuffer
  379. const framebuffer& default_framebuffer = ctx->rasterizer->get_default_framebuffer();
  380. const auto& viewport_dimensions = default_framebuffer.get_dimensions();
  381. // Create HDR framebuffer (32F color, 32F depth)
  382. ctx->framebuffer_hdr_color = new texture_2d(viewport_dimensions[0], viewport_dimensions[1], pixel_type::float_32, pixel_format::rgb);
  383. ctx->framebuffer_hdr_color->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  384. ctx->framebuffer_hdr_color->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
  385. ctx->framebuffer_hdr_color->set_max_anisotropy(0.0f);
  386. ctx->framebuffer_hdr_depth = new texture_2d(viewport_dimensions[0], viewport_dimensions[1], pixel_type::uint_32, pixel_format::ds);
  387. ctx->framebuffer_hdr_depth->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  388. ctx->framebuffer_hdr_depth->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
  389. ctx->framebuffer_hdr_depth->set_max_anisotropy(0.0f);
  390. ctx->framebuffer_hdr = new framebuffer(viewport_dimensions[0], viewport_dimensions[1]);
  391. ctx->framebuffer_hdr->attach(framebuffer_attachment_type::color, ctx->framebuffer_hdr_color);
  392. ctx->framebuffer_hdr->attach(framebuffer_attachment_type::depth, ctx->framebuffer_hdr_depth);
  393. ctx->framebuffer_hdr->attach(framebuffer_attachment_type::stencil, ctx->framebuffer_hdr_depth);
  394. // Create shadow map framebuffer
  395. int shadow_map_resolution = ctx->config->get<int>("shadow_map_resolution");
  396. ctx->shadow_map_depth_texture = new texture_2d(shadow_map_resolution, shadow_map_resolution, pixel_type::float_32, pixel_format::d);
  397. ctx->shadow_map_depth_texture->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  398. ctx->shadow_map_depth_texture->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
  399. ctx->shadow_map_depth_texture->set_max_anisotropy(0.0f);
  400. ctx->shadow_map_framebuffer = new framebuffer(shadow_map_resolution, shadow_map_resolution);
  401. ctx->shadow_map_framebuffer->attach(framebuffer_attachment_type::depth, ctx->shadow_map_depth_texture);
  402. // Create bloom pingpong framebuffers (16F color, no depth)
  403. int bloom_width = viewport_dimensions[0] >> 1;
  404. int bloom_height = viewport_dimensions[1] >> 1;
  405. ctx->bloom_texture = new texture_2d(bloom_width, bloom_height, pixel_type::float_16, pixel_format::rgb);
  406. ctx->bloom_texture->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  407. ctx->bloom_texture->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
  408. ctx->bloom_texture->set_max_anisotropy(0.0f);
  409. ctx->framebuffer_bloom = new framebuffer(bloom_width, bloom_height);
  410. ctx->framebuffer_bloom->attach(framebuffer_attachment_type::color, ctx->bloom_texture);
  411. // Load fallback material
  412. ctx->fallback_material = ctx->resource_manager->load<material>("fallback.mtl");
  413. // Setup overworld compositor
  414. ctx->overworld_shadow_map_clear_pass = new clear_pass(ctx->rasterizer, ctx->shadow_map_framebuffer);
  415. ctx->overworld_shadow_map_clear_pass->set_cleared_buffers(false, true, false);
  416. ctx->overworld_shadow_map_pass = new shadow_map_pass(ctx->rasterizer, ctx->shadow_map_framebuffer, ctx->resource_manager);
  417. ctx->overworld_shadow_map_pass->set_split_scheme_weight(0.75f);
  418. ctx->overworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  419. ctx->overworld_clear_pass->set_cleared_buffers(false, true, true);
  420. ctx->overworld_sky_pass = new sky_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  421. ctx->overworld_sky_pass->set_enabled(false);
  422. ctx->overworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  423. ctx->overworld_material_pass->set_fallback_material(ctx->fallback_material);
  424. ctx->overworld_material_pass->shadow_map_pass = ctx->overworld_shadow_map_pass;
  425. ctx->overworld_material_pass->shadow_map = ctx->shadow_map_depth_texture;
  426. ctx->overworld_outline_pass = new outline_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  427. ctx->overworld_outline_pass->set_outline_width(0.25f);
  428. ctx->overworld_outline_pass->set_outline_color(float4{1.0f, 1.0f, 1.0f, 1.0f});
  429. ctx->overworld_bloom_pass = new bloom_pass(ctx->rasterizer, ctx->framebuffer_bloom, ctx->resource_manager);
  430. ctx->overworld_bloom_pass->set_source_texture(ctx->framebuffer_hdr_color);
  431. ctx->overworld_bloom_pass->set_brightness_threshold(1.0f);
  432. ctx->overworld_bloom_pass->set_blur_iterations(5);
  433. ctx->overworld_bloom_pass->set_enabled(true);
  434. ctx->overworld_final_pass = new ::final_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  435. ctx->overworld_final_pass->set_color_texture(ctx->framebuffer_hdr_color);
  436. ctx->overworld_final_pass->set_bloom_texture(ctx->bloom_texture);
  437. ctx->overworld_compositor = new compositor();
  438. ctx->overworld_compositor->add_pass(ctx->overworld_shadow_map_clear_pass);
  439. ctx->overworld_compositor->add_pass(ctx->overworld_shadow_map_pass);
  440. ctx->overworld_compositor->add_pass(ctx->overworld_clear_pass);
  441. ctx->overworld_compositor->add_pass(ctx->overworld_sky_pass);
  442. ctx->overworld_compositor->add_pass(ctx->overworld_material_pass);
  443. ctx->overworld_compositor->add_pass(ctx->overworld_outline_pass);
  444. ctx->overworld_compositor->add_pass(ctx->overworld_bloom_pass);
  445. ctx->overworld_compositor->add_pass(ctx->overworld_final_pass);
  446. // Setup underworld compositor
  447. ctx->underworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  448. ctx->underworld_clear_pass->set_cleared_buffers(true, true, false);
  449. ctx->underworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  450. ctx->underworld_material_pass->set_fallback_material(ctx->fallback_material);
  451. shader_program* underworld_final_shader = ctx->resource_manager->load<shader_program>("underground-final.glsl");
  452. ctx->underworld_final_pass = new simple_render_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), underworld_final_shader);
  453. ctx->underground_color_texture_property = ctx->underworld_final_pass->get_material()->add_property<const texture_2d*>("color_texture");
  454. ctx->underground_color_texture_property->set_value(ctx->framebuffer_hdr_color);
  455. ctx->underworld_final_pass->get_material()->update_tweens();
  456. ctx->underworld_compositor = new compositor();
  457. ctx->underworld_compositor->add_pass(ctx->underworld_clear_pass);
  458. ctx->underworld_compositor->add_pass(ctx->underworld_material_pass);
  459. ctx->underworld_compositor->add_pass(ctx->underworld_final_pass);
  460. // Setup UI camera compositor
  461. ctx->ui_clear_pass = new clear_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer());
  462. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  463. ctx->ui_material_pass = new material_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  464. ctx->ui_material_pass->set_fallback_material(ctx->fallback_material);
  465. ctx->ui_compositor = new compositor();
  466. ctx->ui_compositor->add_pass(ctx->ui_clear_pass);
  467. ctx->ui_compositor->add_pass(ctx->ui_material_pass);
  468. // Create billboard VAO
  469. {
  470. const float billboard_vertex_data[] =
  471. {
  472. -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  473. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  474. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  475. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  476. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  477. 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
  478. };
  479. std::size_t billboard_vertex_size = 8;
  480. std::size_t billboard_vertex_stride = sizeof(float) * billboard_vertex_size;
  481. std::size_t billboard_vertex_count = 6;
  482. ctx->billboard_vbo = new vertex_buffer(sizeof(float) * billboard_vertex_size * billboard_vertex_count, billboard_vertex_data);
  483. ctx->billboard_vao = new vertex_array();
  484. ctx->billboard_vao->bind_attribute(VERTEX_POSITION_LOCATION, *ctx->billboard_vbo, 3, vertex_attribute_type::float_32, billboard_vertex_stride, 0);
  485. ctx->billboard_vao->bind_attribute(VERTEX_TEXCOORD_LOCATION, *ctx->billboard_vbo, 2, vertex_attribute_type::float_32, billboard_vertex_stride, sizeof(float) * 3);
  486. ctx->billboard_vao->bind_attribute(VERTEX_BARYCENTRIC_LOCATION, *ctx->billboard_vbo, 3, vertex_attribute_type::float_32, billboard_vertex_stride, sizeof(float) * 5);
  487. }
  488. // Create renderer
  489. ctx->renderer = new renderer();
  490. ctx->renderer->set_billboard_vao(ctx->billboard_vao);
  491. logger->pop_task(EXIT_SUCCESS);
  492. }
  493. void setup_scenes(game_context* ctx)
  494. {
  495. logger* logger = ctx->logger;
  496. logger->push_task("Setting up rendering");
  497. // Get default framebuffer
  498. const auto& viewport_dimensions = ctx->rasterizer->get_default_framebuffer().get_dimensions();
  499. float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
  500. // Create infinite culling mask
  501. float inf = std::numeric_limits<float>::infinity();
  502. ctx->no_cull = {{-inf, -inf, -inf}, {inf, inf, inf}};
  503. // Setup overworld camera
  504. ctx->overworld_camera = new camera();
  505. ctx->overworld_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  506. ctx->overworld_camera->set_compositor(ctx->overworld_compositor);
  507. ctx->overworld_camera->set_composite_index(0);
  508. ctx->overworld_camera->set_active(true);
  509. // Setup underworld camera
  510. ctx->underworld_camera = new camera();
  511. ctx->underworld_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  512. ctx->underworld_camera->look_at({0, 50, 0}, {0, 0, 0}, {0, 0, -1});
  513. ctx->underworld_camera->set_compositor(ctx->underworld_compositor);
  514. ctx->underworld_camera->set_composite_index(0);
  515. ctx->underworld_camera->set_active(false);
  516. // Setup UI camera
  517. ctx->ui_camera = new camera();
  518. ctx->ui_camera->set_compositor(ctx->ui_compositor);
  519. // Setup lights
  520. ctx->sun_indirect = new ambient_light();
  521. ctx->sun_indirect->set_intensity(0.25f);
  522. ctx->sun_indirect->update_tweens();
  523. ctx->sun_direct = new directional_light();
  524. ctx->sun_direct->look_at({-1.0f, 5.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f});
  525. ctx->sun_direct->set_intensity(1.0f);
  526. ctx->sun_direct->update_tweens();
  527. ctx->subterrain_light = new point_light();
  528. ctx->subterrain_light->set_color({1, 1, 1});
  529. ctx->subterrain_light->set_intensity(1.0f);
  530. ctx->subterrain_light->set_attenuation({1.0f, 0.09f, 0.032f});
  531. ctx->subterrain_light->update_tweens();
  532. ctx->underworld_ambient_light = new ambient_light();
  533. ctx->underworld_ambient_light->set_color({1, 1, 1});
  534. ctx->underworld_ambient_light->set_intensity(0.1f);
  535. ctx->underworld_ambient_light->update_tweens();
  536. ctx->lens_spotlight = new spotlight();
  537. ctx->lens_spotlight->set_color({1, 1, 1});
  538. ctx->lens_spotlight->set_intensity(20.0f);
  539. ctx->lens_spotlight->set_attenuation({1.0f, 0.0f, 0.0f});
  540. ctx->lens_spotlight->set_cutoff({math::radians(1.25f), math::radians(1.8f)});
  541. ctx->flashlight_spotlight = new spotlight();
  542. ctx->flashlight_spotlight->set_color({1, 1, 1});
  543. ctx->flashlight_spotlight->set_intensity(1.0f);
  544. ctx->flashlight_spotlight->set_attenuation({1.0f, 0.0f, 0.0f});
  545. ctx->flashlight_spotlight->set_cutoff({math::radians(10.0f), math::radians(19.0f)});
  546. const texture_2d* splash_texture = ctx->resource_manager->load<texture_2d>("splash.png");
  547. auto splash_dimensions = splash_texture->get_dimensions();
  548. ctx->splash_billboard_material = new material();
  549. ctx->splash_billboard_material->set_shader_program(ctx->resource_manager->load<shader_program>("ui-element-textured.glsl"));
  550. ctx->splash_billboard_material->add_property<const texture_2d*>("background")->set_value(splash_texture);
  551. ctx->splash_billboard_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  552. ctx->splash_billboard_material->update_tweens();
  553. ctx->splash_billboard = new billboard();
  554. ctx->splash_billboard->set_material(ctx->splash_billboard_material);
  555. ctx->splash_billboard->set_scale({(float)std::get<0>(splash_dimensions) * 0.5f, (float)std::get<1>(splash_dimensions) * 0.5f, 1.0f});
  556. ctx->splash_billboard->set_translation({0.0f, 0.0f, 0.0f});
  557. ctx->splash_billboard->update_tweens();
  558. material* billboard_material = new material();
  559. billboard_material->set_shader_program(ctx->resource_manager->load<shader_program>("ui-element-textured.glsl"));
  560. billboard_material->add_property<const texture_2d*>("background")->set_value(ctx->resource_manager->load<texture_2d>("arrow.png"));
  561. billboard_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  562. billboard_material->set_flags(MATERIAL_FLAG_TRANSLUCENT | MATERIAL_FLAG_NOT_SHADOW_CASTER);
  563. billboard* arrow_billboard = new billboard();
  564. arrow_billboard->set_material(billboard_material);
  565. arrow_billboard->set_scale(float3{1, 1, 1} * 2.0f);
  566. arrow_billboard->set_translation({0, 10, 0});
  567. arrow_billboard->set_billboard_type(billboard_type::cylindrical);
  568. arrow_billboard->set_alignment_axis({0, 1, 0});
  569. arrow_billboard->update_tweens();
  570. billboard_material = new material();
  571. billboard_material->set_shader_program(ctx->resource_manager->load<shader_program>("portal-card.glsl"));
  572. billboard_material->add_property<float4>("color")->set_value(float4{1, 1, 1, 1});
  573. billboard_material->add_property<float2>("range")->set_value(float2{50.0f, 500.0f});
  574. billboard_material->set_flags(MATERIAL_FLAG_TRANSLUCENT | MATERIAL_FLAG_NOT_SHADOW_CASTER);
  575. billboard* portal_billboard = new billboard();
  576. portal_billboard->set_material(billboard_material);
  577. portal_billboard->set_scale(float3{1, 1, 1} * 10.0f);
  578. portal_billboard->set_translation({0.0f, 0, 0});
  579. portal_billboard->set_billboard_type(billboard_type::spherical);
  580. portal_billboard->set_alignment_axis({0, 1, 0});
  581. portal_billboard->update_tweens();
  582. // Create depth debug billboard
  583. /*
  584. material* depth_debug_material = new material();
  585. depth_debug_material->set_shader_program(ctx->resource_manager->load<shader_program>("ui-element-textured.glsl"));
  586. depth_debug_material->add_property<const texture_2d*>("background")->set_value(shadow_map_depth_texture);
  587. depth_debug_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  588. billboard* depth_debug_billboard = new billboard();
  589. depth_debug_billboard->set_material(depth_debug_material);
  590. depth_debug_billboard->set_scale({128, 128, 1});
  591. depth_debug_billboard->set_translation({-960 + 128, 1080 * 0.5f - 128, 0});
  592. depth_debug_billboard->update_tweens();
  593. ui_system->get_scene()->add_object(depth_debug_billboard);
  594. */
  595. // Setup overworld scene
  596. ctx->overworld_scene = new scene();
  597. ctx->overworld_scene->add_object(ctx->overworld_camera);
  598. ctx->overworld_scene->add_object(ctx->sun_indirect);
  599. ctx->overworld_scene->add_object(ctx->sun_direct);
  600. //ctx->overworld_scene->add_object(ctx->spotlight);
  601. ctx->overworld_scene->add_object(arrow_billboard);
  602. // Setup underworld scene
  603. ctx->underworld_scene = new scene();
  604. ctx->underworld_scene->add_object(ctx->underworld_camera);
  605. ctx->underworld_scene->add_object(ctx->underworld_ambient_light);
  606. //ctx->underworld_scene->add_object(ctx->lantern);
  607. //ctx->underworld_scene->add_object(ctx->subterrain_light);
  608. //ctx->underworld_scene->add_object(ctx->portal_billboard);
  609. //model_instance* larva = new model_instance(ctx->resource_manager->load<model>("larva.obj"));
  610. //ctx->underworld_scene->add_object(larva);
  611. // Setup UI scene
  612. ctx->ui_scene = new scene();
  613. ctx->ui_scene->add_object(ctx->ui_camera);
  614. ctx->overworld_scene->add_object(ctx->lens_spotlight);
  615. ctx->underworld_scene->add_object(ctx->flashlight_spotlight);
  616. // Set overworld as active scene
  617. ctx->active_scene = ctx->overworld_scene;
  618. logger->pop_task(EXIT_SUCCESS);
  619. }
  620. void setup_animation(game_context* ctx)
  621. {
  622. // Setup timeline system
  623. ctx->timeline = new timeline();
  624. ctx->timeline->set_autoremove(true);
  625. // Setup animator
  626. ctx->animator = new animator();
  627. // Initialize time tween
  628. ctx->time_tween = new tween<double>(0.0);
  629. ctx->time_tween->set_interpolator(math::lerp<double, double>);
  630. // Create fade transition
  631. ctx->fade_transition = new screen_transition();
  632. ctx->fade_transition->get_material()->set_shader_program(ctx->resource_manager->load<shader_program>("fade-transition.glsl"));
  633. ctx->ui_scene->add_object(ctx->fade_transition->get_billboard());
  634. ctx->animator->add_animation(ctx->fade_transition->get_animation());
  635. // Create inner radial transition
  636. ctx->radial_transition_inner = new screen_transition();
  637. ctx->radial_transition_inner->get_material()->set_shader_program(ctx->resource_manager->load<shader_program>("radial-transition-inner.glsl"));
  638. ctx->ui_scene->add_object(ctx->radial_transition_inner->get_billboard());
  639. ctx->animator->add_animation(ctx->radial_transition_inner->get_animation());
  640. // Create outer radial transition
  641. ctx->radial_transition_outer = new screen_transition();
  642. ctx->radial_transition_outer->get_material()->set_shader_program(ctx->resource_manager->load<shader_program>("radial-transition-outer.glsl"));
  643. ctx->ui_scene->add_object(ctx->radial_transition_outer->get_billboard());
  644. ctx->animator->add_animation(ctx->radial_transition_outer->get_animation());
  645. // Setup tweens
  646. ctx->focal_point_tween = new tween<float3>();
  647. ctx->focal_point_tween->set_interpolator(math::lerp<float3, float>);
  648. // Set material pass tweens
  649. ctx->overworld_material_pass->set_time_tween(ctx->time_tween);
  650. ctx->overworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  651. ctx->underworld_material_pass->set_time_tween(ctx->time_tween);
  652. ctx->underworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  653. ctx->underworld_final_pass->set_time_tween(ctx->time_tween);
  654. ctx->underworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  655. ctx->ui_material_pass->set_time_tween(ctx->time_tween);
  656. }
  657. void setup_entities(game_context* ctx)
  658. {
  659. // Create ECS registry
  660. ctx->ecs_registry = new entt::registry();
  661. // Reserve named entities
  662. ctx->brush_entity = ctx->ecs_registry->create();
  663. ctx->flashlight_entity = ctx->ecs_registry->create();
  664. ctx->forceps_entity = ctx->ecs_registry->create();
  665. ctx->lens_entity = ctx->ecs_registry->create();
  666. ctx->focal_point_entity = ctx->ecs_registry->create();
  667. }
  668. void setup_systems(game_context* ctx)
  669. {
  670. const auto& viewport_dimensions = ctx->app->get_viewport_dimensions();
  671. float4 viewport = {0.0f, 0.0f, static_cast<float>(viewport_dimensions[0]), static_cast<float>(viewport_dimensions[1])};
  672. // Setup terrain system
  673. ctx->terrain_system = new ::terrain_system(*ctx->ecs_registry, ctx->resource_manager);
  674. ctx->terrain_system->set_patch_size(TERRAIN_PATCH_SIZE);
  675. // Setup vegetation system
  676. ctx->vegetation_system = new ::vegetation_system(*ctx->ecs_registry);
  677. ctx->vegetation_system->set_terrain_patch_size(TERRAIN_PATCH_SIZE);
  678. ctx->vegetation_system->set_vegetation_patch_resolution(VEGETATION_PATCH_RESOLUTION);
  679. ctx->vegetation_system->set_vegetation_density(1.0f);
  680. ctx->vegetation_system->set_vegetation_model(ctx->resource_manager->load<model>("grass-tuft.obj"));
  681. ctx->vegetation_system->set_scene(ctx->overworld_scene);
  682. // Setup camera system
  683. ctx->camera_system = new camera_system(*ctx->ecs_registry);
  684. ctx->camera_system->set_viewport(viewport);
  685. // Setup tool system
  686. ctx->tool_system = new tool_system(*ctx->ecs_registry);
  687. ctx->tool_system->set_camera(ctx->overworld_camera);
  688. ctx->tool_system->set_orbit_cam(ctx->camera_system->get_orbit_cam());
  689. ctx->tool_system->set_viewport(viewport);
  690. // Setup subterrain system
  691. ctx->subterrain_system = new ::subterrain_system(*ctx->ecs_registry, ctx->resource_manager);
  692. ctx->subterrain_system->set_scene(ctx->underworld_scene);
  693. // Setup nest system
  694. ctx->nest_system = new nest_system(*ctx->ecs_registry, ctx->resource_manager);
  695. // Setup collision system
  696. ctx->collision_system = new collision_system(*ctx->ecs_registry);
  697. // Setup samara system
  698. ctx->samara_system = new samara_system(*ctx->ecs_registry);
  699. // Setup snapping system
  700. ctx->snapping_system = new snapping_system(*ctx->ecs_registry);
  701. // Setup behavior system
  702. ctx->behavior_system = new behavior_system(*ctx->ecs_registry);
  703. // Setup locomotion system
  704. ctx->locomotion_system = new locomotion_system(*ctx->ecs_registry);
  705. ctx->constraint_system = new constraint_system(*ctx->ecs_registry);
  706. // Setup pheromone system
  707. ctx->pheromones = new pheromone_matrix();
  708. ctx->pheromones->rows = 256;
  709. ctx->pheromones->columns = 256;
  710. ctx->pheromones->buffers = new float*[2];
  711. ctx->pheromones->buffers[0] = new float[ctx->pheromones->rows * ctx->pheromones->columns];
  712. ctx->pheromones->buffers[1] = new float[ctx->pheromones->rows * ctx->pheromones->columns];
  713. ctx->pheromones->current = 0;
  714. //diffuse(ctx->pheromones);
  715. // Setup render system
  716. ctx->render_system = new ::render_system(*ctx->ecs_registry);
  717. ctx->render_system->add_layer(ctx->overworld_scene);
  718. ctx->render_system->add_layer(ctx->underworld_scene);
  719. ctx->render_system->add_layer(ctx->ui_scene);
  720. ctx->render_system->set_renderer(ctx->renderer);
  721. // Setup control system
  722. ctx->control_system = new ::control_system(*ctx->ecs_registry);
  723. ctx->control_system->set_viewport(viewport);
  724. ctx->control_system->set_underworld_camera(ctx->underworld_camera);
  725. ctx->control_system->set_tool(nullptr);
  726. //ctx->control_system->set_flashlight(flashlight, flashlight_light_cone);
  727. ctx->control_system->get_adjust_camera_control()->set_activated_callback([ctx](){ ctx->app->set_relative_mouse_mode(true); ctx->tool_system->set_pick(false); });
  728. ctx->control_system->get_adjust_camera_control()->set_deactivated_callback([ctx](){ ctx->app->set_relative_mouse_mode(false); ctx->tool_system->set_pick(true); });
  729. ctx->control_system->set_flashlight(ctx->flashlight_entity);
  730. ctx->control_system->set_camera_subject(ctx->focal_point_entity);
  731. ctx->control_system->set_camera_system(ctx->camera_system);
  732. // Setup UI system
  733. ctx->ui_system = new ui_system(ctx->resource_manager);
  734. ctx->ui_system->set_camera(ctx->ui_camera);
  735. ctx->ui_system->set_scene(ctx->ui_scene);
  736. ctx->ui_system->set_viewport(viewport);
  737. ctx->ui_system->set_tool_menu_control(ctx->control_system->get_tool_menu_control());
  738. }
  739. void setup_controls(game_context* ctx)
  740. {
  741. event_dispatcher* event_dispatcher = ctx->app->get_event_dispatcher();
  742. // Setup input event routing
  743. ctx->input_event_router = new input_event_router();
  744. ctx->input_event_router->set_event_dispatcher(event_dispatcher);
  745. // Setup input mapper
  746. ctx->input_mapper = new input_mapper();
  747. ctx->input_mapper->set_event_dispatcher(event_dispatcher);
  748. // Setup input listener
  749. ctx->input_listener = new input_listener();
  750. ctx->input_listener->set_event_dispatcher(event_dispatcher);
  751. // Create toggle fullscreen control
  752. ctx->toggle_fullscreen_control = new control();
  753. ctx->toggle_fullscreen_control->set_activated_callback
  754. (
  755. [ctx]()
  756. {
  757. bool fullscreen = !ctx->app->is_fullscreen();
  758. ctx->app->set_fullscreen(fullscreen);
  759. if (!fullscreen)
  760. {
  761. int2 resolution = ctx->config->get<int2>("windowed_resolution");
  762. ctx->app->resize_window(resolution.x, resolution.y);
  763. }
  764. ctx->config->set<int>("fullscreen", (fullscreen) ? 1 : 0);
  765. }
  766. );
  767. // Create screenshot control
  768. ctx->screenshot_control = new control();
  769. ctx->screenshot_control->set_activated_callback
  770. (
  771. [ctx]()
  772. {
  773. std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png";
  774. ctx->app->save_frame(path);
  775. }
  776. );
  777. // Create rotation controls
  778. ctx->rotate_ccw_control = new control();
  779. ctx->rotate_ccw_control->set_activated_callback
  780. (
  781. std::bind(&camera_system::pan, ctx->camera_system, math::radians(90.0f))
  782. );
  783. ctx->rotate_cw_control = new control();
  784. ctx->rotate_cw_control->set_activated_callback
  785. (
  786. std::bind(&camera_system::pan, ctx->camera_system, math::radians(-90.0f))
  787. );
  788. // Create menu back control
  789. ctx->menu_back_control = new control();
  790. ctx->menu_back_control->set_activated_callback
  791. (
  792. std::bind(&application::close, ctx->app, 0)
  793. );
  794. // Create menu select control
  795. ctx->menu_select_control = new control();
  796. // Create application control set
  797. ctx->application_controls = new control_set();
  798. ctx->application_controls->add_control(ctx->toggle_fullscreen_control);
  799. ctx->application_controls->add_control(ctx->screenshot_control);
  800. ctx->application_controls->add_control(ctx->rotate_ccw_control);
  801. ctx->application_controls->add_control(ctx->rotate_cw_control);
  802. // Create menu control set
  803. ctx->menu_controls = new control_set();
  804. ctx->menu_controls->add_control(ctx->menu_back_control);
  805. ctx->menu_controls->add_control(ctx->menu_select_control);
  806. ctx->camera_controls = ctx->control_system->get_control_set();
  807. // Application control mappings
  808. ctx->input_event_router->add_mapping(key_mapping(ctx->toggle_fullscreen_control, nullptr, scancode::f11));
  809. ctx->input_event_router->add_mapping(key_mapping(ctx->screenshot_control, nullptr, scancode::f12));
  810. ctx->input_event_router->add_mapping(key_mapping(ctx->rotate_ccw_control, nullptr, scancode::q));
  811. ctx->input_event_router->add_mapping(key_mapping(ctx->rotate_cw_control, nullptr, scancode::e));
  812. // Add menu control mappings
  813. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_back_control, nullptr, scancode::escape));
  814. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_back_control, nullptr, scancode::backspace));
  815. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->menu_back_control, nullptr, game_controller_button::b));
  816. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_tool_menu_control(), nullptr, scancode::left_shift));
  817. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->control_system->get_tool_menu_control(), nullptr, game_controller_button::x));
  818. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_select_control, nullptr, scancode::enter));
  819. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_select_control, nullptr, scancode::space));
  820. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_toggle_view_control(), nullptr, scancode::tab));
  821. ctx->control_system->get_toggle_view_control()->set_activated_callback(
  822. [ctx]()
  823. {
  824. if (ctx->active_scene == ctx->overworld_scene)
  825. {
  826. ctx->active_scene = ctx->underworld_scene;
  827. ctx->radial_transition_inner->transition(0.5f, false, ease<float, double>::in_quad);
  828. auto switch_cameras = [ctx]()
  829. {
  830. ctx->overworld_camera->set_active(false);
  831. ctx->underworld_camera->set_active(true);
  832. ctx->fade_transition->transition(0.25f, true, ease<float, double>::out_quad);
  833. };
  834. float t = ctx->timeline->get_position();
  835. ctx->timeline->add_cue({t + 0.5f, switch_cameras});
  836. }
  837. else
  838. {
  839. ctx->active_scene = ctx->overworld_scene;
  840. ctx->fade_transition->transition(0.25f, false, ease<float, double>::out_quad);
  841. auto switch_cameras = [ctx]()
  842. {
  843. ctx->overworld_camera->set_active(true);
  844. ctx->underworld_camera->set_active(false);
  845. ctx->radial_transition_inner->transition(0.5f, true, ease<float, double>::out_quad);
  846. };
  847. float t = ctx->timeline->get_position();
  848. ctx->timeline->add_cue({t + 0.25f, switch_cameras});
  849. }
  850. });
  851. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_forward_control(), nullptr, scancode::w));
  852. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_forward_control(), nullptr, game_controller_axis::left_y, true));
  853. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_back_control(), nullptr, scancode::s));
  854. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_back_control(), nullptr, game_controller_axis::left_y, false));
  855. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_left_control(), nullptr, scancode::a));
  856. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_left_control(), nullptr, game_controller_axis::left_x, true));
  857. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_right_control(), nullptr, scancode::d));
  858. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_right_control(), nullptr, game_controller_axis::left_x, false));
  859. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_rotate_ccw_control(), nullptr, game_controller_axis::right_x, false));
  860. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_rotate_cw_control(), nullptr, game_controller_axis::right_x, true));
  861. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_tilt_up_control(), nullptr, game_controller_axis::right_y, false));
  862. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_tilt_down_control(), nullptr, game_controller_axis::right_y, true));
  863. ctx->input_event_router->add_mapping(mouse_wheel_mapping(ctx->control_system->get_zoom_in_control(), nullptr, mouse_wheel_axis::positive_y));
  864. ctx->input_event_router->add_mapping(mouse_wheel_mapping(ctx->control_system->get_zoom_out_control(), nullptr, mouse_wheel_axis::negative_y));
  865. ctx->input_event_router->add_mapping(mouse_button_mapping(ctx->control_system->get_adjust_camera_control(), nullptr, 3));
  866. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->control_system->get_ascend_control(), nullptr, game_controller_button::y));
  867. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->control_system->get_descend_control(), nullptr, game_controller_button::a));
  868. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_zoom_out_control(), nullptr, game_controller_axis::trigger_left, false));
  869. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_zoom_in_control(), nullptr, game_controller_axis::trigger_right, false));
  870. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_equip_forceps_control(), nullptr, scancode::one));
  871. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_equip_brush_control(), nullptr, scancode::two));
  872. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_equip_lens_control(), nullptr, scancode::three));
  873. ctx->control_system->get_equip_forceps_control()->set_activated_callback
  874. (
  875. [ctx]()
  876. {
  877. ctx->tool_system->set_active_tool(ctx->forceps_entity);
  878. }
  879. );
  880. ctx->control_system->get_equip_brush_control()->set_activated_callback
  881. (
  882. [ctx]()
  883. {
  884. ctx->tool_system->set_active_tool(ctx->brush_entity);
  885. }
  886. );
  887. ctx->control_system->get_equip_lens_control()->set_activated_callback
  888. (
  889. [ctx]()
  890. {
  891. ctx->tool_system->set_active_tool(ctx->lens_entity);
  892. }
  893. );
  894. event_dispatcher->subscribe<mouse_moved_event>(ctx->control_system);
  895. event_dispatcher->subscribe<mouse_moved_event>(ctx->camera_system);
  896. event_dispatcher->subscribe<mouse_moved_event>(ctx->tool_system);
  897. event_dispatcher->subscribe<mouse_moved_event>(ctx->ui_system);
  898. event_dispatcher->subscribe<window_resized_event>(ctx->control_system);
  899. event_dispatcher->subscribe<window_resized_event>(ctx->camera_system);
  900. event_dispatcher->subscribe<window_resized_event>(ctx->tool_system);
  901. event_dispatcher->subscribe<window_resized_event>(ctx->ui_system);
  902. }
  903. void setup_cli(game_context* ctx)
  904. {
  905. ctx->cli = new cli();
  906. ctx->cli->register_command("echo", cc::echo);
  907. ctx->cli->register_command("exit", std::function<std::string()>(std::bind(&cc::exit, ctx)));
  908. ctx->cli->register_command("scrot", std::function<std::string()>(std::bind(&cc::scrot, ctx)));
  909. ctx->cli->register_command("cue", std::function<std::string(float, std::string)>(std::bind(&cc::cue, ctx, std::placeholders::_1, std::placeholders::_2)));
  910. //std::string cmd = "cue 20 exit";
  911. //logger->log(cmd);
  912. //logger->log(cli.interpret(cmd));
  913. }
  914. void setup_callbacks(game_context* ctx)
  915. {
  916. // Set update callback
  917. ctx->app->set_update_callback
  918. (
  919. [ctx](double t, double dt)
  920. {
  921. // Update tweens
  922. ctx->time_tween->update();
  923. (*ctx->time_tween)[1] = t;
  924. ctx->overworld_scene->update_tweens();
  925. ctx->underworld_scene->update_tweens();
  926. ctx->ui_scene->update_tweens();
  927. ctx->focal_point_tween->update();
  928. ctx->underworld_final_pass->get_material()->update_tweens();
  929. ctx->timeline->advance(dt);
  930. ctx->control_system->update(t, dt);
  931. ctx->terrain_system->update(t, dt);
  932. ctx->vegetation_system->update(t, dt);
  933. ctx->snapping_system->update(t, dt);
  934. ctx->nest_system->update(t, dt);
  935. ctx->subterrain_system->update(t, dt);
  936. ctx->collision_system->update(t, dt);
  937. ctx->samara_system->update(t, dt);
  938. ctx->behavior_system->update(t, dt);
  939. ctx->locomotion_system->update(t, dt);
  940. ctx->camera_system->update(t, dt);
  941. ctx->tool_system->update(t, dt);
  942. ctx->constraint_system->update(t, dt);
  943. //(*ctx->focal_point_tween)[1] = ctx->orbit_cam->get_focal_point();
  944. auto xf = ec::get_transform(*ctx->ecs_registry, ctx->lens_entity);
  945. ctx->lens_spotlight->look_at(xf.translation, xf.translation + ctx->sun_direct->get_direction(), {0, 1, 0});
  946. xf = ec::get_transform(*ctx->ecs_registry, ctx->flashlight_entity);
  947. //ctx->flashlight_spotlight->set_transform(xf);
  948. ctx->flashlight_spotlight->look_at(xf.translation, xf.translation + xf.rotation * float3{0, 0, 1}, {0, 0, -1});
  949. ctx->ui_system->update(dt);
  950. ctx->render_system->update(t, dt);
  951. ctx->animator->animate(dt);
  952. ctx->application_controls->update();
  953. ctx->menu_controls->update();
  954. ctx->camera_controls->update();
  955. }
  956. );
  957. // Set render callback
  958. ctx->app->set_render_callback
  959. (
  960. [ctx](double alpha)
  961. {
  962. ctx->render_system->render(alpha);
  963. }
  964. );
  965. }