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

303 lines
7.4 KiB

  1. /*
  2. * Copyright (C) 2023 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 <list>
  22. #include <memory>
  23. #include <unordered_map>
  24. #include "gl/rasterizer.hpp"
  25. #include "input/keyboard.hpp"
  26. #include "input/mouse.hpp"
  27. #include "input/gamepad.hpp"
  28. #include "utility/fundamental-types.hpp"
  29. #include "debug/logger.hpp"
  30. #include "event/signal.hpp"
  31. // Forward declarations
  32. typedef struct SDL_Window SDL_Window;
  33. typedef void* SDL_GLContext;
  34. class event_dispatcher;
  35. /**
  36. *
  37. */
  38. class application
  39. {
  40. public:
  41. /**
  42. * Creates and initializes an application.
  43. *
  44. * @param logger Debug logger.
  45. */
  46. application(debug::logger& log);
  47. /**
  48. * Destroys an application.
  49. */
  50. ~application();
  51. /**
  52. * Requests the application to close.
  53. */
  54. void close();
  55. /**
  56. * Sets the application window's title.
  57. *
  58. * @param title Window title.
  59. */
  60. void set_title(const std::string& title);
  61. /**
  62. * Sets the cursor visibility.
  63. *
  64. * @param visible `true` if the cursor should be visible, `false` otherwise.
  65. */
  66. void set_cursor_visible(bool visible);
  67. /**
  68. * Enables or disables relative mouse mode, in which only relative mouse movement events are generated.
  69. *
  70. * @param enabled `true` if relative mouse mode should be enabled, `false` otherwise.
  71. */
  72. void set_relative_mouse_mode(bool enabled);
  73. /**
  74. * Resizes the application window.
  75. *
  76. * @param width Width of the window, in pixels.
  77. * @param height Height of the window, in pixels.
  78. */
  79. void resize_window(int width, int height);
  80. /**
  81. * Puts the application window into either fullscreen or windowed mode.
  82. *
  83. * @param fullscreen `true` if the window should be fullscreen, `false` if it should be windowed.
  84. */
  85. void set_fullscreen(bool fullscreen);
  86. /**
  87. * Enables or disables v-sync mode.
  88. *
  89. * @param v_sync `true` if v-sync should be enabled, `false` otherwise.
  90. */
  91. void set_v_sync(bool v_sync);
  92. void set_window_opacity(float opacity);
  93. void swap_buffers();
  94. void show_window();
  95. void hide_window();
  96. void add_game_controller_mappings(const void* mappings, std::size_t size);
  97. /// Returns the dimensions of the current display.
  98. const int2& get_display_dimensions() const;
  99. /// Returns the DPI of the display.
  100. float get_display_dpi() const;
  101. /// Returns the dimensions of the window.
  102. const int2& get_window_dimensions() const;
  103. /// Returns the dimensions of the window's drawable viewport.
  104. const int2& get_viewport_dimensions() const;
  105. /// Returns `true` if the window is in fullscreen mode, `false` otherwise.
  106. bool is_fullscreen() const;
  107. /// Returns `true` if the v-sync is enabled, `false` otherwise.
  108. bool get_v_sync() const;
  109. /// Returns the rasterizer for the window.
  110. gl::rasterizer* get_rasterizer();
  111. /// Returns the application logger.
  112. debug::logger* get_logger();
  113. /// Returns the virtual keyboard.
  114. input::keyboard* get_keyboard();
  115. /// Returns the virtual mouse.
  116. input::mouse* get_mouse();
  117. /// Returns the list of virtual gamepads.
  118. const std::list<input::gamepad*>& get_gamepads();
  119. /// Returns the application's event dispatcher.
  120. event_dispatcher* get_event_dispatcher();
  121. void process_events();
  122. bool was_closed() const;
  123. /// Returns a connector for the signal emitted when a gamepad is connected or disconnected.
  124. connector<void(input::gamepad&, bool)>& get_gamepad_connection_signal() noexcept;
  125. /// Returns a connector for the signal emitted when the window is requested to close.
  126. connector<void()>& get_window_close_signal() noexcept;
  127. /// Returns a connector for the signal emitted each time the window gains or loses focus.
  128. connector<void(bool)>& get_window_focus_signal() noexcept;
  129. /// Returns a connector for the signal emitted each time the window is moved.
  130. connector<void(int, int)>& get_window_motion_signal() noexcept;
  131. /// Returns a connector for the signal emitted each time the window is resized.
  132. connector<void(int, int)>& get_window_size_signal() noexcept;
  133. /// Returns a connector for the signal emitted each time the window viewport is resized.
  134. connector<void(int, int)>& get_viewport_size_signal() noexcept;
  135. private:
  136. void window_resized();
  137. bool closed;
  138. bool fullscreen;
  139. bool v_sync;
  140. bool cursor_visible;
  141. int2 display_dimensions;
  142. float display_dpi;
  143. int2 window_dimensions;
  144. int2 viewport_dimensions;
  145. int2 mouse_position;
  146. debug::logger* logger;
  147. SDL_Window* sdl_window;
  148. SDL_GLContext sdl_gl_context;
  149. gl::rasterizer* rasterizer;
  150. // Events
  151. event_dispatcher* event_dispatcher;
  152. // Input devices
  153. input::keyboard* keyboard;
  154. input::mouse* mouse;
  155. std::list<input::gamepad*> gamepads;
  156. std::unordered_map<int, input::gamepad*> gamepad_map;
  157. signal<void(input::gamepad&, bool)> gamepad_connection_signal;
  158. signal<void()> window_close_signal;
  159. signal<void(bool)> window_focus_signal;
  160. signal<void(int, int)> window_motion_signal;
  161. signal<void(int, int)> window_size_signal;
  162. signal<void(int, int)> viewport_size_signal;
  163. };
  164. inline debug::logger* application::get_logger()
  165. {
  166. return logger;
  167. }
  168. inline const int2& application::get_display_dimensions() const
  169. {
  170. return display_dimensions;
  171. }
  172. inline float application::get_display_dpi() const
  173. {
  174. return display_dpi;
  175. }
  176. inline const int2& application::get_window_dimensions() const
  177. {
  178. return window_dimensions;
  179. }
  180. inline const int2& application::get_viewport_dimensions() const
  181. {
  182. return viewport_dimensions;
  183. }
  184. inline bool application::is_fullscreen() const
  185. {
  186. return fullscreen;
  187. }
  188. inline bool application::get_v_sync() const
  189. {
  190. return v_sync;
  191. }
  192. inline gl::rasterizer* application::get_rasterizer()
  193. {
  194. return rasterizer;
  195. }
  196. inline input::keyboard* application::get_keyboard()
  197. {
  198. return keyboard;
  199. }
  200. inline input::mouse* application::get_mouse()
  201. {
  202. return mouse;
  203. }
  204. inline const std::list<input::gamepad*>& application::get_gamepads()
  205. {
  206. return gamepads;
  207. }
  208. inline event_dispatcher* application::get_event_dispatcher()
  209. {
  210. return event_dispatcher;
  211. }
  212. inline bool application::was_closed() const
  213. {
  214. return closed;
  215. }
  216. inline connector<void(input::gamepad&, bool)>& application::get_gamepad_connection_signal() noexcept
  217. {
  218. return gamepad_connection_signal.connector();
  219. }
  220. inline connector<void()>& application::get_window_close_signal() noexcept
  221. {
  222. return window_close_signal.connector();
  223. }
  224. inline connector<void(bool)>& application::get_window_focus_signal() noexcept
  225. {
  226. return window_focus_signal.connector();
  227. }
  228. inline connector<void(int, int)>& application::get_window_motion_signal() noexcept
  229. {
  230. return window_motion_signal.connector();
  231. }
  232. inline connector<void(int, int)>& application::get_window_size_signal() noexcept
  233. {
  234. return window_size_signal.connector();
  235. }
  236. inline connector<void(int, int)>& application::get_viewport_size_signal() noexcept
  237. {
  238. return viewport_size_signal.connector();
  239. }
  240. #endif // ANTKEEPER_APPLICATION_HPP