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

295 lines
7.7 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 "event/publisher.hpp"
  22. #include "gl/rasterizer.hpp"
  23. #include "input/device-manager.hpp"
  24. #include "input/event.hpp"
  25. #include "input/gamepad.hpp"
  26. #include "input/keyboard.hpp"
  27. #include "input/mouse.hpp"
  28. #include "utility/fundamental-types.hpp"
  29. #include <list>
  30. #include <memory>
  31. #include <unordered_map>
  32. // Forward declarations
  33. typedef struct SDL_Window SDL_Window;
  34. typedef void* SDL_GLContext;
  35. /**
  36. *
  37. */
  38. class application
  39. {
  40. public:
  41. /**
  42. * Constructs and initializes an application.
  43. */
  44. application
  45. (
  46. const std::string& window_title,
  47. int window_x,
  48. int window_y,
  49. int window_w,
  50. int window_h,
  51. bool maximized,
  52. bool fullscreen,
  53. bool v_sync
  54. );
  55. /**
  56. * Destructs an application.
  57. */
  58. ~application();
  59. /**
  60. * Requests the application to close.
  61. */
  62. void close();
  63. /**
  64. * Sets the application window's title.
  65. *
  66. * @param title Window title.
  67. */
  68. void set_title(const std::string& title);
  69. /**
  70. * Sets the cursor visibility.
  71. *
  72. * @param visible `true` if the cursor should be visible, `false` otherwise.
  73. */
  74. void set_cursor_visible(bool visible);
  75. /**
  76. * Enables or disables relative mouse mode, in which only relative mouse movement events are generated.
  77. *
  78. * @param enabled `true` if relative mouse mode should be enabled, `false` otherwise.
  79. */
  80. void set_relative_mouse_mode(bool enabled);
  81. /**
  82. * Resizes the application window.
  83. *
  84. * @param width Width of the window, in pixels.
  85. * @param height Height of the window, in pixels.
  86. */
  87. void resize_window(int width, int height);
  88. /**
  89. * Puts the application window into either fullscreen or window mode.
  90. *
  91. * @param fullscreen `true` if the window should be fullscreen, `false` if it should be window.
  92. */
  93. void set_fullscreen(bool fullscreen);
  94. /**
  95. * Enables or disables v-sync mode.
  96. *
  97. * @param v_sync `true` if v-sync should be enabled, `false` otherwise.
  98. */
  99. void set_v_sync(bool v_sync);
  100. void set_window_opacity(float opacity);
  101. void swap_buffers();
  102. void show_window();
  103. void hide_window();
  104. void add_game_controller_mappings(const void* mappings, std::size_t size);
  105. /// Returns the dimensions of the current display.
  106. [[nodiscard]] const int2& get_display_size() const;
  107. /// Returns the DPI of the display.
  108. [[nodiscard]] float get_display_dpi() const;
  109. /// Returns the position of the window when not maximized or fullscreen.
  110. [[nodiscard]] const int2& get_windowed_position() const;
  111. /// Returns the dimensions of the window when not maximized or fullscreen.
  112. [[nodiscard]] const int2& get_windowed_size() const;
  113. /// Returns the dimensions of the window's drawable viewport.
  114. [[nodiscard]] const int2& get_viewport_size() const;
  115. /// Returns `true` if the window is maximized, `false` otherwise.
  116. [[nodiscard]] bool is_maximized() const;
  117. /// Returns `true` if the window is in fullscreen mode, `false` otherwise.
  118. [[nodiscard]] bool is_fullscreen() const;
  119. /// Returns `true` if the v-sync is enabled, `false` otherwise.
  120. [[nodiscard]] bool get_v_sync() const;
  121. /// Returns the rasterizer for the window.
  122. [[nodiscard]] gl::rasterizer* get_rasterizer();
  123. void process_events();
  124. [[nodiscard]] bool was_closed() const;
  125. /**
  126. * Returns the input device manager.
  127. */
  128. /// @{
  129. [[nodiscard]] inline const input::device_manager& get_device_manager() const noexcept
  130. {
  131. return device_manager;
  132. }
  133. [[nodiscard]] inline input::device_manager& get_device_manager() noexcept
  134. {
  135. return device_manager;
  136. }
  137. /// @}
  138. /// Returns the channel through which window closed events are published.
  139. [[nodiscard]] inline event::channel<input::event::window_closed>& get_window_closed_channel() noexcept
  140. {
  141. return window_closed_publisher.channel();
  142. }
  143. /// Returns the channel through which window focus changed events are published.
  144. [[nodiscard]] inline event::channel<input::event::window_focus_changed>& get_window_focus_changed_channel() noexcept
  145. {
  146. return window_focus_changed_publisher.channel();
  147. }
  148. /// Returns the channel through which window moved events are published.
  149. [[nodiscard]] inline event::channel<input::event::window_moved>& get_window_moved_channel() noexcept
  150. {
  151. return window_moved_publisher.channel();
  152. }
  153. /// Returns the channel through which window resized events are published.
  154. [[nodiscard]] inline event::channel<input::event::window_resized>& get_window_resized_channel() noexcept
  155. {
  156. return window_resized_publisher.channel();
  157. }
  158. /// Returns the channel through which window maximized events are published.
  159. [[nodiscard]] inline event::channel<input::event::window_maximized>& get_window_maximized_channel() noexcept
  160. {
  161. return window_maximized_publisher.channel();
  162. }
  163. /// Returns the channel through which window restored events are published.
  164. [[nodiscard]] inline event::channel<input::event::window_restored>& get_window_restored_channel() noexcept
  165. {
  166. return window_restored_publisher.channel();
  167. }
  168. /// Returns the channel through which window minimized events are published.
  169. [[nodiscard]] inline event::channel<input::event::window_minimized>& get_window_minimized_channel() noexcept
  170. {
  171. return window_minimized_publisher.channel();
  172. }
  173. private:
  174. void window_moved();
  175. void window_resized();
  176. bool closed;
  177. bool maximized;
  178. bool fullscreen;
  179. bool v_sync;
  180. bool cursor_visible;
  181. int2 display_size;
  182. float display_dpi;
  183. int2 windowed_position;
  184. int2 windowed_size;
  185. int2 viewport_size;
  186. int2 mouse_position;
  187. SDL_Window* sdl_window;
  188. SDL_GLContext sdl_gl_context;
  189. gl::rasterizer* rasterizer;
  190. // Input devices
  191. input::device_manager device_manager;
  192. input::keyboard keyboard;
  193. input::mouse mouse;
  194. std::unordered_map<int, input::gamepad*> gamepad_map;
  195. event::publisher<input::event::window_closed> window_closed_publisher;
  196. event::publisher<input::event::window_focus_changed> window_focus_changed_publisher;
  197. event::publisher<input::event::window_moved> window_moved_publisher;
  198. event::publisher<input::event::window_resized> window_resized_publisher;
  199. event::publisher<input::event::window_maximized> window_maximized_publisher;
  200. event::publisher<input::event::window_restored> window_restored_publisher;
  201. event::publisher<input::event::window_minimized> window_minimized_publisher;
  202. };
  203. inline const int2& application::get_display_size() const
  204. {
  205. return display_size;
  206. }
  207. inline float application::get_display_dpi() const
  208. {
  209. return display_dpi;
  210. }
  211. inline const int2& application::get_windowed_position() const
  212. {
  213. return windowed_position;
  214. }
  215. inline const int2& application::get_windowed_size() const
  216. {
  217. return windowed_size;
  218. }
  219. inline const int2& application::get_viewport_size() const
  220. {
  221. return viewport_size;
  222. }
  223. inline bool application::is_maximized() const
  224. {
  225. return maximized;
  226. }
  227. inline bool application::is_fullscreen() const
  228. {
  229. return fullscreen;
  230. }
  231. inline bool application::get_v_sync() const
  232. {
  233. return v_sync;
  234. }
  235. inline gl::rasterizer* application::get_rasterizer()
  236. {
  237. return rasterizer;
  238. }
  239. inline bool application::was_closed() const
  240. {
  241. return closed;
  242. }
  243. #endif // ANTKEEPER_APPLICATION_HPP