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 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) {
|
---|
14 | window = nullptr;
|
---|
15 | }
|
---|
16 |
|
---|
17 | string& GameGui_SDL::getError() {
|
---|
18 | s_errorMessage = SDL_GetError();
|
---|
19 |
|
---|
20 | return s_errorMessage;
|
---|
21 | }
|
---|
22 |
|
---|
23 | bool 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 |
|
---|
40 | void GameGui_SDL::shutdown() {
|
---|
41 | SDL_Quit();
|
---|
42 | }
|
---|
43 |
|
---|
44 | void* 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 |
|
---|
73 | void GameGui_SDL::destroyWindow() {
|
---|
74 | // TODO: This function can throw some errors. They should be handled
|
---|
75 | SDL_DestroyWindow(window);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void GameGui_SDL::processEvents() {
|
---|
79 | }
|
---|
80 |
|
---|
81 | int 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 |
|
---|
152 | bool GameGui_SDL::keyPressed(unsigned int key) {
|
---|
153 | return keyState[key];
|
---|
154 | }
|
---|
155 |
|
---|
156 | // TODO: Check that this is only used where the drawable size is needed
|
---|
157 | // Maybe create a separate refreshWindowSize funcion as well.
|
---|
158 | void GameGui_SDL::refreshWindowSize() {
|
---|
159 | // TODO: Make sure this works on a Mac (the analogous glfw function had issues on Mac retina displays)
|
---|
160 |
|
---|
161 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
162 | SDL_Vulkan_GetDrawableSize(window, &windowWidth, &windowHeight);
|
---|
163 | #else
|
---|
164 | SDL_GL_GetDrawableSize(window, &windowWidth, &windowHeight);
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
|
---|
168 | windowWidth = 0;
|
---|
169 | windowHeight = 0;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | int GameGui_SDL::getWindowWidth() {
|
---|
174 | return windowWidth;
|
---|
175 | }
|
---|
176 |
|
---|
177 | int GameGui_SDL::getWindowHeight() {
|
---|
178 | return windowHeight;
|
---|
179 | }
|
---|
180 |
|
---|
181 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
182 |
|
---|
183 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
184 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
185 | }
|
---|
186 |
|
---|
187 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
188 | uint32_t extensionCount = 0;
|
---|
189 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
190 |
|
---|
191 | vector<const char*> extensions(extensionCount);
|
---|
192 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
193 |
|
---|
194 | return extensions;
|
---|
195 | }
|
---|
196 |
|
---|
197 | #endif
|
---|