emu_window_sdl2.cpp 4.9 KB

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