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

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