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

feature/imgui-sdl points-test
Last change on this file since a52ba87 was 0ecab17, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

In GameGui_SDL, start setting a flag for key events to indicate repeated events

  • Property mode set to 100644
File size: 4.7 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
11string GameGui_SDL::s_errorMessage;
12
13GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) {
14}
15
16string& GameGui_SDL::getError() {
17 GameGui_SDL::s_errorMessage = SDL_GetError();
18
19 return GameGui_SDL::s_errorMessage;
20}
21
22bool 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 int imgFlags = IMG_INIT_PNG;
33 if (!(IMG_Init(imgFlags) & imgFlags)) {
34 return RTWO_ERROR;
35 }
36
37 if (TTF_Init() == -1) {
38 return RTWO_ERROR;
39 }
40
41 return RTWO_SUCCESS;
42}
43
44void GameGui_SDL::shutdown() {
45 SDL_Quit();
46}
47
48void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
49 // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
50
51 // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
52 // otherwise you will not receive a High DPI OpenGL canvas.
53
54 SDL_DisplayMode dm;
55 SDL_GetCurrentDisplayMode(0, &dm);
56
57 if (fullscreen) {
58 width = dm.w;
59 height = dm.h;
60 }
61
62#ifdef WINDOWS
63 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
64#else
65 uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
66#endif
67
68 window = SDL_CreateWindow(title.c_str(),
69 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
70 width, height, flags);
71
72 refreshWindowSize();
73
74 return window;
75}
76
77void GameGui_SDL::destroyWindow() {
78 // TODO: This function can throw some errors. They should be handled
79 SDL_DestroyWindow(window);
80}
81
82void GameGui_SDL::processEvents() {
83}
84
85int GameGui_SDL::pollEvent(UIEvent* event) {
86 SDL_Event e;
87
88 /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
89 * as mouse events since this game isn't targeting mobile devices
90 */
91
92 if (SDL_PollEvent(&e)) {
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 {
103 event->type = UI_EVENT_WINDOW;
104 }
105 break;
106 case SDL_KEYDOWN:
107 event->type = UI_EVENT_KEYDOWN;
108 event->key.keycode = e.key.keysym.scancode;
109 event->key.repeat = e.key.repeat != 0;
110 break;
111 case SDL_KEYUP:
112 event->type = UI_EVENT_KEYUP;
113 event->key.keycode = e.key.keysym.scancode;
114 event->key.repeat = e.key.repeat != 0;
115 break;
116 case SDL_MOUSEBUTTONDOWN:
117 case SDL_FINGERDOWN:
118 event->type = UI_EVENT_MOUSEBUTTONDOWN;
119 break;
120 case SDL_MOUSEBUTTONUP:
121 case SDL_FINGERUP:
122 event->type = UI_EVENT_MOUSEBUTTONUP;
123 break;
124 case SDL_MOUSEMOTION:
125 case SDL_FINGERMOTION:
126 event->type = UI_EVENT_MOUSEMOTION;
127 break;
128 // Ignore the following events
129 case SDL_AUDIODEVICEADDED:
130 case SDL_AUDIODEVICEREMOVED:
131 case SDL_TEXTINPUT:
132 case SDL_TEXTEDITING:
133 event->type = UI_EVENT_UNKNOWN;
134 event->unknown.eventType = e.type;
135 break;
136 default:
137 event->type = UI_EVENT_UNKNOWN;
138 event->unknown.eventType = 0;
139 }
140
141 return 1;
142 }
143
144 event = nullptr;
145 return 0;
146}
147
148bool GameGui_SDL::keyPressed(unsigned int key) {
149 return keyState[key];
150}
151
152void GameGui_SDL::refreshWindowSize() {
153 SDL_GetWindowSize(window, &windowWidth, &windowHeight);
154}
155
156int GameGui_SDL::getWindowWidth() {
157 return windowWidth;
158}
159
160int GameGui_SDL::getWindowHeight() {
161 return windowHeight;
162}
163
164#ifdef GAMEGUI_INCLUDE_VULKAN
165
166bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
167 return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
168}
169
170vector<const char*> GameGui_SDL::getRequiredExtensions() {
171 uint32_t extensionCount = 0;
172 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
173
174 vector<const char*> extensions(extensionCount);
175 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
176
177 return extensions;
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.