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

250 lines
5.4 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 <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. // Forward declarations
  30. typedef struct SDL_Window SDL_Window;
  31. typedef void* SDL_GLContext;
  32. class event_dispatcher;
  33. namespace debug
  34. {
  35. class logger;
  36. class performance_sampler;
  37. }
  38. /**
  39. *
  40. */
  41. class application
  42. {
  43. public:
  44. /**
  45. * Creates and initializes an application.
  46. */
  47. application();
  48. /**
  49. * Destroys an application.
  50. */
  51. ~application();
  52. /**
  53. * Requests the application to close.
  54. */
  55. void close();
  56. /**
  57. * Sets the application window's title.
  58. *
  59. * @param title Window title.
  60. */
  61. void set_title(const std::string& title);
  62. /**
  63. * Sets the cursor visibility.
  64. *
  65. * @param visible `true` if the cursor should be visible, `false` otherwise.
  66. */
  67. void set_cursor_visible(bool visible);
  68. /**
  69. * Enables or disables relative mouse mode, in which only relative mouse movement events are generated.
  70. *
  71. * @param enabled `true` if relative mouse mode should be enabled, `false` otherwise.
  72. */
  73. void set_relative_mouse_mode(bool enabled);
  74. /**
  75. * Resizes the application window.
  76. *
  77. * @param width Width of the window, in pixels.
  78. * @param height Height of the window, in pixels.
  79. */
  80. void resize_window(int width, int height);
  81. /**
  82. * Puts the application window into either fullscreen or windowed mode.
  83. *
  84. * @param fullscreen `true` if the window should be fullscreen, `false` if it should be windowed.
  85. */
  86. void set_fullscreen(bool fullscreen);
  87. /**
  88. * Enables or disables v-sync mode.
  89. *
  90. * @param v_sync `true` if v-sync should be enabled, `false` otherwise.
  91. */
  92. void set_v_sync(bool v_sync);
  93. void set_window_opacity(float opacity);
  94. void swap_buffers();
  95. void show_window();
  96. void hide_window();
  97. void add_game_controller_mappings(const void* mappings, std::size_t size);
  98. /// Returns the dimensions of the current display.
  99. const int2& get_display_dimensions() const;
  100. /// Returns the DPI of the display.
  101. float get_display_dpi() const;
  102. /// Returns the dimensions of the window.
  103. const int2& get_window_dimensions() const;
  104. /// Returns the dimensions of the window's drawable viewport.
  105. const int2& get_viewport_dimensions() const;
  106. /// Returns `true` if the window is in fullscreen mode, `false` otherwise.
  107. bool is_fullscreen() const;
  108. /// Returns `true` if the v-sync is enabled, `false` otherwise.
  109. bool get_v_sync() const;
  110. /// Returns the rasterizer for the window.
  111. gl::rasterizer* get_rasterizer();
  112. /// Returns the application logger.
  113. debug::logger* get_logger();
  114. /// Returns the virtual keyboard.
  115. input::keyboard* get_keyboard();
  116. /// Returns the virtual mouse.
  117. input::mouse* get_mouse();
  118. /// Returns the list of virtual gamepads.
  119. const std::list<input::gamepad*>& get_gamepads();
  120. /// Returns the application's event dispatcher.
  121. event_dispatcher* get_event_dispatcher();
  122. void process_events();
  123. bool was_closed() const;
  124. private:
  125. void window_resized();
  126. bool closed;
  127. bool fullscreen;
  128. bool v_sync;
  129. bool cursor_visible;
  130. int2 display_dimensions;
  131. float display_dpi;
  132. int2 window_dimensions;
  133. int2 viewport_dimensions;
  134. int2 mouse_position;
  135. debug::logger* logger;
  136. SDL_Window* sdl_window;
  137. SDL_GLContext sdl_gl_context;
  138. gl::rasterizer* rasterizer;
  139. // Events
  140. event_dispatcher* event_dispatcher;
  141. // Input devices
  142. input::keyboard* keyboard;
  143. input::mouse* mouse;
  144. std::list<input::gamepad*> gamepads;
  145. std::unordered_map<int, input::gamepad*> gamepad_map;
  146. };
  147. inline debug::logger* application::get_logger()
  148. {
  149. return logger;
  150. }
  151. inline const int2& application::get_display_dimensions() const
  152. {
  153. return display_dimensions;
  154. }
  155. inline float application::get_display_dpi() const
  156. {
  157. return display_dpi;
  158. }
  159. inline const int2& application::get_window_dimensions() const
  160. {
  161. return window_dimensions;
  162. }
  163. inline const int2& application::get_viewport_dimensions() const
  164. {
  165. return viewport_dimensions;
  166. }
  167. inline bool application::is_fullscreen() const
  168. {
  169. return fullscreen;
  170. }
  171. inline bool application::get_v_sync() const
  172. {
  173. return v_sync;
  174. }
  175. inline gl::rasterizer* application::get_rasterizer()
  176. {
  177. return rasterizer;
  178. }
  179. inline input::keyboard* application::get_keyboard()
  180. {
  181. return keyboard;
  182. }
  183. inline input::mouse* application::get_mouse()
  184. {
  185. return mouse;
  186. }
  187. inline const std::list<input::gamepad*>& application::get_gamepads()
  188. {
  189. return gamepads;
  190. }
  191. inline event_dispatcher* application::get_event_dispatcher()
  192. {
  193. return event_dispatcher;
  194. }
  195. inline bool application::was_closed() const
  196. {
  197. return closed;
  198. }
  199. #endif // ANTKEEPER_APPLICATION_HPP