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

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