[f898c5f] | 1 | #include "game-gui-sdl.hpp"
|
---|
| 2 |
|
---|
[27c40ce] | 3 | #include <map>
|
---|
| 4 | #include <queue>
|
---|
| 5 |
|
---|
[054d9ed] | 6 | #include "compiler.hpp"
|
---|
[9546928] | 7 | #include "consts.hpp"
|
---|
| 8 |
|
---|
[27c40ce] | 9 | using namespace std;
|
---|
| 10 |
|
---|
[d5f2b42] | 11 | string GameGui_SDL::s_errorMessage;
|
---|
| 12 |
|
---|
[cd1cb0f] | 13 | GameGui_SDL::GameGui_SDL() : keyState(SDL_GetKeyboardState(NULL)) {
|
---|
| 14 | }
|
---|
| 15 |
|
---|
[7fc5e27] | 16 | string& GameGui_SDL::getError() {
|
---|
[d5f2b42] | 17 | GameGui_SDL::s_errorMessage = SDL_GetError();
|
---|
| 18 |
|
---|
| 19 | return GameGui_SDL::s_errorMessage;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
[7fc5e27] | 22 | bool GameGui_SDL::init() {
|
---|
[f898c5f] | 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 |
|
---|
[d5f2b42] | 26 | GameGui_SDL::s_errorMessage = "No error";
|
---|
[f898c5f] | 27 |
|
---|
[5f3dba8] | 28 | if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
---|
| 29 | return RTWO_ERROR;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | if (TTF_Init() == -1) {
|
---|
| 33 | return RTWO_ERROR;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | return RTWO_SUCCESS;
|
---|
[f898c5f] | 37 | }
|
---|
| 38 |
|
---|
[7fc5e27] | 39 | void GameGui_SDL::shutdown() {
|
---|
[f898c5f] | 40 | SDL_Quit();
|
---|
[826df16] | 41 | }
|
---|
[0e6ecf3] | 42 |
|
---|
[7fc5e27] | 43 | void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
[1ce9afe] | 44 | // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
|
---|
| 45 |
|
---|
[0e6ecf3] | 46 | // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
|
---|
| 47 | // otherwise you will not receive a High DPI OpenGL canvas.
|
---|
[7fc5e27] | 48 |
|
---|
[d2f607c] | 49 | SDL_DisplayMode dm;
|
---|
| 50 | SDL_GetCurrentDisplayMode(0, &dm);
|
---|
[7fc5e27] | 51 |
|
---|
[d2f607c] | 52 | if (fullscreen) {
|
---|
| 53 | width = dm.w;
|
---|
| 54 | height = dm.h;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[054d9ed] | 57 | #ifdef WINDOWS
|
---|
| 58 | uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
|
---|
| 59 | #else
|
---|
| 60 | uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
|
---|
| 61 | #endif
|
---|
[7fc5e27] | 62 |
|
---|
[0e6ecf3] | 63 | window = SDL_CreateWindow(title.c_str(),
|
---|
| 64 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
[7fc5e27] | 65 | width, height, flags);
|
---|
[0e6ecf3] | 66 |
|
---|
[a6f6833] | 67 | refreshWindowSize();
|
---|
| 68 |
|
---|
[0e6ecf3] | 69 | return window;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[7fc5e27] | 72 | void GameGui_SDL::destroyWindow() {
|
---|
[0e6ecf3] | 73 | // TODO: This function can throw some errors. They should be handled
|
---|
| 74 | SDL_DestroyWindow(window);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[27c40ce] | 77 | void GameGui_SDL::processEvents() {
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[f6521fb] | 80 | int GameGui_SDL::pollEvent(UIEvent* event) {
|
---|
| 81 | SDL_Event e;
|
---|
[27c40ce] | 82 |
|
---|
[f6521fb] | 83 | /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
|
---|
| 84 | * as mouse events since this game isn't targeting mobile devices
|
---|
| 85 | */
|
---|
| 86 |
|
---|
| 87 | if (SDL_PollEvent(&e)) {
|
---|
| 88 | switch(e.type) {
|
---|
| 89 | case SDL_QUIT:
|
---|
| 90 | event->type = UI_EVENT_QUIT;
|
---|
| 91 | break;
|
---|
| 92 | case SDL_WINDOWEVENT:
|
---|
[0e09340] | 93 | if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
|
---|
| 94 | e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
|
---|
| 95 | e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
|
---|
| 96 | event->type = UI_EVENT_WINDOWRESIZE;
|
---|
| 97 | } else {
|
---|
| 98 | event->type = UI_EVENT_WINDOW;
|
---|
| 99 | }
|
---|
[f6521fb] | 100 | break;
|
---|
| 101 | case SDL_KEYDOWN:
|
---|
[5a23277] | 102 | event->type = UI_EVENT_KEYDOWN;
|
---|
| 103 | event->key.keycode = e.key.keysym.scancode;
|
---|
[0ecab17] | 104 | event->key.repeat = e.key.repeat != 0;
|
---|
[5a23277] | 105 | break;
|
---|
| 106 | case SDL_KEYUP:
|
---|
| 107 | event->type = UI_EVENT_KEYUP;
|
---|
[f6521fb] | 108 | event->key.keycode = e.key.keysym.scancode;
|
---|
[0ecab17] | 109 | event->key.repeat = e.key.repeat != 0;
|
---|
[f6521fb] | 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:
|
---|
[b8d4456] | 120 | event->mouse.x = e.motion.x;
|
---|
| 121 | event->mouse.y = e.motion.y;
|
---|
[f6521fb] | 122 | case SDL_FINGERMOTION:
|
---|
| 123 | event->type = UI_EVENT_MOUSEMOTION;
|
---|
| 124 | break;
|
---|
| 125 | // Ignore the following events
|
---|
| 126 | case SDL_AUDIODEVICEADDED:
|
---|
| 127 | case SDL_AUDIODEVICEREMOVED:
|
---|
[4ece3bf] | 128 | case SDL_TEXTINPUT:
|
---|
[0ecab17] | 129 | case SDL_TEXTEDITING:
|
---|
[a0da009] | 130 | event->type = UI_EVENT_UNKNOWN;
|
---|
| 131 | event->unknown.eventType = e.type;
|
---|
[f6521fb] | 132 | break;
|
---|
| 133 | default:
|
---|
[a0da009] | 134 | event->type = UI_EVENT_UNKNOWN;
|
---|
| 135 | event->unknown.eventType = 0;
|
---|
[f6521fb] | 136 | }
|
---|
[27c40ce] | 137 |
|
---|
[f6521fb] | 138 | return 1;
|
---|
[27c40ce] | 139 | }
|
---|
| 140 |
|
---|
[f6521fb] | 141 | event = nullptr;
|
---|
| 142 | return 0;
|
---|
[27c40ce] | 143 | }
|
---|
| 144 |
|
---|
[cd1cb0f] | 145 | bool GameGui_SDL::keyPressed(unsigned int key) {
|
---|
| 146 | return keyState[key];
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[a6f6833] | 149 | void GameGui_SDL::refreshWindowSize() {
|
---|
| 150 | SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | int GameGui_SDL::getWindowWidth() {
|
---|
| 154 | return windowWidth;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | int GameGui_SDL::getWindowHeight() {
|
---|
| 158 | return windowHeight;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[4eb4d0a] | 161 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 162 |
|
---|
[7fc5e27] | 163 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[c6fec84] | 164 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 165 | }
|
---|
[8667f76] | 166 |
|
---|
[7fc5e27] | 167 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 168 | uint32_t extensionCount = 0;
|
---|
| 169 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
| 170 |
|
---|
| 171 | vector<const char*> extensions(extensionCount);
|
---|
| 172 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
| 173 |
|
---|
| 174 | return extensions;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[a6f6833] | 177 | #endif
|
---|