emu_window_sdl2.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_program_interface_query)
  95. unsupported_ext.push_back("ARB_program_interface_query");
  96. if (!GLAD_GL_ARB_separate_shader_objects)
  97. unsupported_ext.push_back("ARB_separate_shader_objects");
  98. if (!GLAD_GL_ARB_vertex_attrib_binding)
  99. unsupported_ext.push_back("ARB_vertex_attrib_binding");
  100. if (!GLAD_GL_ARB_vertex_type_10f_11f_11f_rev)
  101. unsupported_ext.push_back("ARB_vertex_type_10f_11f_11f_rev");
  102. if (!GLAD_GL_ARB_texture_mirror_clamp_to_edge)
  103. unsupported_ext.push_back("ARB_texture_mirror_clamp_to_edge");
  104. if (!GLAD_GL_ARB_base_instance)
  105. unsupported_ext.push_back("ARB_base_instance");
  106. if (!GLAD_GL_ARB_texture_storage)
  107. unsupported_ext.push_back("ARB_texture_storage");
  108. if (!GLAD_GL_ARB_multi_bind)
  109. unsupported_ext.push_back("ARB_multi_bind");
  110. if (!GLAD_GL_ARB_copy_image)
  111. unsupported_ext.push_back("ARB_copy_image");
  112. // Extensions required to support some texture formats.
  113. if (!GLAD_GL_EXT_texture_compression_s3tc)
  114. unsupported_ext.push_back("EXT_texture_compression_s3tc");
  115. if (!GLAD_GL_ARB_texture_compression_rgtc)
  116. unsupported_ext.push_back("ARB_texture_compression_rgtc");
  117. if (!GLAD_GL_ARB_texture_compression_bptc)
  118. unsupported_ext.push_back("ARB_texture_compression_bptc");
  119. if (!GLAD_GL_ARB_depth_buffer_float)
  120. unsupported_ext.push_back("ARB_depth_buffer_float");
  121. for (const std::string& ext : unsupported_ext)
  122. LOG_CRITICAL(Frontend, "Unsupported GL extension: {}", ext);
  123. return unsupported_ext.empty();
  124. }
  125. EmuWindow_SDL2::EmuWindow_SDL2(bool fullscreen) {
  126. InputCommon::Init();
  127. SDL_SetMainReady();
  128. // Initialize the window
  129. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  130. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  131. exit(1);
  132. }
  133. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  134. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  135. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  136. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  137. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  138. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  139. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  140. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  141. std::string window_title = fmt::format("yuzu {} | {}-{}", Common::g_build_fullname,
  142. Common::g_scm_branch, Common::g_scm_desc);
  143. render_window =
  144. SDL_CreateWindow(window_title.c_str(),
  145. SDL_WINDOWPOS_UNDEFINED, // x position
  146. SDL_WINDOWPOS_UNDEFINED, // y position
  147. Layout::ScreenUndocked::Width, Layout::ScreenUndocked::Height,
  148. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  149. if (render_window == nullptr) {
  150. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! {}", SDL_GetError());
  151. exit(1);
  152. }
  153. if (fullscreen) {
  154. Fullscreen();
  155. }
  156. gl_context = SDL_GL_CreateContext(render_window);
  157. if (gl_context == nullptr) {
  158. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! {}", SDL_GetError());
  159. exit(1);
  160. }
  161. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  162. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! {}", SDL_GetError());
  163. exit(1);
  164. }
  165. if (!SupportsRequiredGLExtensions()) {
  166. LOG_CRITICAL(Frontend, "GPU does not support all required OpenGL extensions! Exiting...");
  167. exit(1);
  168. }
  169. OnResize();
  170. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  171. SDL_PumpEvents();
  172. SDL_GL_SetSwapInterval(false);
  173. LOG_INFO(Frontend, "yuzu Version: {} | {}-{}", Common::g_build_fullname, Common::g_scm_branch,
  174. Common::g_scm_desc);
  175. DoneCurrent();
  176. }
  177. EmuWindow_SDL2::~EmuWindow_SDL2() {
  178. InputCommon::SDL::CloseSDLJoysticks();
  179. SDL_GL_DeleteContext(gl_context);
  180. SDL_Quit();
  181. InputCommon::Shutdown();
  182. }
  183. void EmuWindow_SDL2::SwapBuffers() {
  184. SDL_GL_SwapWindow(render_window);
  185. }
  186. void EmuWindow_SDL2::PollEvents() {
  187. SDL_Event event;
  188. // SDL_PollEvent returns 0 when there are no more events in the event queue
  189. while (SDL_PollEvent(&event)) {
  190. switch (event.type) {
  191. case SDL_WINDOWEVENT:
  192. switch (event.window.event) {
  193. case SDL_WINDOWEVENT_SIZE_CHANGED:
  194. case SDL_WINDOWEVENT_RESIZED:
  195. case SDL_WINDOWEVENT_MAXIMIZED:
  196. case SDL_WINDOWEVENT_RESTORED:
  197. case SDL_WINDOWEVENT_MINIMIZED:
  198. OnResize();
  199. break;
  200. case SDL_WINDOWEVENT_CLOSE:
  201. is_open = false;
  202. break;
  203. }
  204. break;
  205. case SDL_KEYDOWN:
  206. case SDL_KEYUP:
  207. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  208. break;
  209. case SDL_MOUSEMOTION:
  210. // ignore if it came from touch
  211. if (event.button.which != SDL_TOUCH_MOUSEID)
  212. OnMouseMotion(event.motion.x, event.motion.y);
  213. break;
  214. case SDL_MOUSEBUTTONDOWN:
  215. case SDL_MOUSEBUTTONUP:
  216. // ignore if it came from touch
  217. if (event.button.which != SDL_TOUCH_MOUSEID) {
  218. OnMouseButton(event.button.button, event.button.state, event.button.x,
  219. event.button.y);
  220. }
  221. break;
  222. case SDL_FINGERDOWN:
  223. OnFingerDown(event.tfinger.x, event.tfinger.y);
  224. break;
  225. case SDL_FINGERMOTION:
  226. OnFingerMotion(event.tfinger.x, event.tfinger.y);
  227. break;
  228. case SDL_FINGERUP:
  229. OnFingerUp();
  230. break;
  231. case SDL_QUIT:
  232. is_open = false;
  233. break;
  234. default:
  235. InputCommon::SDL::HandleGameControllerEvent(event);
  236. break;
  237. }
  238. }
  239. }
  240. void EmuWindow_SDL2::MakeCurrent() {
  241. SDL_GL_MakeCurrent(render_window, gl_context);
  242. }
  243. void EmuWindow_SDL2::DoneCurrent() {
  244. SDL_GL_MakeCurrent(render_window, nullptr);
  245. }
  246. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(
  247. const std::pair<unsigned, unsigned>& minimal_size) {
  248. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  249. }