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

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