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

296 lines
8.6 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. #include <engine/app/sdl/sdl-window.hpp>
  20. #include <engine/config.hpp>
  21. #include <engine/debug/log.hpp>
  22. #include <glad/glad.h>
  23. #include <stdexcept>
  24. namespace app {
  25. sdl_window::sdl_window
  26. (
  27. const std::string& title,
  28. const math::vector<int, 2>& windowed_position,
  29. const math::vector<int, 2>& windowed_size,
  30. bool maximized,
  31. bool fullscreen,
  32. bool v_sync
  33. )
  34. {
  35. // Determine SDL window creation flags
  36. Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  37. if (maximized)
  38. {
  39. window_flags |= SDL_WINDOW_MAXIMIZED;
  40. }
  41. if (fullscreen)
  42. {
  43. window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  44. }
  45. // Create SDL window
  46. debug::log::trace("Creating SDL window...");
  47. internal_window = SDL_CreateWindow
  48. (
  49. title.c_str(),
  50. windowed_position.x(),
  51. windowed_position.y(),
  52. windowed_size.x(),
  53. windowed_size.y(),
  54. window_flags
  55. );
  56. if (!internal_window)
  57. {
  58. debug::log::fatal("Failed to create SDL window: {}", SDL_GetError());
  59. throw std::runtime_error("Failed to create SDL window");
  60. }
  61. debug::log::trace("Created SDL window");
  62. // Create OpenGL context
  63. debug::log::trace("Creating OpenGL context...");
  64. internal_context = SDL_GL_CreateContext(internal_window);
  65. if (!internal_context)
  66. {
  67. debug::log::fatal("Failed to create OpenGL context: {}", SDL_GetError());
  68. throw std::runtime_error("Failed to create OpenGL context");
  69. }
  70. debug::log::trace("Created OpenGL context");
  71. // Query OpenGL context info
  72. int opengl_context_version_major = -1;
  73. int opengl_context_version_minor = -1;
  74. int opengl_context_red_size = -1;
  75. int opengl_context_green_size = -1;
  76. int opengl_context_blue_size = -1;
  77. int opengl_context_alpha_size = -1;
  78. int opengl_context_depth_size = -1;
  79. int opengl_context_stencil_size = -1;
  80. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &opengl_context_version_major);
  81. SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &opengl_context_version_minor);
  82. SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &opengl_context_red_size);
  83. SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &opengl_context_green_size);
  84. SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &opengl_context_blue_size);
  85. SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &opengl_context_alpha_size);
  86. SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &opengl_context_depth_size);
  87. SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &opengl_context_stencil_size);
  88. // Log OpenGL context info
  89. debug::log::info
  90. (
  91. "OpenGL context version: {}.{}; format: R{}G{}B{}A{}D{}S{}",
  92. opengl_context_version_major,
  93. opengl_context_version_minor,
  94. opengl_context_red_size,
  95. opengl_context_green_size,
  96. opengl_context_blue_size,
  97. opengl_context_alpha_size,
  98. opengl_context_depth_size,
  99. opengl_context_stencil_size
  100. );
  101. // Compare OpenGL context version with requested version
  102. if (opengl_context_version_major != config::opengl_version_major ||
  103. opengl_context_version_minor != config::opengl_version_minor)
  104. {
  105. debug::log::warning("Requested OpenGL context version {}.{} but got version {}.{}", config::opengl_version_major, config::opengl_version_minor, opengl_context_version_major, opengl_context_version_minor);
  106. }
  107. // Compare OpenGL context format with requested format
  108. if (opengl_context_red_size < config::opengl_min_red_size ||
  109. opengl_context_green_size < config::opengl_min_green_size ||
  110. opengl_context_blue_size < config::opengl_min_blue_size ||
  111. opengl_context_alpha_size < config::opengl_min_alpha_size ||
  112. opengl_context_depth_size < config::opengl_min_depth_size ||
  113. opengl_context_stencil_size < config::opengl_min_stencil_size)
  114. {
  115. debug::log::warning
  116. (
  117. "OpenGL context format (R{}G{}B{}A{}D{}S{}) does not meet minimum requested format (R{}G{}B{}A{}D{}S{})",
  118. opengl_context_red_size,
  119. opengl_context_green_size,
  120. opengl_context_blue_size,
  121. opengl_context_alpha_size,
  122. opengl_context_depth_size,
  123. opengl_context_stencil_size,
  124. config::opengl_min_red_size,
  125. config::opengl_min_green_size,
  126. config::opengl_min_blue_size,
  127. config::opengl_min_alpha_size,
  128. config::opengl_min_depth_size,
  129. config::opengl_min_stencil_size
  130. );
  131. }
  132. // Load OpenGL functions via GLAD
  133. debug::log::trace("Loading OpenGL functions...");
  134. if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
  135. {
  136. debug::log::fatal("Failed to load OpenGL functions", SDL_GetError());
  137. throw std::runtime_error("Failed to load OpenGL functions");
  138. }
  139. debug::log::trace("Loaded OpenGL functions");
  140. // Log OpenGL information
  141. debug::log::info
  142. (
  143. "OpenGL vendor: {}; renderer: {}; version: {}; shading language version: {}",
  144. reinterpret_cast<const char*>(glGetString(GL_VENDOR)),
  145. reinterpret_cast<const char*>(glGetString(GL_RENDERER)),
  146. reinterpret_cast<const char*>(glGetString(GL_VERSION)),
  147. reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION))
  148. );
  149. // Fill window with color
  150. //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  151. glClear(GL_COLOR_BUFFER_BIT);
  152. swap_buffers();
  153. // Enable or disable v-sync
  154. set_v_sync(v_sync);
  155. // Update window state
  156. this->title = title;
  157. this->windowed_position = windowed_position;
  158. this->windowed_size = windowed_size;
  159. this->maximized = maximized;
  160. this->fullscreen = fullscreen;
  161. SDL_GetWindowPosition(internal_window, &this->position.x(), &this->position.y());
  162. SDL_GetWindowSize(internal_window, &this->size.x(), &this->size.y());
  163. SDL_GetWindowMinimumSize(internal_window, &this->minimum_size.x(), &this->minimum_size.y());
  164. SDL_GetWindowMaximumSize(internal_window, &this->maximum_size.x(), &this->maximum_size.y());
  165. SDL_GL_GetDrawableSize(internal_window, &this->viewport_size.x(), &this->viewport_size.y());
  166. // Allocate rasterizer
  167. this->rasterizer = new gl::rasterizer();
  168. }
  169. sdl_window::~sdl_window()
  170. {
  171. // Destruct rasterizer
  172. delete rasterizer;
  173. // Destruct the OpenGL context
  174. SDL_GL_DeleteContext(internal_context);
  175. // Destruct the SDL window
  176. SDL_DestroyWindow(internal_window);
  177. }
  178. void sdl_window::set_title(const std::string& title)
  179. {
  180. SDL_SetWindowTitle(internal_window, title.c_str());
  181. this->title = title;
  182. }
  183. void sdl_window::set_position(const math::vector<int, 2>& position)
  184. {
  185. SDL_SetWindowPosition(internal_window, position.x(), position.y());
  186. }
  187. void sdl_window::set_size(const math::vector<int, 2>& size)
  188. {
  189. SDL_SetWindowSize(internal_window, size.x(), size.y());
  190. }
  191. void sdl_window::set_minimum_size(const math::vector<int, 2>& size)
  192. {
  193. SDL_SetWindowMinimumSize(internal_window, size.x(), size.y());
  194. this->minimum_size = size;
  195. }
  196. void sdl_window::set_maximum_size(const math::vector<int, 2>& size)
  197. {
  198. SDL_SetWindowMaximumSize(internal_window, size.x(), size.y());
  199. this->maximum_size = size;
  200. }
  201. void sdl_window::set_maximized(bool maximized)
  202. {
  203. if (maximized)
  204. {
  205. SDL_MaximizeWindow(internal_window);
  206. }
  207. else
  208. {
  209. SDL_RestoreWindow(internal_window);
  210. }
  211. }
  212. void sdl_window::set_fullscreen(bool fullscreen)
  213. {
  214. //SDL_HideWindow(internal_window);
  215. SDL_SetWindowFullscreen(internal_window, (fullscreen) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
  216. //SDL_ShowWindow(internal_window);
  217. this->fullscreen = fullscreen;
  218. }
  219. void sdl_window::set_v_sync(bool v_sync)
  220. {
  221. if (v_sync)
  222. {
  223. debug::log::trace("Enabling adaptive v-sync...");
  224. if (SDL_GL_SetSwapInterval(-1) != 0)
  225. {
  226. debug::log::error("Failed to enable adaptive v-sync: {}", SDL_GetError());
  227. debug::log::trace("Enabling synchronized v-sync...");
  228. if (SDL_GL_SetSwapInterval(1) != 0)
  229. {
  230. debug::log::error("Failed to enable synchronized v-sync: {}", SDL_GetError());
  231. v_sync = false;
  232. }
  233. else
  234. {
  235. debug::log::debug("Enabled synchronized v-sync");
  236. }
  237. }
  238. else
  239. {
  240. debug::log::debug("Enabled adaptive v-sync");
  241. }
  242. }
  243. else
  244. {
  245. debug::log::trace("Disabling v-sync...");
  246. if (SDL_GL_SetSwapInterval(0) != 0)
  247. {
  248. debug::log::error("Failed to disable v-sync: {}", SDL_GetError());
  249. v_sync = true;
  250. }
  251. else
  252. {
  253. debug::log::debug("Disabled v-sync");
  254. }
  255. }
  256. this->v_sync = v_sync;
  257. }
  258. void sdl_window::make_current()
  259. {
  260. SDL_GL_MakeCurrent(internal_window, internal_context);
  261. }
  262. void sdl_window::swap_buffers()
  263. {
  264. SDL_GL_SwapWindow(internal_window);
  265. }
  266. } // namespace app