[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 |
|
---|
[f6521fb] | 81 | int GameGui_SDL::pollEvent(UIEvent* event) {
|
---|
| 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)) {
|
---|
| 89 | switch(e.type) {
|
---|
| 90 | case SDL_QUIT:
|
---|
| 91 | event->type = UI_EVENT_QUIT;
|
---|
| 92 | break;
|
---|
| 93 | case SDL_WINDOWEVENT:
|
---|
[0e09340] | 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 | }
|
---|
[f6521fb] | 101 | break;
|
---|
| 102 | case SDL_KEYDOWN:
|
---|
[5a23277] | 103 | event->type = UI_EVENT_KEYDOWN;
|
---|
| 104 | event->key.keycode = e.key.keysym.scancode;
|
---|
[0ecab17] | 105 | event->key.repeat = e.key.repeat != 0;
|
---|
[5a23277] | 106 | break;
|
---|
| 107 | case SDL_KEYUP:
|
---|
| 108 | event->type = UI_EVENT_KEYUP;
|
---|
[f6521fb] | 109 | event->key.keycode = e.key.keysym.scancode;
|
---|
[0ecab17] | 110 | event->key.repeat = e.key.repeat != 0;
|
---|
[f6521fb] | 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:
|
---|
[b8d4456] | 121 | event->mouse.x = e.motion.x;
|
---|
| 122 | event->mouse.y = e.motion.y;
|
---|
[f6521fb] | 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:
|
---|
[4ece3bf] | 129 | case SDL_TEXTINPUT:
|
---|
[0ecab17] | 130 | case SDL_TEXTEDITING:
|
---|
[a0da009] | 131 | event->type = UI_EVENT_UNKNOWN;
|
---|
| 132 | event->unknown.eventType = e.type;
|
---|
[f6521fb] | 133 | break;
|
---|
| 134 | default:
|
---|
[a0da009] | 135 | event->type = UI_EVENT_UNKNOWN;
|
---|
| 136 | event->unknown.eventType = 0;
|
---|
[f6521fb] | 137 | }
|
---|
[27c40ce] | 138 |
|
---|
[f6521fb] | 139 | return 1;
|
---|
[27c40ce] | 140 | }
|
---|
| 141 |
|
---|
[f6521fb] | 142 | event = nullptr;
|
---|
| 143 | return 0;
|
---|
[27c40ce] | 144 | }
|
---|
| 145 |
|
---|
[cd1cb0f] | 146 | bool GameGui_SDL::keyPressed(unsigned int key) {
|
---|
| 147 | return keyState[key];
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[a6f6833] | 150 | void GameGui_SDL::refreshWindowSize() {
|
---|
[c324d6a] | 151 | // TODO: Make sure this works on a mac (the analogous glfw function had issues on Mac retina displays)
|
---|
[a6f6833] | 152 | SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | int GameGui_SDL::getWindowWidth() {
|
---|
| 156 | return windowWidth;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | int GameGui_SDL::getWindowHeight() {
|
---|
| 160 | return windowHeight;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[4eb4d0a] | 163 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 164 |
|
---|
[7fc5e27] | 165 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[c6fec84] | 166 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 167 | }
|
---|
[8667f76] | 168 |
|
---|
[7fc5e27] | 169 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 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 |
|
---|
[a6f6833] | 179 | #endif
|
---|