emu_window_sdl2.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. std::pair<unsigned, unsigned> EmuWindow_SDL2::TouchToPixelPos(float touch_x, float touch_y) const {
  40. int w, h;
  41. SDL_GetWindowSize(render_window, &w, &h);
  42. touch_x *= w;
  43. touch_y *= h;
  44. return {static_cast<unsigned>(std::max(std::round(touch_x), 0.0f)),
  45. static_cast<unsigned>(std::max(std::round(touch_y), 0.0f))};
  46. }
  47. void EmuWindow_SDL2::OnFingerDown(float x, float y) {
  48. // TODO(NeatNit): keep track of multitouch using the fingerID and a dictionary of some kind
  49. // This isn't critical because the best we can do when we have that is to average them, like the
  50. // 3DS does
  51. const auto [px, py] = TouchToPixelPos(x, y);
  52. TouchPressed(px, py);
  53. }
  54. void EmuWindow_SDL2::OnFingerMotion(float x, float y) {
  55. const auto [px, py] = TouchToPixelPos(x, y);
  56. TouchMoved(px, py);
  57. }
  58. void EmuWindow_SDL2::OnFingerUp() {
  59. TouchReleased();
  60. }
  61. void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
  62. if (state == SDL_PRESSED) {
  63. InputCommon::GetKeyboard()->PressKey(key);
  64. } else if (state == SDL_RELEASED) {
  65. InputCommon::GetKeyboard()->ReleaseKey(key);
  66. }
  67. }
  68. bool EmuWindow_SDL2::IsOpen() const {
  69. return is_open;
  70. }
  71. void EmuWindow_SDL2::OnResize() {
  72. int width, height;
  73. SDL_GetWindowSize(render_window, &width, &height);
  74. UpdateCurrentFramebufferLayout(width, height);
  75. }
  76. void EmuWindow_SDL2::Fullscreen() {
  77. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) {
  78. return;
  79. }
  80. LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError());
  81. // Try a different fullscreening method
  82. LOG_INFO(Frontend, "Attempting to use borderless fullscreen...");
  83. if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
  84. return;
  85. }
  86. LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError());
  87. // Fallback algorithm: Maximise window.
  88. // Works on all systems (unless something is seriously wrong), so no fallback for this one.
  89. LOG_INFO(Frontend, "Falling back on a maximised window...");
  90. SDL_MaximizeWindow(render_window);
  91. }
  92. bool EmuWindow_SDL2::SupportsRequiredGLExtensions() {
  93. std::vector<std::string> unsupported_ext;
  94. if (!GLAD_GL_ARB_direct_state_access)
  95. unsupported_ext.push_back("ARB_direct_state_access");
  96. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  97. unsupported_ext.push_back("ARB_vertex_type_10f_11f_11f_rev");
  98. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  99. unsupported_ext.push_back("ARB_texture_mirror_clamp_to_edge");
  100. if (!GLAD_GL_ARB_multi_bind)
  101. unsupported_ext.push_back("ARB_multi_bind");
  102. // Extensions required to support some texture formats.
  103. if (!GLAD_GL_EXT_texture_compression_s3tc)
  104. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  105. if (!GLAD_GL_ARB_texture_compression_rgtc)
  106. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  107. if (!GLAD_GL_ARB_depth_buffer_float)
  108. unsupported_ext.push_back("ARB_depth_buffer_float");
  109. for (const std::string& ext : unsupported_ext)
  110. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", ext);
  111. return unsupported_ext.empty();
  112. }
  113. EmuWindow_SDL2::EmuWindow_SDL2(bool fullscreen) {
  114. InputCommon::Init();
  115. SDL_SetMainReady();
  116. // Initialize the window
  117. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  118. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  119. exit(1);
  120. }
  121. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  122. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  123. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  124. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  125. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  126. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  127. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  128. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  129. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  130. Common::g_scm_branch, Common::g_scm_desc);
  131. render_window =
  132. SDL_CreateWindow(window_title.c_str(),
  133. SDL_WINDOWPOS_UNDEFINED, // x position
  134. SDL_WINDOWPOS_UNDEFINED, // y position
  135. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  136. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  137. if (render_window == nullptr) {
  138. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  139. exit(1);
  140. }
  141. if (fullscreen) {
  142. Fullscreen();
  143. }
  144. gl_context = SDL_GL_CreateContext(render_window);
  145. if (gl_context == nullptr) {
  146. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! {}", SDL_GetError());
  147. exit(1);
  148. }
  149. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  150. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  151. exit(1);
  152. }
  153. if (!SupportsRequiredGLExtensions()) {
  154. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  155. exit(1);
  156. }
  157. OnResize();
  158. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  159. SDL_PumpEvents();
  160. SDL_GL_SetSwapInterval(false);
  161. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  162. Common::g_scm_desc);
  163. Settings::LogSettings();
  164. DoneCurrent();
  165. }
  166. EmuWindow_SDL2::~EmuWindow_SDL2() {
  167. InputCommon::SDL::CloseSDLJoysticks();
  168. SDL_GL_DeleteContext(gl_context);
  169. SDL_Quit();
  170. InputCommon::Shutdown();
  171. }
  172. void EmuWindow_SDL2::SwapBuffers() {
  173. SDL_GL_SwapWindow(render_window);
  174. }
  175. void EmuWindow_SDL2::PollEvents() {
  176. SDL_Event event;
  177. // SDL_PollEvent returns 0 when there are no more events in the event queue
  178. while (SDL_PollEvent(&event)) {
  179. switch (event.type) {
  180. case SDL_WINDOWEVENT:
  181. switch (event.window.event) {
  182. case SDL_WINDOWEVENT_SIZE_CHANGED:
  183. case SDL_WINDOWEVENT_RESIZED:
  184. case SDL_WINDOWEVENT_MAXIMIZED:
  185. case SDL_WINDOWEVENT_RESTORED:
  186. case SDL_WINDOWEVENT_MINIMIZED:
  187. OnResize();
  188. break;
  189. case SDL_WINDOWEVENT_CLOSE:
  190. is_open = false;
  191. break;
  192. }
  193. break;
  194. case SDL_KEYDOWN:
  195. case SDL_KEYUP:
  196. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  197. break;
  198. case SDL_MOUSEMOTION:
  199. // ignore if it came from touch
  200. if (event.button.which != SDL_TOUCH_MOUSEID)
  201. OnMouseMotion(event.motion.x, event.motion.y);
  202. break;
  203. case SDL_MOUSEBUTTONDOWN:
  204. case SDL_MOUSEBUTTONUP:
  205. // ignore if it came from touch
  206. if (event.button.which != SDL_TOUCH_MOUSEID) {
  207. OnMouseButton(event.button.button, event.button.state, event.button.x,
  208. event.button.y);
  209. }
  210. break;
  211. case SDL_FINGERDOWN:
  212. OnFingerDown(event.tfinger.x, event.tfinger.y);
  213. break;
  214. case SDL_FINGERMOTION:
  215. OnFingerMotion(event.tfinger.x, event.tfinger.y);
  216. break;
  217. case SDL_FINGERUP:
  218. OnFingerUp();
  219. break;
  220. case SDL_QUIT:
  221. is_open = false;
  222. break;
  223. default:
  224. InputCommon::SDL::HandleGameControllerEvent(event);
  225. break;
  226. }
  227. }
  228. }
  229. void EmuWindow_SDL2::MakeCurrent() {
  230. SDL_GL_MakeCurrent(render_window, gl_context);
  231. }
  232. void EmuWindow_SDL2::DoneCurrent() {
  233. SDL_GL_MakeCurrent(render_window, nullptr);
  234. }
  235. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(
  236. const std::pair<unsigned, unsigned>& minimal_size) {
  237. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  238. }