source: opengl-game/game-gui-sdl.cpp@ 187b0f5

feature/imgui-sdl
Last change on this file since 187b0f5 was 429ac01, checked in by Dmitry Portnoy <dportnoy@…>, 4 years ago

Remove UnknownEvent since it doesn't add any new fields and add a UI_EVENT_UNHANDLED type to distinguish between UI events I haven't considered handling at all and ones I am aware of, but do not use right now

  • Property mode set to 100644
File size: 5.2 KB
Line 
1#include "game-gui-sdl.hpp"
2
3#include <map>
4#include <queue>
5
6#include <SDL2/SDL_ttf.h>
7
8#include "compiler.hpp"
9#include "consts.hpp"
10
11using namespace std;
12
13GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) {
14 window = nullptr;
15}
16
17string& GameGui_SDL::getError() {
18 s_errorMessage = SDL_GetError();
19
20 return s_errorMessage;
21}
22
23bool GameGui_SDL::init() {
24 // may want to define SDL_INIT_NOPARACHUTE when I start handling crashes since it
25 // prevents SDL from setting up its own handlers for SIG_SEGV and stuff like that
26
27 s_errorMessage = "No error";
28
29 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
30 return RTWO_ERROR;
31 }
32
33 if (TTF_Init() == -1) {
34 return RTWO_ERROR;
35 }
36
37 return RTWO_SUCCESS;
38}
39
40void GameGui_SDL::shutdown() {
41 SDL_Quit();
42}
43
44void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
45 // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
46
47 // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
48 // otherwise you will not receive a High DPI OpenGL canvas.
49
50 SDL_DisplayMode dm;
51 SDL_GetCurrentDisplayMode(0, &dm);
52
53 if (fullscreen) {
54 width = dm.w;
55 height = dm.h;
56 }
57
58#ifdef WINDOWS
59 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI |
60 (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
61#else
62 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI |
63 (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
64#endif
65
66 window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
67
68 refreshWindowSize();
69
70 return window;
71}
72
73void GameGui_SDL::destroyWindow() {
74 // TODO: This function can throw some errors. They should be handled
75 SDL_DestroyWindow(window);
76}
77
78void GameGui_SDL::processEvents() {
79}
80
81int GameGui_SDL::pollEvent(UIEvent* uiEvent) {
82 SDL_Event e;
83
84 /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
85 * as mouse events since this game isn't targeting mobile devices
86 */
87
88 if (SDL_PollEvent(&e)) {
89 uiEvent->rawEvent.sdl = e;
90
91 GameEvent* event = &uiEvent->event;
92
93 switch(e.type) {
94 case SDL_QUIT:
95 event->type = UI_EVENT_QUIT;
96 break;
97 case SDL_WINDOWEVENT:
98 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
99 e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
100 e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
101 event->type = UI_EVENT_WINDOWRESIZE;
102 } else if (e.window.event == SDL_WINDOWEVENT_CLOSE &&
103 e.window.windowID == SDL_GetWindowID(window)) {
104 event->type = UI_EVENT_QUIT;
105 } else {
106 event->type = UI_EVENT_WINDOW;
107 }
108 break;
109 case SDL_KEYDOWN:
110 event->type = UI_EVENT_KEYDOWN;
111 event->key.keycode = e.key.keysym.scancode;
112 event->key.repeat = e.key.repeat != 0;
113 break;
114 case SDL_KEYUP:
115 event->type = UI_EVENT_KEYUP;
116 event->key.keycode = e.key.keysym.scancode;
117 event->key.repeat = e.key.repeat != 0;
118 break;
119 case SDL_MOUSEBUTTONDOWN:
120 case SDL_FINGERDOWN:
121 event->type = UI_EVENT_MOUSEBUTTONDOWN;
122 break;
123 case SDL_MOUSEBUTTONUP:
124 case SDL_FINGERUP:
125 event->type = UI_EVENT_MOUSEBUTTONUP;
126 break;
127 case SDL_MOUSEMOTION:
128 event->mouse.x = e.motion.x;
129 event->mouse.y = e.motion.y;
130 case SDL_FINGERMOTION:
131 // TODO: Get coordinates for finger events
132 event->type = UI_EVENT_MOUSEMOTION;
133 break;
134 // The following events are not currently supported
135 case SDL_AUDIODEVICEADDED:
136 case SDL_AUDIODEVICEREMOVED:
137 case SDL_TEXTINPUT:
138 case SDL_TEXTEDITING:
139 case SDL_MOUSEWHEEL:
140 event->type = UI_EVENT_UNHANDLED;
141 break;
142 default:
143 event->type = UI_EVENT_UNKNOWN;
144 }
145
146 return 1;
147 } else {
148 return 0;
149 }
150}
151
152bool GameGui_SDL::keyPressed(unsigned int key) {
153 return keyState[key];
154}
155
156void GameGui_SDL::refreshWindowSize() {
157 // TODO: Make sure this works on a mac (the analogous glfw function had issues on Mac retina displays)
158 SDL_GetWindowSize(window, &windowWidth, &windowHeight);
159
160 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
161 windowWidth = 0;
162 windowHeight = 0;
163 }
164}
165
166int GameGui_SDL::getWindowWidth() {
167 return windowWidth;
168}
169
170int GameGui_SDL::getWindowHeight() {
171 return windowHeight;
172}
173
174#ifdef GAMEGUI_INCLUDE_VULKAN
175
176bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
177 return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
178}
179
180vector<const char*> GameGui_SDL::getRequiredExtensions() {
181 uint32_t extensionCount = 0;
182 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
183
184 vector<const char*> extensions(extensionCount);
185 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
186
187 return extensions;
188}
189
190#endif
Note: See TracBrowser for help on using the repository browser.