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

feature/imgui-sdl points-test
Last change on this file since 1908591 was 5a23277, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Replace the key event in game-gui with distinct key up and key down events

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