[f898c5f] | 1 | #include "game-gui-sdl.hpp"
|
---|
| 2 |
|
---|
[27c40ce] | 3 | #include <map>
|
---|
| 4 | #include <queue>
|
---|
| 5 |
|
---|
[c324d6a] | 6 | #include <SDL2/SDL_ttf.h>
|
---|
| 7 |
|
---|
[054d9ed] | 8 | #include "compiler.hpp"
|
---|
[9546928] | 9 | #include "consts.hpp"
|
---|
| 10 |
|
---|
[27c40ce] | 11 | using namespace std;
|
---|
| 12 |
|
---|
[cd1cb0f] | 13 | GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) {
|
---|
[c324d6a] | 14 | window = nullptr;
|
---|
[cd1cb0f] | 15 | }
|
---|
| 16 |
|
---|
[7fc5e27] | 17 | string& GameGui_SDL::getError() {
|
---|
[c324d6a] | 18 | s_errorMessage = SDL_GetError();
|
---|
[d5f2b42] | 19 |
|
---|
[c324d6a] | 20 | return s_errorMessage;
|
---|
[d5f2b42] | 21 | }
|
---|
| 22 |
|
---|
[7fc5e27] | 23 | bool GameGui_SDL::init() {
|
---|
[f898c5f] | 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 |
|
---|
[c324d6a] | 27 | s_errorMessage = "No error";
|
---|
[f898c5f] | 28 |
|
---|
[5f3dba8] | 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;
|
---|
[f898c5f] | 38 | }
|
---|
| 39 |
|
---|
[7fc5e27] | 40 | void GameGui_SDL::shutdown() {
|
---|
[f898c5f] | 41 | SDL_Quit();
|
---|
[826df16] | 42 | }
|
---|
[0e6ecf3] | 43 |
|
---|
[7fc5e27] | 44 | void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
[1ce9afe] | 45 | // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
|
---|
| 46 |
|
---|
[0e6ecf3] | 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.
|
---|
[7fc5e27] | 49 |
|
---|
[d2f607c] | 50 | SDL_DisplayMode dm;
|
---|
| 51 | SDL_GetCurrentDisplayMode(0, &dm);
|
---|
[7fc5e27] | 52 |
|
---|
[d2f607c] | 53 | if (fullscreen) {
|
---|
| 54 | width = dm.w;
|
---|
| 55 | height = dm.h;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[054d9ed] | 58 | #ifdef WINDOWS
|
---|
[c324d6a] | 59 | uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI |
|
---|
| 60 | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
|
---|
[054d9ed] | 61 | #else
|
---|
[c324d6a] | 62 | uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI |
|
---|
| 63 | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
|
---|
[054d9ed] | 64 | #endif
|
---|
[7fc5e27] | 65 |
|
---|
[c324d6a] | 66 | window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
|
---|
[0e6ecf3] | 67 |
|
---|
[a6f6833] | 68 | refreshWindowSize();
|
---|
| 69 |
|
---|
[0e6ecf3] | 70 | return window;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[7fc5e27] | 73 | void GameGui_SDL::destroyWindow() {
|
---|
[0e6ecf3] | 74 | // TODO: This function can throw some errors. They should be handled
|
---|
| 75 | SDL_DestroyWindow(window);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[27c40ce] | 78 | void GameGui_SDL::processEvents() {
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[d8cf709] | 81 | int GameGui_SDL::pollEvent(UIEvent* uiEvent) {
|
---|
[f6521fb] | 82 | SDL_Event e;
|
---|
[27c40ce] | 83 |
|
---|
[f6521fb] | 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)) {
|
---|
[d8cf709] | 89 | uiEvent->rawEvent.sdl = e;
|
---|
| 90 |
|
---|
| 91 | GameEvent* event = &uiEvent->event;
|
---|
| 92 |
|
---|
[f6521fb] | 93 | switch(e.type) {
|
---|
| 94 | case SDL_QUIT:
|
---|
| 95 | event->type = UI_EVENT_QUIT;
|
---|
| 96 | break;
|
---|
| 97 | case SDL_WINDOWEVENT:
|
---|
[0e09340] | 98 | if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
|
---|
[d8cf709] | 99 | e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
|
---|
| 100 | e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
|
---|
[0e09340] | 101 | event->type = UI_EVENT_WINDOWRESIZE;
|
---|
[d8cf709] | 102 | } else if (e.window.event == SDL_WINDOWEVENT_CLOSE &&
|
---|
| 103 | e.window.windowID == SDL_GetWindowID(window)) {
|
---|
| 104 | event->type = UI_EVENT_QUIT;
|
---|
[0e09340] | 105 | } else {
|
---|
| 106 | event->type = UI_EVENT_WINDOW;
|
---|
| 107 | }
|
---|
[f6521fb] | 108 | break;
|
---|
| 109 | case SDL_KEYDOWN:
|
---|
[5a23277] | 110 | event->type = UI_EVENT_KEYDOWN;
|
---|
| 111 | event->key.keycode = e.key.keysym.scancode;
|
---|
[0ecab17] | 112 | event->key.repeat = e.key.repeat != 0;
|
---|
[5a23277] | 113 | break;
|
---|
| 114 | case SDL_KEYUP:
|
---|
| 115 | event->type = UI_EVENT_KEYUP;
|
---|
[f6521fb] | 116 | event->key.keycode = e.key.keysym.scancode;
|
---|
[0ecab17] | 117 | event->key.repeat = e.key.repeat != 0;
|
---|
[f6521fb] | 118 | break;
|
---|
| 119 | case SDL_MOUSEBUTTONDOWN:
|
---|
| 120 | case SDL_FINGERDOWN:
|
---|
| 121 | event->type = UI_EVENT_MOUSEBUTTONDOWN;
|
---|
| 122 | break;
|
---|
| 123 | case SDL_MOUSEBUTTONUP:
|
---|
| 124 | case SDL_FINGERUP:
|
---|
| 125 | event->type = UI_EVENT_MOUSEBUTTONUP;
|
---|
| 126 | break;
|
---|
| 127 | case SDL_MOUSEMOTION:
|
---|
[b8d4456] | 128 | event->mouse.x = e.motion.x;
|
---|
| 129 | event->mouse.y = e.motion.y;
|
---|
[f6521fb] | 130 | case SDL_FINGERMOTION:
|
---|
[d8cf709] | 131 | // TODO: Get coordinates for finger events
|
---|
[f6521fb] | 132 | event->type = UI_EVENT_MOUSEMOTION;
|
---|
| 133 | break;
|
---|
[d8cf709] | 134 | // The following events are not currently supported
|
---|
[f6521fb] | 135 | case SDL_AUDIODEVICEADDED:
|
---|
| 136 | case SDL_AUDIODEVICEREMOVED:
|
---|
[4ece3bf] | 137 | case SDL_TEXTINPUT:
|
---|
[0ecab17] | 138 | case SDL_TEXTEDITING:
|
---|
[d8cf709] | 139 | case SDL_MOUSEWHEEL:
|
---|
[429ac01] | 140 | event->type = UI_EVENT_UNHANDLED;
|
---|
[f6521fb] | 141 | break;
|
---|
| 142 | default:
|
---|
[a0da009] | 143 | event->type = UI_EVENT_UNKNOWN;
|
---|
[f6521fb] | 144 | }
|
---|
[27c40ce] | 145 |
|
---|
[f6521fb] | 146 | return 1;
|
---|
[d8cf709] | 147 | } else {
|
---|
| 148 | return 0;
|
---|
[27c40ce] | 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[cd1cb0f] | 152 | bool GameGui_SDL::keyPressed(unsigned int key) {
|
---|
| 153 | return keyState[key];
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[301c90a] | 156 | // TODO: Check that this is only used where the drawable size is needed
|
---|
| 157 | // Maybe create a separate refreshWindowSize funcion as well.
|
---|
[a6f6833] | 158 | void GameGui_SDL::refreshWindowSize() {
|
---|
[301c90a] | 159 | // TODO: Make sure this works on a Mac (the analogous glfw function had issues on Mac retina displays)
|
---|
| 160 |
|
---|
| 161 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 162 | SDL_Vulkan_GetDrawableSize(window, &windowWidth, &windowHeight);
|
---|
| 163 | #else
|
---|
| 164 | SDL_GL_GetDrawableSize(window, &windowWidth, &windowHeight);
|
---|
| 165 | #endif
|
---|
[8d96e95] | 166 |
|
---|
| 167 | if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) {
|
---|
| 168 | windowWidth = 0;
|
---|
| 169 | windowHeight = 0;
|
---|
| 170 | }
|
---|
[a6f6833] | 171 | }
|
---|
| 172 |
|
---|
| 173 | int GameGui_SDL::getWindowWidth() {
|
---|
| 174 | return windowWidth;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | int GameGui_SDL::getWindowHeight() {
|
---|
| 178 | return windowHeight;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[4eb4d0a] | 181 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 182 |
|
---|
[7fc5e27] | 183 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[c6fec84] | 184 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 185 | }
|
---|
[8667f76] | 186 |
|
---|
[7fc5e27] | 187 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 188 | uint32_t extensionCount = 0;
|
---|
| 189 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
| 190 |
|
---|
| 191 | vector<const char*> extensions(extensionCount);
|
---|
| 192 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
| 193 |
|
---|
| 194 | return extensions;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[a6f6833] | 197 | #endif
|
---|