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

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

Make some minor updates to VulkanGame

  • Property mode set to 100644
File size: 4.8 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* event) {
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 switch(e.type) {
90 case SDL_QUIT:
91 event->type = UI_EVENT_QUIT;
92 break;
93 case SDL_WINDOWEVENT:
94 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
95 e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
96 e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
97 event->type = UI_EVENT_WINDOWRESIZE;
98 } else {
99 event->type = UI_EVENT_WINDOW;
100 }
101 break;
102 case SDL_KEYDOWN:
103 event->type = UI_EVENT_KEYDOWN;
104 event->key.keycode = e.key.keysym.scancode;
105 event->key.repeat = e.key.repeat != 0;
106 break;
107 case SDL_KEYUP:
108 event->type = UI_EVENT_KEYUP;
109 event->key.keycode = e.key.keysym.scancode;
110 event->key.repeat = e.key.repeat != 0;
111 break;
112 case SDL_MOUSEBUTTONDOWN:
113 case SDL_FINGERDOWN:
114 event->type = UI_EVENT_MOUSEBUTTONDOWN;
115 break;
116 case SDL_MOUSEBUTTONUP:
117 case SDL_FINGERUP:
118 event->type = UI_EVENT_MOUSEBUTTONUP;
119 break;
120 case SDL_MOUSEMOTION:
121 event->mouse.x = e.motion.x;
122 event->mouse.y = e.motion.y;
123 case SDL_FINGERMOTION:
124 event->type = UI_EVENT_MOUSEMOTION;
125 break;
126 // Ignore the following events
127 case SDL_AUDIODEVICEADDED:
128 case SDL_AUDIODEVICEREMOVED:
129 case SDL_TEXTINPUT:
130 case SDL_TEXTEDITING:
131 event->type = UI_EVENT_UNKNOWN;
132 event->unknown.eventType = e.type;
133 break;
134 default:
135 event->type = UI_EVENT_UNKNOWN;
136 event->unknown.eventType = 0;
137 }
138
139 return 1;
140 }
141
142 event = nullptr;
143 return 0;
144}
145
146bool GameGui_SDL::keyPressed(unsigned int key) {
147 return keyState[key];
148}
149
150void GameGui_SDL::refreshWindowSize() {
151 // TODO: Make sure this works on a mac (the analogous glfw function had issues on Mac retina displays)
152 SDL_GetWindowSize(window, &windowWidth, &windowHeight);
153}
154
155int GameGui_SDL::getWindowWidth() {
156 return windowWidth;
157}
158
159int GameGui_SDL::getWindowHeight() {
160 return windowHeight;
161}
162
163#ifdef GAMEGUI_INCLUDE_VULKAN
164
165bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
166 return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
167}
168
169vector<const char*> GameGui_SDL::getRequiredExtensions() {
170 uint32_t extensionCount = 0;
171 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
172
173 vector<const char*> extensions(extensionCount);
174 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
175
176 return extensions;
177}
178
179#endif
Note: See TracBrowser for help on using the repository browser.