15 #define SUBSYSTEMS_USED (SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER)
31 int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
33 flags |= SDL_WINDOW_FULLSCREEN;
35 if ((SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) != 0)
36 || (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) != 0)
37 || (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) != 0)
38 || (SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) != 0)
39 || (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24) != 0)
40 || (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0)
41 || (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1) != 0)
43 || (SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4) != 0)
44 || (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3) != 0)
45 || (SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3) != 0)
46 || (SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE))) {
51 window = SDL_CreateWindow(VERSION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
59 SDL_GetWindowSize(
window, &w, &h);
72 if (SDL_NumJoysticks() == 0) {
77 SDL_GameControllerAddMapping(
"341a0000000000000208000000000000,"
79 "a:b0,x:b2,start:b7,back:b6,leftstick:b8,rightstick:b9,"
80 "leftshoulder:b4,rightshoulder:b5,"
81 "dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,"
82 "leftx:a0,lefty:a1,rightx:a3,righty:a2,"
83 "lefttrigger:,b:b1,y:b3,lefttrigger:a4,righttrigger:a4");
85 SDL_GameControllerAddMapping(
"10080000000000000100000000000000,"
87 "a:b4,b:b2,y:b0,x:b6,start:b18,back:b16,leftstick:b20,"
88 "rightstick:b22,leftshoulder:b12,rightshoulder:b14,dpup:h0.1,"
89 "dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a2,rightx:a6,"
90 "righty:a4,lefttrigger:b8,righttrigger:b10");
92 for (
int i = 0; i < SDL_NumJoysticks(); i++) {
93 if (SDL_IsGameController(i)) {
100 Log::get(
LOG_WARNING) <<
"Couldn't open controller \"" << SDL_GameControllerNameForIndex(i)
105 <<
"\" is no compatible controller!" <<
Log::endl;
116 while (SDL_PollEvent(&event)) {
117 switch (event.type) {
118 case SDL_CONTROLLERAXISMOTION:
120 if (event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX)
122 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY)
124 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_RIGHTX)
126 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
128 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT)
130 else if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
136 case SDL_CONTROLLERBUTTONDOWN:
137 case SDL_CONTROLLERBUTTONUP:
139 if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A)
141 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_B)
143 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_X)
145 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_Y)
147 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK)
149 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_GUIDE)
151 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_START)
153 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSTICK)
155 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSTICK)
157 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER)
159 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)
161 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP)
163 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
165 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT)
167 else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT)
173 case SDL_MOUSEMOTION:
177 case SDL_MOUSEBUTTONDOWN:
178 case SDL_MOUSEBUTTONUP:
180 if (event.button.button == SDL_BUTTON_LEFT)
182 else if (event.button.button == SDL_BUTTON_RIGHT)
184 else if (event.button.button == SDL_BUTTON_MIDDLE)
186 else if (event.button.button == SDL_BUTTON_X1)
188 else if (event.button.button == SDL_BUTTON_X2)
192 (event.type == SDL_MOUSEBUTTONUP));
196 if ((event.wheel.x != 0) || (event.wheel.y != 0))
201 case SDL_TEXTEDITING:
202 if (event.text.text !=
nullptr)
209 switch (event.key.keysym.sym) {
408 case SDLK_LEFTBRACKET:
420 case SDLK_NUMLOCKCLEAR:
448 case SDLK_RIGHTBRACKET:
454 case SDLK_SCROLLLOCK:
477 case SDL_WINDOWEVENT:
478 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
479 size = glm::i32vec2(event.window.data1, event.window.data2);
494 SDL_GL_SwapWindow(
window);
509 SDL_DestroyWindow(
window);
517 assert((s.x > 0) && (s.y > 0));
519 if ((s.x !=
size.x) || (s.y !=
size.y)) {
520 SDL_SetWindowSize(
window, s.x, s.y);
530 SDL_SetWindowFullscreen(
window, SDL_WINDOW_FULLSCREEN_DESKTOP);
532 SDL_SetWindowFullscreen(
window, 0);
541 SDL_SetRelativeMouseMode(SDL_TRUE);
543 SDL_SetRelativeMouseMode(SDL_FALSE);
552 SDL_StartTextInput();
559 if (SDL_SetClipboardText(s) < 0) {
568 const char* ret = SDL_GetClipboardText();
569 if (ret ==
nullptr) {
584 SDL_SetTextInputRect(&rect);
static void handleMouseScroll(int xrel, int yrel)
SDL2 Windowing Implementation.
static void handleControllerAxis(float value, KeyboardButton axis)
static SDL_Window * window
static void swapBuffers()
static void setMousegrab(bool g)
static void handleControllerButton(KeyboardButton button, bool released)
static void setClipboard(const char *s)
static void setTextInput(bool t)
static SDL_GameController * controller
static void inputPositionCallback(int x, int y)
static LogLevel & get(int level)
static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs)
static void setFullscreen(bool f)
static void handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released)
static void setRunning(bool run)
Runtime Configuration Storage.
static void handleKeyboard(KeyboardButton key, bool pressed)
static SDL_GLContext context
static void eventsFinished()
static void handleText(char *text, bool notFinished)
static void setSize(glm::i32vec2 s)
static void setSize(glm::i32vec2 s)
static void eventHandling()
static const char * getClipboard()