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

471 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/timeline.hpp"
  56. #include "animation/tween.hpp"
  57. #include "animation/animation.hpp"
  58. // Misc
  59. #include "state/fsm.hpp"
  60. #include "frame-scheduler.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 model_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 update(double t, double dt);
  161. void render(double alpha);
  162. void translate_sdl_events();
  163. void set_relative_mouse_mode(bool enabled);
  164. void toggle_fullscreen();
  165. void window_resized();
  166. static void save_image(const std::string& filename, int w, int h, const unsigned char* pixels);
  167. bool fullscreen;
  168. std::tuple<int, int> saved_mouse_position;
  169. std::tuple<int, int> window_dimensions;
  170. std::tuple<int, int> window_position;
  171. std::tuple<int, int> display_dimensions;
  172. float4 viewport;
  173. // Debugging
  174. std::ofstream log_filestream;
  175. ::logger logger;
  176. ::cli cli;
  177. // Paths
  178. std::string data_path;
  179. std::string data_package_path;
  180. std::string config_path;
  181. std::string mods_path;
  182. std::string saves_path;
  183. std::string screenshots_path;
  184. // Resources
  185. resource_manager* resource_manager;
  186. SDL_Window* window;
  187. SDL_GLContext context;
  188. bool closed;
  189. int exit_status;
  190. // Updatable systems
  191. timeline timeline;
  192. animator* animator;
  193. animation<float>* radial_transition_in;
  194. animation<float>* radial_transition_out;
  195. std::list<std::function<void(double, double)>> systems;
  196. int shadow_map_resolution;
  197. framebuffer* shadow_map_framebuffer;
  198. texture_2d* shadow_map_depth_texture;
  199. framebuffer* framebuffer_hdr;
  200. texture_2d* framebuffer_hdr_color;
  201. texture_2d* framebuffer_hdr_depth;
  202. framebuffer* framebuffer_bloom; // General purpose framebuffer A
  203. texture_2d* bloom_texture;
  204. // Rendering
  205. rasterizer* rasterizer;
  206. material* fallback_material;
  207. ambient_light sun_indirect;
  208. directional_light sun_direct;
  209. point_light subterrain_light;
  210. ambient_light underworld_ambient_light;
  211. model_instance lantern;
  212. model_instance cloud;
  213. model_instance* grass_patches;
  214. ::spotlight spotlight;
  215. vertex_buffer* billboard_vbo;
  216. vertex_array* billboard_vao;
  217. ::renderer renderer;
  218. scene* active_scene;
  219. // Overworld
  220. scene overworld_scene;
  221. camera overworld_camera;
  222. ::clear_pass* clear_pass;
  223. ::sky_pass* sky_pass;
  224. ::material_pass* material_pass;
  225. ::clear_pass* shadow_map_clear_pass;
  226. ::shadow_map_pass* shadow_map_pass;
  227. ::bloom_pass* bloom_pass;
  228. ::final_pass* final_pass;
  229. compositor overworld_compositor;
  230. // Underworld
  231. scene underworld_scene;
  232. camera underworld_camera;
  233. ::clear_pass* underworld_clear_pass;
  234. ::material_pass* underworld_material_pass;
  235. simple_render_pass* underworld_final_pass;
  236. material_property<const texture_2d*>* underground_color_texture_property;
  237. compositor underworld_compositor;
  238. // FSM
  239. fsm::machine state_machine;
  240. fsm::state loading_state;
  241. fsm::state language_select_state;
  242. fsm::state splash_state;
  243. fsm::state title_state;
  244. fsm::state play_state;
  245. fsm::state pause_state;
  246. fsm::state* initial_state;
  247. // Frame timing
  248. frame_scheduler frame_scheduler;
  249. performance_sampler performance_sampler;
  250. tween<double> time;
  251. // Events
  252. event_dispatcher event_dispatcher;
  253. input_event_router input_event_router;
  254. input_mapper input_mapper;
  255. // Input devices
  256. keyboard keyboard;
  257. mouse mouse;
  258. game_controller game_controller;
  259. // Controls
  260. control_set menu_controls;
  261. control menu_back_control;
  262. control menu_select_control;
  263. control_set* camera_controls;
  264. // System controls
  265. control_set application_controls;
  266. control toggle_fullscreen_control;
  267. control screenshot_control;
  268. control dig_control;
  269. // Game
  270. orbit_cam orbit_cam;
  271. pheromone_matrix pheromones;
  272. control_system* control_system;
  273. // ECS
  274. entt::registry ecs_registry;
  275. behavior_system* behavior_system;
  276. camera_system* camera_system;
  277. collision_system* collision_system;
  278. locomotion_system* locomotion_system;
  279. model_system* model_system;
  280. nest_system* nest_system;
  281. placement_system* placement_system;
  282. samara_system* samara_system;
  283. subterrain_system* subterrain_system;
  284. terrain_system* terrain_system;
  285. vegetation_system* vegetation_system;
  286. tool_system* tool_system;
  287. constraint_system* constraint_system;
  288. // UI
  289. ui_system* ui_system;
  290. compositor ui_compositor;
  291. ::clear_pass* ui_clear_pass;
  292. ::material_pass* ui_material_pass;
  293. billboard* splash_billboard;
  294. material* splash_billboard_material;
  295. // Animation
  296. tween<float3> focal_point_tween;
  297. screen_transition* fade_transition;
  298. screen_transition* radial_transition_inner;
  299. screen_transition* radial_transition_outer;
  300. };
  301. inline logger* application::get_logger()
  302. {
  303. return &logger;
  304. }
  305. inline cli* application::get_cli()
  306. {
  307. return &cli;
  308. }
  309. inline resource_manager* application::get_resource_manager()
  310. {
  311. return resource_manager;
  312. }
  313. inline fsm::machine* application::get_state_machine()
  314. {
  315. return &state_machine;
  316. }
  317. inline const fsm::state& application::get_loading_state() const
  318. {
  319. return loading_state;
  320. }
  321. inline const fsm::state& application::get_language_select_state() const
  322. {
  323. return language_select_state;
  324. }
  325. inline const fsm::state& application::get_splash_state() const
  326. {
  327. return splash_state;
  328. }
  329. inline const fsm::state& application::get_title_state() const
  330. {
  331. return title_state;
  332. }
  333. inline const fsm::state& application::get_play_state() const
  334. {
  335. return play_state;
  336. }
  337. inline const fsm::state& application::get_pause_state() const
  338. {
  339. return pause_state;
  340. }
  341. inline timeline* application::get_timeline()
  342. {
  343. return &timeline;
  344. }
  345. inline animator* application::get_animator()
  346. {
  347. return animator;
  348. }
  349. inline camera* application::get_overworld_camera()
  350. {
  351. return &overworld_camera;
  352. }
  353. inline camera* application::get_underworld_camera()
  354. {
  355. return &underworld_camera;
  356. }
  357. inline orbit_cam* application::get_orbit_cam()
  358. {
  359. return &orbit_cam;
  360. }
  361. inline control_system* application::get_control_system()
  362. {
  363. return control_system;
  364. }
  365. inline entt::registry& application::get_ecs_registry()
  366. {
  367. return ecs_registry;
  368. }
  369. inline scene& application::get_scene()
  370. {
  371. return overworld_scene;
  372. }
  373. inline billboard* application::get_splash_billboard()
  374. {
  375. return splash_billboard;
  376. }
  377. inline sky_pass* application::get_sky_pass()
  378. {
  379. return sky_pass;
  380. }
  381. inline screen_transition* application::get_fade_transition()
  382. {
  383. return fade_transition;
  384. }
  385. inline screen_transition* application::get_radial_transition_inner()
  386. {
  387. return radial_transition_inner;
  388. }
  389. inline screen_transition* application::get_radial_transition_outer()
  390. {
  391. return radial_transition_outer;
  392. }
  393. #endif // ANTKEEPER_APPLICATION_HPP