emu_window_sdl2.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <glad/glad.h>
  10. #include "citra/emu_window/emu_window_sdl2.h"
  11. #include "common/logging/log.h"
  12. #include "common/scm_rev.h"
  13. #include "common/string_util.h"
  14. #include "core/3ds.h"
  15. #include "core/settings.h"
  16. #include "input_common/keyboard.h"
  17. #include "input_common/main.h"
  18. void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
  19. TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
  20. motion_emu->Tilt(x, y);
  21. }
  22. void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
  23. if (button == SDL_BUTTON_LEFT) {
  24. if (state == SDL_PRESSED) {
  25. TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
  26. } else {
  27. TouchReleased();
  28. }
  29. } else if (button == SDL_BUTTON_RIGHT) {
  30. if (state == SDL_PRESSED) {
  31. motion_emu->BeginTilt(x, y);
  32. } else {
  33. motion_emu->EndTilt();
  34. }
  35. }
  36. }
  37. void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
  38. if (state == SDL_PRESSED) {
  39. InputCommon::GetKeyboard()->PressKey(key);
  40. } else if (state == SDL_RELEASED) {
  41. InputCommon::GetKeyboard()->ReleaseKey(key);
  42. }
  43. }
  44. bool EmuWindow_SDL2::IsOpen() const {
  45. return is_open;
  46. }
  47. void EmuWindow_SDL2::OnResize() {
  48. int width, height;
  49. SDL_GetWindowSize(render_window, &width, &height);
  50. UpdateCurrentFramebufferLayout(width, height);
  51. }
  52. EmuWindow_SDL2::EmuWindow_SDL2() {
  53. InputCommon::Init();
  54. motion_emu = std::make_unique<Motion::MotionEmu>(*this);
  55. SDL_SetMainReady();
  56. // Initialize the window
  57. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  58. LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
  59. exit(1);
  60. }
  61. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  62. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  63. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  64. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  65. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  66. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  67. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  68. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
  69. std::string window_title = Common::StringFromFormat("Citra %s| %s-%s ", Common::g_build_name,
  70. Common::g_scm_branch, Common::g_scm_desc);
  71. render_window =
  72. SDL_CreateWindow(window_title.c_str(),
  73. SDL_WINDOWPOS_UNDEFINED, // x position
  74. SDL_WINDOWPOS_UNDEFINED, // y position
  75. Core::kScreenTopWidth, Core::kScreenTopHeight + Core::kScreenBottomHeight,
  76. SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  77. if (render_window == nullptr) {
  78. LOG_CRITICAL(Frontend, "Failed to create SDL2 window! Exiting...");
  79. exit(1);
  80. }
  81. gl_context = SDL_GL_CreateContext(render_window);
  82. if (gl_context == nullptr) {
  83. LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! Exiting...");
  84. exit(1);
  85. }
  86. if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) {
  87. LOG_CRITICAL(Frontend, "Failed to initialize GL functions! Exiting...");
  88. exit(1);
  89. }
  90. OnResize();
  91. OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
  92. SDL_PumpEvents();
  93. SDL_GL_SetSwapInterval(Settings::values.use_vsync);
  94. DoneCurrent();
  95. }
  96. EmuWindow_SDL2::~EmuWindow_SDL2() {
  97. SDL_GL_DeleteContext(gl_context);
  98. SDL_Quit();
  99. motion_emu = nullptr;
  100. InputCommon::Shutdown();
  101. }
  102. void EmuWindow_SDL2::SwapBuffers() {
  103. SDL_GL_SwapWindow(render_window);
  104. }
  105. void EmuWindow_SDL2::PollEvents() {
  106. SDL_Event event;
  107. // SDL_PollEvent returns 0 when there are no more events in the event queue
  108. while (SDL_PollEvent(&event)) {
  109. switch (event.type) {
  110. case SDL_WINDOWEVENT:
  111. switch (event.window.event) {
  112. case SDL_WINDOWEVENT_SIZE_CHANGED:
  113. case SDL_WINDOWEVENT_RESIZED:
  114. case SDL_WINDOWEVENT_MAXIMIZED:
  115. case SDL_WINDOWEVENT_RESTORED:
  116. case SDL_WINDOWEVENT_MINIMIZED:
  117. OnResize();
  118. break;
  119. case SDL_WINDOWEVENT_CLOSE:
  120. is_open = false;
  121. break;
  122. }
  123. break;
  124. case SDL_KEYDOWN:
  125. case SDL_KEYUP:
  126. OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
  127. break;
  128. case SDL_MOUSEMOTION:
  129. OnMouseMotion(event.motion.x, event.motion.y);
  130. break;
  131. case SDL_MOUSEBUTTONDOWN:
  132. case SDL_MOUSEBUTTONUP:
  133. OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
  134. break;
  135. case SDL_QUIT:
  136. is_open = false;
  137. break;
  138. }
  139. }
  140. }
  141. void EmuWindow_SDL2::MakeCurrent() {
  142. SDL_GL_MakeCurrent(render_window, gl_context);
  143. }
  144. void EmuWindow_SDL2::DoneCurrent() {
  145. SDL_GL_MakeCurrent(render_window, nullptr);
  146. }
  147. void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(
  148. const std::pair<unsigned, unsigned>& minimal_size) {
  149. SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
  150. }