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

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