emu_window_sdl2.cpp 8.0 KB

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