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

410 lines
13 KiB

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