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

483 lines
10 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_APPLICATION_HPP
  20. #define ANTKEEPER_APPLICATION_HPP
  21. // STL
  22. #include <fstream>
  23. #include <functional>
  24. #include <list>
  25. #include <map>
  26. #include <string>
  27. // External
  28. #include <entt/entt.hpp>
  29. // Debug
  30. #include "debug/logger.hpp"
  31. #include "debug/performance-sampler.hpp"
  32. #include "debug/cli.hpp"
  33. // Input
  34. #include "input/control.hpp"
  35. #include "input/control-set.hpp"
  36. #include "input/keyboard.hpp"
  37. #include "input/mouse.hpp"
  38. #include "input/game-controller.hpp"
  39. #include "input/input-event-router.hpp"
  40. #include "input/input-mapper.hpp"
  41. // Event
  42. #include "event/event-dispatcher.hpp"
  43. // Renderer
  44. #include "renderer/compositor.hpp"
  45. #include "renderer/renderer.hpp"
  46. // Scene
  47. #include "scene/scene.hpp"
  48. #include "scene/camera.hpp"
  49. #include "scene/ambient-light.hpp"
  50. #include "scene/directional-light.hpp"
  51. #include "scene/point-light.hpp"
  52. #include "scene/spotlight.hpp"
  53. #include "scene/model-instance.hpp"
  54. // Animation
  55. #include "animation/frame-scheduler.hpp"
  56. #include "animation/timeline.hpp"
  57. #include "animation/tween.hpp"
  58. #include "animation/animation.hpp"
  59. // Misc
  60. #include "state/fsm.hpp"
  61. #include "pheromone-matrix.hpp"
  62. #include "orbit-cam.hpp"
  63. // Forward declarations
  64. //{
  65. // SDL
  66. typedef struct SDL_Window SDL_Window;
  67. typedef void* SDL_GLContext;
  68. // Resources
  69. class resource_manager;
  70. // Rasterizer
  71. class rasterizer;
  72. class framebuffer;
  73. class vertex_buffer;
  74. class vertex_array;
  75. class texture_2d;
  76. // Renderer
  77. class clear_pass;
  78. class shadow_map_pass;
  79. class material_pass;
  80. class sky_pass;
  81. class bloom_pass;
  82. class final_pass;
  83. class simple_render_pass;
  84. template <class T>
  85. class material_property;
  86. // Animation
  87. class animator;
  88. template <class T>
  89. class animation;
  90. class screen_transition;
  91. // Systems
  92. class behavior_system;
  93. class camera_system;
  94. class collision_system;
  95. class locomotion_system;
  96. class render_system;
  97. class nest_system;
  98. class placement_system;
  99. class samara_system;
  100. class subterrain_system;
  101. class terrain_system;
  102. class vegetation_system;
  103. class tool_system;
  104. class control_system;
  105. class constraint_system;
  106. class ui_system;
  107. // Scene
  108. class billboard;
  109. //}
  110. class application
  111. {
  112. public:
  113. /**
  114. * Creates and initializes an application.
  115. */
  116. application(int argc, char** argv);
  117. /**
  118. * Destroys an application.
  119. */
  120. ~application();
  121. /**
  122. * Executes the application, causing it to enter the execution loop until closed.
  123. *
  124. * @return Exit status code.
  125. */
  126. int execute();
  127. /**
  128. * Requests the application's execution loop to cleanly terminate, and specifies its exit status code.
  129. *
  130. * @param status Status to be returned by application::execute() upon execution loop termination.
  131. */
  132. void close(int status);
  133. logger* get_logger();
  134. cli* get_cli();
  135. resource_manager* get_resource_manager();
  136. fsm::machine* get_state_machine();
  137. const fsm::state& get_loading_state() const;
  138. const fsm::state& get_language_select_state() const;
  139. const fsm::state& get_splash_state() const;
  140. const fsm::state& get_title_state() const;
  141. const fsm::state& get_play_state() const;
  142. const fsm::state& get_pause_state() const;
  143. timeline* get_timeline();
  144. animator* get_animator();
  145. camera* get_overworld_camera();
  146. camera* get_underworld_camera();
  147. orbit_cam* get_orbit_cam();
  148. control_system* get_control_system();
  149. entt::registry& get_ecs_registry();
  150. scene& get_scene();
  151. void take_screenshot();
  152. // UI
  153. scene* get_ui_scene();
  154. billboard* get_splash_billboard();
  155. ::sky_pass* get_sky_pass();
  156. screen_transition* get_fade_transition();
  157. screen_transition* get_radial_transition_inner();
  158. screen_transition* get_radial_transition_outer();
  159. private:
  160. void setup_fsm();
  161. void load_config();
  162. void parse_options(int argc, char** argv);
  163. void update(double t, double dt);
  164. void render(double alpha);
  165. void translate_sdl_events();
  166. void set_relative_mouse_mode(bool enabled);
  167. void toggle_fullscreen();
  168. void window_resized();
  169. static void save_image(const std::string& filename, int w, int h, const unsigned char* pixels);
  170. bool fullscreen;
  171. bool vsync;
  172. std::tuple<int, int> saved_mouse_position;
  173. std::tuple<int, int> window_dimensions;
  174. std::tuple<int, int> window_position;
  175. std::tuple<int, int> display_dimensions;
  176. float4 viewport;
  177. // Debugging
  178. std::ofstream log_filestream;
  179. ::logger logger;
  180. ::cli cli;
  181. // Paths
  182. std::string data_path;
  183. std::string data_package_path;
  184. std::string config_path;
  185. std::string mods_path;
  186. std::string saves_path;
  187. std::string screenshots_path;
  188. // Resources
  189. resource_manager* resource_manager;
  190. SDL_Window* window;
  191. SDL_GLContext context;
  192. bool closed;
  193. int exit_status;
  194. // Updatable systems
  195. timeline timeline;
  196. animator* animator;
  197. animation<float>* radial_transition_in;
  198. animation<float>* radial_transition_out;
  199. std::list<std::function<void(double, double)>> systems;
  200. int shadow_map_resolution;
  201. framebuffer* shadow_map_framebuffer;
  202. texture_2d* shadow_map_depth_texture;
  203. framebuffer* framebuffer_hdr;
  204. texture_2d* framebuffer_hdr_color;
  205. texture_2d* framebuffer_hdr_depth;
  206. framebuffer* framebuffer_bloom; // General purpose framebuffer A
  207. texture_2d* bloom_texture;
  208. // Rendering
  209. rasterizer* rasterizer;
  210. material* fallback_material;
  211. ambient_light sun_indirect;
  212. directional_light sun_direct;
  213. point_light subterrain_light;
  214. ambient_light underworld_ambient_light;
  215. model_instance lantern;
  216. model_instance cloud;
  217. model_instance* grass_patches;
  218. ::spotlight spotlight;
  219. vertex_buffer* billboard_vbo;
  220. vertex_array* billboard_vao;
  221. ::renderer renderer;
  222. scene* active_scene;
  223. // Overworld
  224. scene overworld_scene;
  225. camera overworld_camera;
  226. ::clear_pass* clear_pass;
  227. ::sky_pass* sky_pass;
  228. ::material_pass* material_pass;
  229. ::clear_pass* shadow_map_clear_pass;
  230. ::shadow_map_pass* shadow_map_pass;
  231. ::bloom_pass* bloom_pass;
  232. ::final_pass* final_pass;
  233. compositor overworld_compositor;
  234. // Underworld
  235. scene underworld_scene;
  236. camera underworld_camera;
  237. ::clear_pass* underworld_clear_pass;
  238. ::material_pass* underworld_material_pass;
  239. simple_render_pass* underworld_final_pass;
  240. material_property<const texture_2d*>* underground_color_texture_property;
  241. compositor underworld_compositor;
  242. // FSM
  243. fsm::machine state_machine;
  244. fsm::state loading_state;
  245. fsm::state language_select_state;
  246. fsm::state splash_state;
  247. fsm::state title_state;
  248. fsm::state play_state;
  249. fsm::state pause_state;
  250. fsm::state* initial_state;
  251. // Frame timing
  252. frame_scheduler frame_scheduler;
  253. performance_sampler performance_sampler;
  254. tween<double> time;
  255. // Events
  256. event_dispatcher event_dispatcher;
  257. input_event_router input_event_router;
  258. input_mapper input_mapper;
  259. // Input devices
  260. keyboard keyboard;
  261. mouse mouse;
  262. game_controller game_controller;
  263. // Controls
  264. control_set menu_controls;
  265. control menu_back_control;
  266. control menu_select_control;
  267. control_set* camera_controls;
  268. // System controls
  269. control_set application_controls;
  270. control toggle_fullscreen_control;
  271. control screenshot_control;
  272. control dig_control;
  273. // Game
  274. orbit_cam orbit_cam;
  275. pheromone_matrix pheromones;
  276. control_system* control_system;
  277. // ECS
  278. entt::registry ecs_registry;
  279. behavior_system* behavior_system;
  280. camera_system* camera_system;
  281. collision_system* collision_system;
  282. locomotion_system* locomotion_system;
  283. render_system* render_system;
  284. nest_system* nest_system;
  285. placement_system* placement_system;
  286. samara_system* samara_system;
  287. subterrain_system* subterrain_system;
  288. terrain_system* terrain_system;
  289. vegetation_system* vegetation_system;
  290. tool_system* tool_system;
  291. constraint_system* constraint_system;
  292. // UI
  293. ui_system* ui_system;
  294. compositor ui_compositor;
  295. ::clear_pass* ui_clear_pass;
  296. ::material_pass* ui_material_pass;
  297. billboard* splash_billboard;
  298. material* splash_billboard_material;
  299. // Animation
  300. tween<float3> focal_point_tween;
  301. screen_transition* fade_transition;
  302. screen_transition* radial_transition_inner;
  303. screen_transition* radial_transition_outer;
  304. // Entities
  305. entt::entity forceps_entity;
  306. entt::entity lens_entity;
  307. entt::entity brush_entity;
  308. entt::entity flashlight_entity;
  309. };
  310. inline logger* application::get_logger()
  311. {
  312. return &logger;
  313. }
  314. inline cli* application::get_cli()
  315. {
  316. return &cli;
  317. }
  318. inline resource_manager* application::get_resource_manager()
  319. {
  320. return resource_manager;
  321. }
  322. inline fsm::machine* application::get_state_machine()
  323. {
  324. return &state_machine;
  325. }
  326. inline const fsm::state& application::get_loading_state() const
  327. {
  328. return loading_state;
  329. }
  330. inline const fsm::state& application::get_language_select_state() const
  331. {
  332. return language_select_state;
  333. }
  334. inline const fsm::state& application::get_splash_state() const
  335. {
  336. return splash_state;
  337. }
  338. inline const fsm::state& application::get_title_state() const
  339. {
  340. return title_state;
  341. }
  342. inline const fsm::state& application::get_play_state() const
  343. {
  344. return play_state;
  345. }
  346. inline const fsm::state& application::get_pause_state() const
  347. {
  348. return pause_state;
  349. }
  350. inline timeline* application::get_timeline()
  351. {
  352. return &timeline;
  353. }
  354. inline animator* application::get_animator()
  355. {
  356. return animator;
  357. }
  358. inline camera* application::get_overworld_camera()
  359. {
  360. return &overworld_camera;
  361. }
  362. inline camera* application::get_underworld_camera()
  363. {
  364. return &underworld_camera;
  365. }
  366. inline orbit_cam* application::get_orbit_cam()
  367. {
  368. return &orbit_cam;
  369. }
  370. inline control_system* application::get_control_system()
  371. {
  372. return control_system;
  373. }
  374. inline entt::registry& application::get_ecs_registry()
  375. {
  376. return ecs_registry;
  377. }
  378. inline scene& application::get_scene()
  379. {
  380. return overworld_scene;
  381. }
  382. inline billboard* application::get_splash_billboard()
  383. {
  384. return splash_billboard;
  385. }
  386. inline sky_pass* application::get_sky_pass()
  387. {
  388. return sky_pass;
  389. }
  390. inline screen_transition* application::get_fade_transition()
  391. {
  392. return fade_transition;
  393. }
  394. inline screen_transition* application::get_radial_transition_inner()
  395. {
  396. return radial_transition_inner;
  397. }
  398. inline screen_transition* application::get_radial_transition_outer()
  399. {
  400. return radial_transition_outer;
  401. }
  402. #endif // ANTKEEPER_APPLICATION_HPP