source: opengl-game/game-gui-sdl.cpp

feature/imgui-sdl
Last change on this file was 27e580e, checked in by Dmitry Portnoy <dportnoy@…>, 3 years ago

Stop using SDL_ttf

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