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 | string& GameGui_SDL::getError() {
|
---|
14 | GameGui_SDL::s_errorMessage = SDL_GetError();
|
---|
15 |
|
---|
16 | return GameGui_SDL::s_errorMessage;
|
---|
17 | }
|
---|
18 |
|
---|
19 | bool 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 |
|
---|
41 | void GameGui_SDL::shutdown() {
|
---|
42 | SDL_Quit();
|
---|
43 | }
|
---|
44 |
|
---|
45 | void* 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 |
|
---|
74 | void GameGui_SDL::destroyWindow() {
|
---|
75 | // TODO: This function can throw some errors. They should be handled
|
---|
76 | SDL_DestroyWindow(window);
|
---|
77 | }
|
---|
78 |
|
---|
79 | void GameGui_SDL::processEvents() {
|
---|
80 | }
|
---|
81 |
|
---|
82 | int 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_KEYUP:
|
---|
104 | case SDL_KEYDOWN:
|
---|
105 | event->type = UI_EVENT_KEY;
|
---|
106 | event->key.keycode = e.key.keysym.scancode;
|
---|
107 | break;
|
---|
108 | case SDL_MOUSEBUTTONDOWN:
|
---|
109 | case SDL_FINGERDOWN:
|
---|
110 | event->type = UI_EVENT_MOUSEBUTTONDOWN;
|
---|
111 | break;
|
---|
112 | case SDL_MOUSEBUTTONUP:
|
---|
113 | case SDL_FINGERUP:
|
---|
114 | event->type = UI_EVENT_MOUSEBUTTONUP;
|
---|
115 | break;
|
---|
116 | case SDL_MOUSEMOTION:
|
---|
117 | case SDL_FINGERMOTION:
|
---|
118 | event->type = UI_EVENT_MOUSEMOTION;
|
---|
119 | break;
|
---|
120 | // Ignore the following events
|
---|
121 | case SDL_AUDIODEVICEADDED:
|
---|
122 | case SDL_AUDIODEVICEREMOVED:
|
---|
123 | case SDL_TEXTEDITING: // TODO: Research this one later
|
---|
124 | event->type = UI_EVENT_UNKNOWN;
|
---|
125 | event->unknown.eventType = e.type;
|
---|
126 | break;
|
---|
127 | default:
|
---|
128 | event->type = UI_EVENT_UNKNOWN;
|
---|
129 | event->unknown.eventType = 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | return 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | event = nullptr;
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void GameGui_SDL::refreshWindowSize() {
|
---|
140 | SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
---|
141 | }
|
---|
142 |
|
---|
143 | int GameGui_SDL::getWindowWidth() {
|
---|
144 | return windowWidth;
|
---|
145 | }
|
---|
146 |
|
---|
147 | int GameGui_SDL::getWindowHeight() {
|
---|
148 | return windowHeight;
|
---|
149 | }
|
---|
150 |
|
---|
151 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
152 |
|
---|
153 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
154 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
155 | }
|
---|
156 |
|
---|
157 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
158 | uint32_t extensionCount = 0;
|
---|
159 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
160 |
|
---|
161 | vector<const char*> extensions(extensionCount);
|
---|
162 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
163 |
|
---|
164 | return extensions;
|
---|
165 | }
|
---|
166 |
|
---|
167 | #endif
|
---|