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

1073 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/shadow-map-pass.hpp"
  46. #include "renderer/passes/sky-pass.hpp"
  47. #include "renderer/simple-render-pass.hpp"
  48. #include "renderer/vertex-attributes.hpp"
  49. #include "renderer/compositor.hpp"
  50. #include "renderer/renderer.hpp"
  51. #include "resources/config-file.hpp"
  52. #include "resources/resource-manager.hpp"
  53. #include "resources/resource-manager.hpp"
  54. #include "scene/billboard.hpp"
  55. #include "scene/model-instance.hpp"
  56. #include "scene/point-light.hpp"
  57. #include "scene/directional-light.hpp"
  58. #include "scene/ambient-light.hpp"
  59. #include "scene/spotlight.hpp"
  60. #include "game/states/game-states.hpp"
  61. #include "game/systems/behavior-system.hpp"
  62. #include "game/systems/camera-system.hpp"
  63. #include "game/systems/collision-system.hpp"
  64. #include "game/systems/constraint-system.hpp"
  65. #include "game/systems/control-system.hpp"
  66. #include "game/systems/locomotion-system.hpp"
  67. #include "game/systems/nest-system.hpp"
  68. #include "game/systems/snapping-system.hpp"
  69. #include "game/systems/render-system.hpp"
  70. #include "game/systems/samara-system.hpp"
  71. #include "game/systems/subterrain-system.hpp"
  72. #include "game/systems/terrain-system.hpp"
  73. #include "game/systems/tool-system.hpp"
  74. #include "game/systems/ui-system.hpp"
  75. #include "game/systems/vegetation-system.hpp"
  76. #include "utility/paths.hpp"
  77. #include "event/event-dispatcher.hpp"
  78. #include "input/input-event-router.hpp"
  79. #include "input/input-mapper.hpp"
  80. #include "input/game-controller.hpp"
  81. #include "input/mouse.hpp"
  82. #include "input/keyboard.hpp"
  83. #include "orbit-cam.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::float_32, pixel_format::d);
  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. // Create shadow map framebuffer
  392. int shadow_map_resolution = ctx->config->get<int>("shadow_map_resolution");
  393. ctx->shadow_map_depth_texture = new texture_2d(shadow_map_resolution, shadow_map_resolution, pixel_type::float_32, pixel_format::d);
  394. ctx->shadow_map_depth_texture->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  395. ctx->shadow_map_depth_texture->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
  396. ctx->shadow_map_depth_texture->set_max_anisotropy(0.0f);
  397. ctx->shadow_map_framebuffer = new framebuffer(shadow_map_resolution, shadow_map_resolution);
  398. ctx->shadow_map_framebuffer->attach(framebuffer_attachment_type::depth, ctx->shadow_map_depth_texture);
  399. // Create bloom pingpong framebuffers (32F color, no depth)
  400. int bloom_width = viewport_dimensions[0] >> 1;
  401. int bloom_height = viewport_dimensions[1] >> 1;
  402. ctx->bloom_texture = new texture_2d(bloom_width, bloom_height, pixel_type::float_32, pixel_format::rgb);
  403. ctx->bloom_texture->set_wrapping(texture_wrapping::clamp, texture_wrapping::clamp);
  404. ctx->bloom_texture->set_filters(texture_min_filter::linear, texture_mag_filter::linear);
  405. ctx->bloom_texture->set_max_anisotropy(0.0f);
  406. ctx->framebuffer_bloom = new framebuffer(bloom_width, bloom_height);
  407. ctx->framebuffer_bloom->attach(framebuffer_attachment_type::color, ctx->bloom_texture);
  408. // Load fallback material
  409. ctx->fallback_material = ctx->resource_manager->load<material>("fallback.mtl");
  410. // Setup overworld compositor
  411. ctx->overworld_shadow_map_clear_pass = new clear_pass(ctx->rasterizer, ctx->shadow_map_framebuffer);
  412. ctx->overworld_shadow_map_clear_pass->set_cleared_buffers(false, true, false);
  413. ctx->overworld_shadow_map_pass = new shadow_map_pass(ctx->rasterizer, ctx->shadow_map_framebuffer, ctx->resource_manager);
  414. ctx->overworld_shadow_map_pass->set_split_scheme_weight(0.75f);
  415. ctx->overworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  416. ctx->overworld_clear_pass->set_cleared_buffers(true, true, false);
  417. ctx->overworld_sky_pass = new sky_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  418. ctx->overworld_sky_pass->set_enabled(false);
  419. ctx->overworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  420. ctx->overworld_material_pass->set_fallback_material(ctx->fallback_material);
  421. ctx->overworld_material_pass->shadow_map_pass = ctx->overworld_shadow_map_pass;
  422. ctx->overworld_material_pass->shadow_map = ctx->shadow_map_depth_texture;
  423. ctx->overworld_bloom_pass = new bloom_pass(ctx->rasterizer, ctx->framebuffer_bloom, ctx->resource_manager);
  424. ctx->overworld_bloom_pass->set_source_texture(ctx->framebuffer_hdr_color);
  425. ctx->overworld_bloom_pass->set_brightness_threshold(1.0f);
  426. ctx->overworld_bloom_pass->set_blur_iterations(4);
  427. ctx->overworld_bloom_pass->set_enabled(false);
  428. ctx->overworld_final_pass = new ::final_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  429. ctx->overworld_final_pass->set_color_texture(ctx->framebuffer_hdr_color);
  430. ctx->overworld_final_pass->set_bloom_texture(ctx->bloom_texture);
  431. ctx->overworld_compositor = new compositor();
  432. ctx->overworld_compositor->add_pass(ctx->overworld_shadow_map_clear_pass);
  433. ctx->overworld_compositor->add_pass(ctx->overworld_shadow_map_pass);
  434. ctx->overworld_compositor->add_pass(ctx->overworld_clear_pass);
  435. ctx->overworld_compositor->add_pass(ctx->overworld_sky_pass);
  436. ctx->overworld_compositor->add_pass(ctx->overworld_material_pass);
  437. ctx->overworld_compositor->add_pass(ctx->overworld_bloom_pass);
  438. ctx->overworld_compositor->add_pass(ctx->overworld_final_pass);
  439. // Setup underworld compositor
  440. ctx->underworld_clear_pass = new clear_pass(ctx->rasterizer, ctx->framebuffer_hdr);
  441. ctx->underworld_clear_pass->set_cleared_buffers(true, true, false);
  442. ctx->underworld_material_pass = new material_pass(ctx->rasterizer, ctx->framebuffer_hdr, ctx->resource_manager);
  443. ctx->underworld_material_pass->set_fallback_material(ctx->fallback_material);
  444. shader_program* underworld_final_shader = ctx->resource_manager->load<shader_program>("underground-final.glsl");
  445. ctx->underworld_final_pass = new simple_render_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), underworld_final_shader);
  446. ctx->underground_color_texture_property = ctx->underworld_final_pass->get_material()->add_property<const texture_2d*>("color_texture");
  447. ctx->underground_color_texture_property->set_value(ctx->framebuffer_hdr_color);
  448. ctx->underworld_final_pass->get_material()->update_tweens();
  449. ctx->underworld_compositor = new compositor();
  450. ctx->underworld_compositor->add_pass(ctx->underworld_clear_pass);
  451. ctx->underworld_compositor->add_pass(ctx->underworld_material_pass);
  452. ctx->underworld_compositor->add_pass(ctx->underworld_final_pass);
  453. // Setup UI camera compositor
  454. ctx->ui_clear_pass = new clear_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer());
  455. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  456. ctx->ui_material_pass = new material_pass(ctx->rasterizer, &ctx->rasterizer->get_default_framebuffer(), ctx->resource_manager);
  457. ctx->ui_material_pass->set_fallback_material(ctx->fallback_material);
  458. ctx->ui_compositor = new compositor();
  459. ctx->ui_compositor->add_pass(ctx->ui_clear_pass);
  460. ctx->ui_compositor->add_pass(ctx->ui_material_pass);
  461. // Create billboard VAO
  462. {
  463. const float billboard_vertex_data[] =
  464. {
  465. -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  466. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  467. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  468. 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  469. -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
  470. 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
  471. };
  472. std::size_t billboard_vertex_size = 8;
  473. std::size_t billboard_vertex_stride = sizeof(float) * billboard_vertex_size;
  474. std::size_t billboard_vertex_count = 6;
  475. ctx->billboard_vbo = new vertex_buffer(sizeof(float) * billboard_vertex_size * billboard_vertex_count, billboard_vertex_data);
  476. ctx->billboard_vao = new vertex_array();
  477. ctx->billboard_vao->bind_attribute(VERTEX_POSITION_LOCATION, *ctx->billboard_vbo, 3, vertex_attribute_type::float_32, billboard_vertex_stride, 0);
  478. ctx->billboard_vao->bind_attribute(VERTEX_TEXCOORD_LOCATION, *ctx->billboard_vbo, 2, vertex_attribute_type::float_32, billboard_vertex_stride, sizeof(float) * 3);
  479. ctx->billboard_vao->bind_attribute(VERTEX_BARYCENTRIC_LOCATION, *ctx->billboard_vbo, 3, vertex_attribute_type::float_32, billboard_vertex_stride, sizeof(float) * 5);
  480. }
  481. // Create renderer
  482. ctx->renderer = new renderer();
  483. ctx->renderer->set_billboard_vao(ctx->billboard_vao);
  484. logger->pop_task(EXIT_SUCCESS);
  485. }
  486. void setup_scenes(game_context* ctx)
  487. {
  488. logger* logger = ctx->logger;
  489. logger->push_task("Setting up rendering");
  490. // Get default framebuffer
  491. const auto& viewport_dimensions = ctx->rasterizer->get_default_framebuffer().get_dimensions();
  492. float viewport_aspect_ratio = static_cast<float>(viewport_dimensions[0]) / static_cast<float>(viewport_dimensions[1]);
  493. // Setup overworld camera
  494. ctx->overworld_camera = new camera();
  495. ctx->overworld_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  496. ctx->overworld_camera->set_compositor(ctx->overworld_compositor);
  497. ctx->overworld_camera->set_composite_index(0);
  498. ctx->overworld_camera->set_active(true);
  499. // Setup underworld camera
  500. ctx->underworld_camera = new camera();
  501. ctx->underworld_camera->set_perspective(math::radians<float>(45.0f), viewport_aspect_ratio, 0.1f, 1000.0f);
  502. ctx->underworld_camera->look_at({0, 50, 0}, {0, 0, 0}, {0, 0, -1});
  503. ctx->underworld_camera->set_compositor(ctx->underworld_compositor);
  504. ctx->underworld_camera->set_composite_index(0);
  505. ctx->underworld_camera->set_active(false);
  506. // Setup UI camera
  507. ctx->ui_camera = new camera();
  508. ctx->ui_camera->set_compositor(ctx->ui_compositor);
  509. // Setup lights
  510. ctx->sun_indirect = new ambient_light();
  511. ctx->sun_indirect->set_intensity(0.25f);
  512. ctx->sun_indirect->update_tweens();
  513. ctx->sun_direct = new directional_light();
  514. ctx->sun_direct->look_at({-1.0f, 5.0f, 1.0f}, {0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f});
  515. ctx->sun_direct->set_intensity(1.0f);
  516. ctx->sun_direct->update_tweens();
  517. ctx->subterrain_light = new point_light();
  518. ctx->subterrain_light->set_color({1, 1, 1});
  519. ctx->subterrain_light->set_intensity(1.0f);
  520. ctx->subterrain_light->set_attenuation({1.0f, 0.09f, 0.032f});
  521. ctx->subterrain_light->update_tweens();
  522. ctx->spotlight = new spotlight();
  523. ctx->spotlight->set_color({1, 1, 1});
  524. ctx->spotlight->set_intensity(1.0f);
  525. ctx->spotlight->set_attenuation({1.0f, 0.09f, 0.032f});
  526. ctx->spotlight->set_cutoff({math::radians(15.0f), math::radians(30.0f)});
  527. ctx->spotlight->update_tweens();
  528. ctx->spotlight->set_active(false);
  529. ctx->underworld_ambient_light = new ambient_light();
  530. ctx->underworld_ambient_light->set_color({1, 1, 1});
  531. ctx->underworld_ambient_light->set_intensity(0.1f);
  532. ctx->underworld_ambient_light->update_tweens();
  533. const texture_2d* splash_texture = ctx->resource_manager->load<texture_2d>("splash.png");
  534. auto splash_dimensions = splash_texture->get_dimensions();
  535. ctx->splash_billboard_material = new material();
  536. ctx->splash_billboard_material->set_shader_program(ctx->resource_manager->load<shader_program>("ui-element-textured.glsl"));
  537. ctx->splash_billboard_material->add_property<const texture_2d*>("background")->set_value(splash_texture);
  538. ctx->splash_billboard_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  539. ctx->splash_billboard_material->update_tweens();
  540. ctx->splash_billboard = new billboard();
  541. ctx->splash_billboard->set_material(ctx->splash_billboard_material);
  542. ctx->splash_billboard->set_scale({(float)std::get<0>(splash_dimensions) * 0.5f, (float)std::get<1>(splash_dimensions) * 0.5f, 1.0f});
  543. ctx->splash_billboard->set_translation({0.0f, 0.0f, 0.0f});
  544. ctx->splash_billboard->update_tweens();
  545. material* billboard_material = new material();
  546. billboard_material->set_shader_program(ctx->resource_manager->load<shader_program>("ui-element-textured.glsl"));
  547. billboard_material->add_property<const texture_2d*>("background")->set_value(ctx->resource_manager->load<texture_2d>("arrow.png"));
  548. billboard_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  549. billboard_material->set_flags(MATERIAL_FLAG_TRANSLUCENT | MATERIAL_FLAG_NOT_SHADOW_CASTER);
  550. billboard* arrow_billboard = new billboard();
  551. arrow_billboard->set_material(billboard_material);
  552. arrow_billboard->set_scale(float3{1, 1, 1} * 2.0f);
  553. arrow_billboard->set_translation({0, 10, 0});
  554. arrow_billboard->set_billboard_type(billboard_type::cylindrical);
  555. arrow_billboard->set_alignment_axis({0, 1, 0});
  556. arrow_billboard->update_tweens();
  557. billboard_material = new material();
  558. billboard_material->set_shader_program(ctx->resource_manager->load<shader_program>("portal-card.glsl"));
  559. billboard_material->add_property<float4>("color")->set_value(float4{1, 1, 1, 1});
  560. billboard_material->add_property<float2>("range")->set_value(float2{50.0f, 500.0f});
  561. billboard_material->set_flags(MATERIAL_FLAG_TRANSLUCENT | MATERIAL_FLAG_NOT_SHADOW_CASTER);
  562. billboard* portal_billboard = new billboard();
  563. portal_billboard->set_material(billboard_material);
  564. portal_billboard->set_scale(float3{1, 1, 1} * 10.0f);
  565. portal_billboard->set_translation({0.0f, 0, 0});
  566. portal_billboard->set_billboard_type(billboard_type::spherical);
  567. portal_billboard->set_alignment_axis({0, 1, 0});
  568. portal_billboard->update_tweens();
  569. // Create depth debug billboard
  570. /*
  571. material* depth_debug_material = new material();
  572. depth_debug_material->set_shader_program(ctx->resource_manager->load<shader_program>("ui-element-textured.glsl"));
  573. depth_debug_material->add_property<const texture_2d*>("background")->set_value(shadow_map_depth_texture);
  574. depth_debug_material->add_property<float4>("tint")->set_value(float4{1, 1, 1, 1});
  575. billboard* depth_debug_billboard = new billboard();
  576. depth_debug_billboard->set_material(depth_debug_material);
  577. depth_debug_billboard->set_scale({128, 128, 1});
  578. depth_debug_billboard->set_translation({-960 + 128, 1080 * 0.5f - 128, 0});
  579. depth_debug_billboard->update_tweens();
  580. ui_system->get_scene()->add_object(depth_debug_billboard);
  581. */
  582. // Setup overworld scene
  583. ctx->overworld_scene = new scene();
  584. ctx->overworld_scene->add_object(ctx->overworld_camera);
  585. ctx->overworld_scene->add_object(ctx->sun_indirect);
  586. ctx->overworld_scene->add_object(ctx->sun_direct);
  587. //ctx->overworld_scene->add_object(ctx->spotlight);
  588. ctx->overworld_scene->add_object(arrow_billboard);
  589. // Setup underworld scene
  590. ctx->underworld_scene = new scene();
  591. ctx->underworld_scene->add_object(ctx->underworld_camera);
  592. ctx->underworld_scene->add_object(ctx->underworld_ambient_light);
  593. //ctx->underworld_scene->add_object(ctx->lantern);
  594. //ctx->underworld_scene->add_object(ctx->subterrain_light);
  595. //ctx->underworld_scene->add_object(ctx->portal_billboard);
  596. //model_instance* larva = new model_instance(ctx->resource_manager->load<model>("larva.obj"));
  597. //ctx->underworld_scene->add_object(larva);
  598. // Setup UI scene
  599. ctx->ui_scene = new scene();
  600. ctx->ui_scene->add_object(ctx->ui_camera);
  601. // Set overworld as active scene
  602. ctx->active_scene = ctx->overworld_scene;
  603. logger->pop_task(EXIT_SUCCESS);
  604. }
  605. void setup_animation(game_context* ctx)
  606. {
  607. // Setup timeline system
  608. ctx->timeline = new timeline();
  609. ctx->timeline->set_autoremove(true);
  610. // Setup animator
  611. ctx->animator = new animator();
  612. // Initialize time tween
  613. ctx->time_tween = new tween<double>(0.0);
  614. ctx->time_tween->set_interpolator(math::lerp<double, double>);
  615. // Create fade transition
  616. ctx->fade_transition = new screen_transition();
  617. ctx->fade_transition->get_material()->set_shader_program(ctx->resource_manager->load<shader_program>("fade-transition.glsl"));
  618. ctx->ui_scene->add_object(ctx->fade_transition->get_billboard());
  619. ctx->animator->add_animation(ctx->fade_transition->get_animation());
  620. // Create inner radial transition
  621. ctx->radial_transition_inner = new screen_transition();
  622. ctx->radial_transition_inner->get_material()->set_shader_program(ctx->resource_manager->load<shader_program>("radial-transition-inner.glsl"));
  623. ctx->ui_scene->add_object(ctx->radial_transition_inner->get_billboard());
  624. ctx->animator->add_animation(ctx->radial_transition_inner->get_animation());
  625. // Create outer radial transition
  626. ctx->radial_transition_outer = new screen_transition();
  627. ctx->radial_transition_outer->get_material()->set_shader_program(ctx->resource_manager->load<shader_program>("radial-transition-outer.glsl"));
  628. ctx->ui_scene->add_object(ctx->radial_transition_outer->get_billboard());
  629. ctx->animator->add_animation(ctx->radial_transition_outer->get_animation());
  630. // Setup tweens
  631. ctx->focal_point_tween = new tween<float3>();
  632. ctx->focal_point_tween->set_interpolator(math::lerp<float3, float>);
  633. // Set material pass tweens
  634. ctx->overworld_material_pass->set_time_tween(ctx->time_tween);
  635. ctx->overworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  636. ctx->underworld_material_pass->set_time_tween(ctx->time_tween);
  637. ctx->underworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  638. ctx->underworld_final_pass->set_time_tween(ctx->time_tween);
  639. ctx->underworld_material_pass->set_focal_point_tween(ctx->focal_point_tween);
  640. ctx->ui_material_pass->set_time_tween(ctx->time_tween);
  641. }
  642. void setup_entities(game_context* ctx)
  643. {
  644. // Create ECS registry
  645. ctx->ecs_registry = new entt::registry();
  646. // Reserve named entities
  647. ctx->brush_entity = ctx->ecs_registry->create();
  648. ctx->flashlight_entity = ctx->ecs_registry->create();
  649. ctx->forceps_entity = ctx->ecs_registry->create();
  650. ctx->lens_entity = ctx->ecs_registry->create();
  651. ctx->focal_point_entity = ctx->ecs_registry->create();
  652. }
  653. void setup_systems(game_context* ctx)
  654. {
  655. const auto& viewport_dimensions = ctx->app->get_viewport_dimensions();
  656. float4 viewport = {0.0f, 0.0f, static_cast<float>(viewport_dimensions[0]), static_cast<float>(viewport_dimensions[1])};
  657. ctx->orbit_cam = new orbit_cam();
  658. ctx->orbit_cam->attach(ctx->overworld_camera);
  659. // Setup terrain system
  660. ctx->terrain_system = new ::terrain_system(*ctx->ecs_registry, ctx->resource_manager);
  661. ctx->terrain_system->set_patch_size(TERRAIN_PATCH_SIZE);
  662. // Setup vegetation system
  663. ctx->vegetation_system = new ::vegetation_system(*ctx->ecs_registry);
  664. ctx->vegetation_system->set_terrain_patch_size(TERRAIN_PATCH_SIZE);
  665. ctx->vegetation_system->set_vegetation_patch_resolution(VEGETATION_PATCH_RESOLUTION);
  666. ctx->vegetation_system->set_vegetation_density(1.0f);
  667. ctx->vegetation_system->set_vegetation_model(ctx->resource_manager->load<model>("grass-tuft.obj"));
  668. ctx->vegetation_system->set_scene(ctx->overworld_scene);
  669. // Setup tool system
  670. ctx->tool_system = new tool_system(*ctx->ecs_registry);
  671. ctx->tool_system->set_camera(ctx->overworld_camera);
  672. ctx->tool_system->set_orbit_cam(ctx->orbit_cam);
  673. ctx->tool_system->set_viewport(viewport);
  674. // Setup camera system
  675. ctx->camera_system = new camera_system(*ctx->ecs_registry);
  676. ctx->camera_system->set_viewport(viewport);
  677. // Setup subterrain system
  678. ctx->subterrain_system = new ::subterrain_system(*ctx->ecs_registry, ctx->resource_manager);
  679. ctx->subterrain_system->set_scene(ctx->underworld_scene);
  680. // Setup nest system
  681. ctx->nest_system = new nest_system(*ctx->ecs_registry, ctx->resource_manager);
  682. // Setup collision system
  683. ctx->collision_system = new collision_system(*ctx->ecs_registry);
  684. // Setup samara system
  685. ctx->samara_system = new samara_system(*ctx->ecs_registry);
  686. // Setup snapping system
  687. ctx->snapping_system = new snapping_system(*ctx->ecs_registry);
  688. // Setup behavior system
  689. ctx->behavior_system = new behavior_system(*ctx->ecs_registry);
  690. // Setup locomotion system
  691. ctx->locomotion_system = new locomotion_system(*ctx->ecs_registry);
  692. ctx->constraint_system = new constraint_system(*ctx->ecs_registry);
  693. // Setup pheromone system
  694. ctx->pheromones = new pheromone_matrix();
  695. ctx->pheromones->rows = 256;
  696. ctx->pheromones->columns = 256;
  697. ctx->pheromones->buffers = new float*[2];
  698. ctx->pheromones->buffers[0] = new float[ctx->pheromones->rows * ctx->pheromones->columns];
  699. ctx->pheromones->buffers[1] = new float[ctx->pheromones->rows * ctx->pheromones->columns];
  700. ctx->pheromones->current = 0;
  701. //diffuse(ctx->pheromones);
  702. // Setup render system
  703. ctx->render_system = new ::render_system(*ctx->ecs_registry);
  704. ctx->render_system->add_layer(ctx->overworld_scene);
  705. ctx->render_system->add_layer(ctx->underworld_scene);
  706. ctx->render_system->add_layer(ctx->ui_scene);
  707. ctx->render_system->set_renderer(ctx->renderer);
  708. // Setup control system
  709. ctx->control_system = new ::control_system(*ctx->ecs_registry);
  710. ctx->control_system->set_orbit_cam(ctx->orbit_cam);
  711. ctx->control_system->set_viewport(viewport);
  712. ctx->control_system->set_underworld_camera(ctx->underworld_camera);
  713. ctx->control_system->set_tool(nullptr);
  714. //ctx->control_system->set_flashlight(flashlight, flashlight_light_cone);
  715. ctx->control_system->get_adjust_camera_control()->set_activated_callback([ctx](){ ctx->app->set_relative_mouse_mode(true); ctx->tool_system->set_pick(false); });
  716. ctx->control_system->get_adjust_camera_control()->set_deactivated_callback([ctx](){ ctx->app->set_relative_mouse_mode(false); ctx->tool_system->set_pick(true); });
  717. ctx->control_system->set_flashlight(ctx->flashlight_entity);
  718. ctx->control_system->set_camera_subject(ctx->focal_point_entity);
  719. ctx->control_system->set_camera_system(ctx->camera_system);
  720. // Setup UI system
  721. ctx->ui_system = new ui_system(ctx->resource_manager);
  722. ctx->ui_system->set_camera(ctx->ui_camera);
  723. ctx->ui_system->set_scene(ctx->ui_scene);
  724. ctx->ui_system->set_viewport(viewport);
  725. ctx->ui_system->set_tool_menu_control(ctx->control_system->get_tool_menu_control());
  726. ctx->app->get_event_dispatcher()->subscribe<mouse_moved_event>(ctx->ui_system);
  727. }
  728. void setup_controls(game_context* ctx)
  729. {
  730. event_dispatcher* event_dispatcher = ctx->app->get_event_dispatcher();
  731. // Setup input event routing
  732. ctx->input_event_router = new input_event_router();
  733. ctx->input_event_router->set_event_dispatcher(event_dispatcher);
  734. // Setup input mapper
  735. ctx->input_mapper = new input_mapper();
  736. ctx->input_mapper->set_event_dispatcher(event_dispatcher);
  737. // Create toggle fullscreen control
  738. ctx->toggle_fullscreen_control = new control();
  739. ctx->toggle_fullscreen_control->set_activated_callback
  740. (
  741. [ctx]()
  742. {
  743. bool fullscreen = !ctx->app->is_fullscreen();
  744. ctx->app->set_fullscreen(fullscreen);
  745. if (!fullscreen)
  746. {
  747. int2 resolution = ctx->config->get<int2>("windowed_resolution");
  748. ctx->app->resize_window(resolution.x, resolution.y);
  749. }
  750. ctx->config->set<int>("fullscreen", (fullscreen) ? 1 : 0);
  751. }
  752. );
  753. // Create screenshot control
  754. ctx->screenshot_control = new control();
  755. ctx->screenshot_control->set_activated_callback
  756. (
  757. [ctx]()
  758. {
  759. std::string path = ctx->screenshots_path + "antkeeper-" + timestamp() + ".png";
  760. ctx->app->save_frame(path);
  761. }
  762. );
  763. // Create rotation controls
  764. ctx->rotate_ccw_control = new control();
  765. ctx->rotate_ccw_control->set_activated_callback
  766. (
  767. std::bind(&camera_system::rotate, ctx->camera_system, math::radians(-90.0f))
  768. );
  769. ctx->rotate_cw_control = new control();
  770. ctx->rotate_cw_control->set_activated_callback
  771. (
  772. std::bind(&camera_system::rotate, ctx->camera_system, math::radians(90.0f))
  773. );
  774. // Create menu back control
  775. ctx->menu_back_control = new control();
  776. ctx->menu_back_control->set_activated_callback
  777. (
  778. std::bind(&application::close, ctx->app, 0)
  779. );
  780. // Create menu select control
  781. ctx->menu_select_control = new control();
  782. // Create application control set
  783. ctx->application_controls = new control_set();
  784. ctx->application_controls->add_control(ctx->toggle_fullscreen_control);
  785. ctx->application_controls->add_control(ctx->screenshot_control);
  786. ctx->application_controls->add_control(ctx->rotate_ccw_control);
  787. ctx->application_controls->add_control(ctx->rotate_cw_control);
  788. // Create menu control set
  789. ctx->menu_controls = new control_set();
  790. ctx->menu_controls->add_control(ctx->menu_back_control);
  791. ctx->menu_controls->add_control(ctx->menu_select_control);
  792. ctx->camera_controls = ctx->control_system->get_control_set();
  793. // Application control mappings
  794. ctx->input_event_router->add_mapping(key_mapping(ctx->toggle_fullscreen_control, nullptr, scancode::f11));
  795. ctx->input_event_router->add_mapping(key_mapping(ctx->screenshot_control, nullptr, scancode::f12));
  796. ctx->input_event_router->add_mapping(key_mapping(ctx->rotate_ccw_control, nullptr, scancode::q));
  797. ctx->input_event_router->add_mapping(key_mapping(ctx->rotate_cw_control, nullptr, scancode::e));
  798. // Add menu control mappings
  799. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_back_control, nullptr, scancode::escape));
  800. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_back_control, nullptr, scancode::backspace));
  801. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->menu_back_control, nullptr, game_controller_button::b));
  802. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_tool_menu_control(), nullptr, scancode::left_shift));
  803. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->control_system->get_tool_menu_control(), nullptr, game_controller_button::x));
  804. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_select_control, nullptr, scancode::enter));
  805. ctx->input_event_router->add_mapping(key_mapping(ctx->menu_select_control, nullptr, scancode::space));
  806. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_toggle_view_control(), nullptr, scancode::tab));
  807. ctx->control_system->get_toggle_view_control()->set_activated_callback(
  808. [ctx]()
  809. {
  810. if (ctx->active_scene == ctx->overworld_scene)
  811. {
  812. ctx->active_scene = ctx->underworld_scene;
  813. ctx->radial_transition_inner->transition(0.5f, false, ease<float, double>::in_quad);
  814. auto switch_cameras = [ctx]()
  815. {
  816. ctx->overworld_camera->set_active(false);
  817. ctx->underworld_camera->set_active(true);
  818. ctx->fade_transition->transition(0.25f, true, ease<float, double>::out_quad);
  819. };
  820. float t = ctx->timeline->get_position();
  821. ctx->timeline->add_cue({t + 0.5f, switch_cameras});
  822. }
  823. else
  824. {
  825. ctx->active_scene = ctx->overworld_scene;
  826. ctx->fade_transition->transition(0.25f, false, ease<float, double>::out_quad);
  827. auto switch_cameras = [ctx]()
  828. {
  829. ctx->overworld_camera->set_active(true);
  830. ctx->underworld_camera->set_active(false);
  831. ctx->radial_transition_inner->transition(0.5f, true, ease<float, double>::out_quad);
  832. };
  833. float t = ctx->timeline->get_position();
  834. ctx->timeline->add_cue({t + 0.25f, switch_cameras});
  835. }
  836. });
  837. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_forward_control(), nullptr, scancode::w));
  838. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_forward_control(), nullptr, game_controller_axis::left_y, true));
  839. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_back_control(), nullptr, scancode::s));
  840. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_back_control(), nullptr, game_controller_axis::left_y, false));
  841. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_left_control(), nullptr, scancode::a));
  842. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_left_control(), nullptr, game_controller_axis::left_x, true));
  843. ctx->input_event_router->add_mapping(key_mapping(ctx->control_system->get_move_right_control(), nullptr, scancode::d));
  844. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_move_right_control(), nullptr, game_controller_axis::left_x, false));
  845. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_rotate_ccw_control(), nullptr, game_controller_axis::right_x, false));
  846. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_rotate_cw_control(), nullptr, game_controller_axis::right_x, true));
  847. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_tilt_up_control(), nullptr, game_controller_axis::right_y, false));
  848. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_tilt_down_control(), nullptr, game_controller_axis::right_y, true));
  849. ctx->input_event_router->add_mapping(mouse_wheel_mapping(ctx->control_system->get_zoom_in_control(), nullptr, mouse_wheel_axis::positive_y));
  850. ctx->input_event_router->add_mapping(mouse_wheel_mapping(ctx->control_system->get_zoom_out_control(), nullptr, mouse_wheel_axis::negative_y));
  851. ctx->input_event_router->add_mapping(mouse_button_mapping(ctx->control_system->get_adjust_camera_control(), nullptr, 3));
  852. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->control_system->get_ascend_control(), nullptr, game_controller_button::y));
  853. ctx->input_event_router->add_mapping(game_controller_button_mapping(ctx->control_system->get_descend_control(), nullptr, game_controller_button::a));
  854. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_zoom_out_control(), nullptr, game_controller_axis::trigger_left, false));
  855. ctx->input_event_router->add_mapping(game_controller_axis_mapping(ctx->control_system->get_zoom_in_control(), nullptr, game_controller_axis::trigger_right, false));
  856. event_dispatcher->subscribe<mouse_moved_event>(ctx->control_system);
  857. event_dispatcher->subscribe<window_resized_event>(ctx->control_system);
  858. event_dispatcher->subscribe<mouse_moved_event>(ctx->camera_system);
  859. event_dispatcher->subscribe<window_resized_event>(ctx->camera_system);
  860. event_dispatcher->subscribe<mouse_moved_event>(ctx->tool_system);
  861. }
  862. void setup_cli(game_context* ctx)
  863. {
  864. ctx->cli = new cli();
  865. ctx->cli->register_command("echo", cc::echo);
  866. ctx->cli->register_command("exit", std::function<std::string()>(std::bind(&cc::exit, ctx)));
  867. ctx->cli->register_command("scrot", std::function<std::string()>(std::bind(&cc::scrot, ctx)));
  868. ctx->cli->register_command("cue", std::function<std::string(float, std::string)>(std::bind(&cc::cue, ctx, std::placeholders::_1, std::placeholders::_2)));
  869. //std::string cmd = "cue 20 exit";
  870. //logger->log(cmd);
  871. //logger->log(cli.interpret(cmd));
  872. }
  873. void setup_callbacks(game_context* ctx)
  874. {
  875. // Set update callback
  876. ctx->app->set_update_callback
  877. (
  878. [ctx](double t, double dt)
  879. {
  880. // Update tweens
  881. ctx->time_tween->update();
  882. (*ctx->time_tween)[1] = t;
  883. ctx->overworld_scene->update_tweens();
  884. ctx->underworld_scene->update_tweens();
  885. ctx->ui_scene->update_tweens();
  886. ctx->focal_point_tween->update();
  887. ctx->underworld_final_pass->get_material()->update_tweens();
  888. ctx->timeline->advance(dt);
  889. ctx->control_system->update(t, dt);
  890. ctx->terrain_system->update(t, dt);
  891. ctx->vegetation_system->update(t, dt);
  892. ctx->snapping_system->update(t, dt);
  893. ctx->nest_system->update(t, dt);
  894. ctx->subterrain_system->update(t, dt);
  895. ctx->collision_system->update(t, dt);
  896. ctx->samara_system->update(t, dt);
  897. ctx->behavior_system->update(t, dt);
  898. ctx->locomotion_system->update(t, dt);
  899. ctx->camera_system->update(t, dt);
  900. ctx->tool_system->update(t, dt);
  901. ctx->constraint_system->update(t, dt);
  902. (*ctx->focal_point_tween)[1] = ctx->orbit_cam->get_focal_point();
  903. ctx->ui_system->update(dt);
  904. ctx->render_system->update(t, dt);
  905. ctx->animator->animate(dt);
  906. ctx->application_controls->update();
  907. ctx->menu_controls->update();
  908. ctx->camera_controls->update();
  909. }
  910. );
  911. // Set render callback
  912. ctx->app->set_render_callback
  913. (
  914. [ctx](double alpha)
  915. {
  916. ctx->render_system->render(alpha);
  917. }
  918. );
  919. }