source: opengl-game/game-gui-sdl.cpp@ 2d87297

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

In VulkanGame, add the ability to delete asteroids and move them at a constant speed

  • 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 break;
110 case SDL_KEYUP:
111 event->type = UI_EVENT_KEYUP;
112 event->key.keycode = e.key.keysym.scancode;
113 break;
114 case SDL_MOUSEBUTTONDOWN:
115 case SDL_FINGERDOWN:
116 event->type = UI_EVENT_MOUSEBUTTONDOWN;
117 break;
118 case SDL_MOUSEBUTTONUP:
119 case SDL_FINGERUP:
120 event->type = UI_EVENT_MOUSEBUTTONUP;
121 break;
122 case SDL_MOUSEMOTION:
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: // TODO: Research this one later
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 SDL_GetWindowSize(window, &windowWidth, &windowHeight);
152}
153
154int GameGui_SDL::getWindowWidth() {
155 return windowWidth;
156}
157
158int GameGui_SDL::getWindowHeight() {
159 return windowHeight;
160}
161
162#ifdef GAMEGUI_INCLUDE_VULKAN
163
164bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
165 return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
166}
167
168vector<const char*> GameGui_SDL::getRequiredExtensions() {
169 uint32_t extensionCount = 0;
170 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
171
172 vector<const char*> extensions(extensionCount);
173 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
174
175 return extensions;
176}
177
178#endif
Note: See TracBrowser for help on using the repository browser.