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

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