emu_window_sdl2.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <string>
  7. #define SDL_MAIN_HANDLED
  8. #include <SDL.h>
  9. #include <fmt/format.h>
  10. #include <glad/glad.h>
  11. #include "common/logging/log.h"
  12. #include "common/scm_rev.h"
  13. #include "common/string_util.h"
  14. #include "core/settings.h"
  15. #include "input_common/keyboard.h"
  16. #include "input_common/main.h"
  17. #include "input_common/motion_emu.h"
  18. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  19. void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
  20. TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
  21. InputCommon::GetMotionEmu()->Tilt(x, y);
  22. }
  23. void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
  24. if (button == SDL_BUTTON_LEFT) {
  25. if (state == SDL_PRESSED) {
  26. TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
  27. } else {
  28. TouchReleased();
  29. }
  30. } else if (button == SDL_BUTTON_RIGHT) {
  31. if (state == SDL_PRESSED) {
  32. InputCommon::GetMotionEmu()->BeginTilt(x, y);
  33. } else {
  34. InputCommon::GetMotionEmu()->EndTilt();
  35. }
  36. }
  37. }
  38. void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
  39. if (state == SDL_PRESSED) {
  40. InputCommon::GetKeyboard()->PressKey(key);
  41. } else if (state == SDL_RELEASED) {
  42. InputCommon::GetKeyboard()->ReleaseKey(key);
  43. }
  44. }
  45. bool EmuWindow_SDL2::IsOpen() const {
  46. return is_open;
  47. }
  48. void EmuWindow_SDL2::OnResize() {
  49. int width, height;
  50. SDL_GetWindowSize(render_window, &width, &height);
  51. UpdateCurrentFramebufferLayout(width, height);
  52. }
  53. void EmuWindow_SDL2::Fullscreen() {
  54. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) {
  55. return;
  56. }
  57. LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError());
  58. // Try a different fullscreening method
  59. LOG_INFO(Frontend, "Attempting to use borderless fullscreen...");
  60. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
  61. return;
  62. }
  63. LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError());
  64. // Fallback algorithm: Maximise window.
  65. // Works on all systems (unless something is seriously wrong), so no fallback for this one.
  66. LOG_INFO(Frontend, "Falling back on a maximised window...");
  67. SDL_MaximizeWindow(render_window);
  68. }
  69. bool EmuWindow_SDL2::SupportsRequiredGLExtensions() {
  70. std::vector<std::string> unsupported_ext;
  71. if (!GLAD_GL_ARB_program_interface_query)
  72. unsupported_ext.push_back("ARB_program_interface_query");
  73. if (!GLAD_GL_ARB_separate_shader_objects)
  74. unsupported_ext.push_back("ARB_separate_shader_objects");
  75. if (!GLAD_GL_ARB_vertex_attrib_binding)
  76. unsupported_ext.push_back("ARB_vertex_attrib_binding");
  77. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  78. unsupported_ext.push_back("ARB_vertex_type_10f_11f_11f_rev");
  79. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  80. unsupported_ext.push_back("ARB_texture_mirror_clamp_to_edge");
  81. // Extensions required to support some texture formats.
  82. if (!GLAD_GL_EXT_texture_compression_s3tc)
  83. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  84. if (!GLAD_GL_ARB_texture_compression_rgtc)
  85. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  86. if (!GLAD_GL_ARB_texture_compression_bptc)
  87. unsupported_ext.push_back("ARB_texture_compression_bptc");
  88. if (!GLAD_GL_ARB_depth_buffer_float)
  89. unsupported_ext.push_back("ARB_depth_buffer_float");
  90. for (const std::string& ext : unsupported_ext)
  91. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", ext);
  92. return unsupported_ext.empty();
  93. }
  94. EmuWindow_SDL2::EmuWindow_SDL2(bool fullscreen) {
  95. InputCommon::Init();
  96. SDL_SetMainReady();
  97. // Initialize the window
  98. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  99. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  100. exit(1);
  101. }
  102. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  103. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  104. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  105. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  106. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  107. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  108. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  109. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  110. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  111. Common::g_scm_branch, Common::g_scm_desc);
  112. render_window =
  113. SDL_CreateWindow(window_title.c_str(),
  114. SDL_WINDOWPOS_UNDEFINED, // x position
  115. SDL_WINDOWPOS_UNDEFINED, // y position
  116. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  117. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  118. if (render_window == nullptr) {
  119. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  120. exit(1);
  121. }
  122. if (fullscreen) {
  123. Fullscreen();
  124. }
  125. gl_context = SDL_GL_CreateContext(render_window);
  126. if (gl_context == nullptr) {
  127. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! {}", SDL_GetError());
  128. exit(1);
  129. }
  130. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  131. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  132. exit(1);
  133. }
  134. if (!SupportsRequiredGLExtensions()) {
  135. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  136. exit(1);
  137. }
  138. OnResize();
  139. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  140. SDL_PumpEvents();
  141. SDL_GL_SetSwapInterval(false);
  142. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  143. Common::g_scm_desc);
  144. DoneCurrent();
  145. }
  146. EmuWindow_SDL2::~EmuWindow_SDL2() {
  147. SDL_GL_DeleteContext(gl_context);
  148. SDL_Quit();
  149. InputCommon::Shutdown();
  150. }
  151. void EmuWindow_SDL2::SwapBuffers() {
  152. SDL_GL_SwapWindow(render_window);
  153. }
  154. void EmuWindow_SDL2::PollEvents() {
  155. SDL_Event event;
  156. // SDL_PollEvent returns 0 when there are no more events in the event queue
  157. while (SDL_PollEvent(&event)) {
  158. switch (event.type) {
  159. case SDL_WINDOWEVENT:
  160. switch (event.window.event) {
  161. case SDL_WINDOWEVENT_SIZE_CHANGED:
  162. case SDL_WINDOWEVENT_RESIZED:
  163. case SDL_WINDOWEVENT_MAXIMIZED:
  164. case SDL_WINDOWEVENT_RESTORED:
  165. case SDL_WINDOWEVENT_MINIMIZED:
  166. OnResize();
  167. break;
  168. case SDL_WINDOWEVENT_CLOSE:
  169. is_open = false;
  170. break;
  171. }
  172. break;
  173. case SDL_KEYDOWN:
  174. case SDL_KEYUP:
  175. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  176. break;
  177. case SDL_MOUSEMOTION:
  178. OnMouseMotion(event.motion.x, event.motion.y);
  179. break;
  180. case SDL_MOUSEBUTTONDOWN:
  181. case SDL_MOUSEBUTTONUP:
  182. OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
  183. break;
  184. case SDL_QUIT:
  185. is_open = false;
  186. break;
  187. }
  188. }
  189. }
  190. void EmuWindow_SDL2::MakeCurrent() {
  191. SDL_GL_MakeCurrent(render_window, gl_context);
  192. }
  193. void EmuWindow_SDL2::DoneCurrent() {
  194. SDL_GL_MakeCurrent(render_window, nullptr);
  195. }
  196. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(
  197. const std::pair<unsigned, unsigned>& minimal_size) {
  198. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  199. }