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

466 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 ui_system;
  106. // Scene
  107. class billboard;
  108. //}
  109. class application
  110. {
  111. public:
  112. /**
  113. * Creates and initializes an application.
  114. */
  115. application(int argc, char** argv);
  116. /**
  117. * Destroys an application.
  118. */
  119. ~application();
  120. /**
  121. * Executes the application, causing it to enter the execution loop until closed.
  122. *
  123. * @return Exit status code.
  124. */
  125. int execute();
  126. /**
  127. * Requests the application's execution loop to cleanly terminate, and specifies its exit status code.
  128. *
  129. * @param status Status to be returned by application::execute() upon execution loop termination.
  130. */
  131. void close(int status);
  132. logger* get_logger();
  133. cli* get_cli();
  134. resource_manager* get_resource_manager();
  135. fsm::machine* get_state_machine();
  136. const fsm::state& get_loading_state() const;
  137. const fsm::state& get_language_select_state() const;
  138. const fsm::state& get_splash_state() const;
  139. const fsm::state& get_title_state() const;
  140. const fsm::state& get_play_state() const;
  141. const fsm::state& get_pause_state() const;
  142. timeline* get_timeline();
  143. animator* get_animator();
  144. camera* get_overworld_camera();
  145. camera* get_underworld_camera();
  146. orbit_cam* get_orbit_cam();
  147. control_system* get_control_system();
  148. entt::registry& get_ecs_registry();
  149. scene& get_scene();
  150. void take_screenshot();
  151. // UI
  152. scene* get_ui_scene();
  153. billboard* get_splash_billboard();
  154. ::sky_pass* get_sky_pass();
  155. screen_transition* get_fade_transition();
  156. screen_transition* get_radial_transition_inner();
  157. screen_transition* get_radial_transition_outer();
  158. private:
  159. void update(double t, double dt);
  160. void render(double alpha);
  161. void translate_sdl_events();
  162. void set_relative_mouse_mode(bool enabled);
  163. void toggle_fullscreen();
  164. void window_resized();
  165. static void save_image(const std::string& filename, int w, int h, const unsigned char* pixels);
  166. bool fullscreen;
  167. std::tuple<int, int> saved_mouse_position;
  168. std::tuple<int, int> window_dimensions;
  169. std::tuple<int, int> window_position;
  170. std::tuple<int, int> display_dimensions;
  171. float4 viewport;
  172. // Debugging
  173. std::ofstream log_filestream;
  174. ::logger logger;
  175. ::cli cli;
  176. // Paths
  177. std::string data_path;
  178. std::string config_path;
  179. std::string screenshots_path;
  180. // Resources
  181. resource_manager* resource_manager;
  182. SDL_Window* window;
  183. SDL_GLContext context;
  184. bool closed;
  185. int exit_status;
  186. // Updatable systems
  187. timeline timeline;
  188. animator* animator;
  189. animation<float>* radial_transition_in;
  190. animation<float>* radial_transition_out;
  191. std::list<std::function<void(double, double)>> systems;
  192. int shadow_map_resolution;
  193. framebuffer* shadow_map_framebuffer;
  194. texture_2d* shadow_map_depth_texture;
  195. framebuffer* framebuffer_hdr;
  196. texture_2d* framebuffer_hdr_color;
  197. texture_2d* framebuffer_hdr_depth;
  198. framebuffer* framebuffer_bloom; // General purpose framebuffer A
  199. texture_2d* bloom_texture;
  200. // Rendering
  201. rasterizer* rasterizer;
  202. material* fallback_material;
  203. ambient_light sun_indirect;
  204. directional_light sun_direct;
  205. point_light subterrain_light;
  206. ambient_light underworld_ambient_light;
  207. model_instance lantern;
  208. model_instance cloud;
  209. model_instance* grass_patches;
  210. ::spotlight spotlight;
  211. vertex_buffer* billboard_vbo;
  212. vertex_array* billboard_vao;
  213. ::renderer renderer;
  214. scene* active_scene;
  215. // Overworld
  216. scene overworld_scene;
  217. camera overworld_camera;
  218. ::clear_pass* clear_pass;
  219. ::sky_pass* sky_pass;
  220. ::material_pass* material_pass;
  221. ::clear_pass* shadow_map_clear_pass;
  222. ::shadow_map_pass* shadow_map_pass;
  223. ::bloom_pass* bloom_pass;
  224. ::final_pass* final_pass;
  225. compositor overworld_compositor;
  226. // Underworld
  227. scene underworld_scene;
  228. camera underworld_camera;
  229. ::clear_pass* underworld_clear_pass;
  230. ::material_pass* underworld_material_pass;
  231. simple_render_pass* underworld_final_pass;
  232. material_property<const texture_2d*>* underground_color_texture_property;
  233. compositor underworld_compositor;
  234. // FSM
  235. fsm::machine state_machine;
  236. fsm::state loading_state;
  237. fsm::state language_select_state;
  238. fsm::state splash_state;
  239. fsm::state title_state;
  240. fsm::state play_state;
  241. fsm::state pause_state;
  242. fsm::state* initial_state;
  243. // Frame timing
  244. frame_scheduler frame_scheduler;
  245. performance_sampler performance_sampler;
  246. tween<double> time;
  247. // Events
  248. event_dispatcher event_dispatcher;
  249. input_event_router input_event_router;
  250. input_mapper input_mapper;
  251. // Input devices
  252. keyboard keyboard;
  253. mouse mouse;
  254. game_controller game_controller;
  255. // Controls
  256. control_set menu_controls;
  257. control menu_back_control;
  258. control menu_select_control;
  259. control_set* camera_controls;
  260. // System controls
  261. control_set application_controls;
  262. control toggle_fullscreen_control;
  263. control screenshot_control;
  264. control dig_control;
  265. // Game
  266. orbit_cam orbit_cam;
  267. pheromone_matrix pheromones;
  268. control_system* control_system;
  269. // ECS
  270. entt::registry ecs_registry;
  271. behavior_system* behavior_system;
  272. camera_system* camera_system;
  273. collision_system* collision_system;
  274. locomotion_system* locomotion_system;
  275. model_system* model_system;
  276. nest_system* nest_system;
  277. placement_system* placement_system;
  278. samara_system* samara_system;
  279. subterrain_system* subterrain_system;
  280. terrain_system* terrain_system;
  281. vegetation_system* vegetation_system;
  282. tool_system* tool_system;
  283. // UI
  284. ui_system* ui_system;
  285. compositor ui_compositor;
  286. ::clear_pass* ui_clear_pass;
  287. ::material_pass* ui_material_pass;
  288. billboard* splash_billboard;
  289. material* splash_billboard_material;
  290. // Animation
  291. tween<float3> focal_point_tween;
  292. screen_transition* fade_transition;
  293. screen_transition* radial_transition_inner;
  294. screen_transition* radial_transition_outer;
  295. };
  296. inline logger* application::get_logger()
  297. {
  298. return &logger;
  299. }
  300. inline cli* application::get_cli()
  301. {
  302. return &cli;
  303. }
  304. inline resource_manager* application::get_resource_manager()
  305. {
  306. return resource_manager;
  307. }
  308. inline fsm::machine* application::get_state_machine()
  309. {
  310. return &state_machine;
  311. }
  312. inline const fsm::state& application::get_loading_state() const
  313. {
  314. return loading_state;
  315. }
  316. inline const fsm::state& application::get_language_select_state() const
  317. {
  318. return language_select_state;
  319. }
  320. inline const fsm::state& application::get_splash_state() const
  321. {
  322. return splash_state;
  323. }
  324. inline const fsm::state& application::get_title_state() const
  325. {
  326. return title_state;
  327. }
  328. inline const fsm::state& application::get_play_state() const
  329. {
  330. return play_state;
  331. }
  332. inline const fsm::state& application::get_pause_state() const
  333. {
  334. return pause_state;
  335. }
  336. inline timeline* application::get_timeline()
  337. {
  338. return &timeline;
  339. }
  340. inline animator* application::get_animator()
  341. {
  342. return animator;
  343. }
  344. inline camera* application::get_overworld_camera()
  345. {
  346. return &overworld_camera;
  347. }
  348. inline camera* application::get_underworld_camera()
  349. {
  350. return &underworld_camera;
  351. }
  352. inline orbit_cam* application::get_orbit_cam()
  353. {
  354. return &orbit_cam;
  355. }
  356. inline control_system* application::get_control_system()
  357. {
  358. return control_system;
  359. }
  360. inline entt::registry& application::get_ecs_registry()
  361. {
  362. return ecs_registry;
  363. }
  364. inline scene& application::get_scene()
  365. {
  366. return overworld_scene;
  367. }
  368. inline billboard* application::get_splash_billboard()
  369. {
  370. return splash_billboard;
  371. }
  372. inline sky_pass* application::get_sky_pass()
  373. {
  374. return sky_pass;
  375. }
  376. inline screen_transition* application::get_fade_transition()
  377. {
  378. return fade_transition;
  379. }
  380. inline screen_transition* application::get_radial_transition_inner()
  381. {
  382. return radial_transition_inner;
  383. }
  384. inline screen_transition* application::get_radial_transition_outer()
  385. {
  386. return radial_transition_outer;
  387. }
  388. #endif // ANTKEEPER_APPLICATION_HPP