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