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

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