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

420 lines
9.1 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. // Misc
  58. #include "state/fsm.hpp"
  59. #include "frame-scheduler.hpp"
  60. #include "pheromone-matrix.hpp"
  61. #include "orbit-cam.hpp"
  62. // Forward declarations
  63. //{
  64. // SDL
  65. typedef struct SDL_Window SDL_Window;
  66. typedef void* SDL_GLContext;
  67. // Resources
  68. class resource_manager;
  69. // Rasterizer
  70. class rasterizer;
  71. class framebuffer;
  72. class vertex_buffer;
  73. class vertex_array;
  74. class texture_2d;
  75. // Renderer
  76. class clear_pass;
  77. class shadow_map_pass;
  78. class material_pass;
  79. class sky_pass;
  80. class bloom_pass;
  81. class final_pass;
  82. class simple_render_pass;
  83. // Animation
  84. class animator;
  85. // Systems
  86. class behavior_system;
  87. class camera_system;
  88. class collision_system;
  89. class locomotion_system;
  90. class model_system;
  91. class nest_system;
  92. class placement_system;
  93. class samara_system;
  94. class subterrain_system;
  95. class terrain_system;
  96. class vegetation_system;
  97. class tool_system;
  98. class control_system;
  99. class ui_system;
  100. template <class T>
  101. class material_property;
  102. //}
  103. class application
  104. {
  105. public:
  106. /**
  107. * Creates and initializes an application.
  108. */
  109. application(int argc, char** argv);
  110. /**
  111. * Destroys an application.
  112. */
  113. ~application();
  114. /**
  115. * Executes the application, causing it to enter the execution loop until closed.
  116. *
  117. * @return Exit status code.
  118. */
  119. int execute();
  120. /**
  121. * Requests the application's execution loop to cleanly terminate, and specifies its exit status code.
  122. *
  123. * @param status Status to be returned by application::execute() upon execution loop termination.
  124. */
  125. void close(int status);
  126. logger* get_logger();
  127. cli* get_cli();
  128. resource_manager* get_resource_manager();
  129. fsm::machine* get_state_machine();
  130. const fsm::state& get_loading_state() const;
  131. const fsm::state& get_language_select_state() const;
  132. const fsm::state& get_splash_state() const;
  133. const fsm::state& get_title_state() const;
  134. const fsm::state& get_play_state() const;
  135. const fsm::state& get_pause_state() const;
  136. timeline* get_timeline();
  137. animator* get_animator();
  138. camera* get_overworld_camera();
  139. camera* get_underworld_camera();
  140. orbit_cam* get_orbit_cam();
  141. control_system* get_control_system();
  142. entt::registry& get_ecs_registry();
  143. scene& get_scene();
  144. void take_screenshot() const;
  145. private:
  146. void update(double t, double dt);
  147. void render(double alpha);
  148. void translate_sdl_events();
  149. void set_relative_mouse_mode(bool enabled);
  150. void toggle_fullscreen();
  151. void window_resized();
  152. static void save_image(const std::string& filename, int w, int h, const unsigned char* pixels);
  153. bool fullscreen;
  154. std::tuple<int, int> saved_mouse_position;
  155. std::tuple<int, int> window_dimensions;
  156. std::tuple<int, int> window_position;
  157. std::tuple<int, int> display_dimensions;
  158. float4 viewport;
  159. // Debugging
  160. std::ofstream log_filestream;
  161. ::logger logger;
  162. ::cli cli;
  163. // Paths
  164. std::string data_path;
  165. std::string config_path;
  166. std::string screenshots_path;
  167. // Resources
  168. resource_manager* resource_manager;
  169. SDL_Window* window;
  170. SDL_GLContext context;
  171. bool closed;
  172. int exit_status;
  173. // Updatable systems
  174. timeline timeline;
  175. animator* animator;
  176. std::list<std::function<void(double, double)>> systems;
  177. int shadow_map_resolution;
  178. framebuffer* shadow_map_framebuffer;
  179. texture_2d* shadow_map_depth_texture;
  180. framebuffer* framebuffer_hdr;
  181. texture_2d* framebuffer_hdr_color;
  182. texture_2d* framebuffer_hdr_depth;
  183. framebuffer* framebuffer_bloom; // General purpose framebuffer A
  184. texture_2d* bloom_texture;
  185. // Rendering
  186. rasterizer* rasterizer;
  187. material* fallback_material;
  188. ambient_light sun_indirect;
  189. directional_light sun_direct;
  190. point_light subterrain_light;
  191. ambient_light underworld_ambient_light;
  192. model_instance darkness_volume;
  193. model_instance lantern;
  194. model_instance cloud;
  195. model_instance* grass_patches;
  196. ::spotlight spotlight;
  197. vertex_buffer* billboard_vbo;
  198. vertex_array* billboard_vao;
  199. ::renderer renderer;
  200. scene* active_scene;
  201. // Overworld
  202. scene overworld_scene;
  203. camera overworld_camera;
  204. ::clear_pass* clear_pass;
  205. ::sky_pass* sky_pass;
  206. ::material_pass* material_pass;
  207. ::clear_pass* shadow_map_clear_pass;
  208. ::shadow_map_pass* shadow_map_pass;
  209. ::bloom_pass* bloom_pass;
  210. ::final_pass* final_pass;
  211. compositor overworld_compositor;
  212. // Underworld
  213. scene underworld_scene;
  214. camera underworld_camera;
  215. ::clear_pass* underworld_clear_pass;
  216. ::material_pass* underworld_material_pass;
  217. simple_render_pass* underworld_final_pass;
  218. material_property<float>* underground_transition_property;
  219. material_property<const texture_2d*>* underground_color_texture_property;
  220. compositor underworld_compositor;
  221. // FSM
  222. fsm::machine state_machine;
  223. fsm::state loading_state;
  224. fsm::state language_select_state;
  225. fsm::state splash_state;
  226. fsm::state title_state;
  227. fsm::state play_state;
  228. fsm::state pause_state;
  229. // Frame timing
  230. frame_scheduler frame_scheduler;
  231. performance_sampler performance_sampler;
  232. tween<double> time;
  233. // Events
  234. event_dispatcher event_dispatcher;
  235. input_event_router input_event_router;
  236. input_mapper input_mapper;
  237. // Input devices
  238. keyboard keyboard;
  239. mouse mouse;
  240. game_controller game_controller;
  241. // Controls
  242. control_set menu_controls;
  243. control menu_back_control;
  244. control menu_select_control;
  245. control_set* camera_controls;
  246. // System controls
  247. control_set application_controls;
  248. control toggle_fullscreen_control;
  249. control screenshot_control;
  250. control dig_control;
  251. // Game
  252. orbit_cam orbit_cam;
  253. pheromone_matrix pheromones;
  254. control_system* control_system;
  255. // ECS
  256. entt::registry ecs_registry;
  257. behavior_system* behavior_system;
  258. camera_system* camera_system;
  259. collision_system* collision_system;
  260. locomotion_system* locomotion_system;
  261. model_system* model_system;
  262. nest_system* nest_system;
  263. placement_system* placement_system;
  264. samara_system* samara_system;
  265. subterrain_system* subterrain_system;
  266. terrain_system* terrain_system;
  267. vegetation_system* vegetation_system;
  268. tool_system* tool_system;
  269. // UI
  270. ui_system* ui_system;
  271. compositor ui_compositor;
  272. ::clear_pass* ui_clear_pass;
  273. ::material_pass* ui_material_pass;
  274. // Animation
  275. tween<float3> focal_point_tween;
  276. };
  277. inline logger* application::get_logger()
  278. {
  279. return &logger;
  280. }
  281. inline cli* application::get_cli()
  282. {
  283. return &cli;
  284. }
  285. inline resource_manager* application::get_resource_manager()
  286. {
  287. return resource_manager;
  288. }
  289. inline fsm::machine* application::get_state_machine()
  290. {
  291. return &state_machine;
  292. }
  293. inline const fsm::state& application::get_loading_state() const
  294. {
  295. return loading_state;
  296. }
  297. inline const fsm::state& application::get_language_select_state() const
  298. {
  299. return language_select_state;
  300. }
  301. inline const fsm::state& application::get_splash_state() const
  302. {
  303. return splash_state;
  304. }
  305. inline const fsm::state& application::get_title_state() const
  306. {
  307. return title_state;
  308. }
  309. inline const fsm::state& application::get_play_state() const
  310. {
  311. return play_state;
  312. }
  313. inline const fsm::state& application::get_pause_state() const
  314. {
  315. return pause_state;
  316. }
  317. inline timeline* application::get_timeline()
  318. {
  319. return &timeline;
  320. }
  321. inline animator* application::get_animator()
  322. {
  323. return animator;
  324. }
  325. inline camera* application::get_overworld_camera()
  326. {
  327. return &overworld_camera;
  328. }
  329. inline camera* application::get_underworld_camera()
  330. {
  331. return &underworld_camera;
  332. }
  333. inline orbit_cam* application::get_orbit_cam()
  334. {
  335. return &orbit_cam;
  336. }
  337. inline control_system* application::get_control_system()
  338. {
  339. return control_system;
  340. }
  341. inline entt::registry& application::get_ecs_registry()
  342. {
  343. return ecs_registry;
  344. }
  345. inline scene& application::get_scene()
  346. {
  347. return overworld_scene;
  348. }
  349. #endif // ANTKEEPER_APPLICATION_HPP