summaryrefslogtreecommitdiff
path: root/src/citra/emu_window/emu_window_sdl2.cpp
diff options
context:
space:
mode:
authorGravatar MerryMage2016-03-01 17:24:18 +0000
committerGravatar MerryMage2016-03-02 14:09:02 +0000
commitba2a54a9dd6e5a263c5e6886e55b3bc55b95b4ab (patch)
tree182d660b2f2c8572266144f46817e5a221aa4caf /src/citra/emu_window/emu_window_sdl2.cpp
parentMerge pull request #1424 from MerryMage/lut_init (diff)
downloadyuzu-ba2a54a9dd6e5a263c5e6886e55b3bc55b95b4ab.tar.gz
yuzu-ba2a54a9dd6e5a263c5e6886e55b3bc55b95b4ab.tar.xz
yuzu-ba2a54a9dd6e5a263c5e6886e55b3bc55b95b4ab.zip
Dependencies: Remove GLFW, Add SDL2
citra: Remove GLFW, Add SDL2 FindSDL2: Do not CACHE SDL2_* variables if library is not found EmuWindow_SDL2: Set minimal client area at initialisation time EmuWindow_SDL2: Corrections EmuWindow_SDL2: Fix no decorations on startup on OS X cmake: windows_copy_files
Diffstat (limited to 'src/citra/emu_window/emu_window_sdl2.cpp')
-rw-r--r--src/citra/emu_window/emu_window_sdl2.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/citra/emu_window/emu_window_sdl2.cpp b/src/citra/emu_window/emu_window_sdl2.cpp
new file mode 100644
index 000000000..1fed82e78
--- /dev/null
+++ b/src/citra/emu_window/emu_window_sdl2.cpp
@@ -0,0 +1,167 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <cstdlib>
7#include <string>
8
9#define SDL_MAIN_HANDLED
10#include <SDL.h>
11
12#include "common/key_map.h"
13#include "common/logging/log.h"
14#include "common/scm_rev.h"
15#include "common/string_util.h"
16
17#include "core/settings.h"
18#include "core/hle/service/hid/hid.h"
19
20#include "citra/emu_window/emu_window_sdl2.h"
21
22#include "video_core/video_core.h"
23
24void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
25 TouchMoved((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
26}
27
28void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
29 if (button != SDL_BUTTON_LEFT)
30 return;
31
32 if (state == SDL_PRESSED) {
33 TouchPressed((unsigned)std::max(x, 0), (unsigned)std::max(y, 0));
34 } else {
35 TouchReleased();
36 }
37}
38
39void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
40 if (state == SDL_PRESSED) {
41 KeyPressed({ key, keyboard_id });
42 } else if (state == SDL_RELEASED) {
43 KeyReleased({ key, keyboard_id });
44 }
45}
46
47bool EmuWindow_SDL2::IsOpen() const {
48 return is_open;
49}
50
51void EmuWindow_SDL2::OnResize() {
52 int width, height;
53
54 SDL_GetWindowSize(render_window, &width, &height);
55
56 NotifyFramebufferLayoutChanged(EmuWindow::FramebufferLayout::DefaultScreenLayout(width, height));
57}
58
59EmuWindow_SDL2::EmuWindow_SDL2() {
60 keyboard_id = KeyMap::NewDeviceId();
61
62 ReloadSetKeymaps();
63
64 SDL_SetMainReady();
65
66 // Initialize the window
67 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
68 LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting...");
69 exit(1);
70 }
71
72 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
73 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
74 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
75 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
76
77 std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
78 render_window = SDL_CreateWindow(window_title.c_str(),
79 SDL_WINDOWPOS_UNDEFINED, // x position
80 SDL_WINDOWPOS_UNDEFINED, // y position
81 VideoCore::kScreenTopWidth,
82 VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight,
83 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
84
85 if (render_window == nullptr) {
86 LOG_CRITICAL(Frontend, "Failed to create SDL2 window! Exiting...");
87 exit(1);
88 }
89
90 gl_context = SDL_GL_CreateContext(render_window);
91
92 if (gl_context == nullptr) {
93 LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! Exiting...");
94 exit(1);
95 }
96
97 OnResize();
98 OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size);
99 SDL_PumpEvents();
100
101 DoneCurrent();
102}
103
104EmuWindow_SDL2::~EmuWindow_SDL2() {
105 SDL_GL_DeleteContext(gl_context);
106 SDL_Quit();
107}
108
109void EmuWindow_SDL2::SwapBuffers() {
110 SDL_GL_SwapWindow(render_window);
111}
112
113void EmuWindow_SDL2::PollEvents() {
114 SDL_Event event;
115
116 // SDL_PollEvent returns 0 when there are no more events in the event queue
117 while (SDL_PollEvent(&event)) {
118 switch (event.type) {
119 case SDL_WINDOWEVENT:
120 switch (event.window.event) {
121 case SDL_WINDOWEVENT_SIZE_CHANGED:
122 case SDL_WINDOWEVENT_RESIZED:
123 case SDL_WINDOWEVENT_MAXIMIZED:
124 case SDL_WINDOWEVENT_RESTORED:
125 case SDL_WINDOWEVENT_MINIMIZED:
126 OnResize();
127 break;
128 case SDL_WINDOWEVENT_CLOSE:
129 is_open = false;
130 break;
131 }
132 break;
133 case SDL_KEYDOWN:
134 case SDL_KEYUP:
135 OnKeyEvent(static_cast<int>(event.key.keysym.scancode), event.key.state);
136 break;
137 case SDL_MOUSEMOTION:
138 OnMouseMotion(event.motion.x, event.motion.y);
139 break;
140 case SDL_MOUSEBUTTONDOWN:
141 case SDL_MOUSEBUTTONUP:
142 OnMouseButton(event.button.button, event.button.state, event.button.x, event.button.y);
143 break;
144 case SDL_QUIT:
145 is_open = false;
146 break;
147 }
148 }
149}
150
151void EmuWindow_SDL2::MakeCurrent() {
152 SDL_GL_MakeCurrent(render_window, gl_context);
153}
154
155void EmuWindow_SDL2::DoneCurrent() {
156 SDL_GL_MakeCurrent(render_window, nullptr);
157}
158
159void EmuWindow_SDL2::ReloadSetKeymaps() {
160 for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
161 KeyMap::SetKeyMapping({ Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id }, Service::HID::pad_mapping[i]);
162 }
163}
164
165void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(const std::pair<unsigned, unsigned>& minimal_size) {
166 SDL_SetWindowMinimumSize(render_window, minimal_size.first, minimal_size.second);
167}