emu_window_sdl2.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. if (!GLAD_GL_ARB_texture_storage)
  85. unsupported_ext.push_back("ARB_texture_storage");
  86. // Extensions required to support some texture formats.
  87. if (!GLAD_GL_EXT_texture_compression_s3tc)
  88. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  89. if (!GLAD_GL_ARB_texture_compression_rgtc)
  90. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  91. if (!GLAD_GL_ARB_texture_compression_bptc)
  92. unsupported_ext.push_back("ARB_texture_compression_bptc");
  93. if (!GLAD_GL_ARB_depth_buffer_float)
  94. unsupported_ext.push_back("ARB_depth_buffer_float");
  95. for (const std::string& ext : unsupported_ext)
  96. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", ext);
  97. return unsupported_ext.empty();
  98. }
  99. EmuWindow_SDL2::EmuWindow_SDL2(bool fullscreen) {
  100. InputCommon::Init();
  101. SDL_SetMainReady();
  102. // Initialize the window
  103. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  104. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  105. exit(1);
  106. }
  107. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  108. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  109. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  110. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  111. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  112. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  113. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  114. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  115. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  116. Common::g_scm_branch, Common::g_scm_desc);
  117. render_window =
  118. SDL_CreateWindow(window_title.c_str(),
  119. SDL_WINDOWPOS_UNDEFINED, // x position
  120. SDL_WINDOWPOS_UNDEFINED, // y position
  121. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  122. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  123. if (render_window == nullptr) {
  124. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  125. exit(1);
  126. }
  127. if (fullscreen) {
  128. Fullscreen();
  129. }
  130. gl_context = SDL_GL_CreateContext(render_window);
  131. if (gl_context == nullptr) {
  132. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! {}", SDL_GetError());
  133. exit(1);
  134. }
  135. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  136. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  137. exit(1);
  138. }
  139. if (!SupportsRequiredGLExtensions()) {
  140. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  141. exit(1);
  142. }
  143. OnResize();
  144. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  145. SDL_PumpEvents();
  146. SDL_GL_SetSwapInterval(false);
  147. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  148. Common::g_scm_desc);
  149. DoneCurrent();
  150. }
  151. EmuWindow_SDL2::~EmuWindow_SDL2() {
  152. InputCommon::SDL::CloseSDLJoysticks();
  153. SDL_GL_DeleteContext(gl_context);
  154. SDL_Quit();
  155. InputCommon::Shutdown();
  156. }
  157. void EmuWindow_SDL2::SwapBuffers() {
  158. SDL_GL_SwapWindow(render_window);
  159. }
  160. void EmuWindow_SDL2::PollEvents() {
  161. SDL_Event event;
  162. // SDL_PollEvent returns 0 when there are no more events in the event queue
  163. while (SDL_PollEvent(&event)) {
  164. switch (event.type) {
  165. case SDL_WINDOWEVENT:
  166. switch (event.window.event) {
  167. case SDL_WINDOWEVENT_SIZE_CHANGED:
  168. case SDL_WINDOWEVENT_RESIZED:
  169. case SDL_WINDOWEVENT_MAXIMIZED:
  170. case SDL_WINDOWEVENT_RESTORED:
  171. case SDL_WINDOWEVENT_MINIMIZED:
  172. OnResize();
  173. break;
  174. case SDL_WINDOWEVENT_CLOSE:
  175. is_open = false;
  176. break;
  177. }
  178. break;
  179. case SDL_KEYDOWN:
  180. case SDL_KEYUP:
  181. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  182. break;
  183. case SDL_MOUSEMOTION:
  184. OnMouseMotion(event.motion.x, event.motion.y);
  185. break;
  186. case SDL_MOUSEBUTTONDOWN:
  187. case SDL_MOUSEBUTTONUP:
  188. OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
  189. break;
  190. case SDL_QUIT:
  191. is_open = false;
  192. break;
  193. default:
  194. InputCommon::SDL::HandleGameControllerEvent(event);
  195. break;
  196. }
  197. }
  198. }
  199. void EmuWindow_SDL2::MakeCurrent() {
  200. SDL_GL_MakeCurrent(render_window, gl_context);
  201. }
  202. void EmuWindow_SDL2::DoneCurrent() {
  203. SDL_GL_MakeCurrent(render_window, nullptr);
  204. }
  205. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(
  206. const std::pair<unsigned, unsigned>& minimal_size) {
  207. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  208. }