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

291 lines
7.8 KiB

  1. /*
  2. * Copyright (C) 2021 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. #include <array>
  22. #include <functional>
  23. #include <list>
  24. #include <memory>
  25. #include <unordered_map>
  26. // Forward declarations
  27. typedef struct SDL_Window SDL_Window;
  28. typedef void* SDL_GLContext;
  29. class event_dispatcher;
  30. class frame_scheduler;
  31. class game_controller;
  32. class keyboard;
  33. class mouse;
  34. class rasterizer;
  35. class image;
  36. namespace debug
  37. {
  38. class logger;
  39. class performance_sampler;
  40. }
  41. /**
  42. *
  43. */
  44. class application
  45. {
  46. public:
  47. typedef std::array<std::function<void()>, 2> state_type;
  48. typedef std::function<int(application*)> bootloader_type;
  49. typedef std::function<void(double, double)> update_callback_type;
  50. typedef std::function<void(double)> render_callback_type;
  51. /**
  52. * Creates and initializes an application.
  53. */
  54. application();
  55. /**
  56. * Destroys an application.
  57. */
  58. ~application();
  59. /**
  60. * Executes the application, causing it to run the bootloader then enter the execution loop until closed.
  61. *
  62. * @param bootloader Function which will be executed immediately before the execution loop is entered. The bootloader will be passed a pointer to this application and should return an exit status code.
  63. *
  64. * @return Exit status code.
  65. */
  66. int execute(bootloader_type bootloader);
  67. /**
  68. * Requests the application's execution loop to cleanly terminate, and specifies its exit status code.
  69. *
  70. * @param status Status to be returned by application::execute() upon execution loop termination.
  71. */
  72. void close(int status);
  73. /**
  74. * Changes the applications state, resulting in the execution of the current state's exit function (if any), followed by the new state's enter function (if any).
  75. *
  76. * @param state A pair of enter and exit functions, respectively, which define the state.
  77. */
  78. void change_state(const state_type& state);
  79. /**
  80. * Captures a screenshot of the most recently rendered frame.
  81. *
  82. * @return Image containing the captured frame.
  83. */
  84. std::shared_ptr<image> capture_frame() const;
  85. /**
  86. * Saves a PNG screenshot of the most recently rendered frame.
  87. *
  88. * @param path File path to the where the screenshot should be saved.
  89. */
  90. void save_frame(const std::string& path) const;
  91. /**
  92. * Sets the update callback, which is executed at regular intervals until the application is closed. The update callback expects two parameters, the first being the total time in seconds since the application was executed (t), and the second being the time in seconds since the last update (dt). dt will always be a fixed value, and is determined by the user-specified update rate.
  93. *
  94. * @see application::set_update_rate()
  95. */
  96. void set_update_callback(const update_callback_type& callback);
  97. /**
  98. * Sets the render callback, which is executed as many times as possible between update callbacks. The render callback expects one parameter, alpha, which is always between 0 and 1 and can be used to interpolate between update states.
  99. */
  100. void set_render_callback(const render_callback_type& callback);
  101. /**
  102. * Sets the frequency with which the update callback should be called.
  103. *
  104. * @param frequency Number of times per second the update callback should be called.
  105. */
  106. void set_update_rate(double frequency);
  107. /**
  108. * Sets the application window's title.
  109. *
  110. * @param title Window title.
  111. */
  112. void set_title(const std::string& title);
  113. /**
  114. * Sets the cursor visibility.
  115. *
  116. * @param visible `true` if the cursor should be visible, `false` otherwise.
  117. */
  118. void set_cursor_visible(bool visible);
  119. /**
  120. * Enables or disables relative mouse mode, in which only relative mouse movement events are generated.
  121. *
  122. * @param enabled `true` if relative mouse mode should be enabled, `false` otherwise.
  123. */
  124. void set_relative_mouse_mode(bool enabled);
  125. /**
  126. * Resizes the application window.
  127. *
  128. * @param width Width of the window, in pixels.
  129. * @param height Height of the window, in pixels.
  130. */
  131. void resize_window(int width, int height);
  132. /**
  133. * Puts the application window into either fullscreen or windowed mode.
  134. *
  135. * @param fullscreen `true` if the window should be fullscreen, `false` if it should be windowed.
  136. */
  137. void set_fullscreen(bool fullscreen);
  138. /**
  139. * Enables or disables v-sync mode.
  140. *
  141. * @param vsync `true` if v-sync should be enabled, `false` otherwise.
  142. */
  143. void set_vsync(bool vsync);
  144. void set_window_opacity(float opacity);
  145. void swap_buffers();
  146. /// Returns the dimensions of the current display.
  147. const std::array<int, 2>& get_display_dimensions() const;
  148. /// Returns the dimensions of the window.
  149. const std::array<int, 2>& get_window_dimensions() const;
  150. /// Returns the dimensions of the window's drawable viewport.
  151. const std::array<int, 2>& get_viewport_dimensions() const;
  152. /// Returns `true` if the window is in fullscreen mode, `false` otherwise.
  153. bool is_fullscreen() const;
  154. /// Returns the rasterizer for the window.
  155. ::rasterizer* get_rasterizer();
  156. /// Returns the application logger.
  157. debug::logger* get_logger();
  158. /// Returns the virtual keyboard.
  159. ::keyboard* get_keyboard();
  160. /// Returns the virtual mouse.
  161. ::mouse* get_mouse();
  162. /// Returns the list of virtual game controllers.
  163. const std::list<game_controller*>& get_game_controllers();
  164. /// Returns the application's event dispatcher.
  165. event_dispatcher* get_event_dispatcher();
  166. private:
  167. void update(double t, double dt);
  168. void render(double alpha);
  169. void translate_sdl_events();
  170. void window_resized();
  171. bool closed;
  172. int exit_status;
  173. state_type state;
  174. update_callback_type update_callback;
  175. render_callback_type render_callback;
  176. bool fullscreen;
  177. bool vsync;
  178. bool cursor_visible;
  179. std::array<int, 2> display_dimensions;
  180. std::array<int, 2> window_dimensions;
  181. std::array<int, 2> viewport_dimensions;
  182. std::array<int, 2> mouse_position;
  183. double update_rate;
  184. debug::logger* logger;
  185. SDL_Window* sdl_window;
  186. SDL_GLContext sdl_gl_context;
  187. rasterizer* rasterizer;
  188. // Frame timing
  189. frame_scheduler* frame_scheduler;
  190. debug::performance_sampler* performance_sampler;
  191. // Events
  192. event_dispatcher* event_dispatcher;
  193. // Input devices
  194. keyboard* keyboard;
  195. mouse* mouse;
  196. std::list<game_controller*> game_controllers;
  197. std::unordered_map<int, game_controller*> game_controller_map;
  198. };
  199. inline debug::logger* application::get_logger()
  200. {
  201. return logger;
  202. }
  203. inline const std::array<int, 2>& application::get_display_dimensions() const
  204. {
  205. return display_dimensions;
  206. }
  207. inline const std::array<int, 2>& application::get_window_dimensions() const
  208. {
  209. return window_dimensions;
  210. }
  211. inline const std::array<int, 2>& application::get_viewport_dimensions() const
  212. {
  213. return viewport_dimensions;
  214. }
  215. inline bool application::is_fullscreen() const
  216. {
  217. return fullscreen;
  218. }
  219. inline rasterizer* application::get_rasterizer()
  220. {
  221. return rasterizer;
  222. }
  223. inline keyboard* application::get_keyboard()
  224. {
  225. return keyboard;
  226. }
  227. inline mouse* application::get_mouse()
  228. {
  229. return mouse;
  230. }
  231. inline const std::list<game_controller*>& application::get_game_controllers()
  232. {
  233. return game_controllers;
  234. }
  235. inline event_dispatcher* application::get_event_dispatcher()
  236. {
  237. return event_dispatcher;
  238. }
  239. #endif // ANTKEEPER_APPLICATION_HPP