emu_window_glfw.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #include "common/common.h"
  5. #include "video_core/video_core.h"
  6. #include "core/settings.h"
  7. #include "citra/emu_window/emu_window_glfw.h"
  8. /// Called by GLFW when a key event occurs
  9. void EmuWindow_GLFW::OnKeyEvent(GLFWwindow* win, int key, int scancode, int action, int mods) {
  10. if (!VideoCore::g_emu_window) {
  11. return;
  12. }
  13. int keyboard_id = ((EmuWindow_GLFW*)VideoCore::g_emu_window)->keyboard_id;
  14. if (action == GLFW_PRESS) {
  15. EmuWindow::KeyPressed({key, keyboard_id});
  16. }
  17. if (action == GLFW_RELEASE) {
  18. EmuWindow::KeyReleased({key, keyboard_id});
  19. }
  20. HID_User::PadUpdateComplete();
  21. }
  22. /// Whether the window is still open, and a close request hasn't yet been sent
  23. const bool EmuWindow_GLFW::IsOpen() {
  24. return glfwWindowShouldClose(m_render_window) == 0;
  25. }
  26. /// EmuWindow_GLFW constructor
  27. EmuWindow_GLFW::EmuWindow_GLFW() {
  28. keyboard_id = KeyMap::NewDeviceId();
  29. ReloadSetKeymaps();
  30. // Initialize the window
  31. if(glfwInit() != GL_TRUE) {
  32. printf("Failed to initialize GLFW! Exiting...");
  33. exit(1);
  34. }
  35. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  36. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  37. // GLFW on OSX requires these window hints to be set to create a 3.2+ GL context.
  38. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  39. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  40. m_render_window = glfwCreateWindow(VideoCore::kScreenTopWidth,
  41. (VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight),
  42. m_window_title.c_str(), NULL, NULL);
  43. if (m_render_window == NULL) {
  44. printf("Failed to create GLFW window! Exiting...");
  45. exit(1);
  46. }
  47. // Setup callbacks
  48. glfwSetWindowUserPointer(m_render_window, this);
  49. glfwSetKeyCallback(m_render_window, OnKeyEvent);
  50. DoneCurrent();
  51. }
  52. /// EmuWindow_GLFW destructor
  53. EmuWindow_GLFW::~EmuWindow_GLFW() {
  54. glfwTerminate();
  55. }
  56. /// Swap buffers to display the next frame
  57. void EmuWindow_GLFW::SwapBuffers() {
  58. glfwSwapBuffers(m_render_window);
  59. }
  60. /// Polls window events
  61. void EmuWindow_GLFW::PollEvents() {
  62. glfwPollEvents();
  63. }
  64. /// Makes the GLFW OpenGL context current for the caller thread
  65. void EmuWindow_GLFW::MakeCurrent() {
  66. glfwMakeContextCurrent(m_render_window);
  67. }
  68. /// Releases (dunno if this is the "right" word) the GLFW context from the caller thread
  69. void EmuWindow_GLFW::DoneCurrent() {
  70. glfwMakeContextCurrent(NULL);
  71. }
  72. void EmuWindow_GLFW::ReloadSetKeymaps() {
  73. KeyMap::SetKeyMapping({Settings::values.pad_a_key, keyboard_id}, HID_User::PAD_A);
  74. KeyMap::SetKeyMapping({Settings::values.pad_b_key, keyboard_id}, HID_User::PAD_B);
  75. KeyMap::SetKeyMapping({Settings::values.pad_select_key, keyboard_id}, HID_User::PAD_SELECT);
  76. KeyMap::SetKeyMapping({Settings::values.pad_start_key, keyboard_id}, HID_User::PAD_START);
  77. KeyMap::SetKeyMapping({Settings::values.pad_dright_key, keyboard_id}, HID_User::PAD_RIGHT);
  78. KeyMap::SetKeyMapping({Settings::values.pad_dleft_key, keyboard_id}, HID_User::PAD_LEFT);
  79. KeyMap::SetKeyMapping({Settings::values.pad_dup_key, keyboard_id}, HID_User::PAD_UP);
  80. KeyMap::SetKeyMapping({Settings::values.pad_ddown_key, keyboard_id}, HID_User::PAD_DOWN);
  81. KeyMap::SetKeyMapping({Settings::values.pad_r_key, keyboard_id}, HID_User::PAD_R);
  82. KeyMap::SetKeyMapping({Settings::values.pad_l_key, keyboard_id}, HID_User::PAD_L);
  83. KeyMap::SetKeyMapping({Settings::values.pad_x_key, keyboard_id}, HID_User::PAD_X);
  84. KeyMap::SetKeyMapping({Settings::values.pad_y_key, keyboard_id}, HID_User::PAD_Y);
  85. KeyMap::SetKeyMapping({Settings::values.pad_sright_key, keyboard_id}, HID_User::PAD_CIRCLE_RIGHT);
  86. KeyMap::SetKeyMapping({Settings::values.pad_sleft_key, keyboard_id}, HID_User::PAD_CIRCLE_LEFT);
  87. KeyMap::SetKeyMapping({Settings::values.pad_sup_key, keyboard_id}, HID_User::PAD_CIRCLE_UP);
  88. KeyMap::SetKeyMapping({Settings::values.pad_sdown_key, keyboard_id}, HID_User::PAD_CIRCLE_DOWN);
  89. }