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

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