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

454 lines
15 KiB

1 year ago
1 year ago
  1. /*
  2. * Copyright (C) 2023 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. #ifndef ANTKEEPER_GAME_HPP
  20. #define ANTKEEPER_GAME_HPP
  21. #include "game/ecoregion.hpp"
  22. #include "game/states/game-state.hpp"
  23. #include <AL/al.h>
  24. #include <AL/alc.h>
  25. #include <engine/animation/tween.hpp>
  26. #include <engine/app/input-manager.hpp>
  27. #include <engine/app/window-manager.hpp>
  28. #include <engine/entity/id.hpp>
  29. #include <engine/entity/registry.hpp>
  30. #include <engine/event/subscription.hpp>
  31. #include <engine/gl/framebuffer.hpp>
  32. #include <engine/gl/texture.hpp>
  33. #include <engine/i18n/string-map.hpp>
  34. #include <engine/input/action-map.hpp>
  35. #include <engine/input/action.hpp>
  36. #include <engine/input/mapper.hpp>
  37. #include <engine/math/moving-average.hpp>
  38. #include <engine/render/anti-aliasing-method.hpp>
  39. #include <engine/render/material-variable.hpp>
  40. #include <engine/render/material.hpp>
  41. #include <engine/type/bitmap-font.hpp>
  42. #include <engine/type/typeface.hpp>
  43. #include <engine/utility/dict.hpp>
  44. #include <engine/math/vector.hpp>
  45. #include <engine/utility/state-machine.hpp>
  46. #include <engine/utility/frame-scheduler.hpp>
  47. #include <engine/scene/text.hpp>
  48. #include <engine/scene/directional-light.hpp>
  49. #include <engine/scene/rectangle-light.hpp>
  50. #include <engine/scene/spot-light.hpp>
  51. #include <engine/scene/camera.hpp>
  52. #include <engine/scene/billboard.hpp>
  53. #include <engine/scene/collection.hpp>
  54. #include <engine/scene/text.hpp>
  55. #include <engine/math/angles.hpp>
  56. #include <entt/entt.hpp>
  57. #include <filesystem>
  58. #include <memory>
  59. #include <optional>
  60. #include <queue>
  61. #include <string>
  62. #include <unordered_map>
  63. #include <random>
  64. #include <vector>
  65. // Forward declarations
  66. class animator;
  67. class resource_manager;
  68. class screen_transition;
  69. class timeline;
  70. template <typename T> class animation;
  71. namespace debug
  72. {
  73. class cli;
  74. }
  75. namespace render
  76. {
  77. class bloom_pass;
  78. class compositor;
  79. class final_pass;
  80. class resample_pass;
  81. class material_pass;
  82. class renderer;
  83. class simple_render_pass;
  84. class sky_pass;
  85. }
  86. // Forward declarations of system types.
  87. class astronomy_system;
  88. class atmosphere_system;
  89. class behavior_system;
  90. class blackbody_system;
  91. class camera_system;
  92. class collision_system;
  93. class constraint_system;
  94. class locomotion_system;
  95. class animation_system;
  96. class nest_system;
  97. class orbit_system;
  98. class render_system;
  99. class spatial_system;
  100. class steering_system;
  101. class reproductive_system;
  102. class metabolic_system;
  103. class metamorphosis_system;
  104. class physics_system;
  105. class subterrain_system;
  106. class terrain_system;
  107. class ik_system;
  108. struct control_profile;
  109. class game
  110. {
  111. public:
  112. /**
  113. * Boots up the game.
  114. *
  115. * @param argc Command line argument count.
  116. * @param argv Command line argument vector.
  117. */
  118. game(int argc, const char* const* argv);
  119. /**
  120. * Boots down the game.
  121. */
  122. ~game();
  123. /**
  124. * Executes the game.
  125. */
  126. void execute();
  127. // Command-line options
  128. std::optional<bool> option_continue;
  129. std::optional<std::string> option_data;
  130. std::optional<bool> option_fullscreen;
  131. std::optional<bool> option_new_game;
  132. std::optional<bool> option_quick_start;
  133. std::optional<bool> option_reset;
  134. std::optional<bool> option_v_sync;
  135. std::optional<bool> option_windowed;
  136. // Resource management and paths
  137. std::unique_ptr<resource_manager> resource_manager;
  138. std::filesystem::path data_package_path;
  139. std::filesystem::path mods_path;
  140. std::filesystem::path local_config_path;
  141. std::filesystem::path shared_config_path;
  142. std::filesystem::path saves_path;
  143. std::filesystem::path screenshots_path;
  144. std::filesystem::path controls_path;
  145. // Persistent settings
  146. std::shared_ptr<dict<hash::fnv1a32_t>> settings;
  147. // Window management and window event handling
  148. std::unique_ptr<app::window_manager> window_manager;
  149. std::shared_ptr<app::window> window;
  150. bool closed;
  151. std::shared_ptr<::event::subscription> window_closed_subscription;
  152. std::shared_ptr<::event::subscription> window_resized_subscription;
  153. // Input management and input event handling
  154. std::unique_ptr<app::input_manager> input_manager;
  155. std::shared_ptr<::event::subscription> application_quit_subscription;
  156. std::shared_ptr<::event::subscription> gamepad_axis_moved_subscription;
  157. std::shared_ptr<::event::subscription> gamepad_button_pressed_subscription;
  158. std::shared_ptr<::event::subscription> mouse_button_pressed_subscription;
  159. std::shared_ptr<::event::subscription> mouse_moved_subscription;
  160. std::shared_ptr<::event::subscription> mouse_scrolled_subscription;
  161. bool gamepad_active;
  162. // Localization and internationalization
  163. std::string language_tag;
  164. std::shared_ptr<i18n::string_map> string_map;
  165. // Fonts
  166. std::unordered_map<hash::fnv1a32_t, std::shared_ptr<type::typeface>> typefaces;
  167. type::bitmap_font debug_font;
  168. type::bitmap_font menu_font;
  169. type::bitmap_font title_font;
  170. std::shared_ptr<render::material> debug_font_material;
  171. std::shared_ptr<render::material> menu_font_material;
  172. std::shared_ptr<render::material> title_font_material;
  173. // Action maps, actions, and action event handling
  174. std::string control_profile_filename;
  175. std::shared_ptr<::control_profile> control_profile;
  176. std::unordered_map<hash::fnv1a32_t, input::action> actions;
  177. input::action_map window_action_map;
  178. input::action fullscreen_action;
  179. input::action screenshot_action;
  180. input::action_map menu_action_map;
  181. input::action menu_up_action;
  182. input::action menu_down_action;
  183. input::action menu_left_action;
  184. input::action menu_right_action;
  185. input::action menu_select_action;
  186. input::action menu_back_action;
  187. input::action menu_modifier_action;
  188. input::action_map movement_action_map;
  189. input::mapper input_mapper;
  190. input::action move_forward_action;
  191. input::action move_back_action;
  192. input::action move_left_action;
  193. input::action move_right_action;
  194. input::action move_up_action;
  195. input::action move_down_action;
  196. input::action move_fast_action;
  197. input::action move_slow_action;
  198. input::action pause_action;
  199. input::action_map camera_action_map;
  200. input::action camera_mouse_pick_action;
  201. input::action camera_mouse_look_action;
  202. input::action camera_mouse_drag_action;
  203. input::action camera_mouse_zoom_action;
  204. input::action camera_zoom_in_action;
  205. input::action camera_zoom_out_action;
  206. input::action camera_orbit_left_action;
  207. input::action camera_orbit_right_action;
  208. input::action camera_orbit_up_action;
  209. input::action camera_orbit_down_action;
  210. input::action camera_look_ahead_action;
  211. input::action camera_preset_1_action;
  212. input::action camera_preset_2_action;
  213. input::action camera_preset_3_action;
  214. input::action camera_preset_4_action;
  215. input::action camera_preset_5_action;
  216. input::action camera_preset_6_action;
  217. input::action camera_preset_7_action;
  218. input::action camera_preset_8_action;
  219. input::action camera_preset_9_action;
  220. input::action camera_preset_10_action;
  221. input::action camera_save_preset_action;
  222. input::action_map ant_action_map;
  223. input::action ant_move_forward_action;
  224. input::action ant_move_back_action;
  225. input::action ant_move_left_action;
  226. input::action ant_move_right_action;
  227. input::action ant_move_fast_action;
  228. input::action ant_move_slow_action;
  229. input::action ant_interact_action;
  230. input::action ant_oviposit_action;
  231. input::action_map debug_action_map;
  232. input::action toggle_debug_ui_action;
  233. input::action adjust_exposure_action;
  234. input::action adjust_time_action;
  235. std::vector<std::shared_ptr<::event::subscription>> event_subscriptions;
  236. std::vector<std::shared_ptr<::event::subscription>> menu_action_subscriptions;
  237. std::vector<std::shared_ptr<::event::subscription>> menu_mouse_subscriptions;
  238. std::vector<std::shared_ptr<::event::subscription>> movement_action_subscriptions;
  239. // Mouse settings
  240. double mouse_radians_per_pixel{math::radians(0.1)};
  241. double mouse_pan_sensitivity{1.0};
  242. double mouse_tilt_sensitivity{1.0};
  243. bool mouse_invert_pan{false};
  244. bool mouse_invert_tilt{false};
  245. double mouse_pan_factor{1.0};
  246. double mouse_tilt_factor{1.0};
  247. bool toggle_mouse_look{false};
  248. bool toggle_mouse_grip{false};
  249. bool toggle_mouse_zoom{false};
  250. float zoom_steps{6.0};
  251. // Gamepad settings
  252. double gamepad_radians_per_second{math::radians(180.0)};
  253. double gamepad_pan_sensitivity{1.0};
  254. double gamepad_tilt_sensitivity{1.0};
  255. bool gamepad_invert_pan{false};
  256. bool gamepad_invert_tilt{false};
  257. double gamepad_pan_factor{1.0};
  258. double gamepad_tilt_factor{1.0};
  259. // Debugging
  260. bool debug_ui_visible{false};
  261. std::unique_ptr<scene::text> frame_time_text;
  262. std::unique_ptr<debug::cli> cli;
  263. // Hierarchichal state machine
  264. hsm::state_machine<game_state> state_machine;
  265. std::function<void()> resume_callback;
  266. // Queue for scheduling "next frame" function calls
  267. std::queue<std::function<void()>> function_queue;
  268. // Framebuffers
  269. std::shared_ptr<gl::texture_2d> hdr_color_texture;
  270. std::shared_ptr<gl::texture_2d> hdr_depth_texture;
  271. std::shared_ptr<gl::framebuffer> hdr_framebuffer;
  272. std::shared_ptr<gl::texture_2d> ldr_color_texture_a;
  273. std::shared_ptr<gl::framebuffer> ldr_framebuffer_a;
  274. std::shared_ptr<gl::texture_2d> ldr_color_texture_b;
  275. std::shared_ptr<gl::framebuffer> ldr_framebuffer_b;
  276. std::shared_ptr<gl::texture_2d> shadow_map_depth_texture;
  277. std::shared_ptr<gl::framebuffer> shadow_map_framebuffer;
  278. // Rendering
  279. math::ivec2 render_resolution;
  280. float render_scale;
  281. int shadow_map_resolution;
  282. std::unique_ptr<render::material_pass> ui_material_pass;
  283. std::unique_ptr<render::compositor> ui_compositor;
  284. std::unique_ptr<render::bloom_pass> bloom_pass;
  285. std::unique_ptr<render::final_pass> common_final_pass;
  286. std::unique_ptr<render::resample_pass> resample_pass;
  287. std::unique_ptr<render::material_pass> underground_material_pass;
  288. std::unique_ptr<render::compositor> underground_compositor;
  289. std::unique_ptr<render::sky_pass> sky_pass;
  290. std::unique_ptr<render::material_pass> surface_material_pass;
  291. std::unique_ptr<render::compositor> surface_compositor;
  292. std::unique_ptr<render::renderer> renderer;
  293. // UI
  294. std::unique_ptr<scene::collection> ui_scene;
  295. std::unique_ptr<scene::camera> ui_camera;
  296. std::unique_ptr<scene::billboard> menu_bg_billboard;
  297. std::shared_ptr<render::material> menu_bg_material;
  298. std::unique_ptr<animation<float>> menu_fade_animation;
  299. std::unique_ptr<animation<float>> menu_bg_fade_in_animation;
  300. std::unique_ptr<animation<float>> menu_bg_fade_out_animation;
  301. float font_scale;
  302. bool dyslexia_font;
  303. float debug_font_size_pt;
  304. float menu_font_size_pt;
  305. float title_font_size_pt;
  306. std::vector<std::function<void()>> menu_select_callbacks;
  307. std::vector<std::function<void()>> menu_left_callbacks;
  308. std::vector<std::function<void()>> menu_right_callbacks;
  309. std::function<void()> menu_back_callback;
  310. std::vector<std::tuple<scene::text*, scene::text*>> menu_item_texts;
  311. std::unordered_map<hash::fnv1a32_t, int> menu_item_indices;
  312. int* menu_item_index;
  313. // Scene
  314. std::unique_ptr<scene::collection> surface_scene;
  315. std::shared_ptr<scene::camera> surface_camera;
  316. std::unique_ptr<scene::directional_light> sun_light;
  317. std::unique_ptr<scene::directional_light> moon_light;
  318. std::unique_ptr<scene::collection> underground_scene;
  319. std::shared_ptr<scene::camera> underground_camera;
  320. std::unique_ptr<scene::directional_light> underground_directional_light;
  321. std::unique_ptr<scene::rectangle_light> underground_rectangle_light;
  322. scene::collection* active_scene;
  323. // Animation
  324. std::unique_ptr<timeline> timeline;
  325. std::unique_ptr<animator> animator;
  326. std::unique_ptr<animation<float>> radial_transition_in;
  327. std::unique_ptr<animation<float>> radial_transition_out;
  328. std::unique_ptr<screen_transition> fade_transition;
  329. std::shared_ptr<render::matvar_fvec3> fade_transition_color;
  330. std::unique_ptr<screen_transition> radial_transition_inner;
  331. std::unique_ptr<screen_transition> radial_transition_outer;
  332. std::unique_ptr<animation<float>> equip_tool_animation;
  333. std::unique_ptr<animation<float>> unequip_tool_animation;
  334. // Sound
  335. ALCdevice* alc_device;
  336. ALCcontext* alc_context;
  337. float master_volume;
  338. float ambience_volume;
  339. float effects_volume;
  340. bool mono_audio;
  341. bool captions;
  342. float captions_size;
  343. // Random number generation
  344. std::mt19937 rng;
  345. // Entities
  346. std::unique_ptr<entity::registry> entity_registry;
  347. std::unordered_map<hash::fnv1a32_t, entity::id> entities;
  348. entity::id controlled_ant_eid{entt::null};
  349. entity::id active_camera_eid{entt::null};
  350. // Systems
  351. std::unique_ptr<::behavior_system> behavior_system;
  352. std::unique_ptr<::camera_system> camera_system;
  353. std::unique_ptr<::collision_system> collision_system;
  354. std::unique_ptr<::constraint_system> constraint_system;
  355. std::unique_ptr<::steering_system> steering_system;
  356. std::unique_ptr<::reproductive_system> reproductive_system;
  357. std::unique_ptr<::metabolic_system> metabolic_system;
  358. std::unique_ptr<::metamorphosis_system> metamorphosis_system;
  359. std::unique_ptr<::locomotion_system> locomotion_system;
  360. std::unique_ptr<::ik_system> ik_system;
  361. std::unique_ptr<::animation_system> animation_system;
  362. std::unique_ptr<::physics_system> physics_system;
  363. std::unique_ptr<::render_system> render_system;
  364. std::unique_ptr<::subterrain_system> subterrain_system;
  365. std::unique_ptr<::terrain_system> terrain_system;
  366. std::unique_ptr<::spatial_system> spatial_system;
  367. std::unique_ptr<::blackbody_system> blackbody_system;
  368. std::unique_ptr<::atmosphere_system> atmosphere_system;
  369. std::unique_ptr<::astronomy_system> astronomy_system;
  370. std::unique_ptr<::orbit_system> orbit_system;
  371. // Frame timing
  372. float fixed_update_rate{60.0};
  373. float max_frame_rate{120.0};
  374. bool limit_frame_rate{false};
  375. ::frame_scheduler frame_scheduler;
  376. math::moving_average<float> average_frame_duration;
  377. std::shared_ptr<ecoregion> active_ecoregion;
  378. render::anti_aliasing_method anti_aliasing_method;
  379. private:
  380. void parse_options(int argc, const char* const* argv);
  381. void setup_resources();
  382. void load_settings();
  383. void setup_window();
  384. void setup_input();
  385. void load_strings();
  386. void setup_rendering();
  387. void setup_audio();
  388. void setup_scenes();
  389. void setup_animation();
  390. void setup_ui();
  391. void setup_rng();
  392. void setup_entities();
  393. void setup_systems();
  394. void setup_controls();
  395. void setup_debugging();
  396. void setup_timing();
  397. void shutdown_audio();
  398. void fixed_update(::frame_scheduler::duration_type fixed_update_time, ::frame_scheduler::duration_type fixed_update_interval);
  399. void variable_update(::frame_scheduler::duration_type fixed_update_time, ::frame_scheduler::duration_type fixed_update_interval, ::frame_scheduler::duration_type accumulated_time);
  400. };
  401. #endif // ANTKEEPER_GAME_HPP