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

1309 lines
53 KiB

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