[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 | int imgFlags = IMG_INIT_PNG;
|
---|
| 33 | if (!(IMG_Init(imgFlags) & imgFlags)) {
|
---|
| 34 | return RTWO_ERROR;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | if (TTF_Init() == -1) {
|
---|
| 38 | return RTWO_ERROR;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return RTWO_SUCCESS;
|
---|
[f898c5f] | 42 | }
|
---|
| 43 |
|
---|
[7fc5e27] | 44 | void GameGui_SDL::shutdown() {
|
---|
[f898c5f] | 45 | SDL_Quit();
|
---|
[826df16] | 46 | }
|
---|
[0e6ecf3] | 47 |
|
---|
[7fc5e27] | 48 | void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
[1ce9afe] | 49 | // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
|
---|
| 50 |
|
---|
[0e6ecf3] | 51 | // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
|
---|
| 52 | // otherwise you will not receive a High DPI OpenGL canvas.
|
---|
[7fc5e27] | 53 |
|
---|
[d2f607c] | 54 | SDL_DisplayMode dm;
|
---|
| 55 | SDL_GetCurrentDisplayMode(0, &dm);
|
---|
[7fc5e27] | 56 |
|
---|
[d2f607c] | 57 | if (fullscreen) {
|
---|
| 58 | width = dm.w;
|
---|
| 59 | height = dm.h;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[054d9ed] | 62 | #ifdef WINDOWS
|
---|
| 63 | uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE);
|
---|
| 64 | #else
|
---|
| 65 | uint32_t flags = SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
|
---|
| 66 | #endif
|
---|
[7fc5e27] | 67 |
|
---|
[0e6ecf3] | 68 | window = SDL_CreateWindow(title.c_str(),
|
---|
| 69 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
[7fc5e27] | 70 | width, height, flags);
|
---|
[0e6ecf3] | 71 |
|
---|
[a6f6833] | 72 | refreshWindowSize();
|
---|
| 73 |
|
---|
[0e6ecf3] | 74 | return window;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[7fc5e27] | 77 | void GameGui_SDL::destroyWindow() {
|
---|
[0e6ecf3] | 78 | // TODO: This function can throw some errors. They should be handled
|
---|
| 79 | SDL_DestroyWindow(window);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[27c40ce] | 82 | void GameGui_SDL::processEvents() {
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[f6521fb] | 85 | int GameGui_SDL::pollEvent(UIEvent* event) {
|
---|
| 86 | SDL_Event e;
|
---|
[27c40ce] | 87 |
|
---|
[f6521fb] | 88 | /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
|
---|
| 89 | * as mouse events since this game isn't targeting mobile devices
|
---|
| 90 | */
|
---|
| 91 |
|
---|
| 92 | if (SDL_PollEvent(&e)) {
|
---|
| 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 ||
|
---|
| 99 | e.window.event == SDL_WINDOWEVENT_MINIMIZED ||
|
---|
| 100 | e.window.event == SDL_WINDOWEVENT_MAXIMIZED) {
|
---|
| 101 | event->type = UI_EVENT_WINDOWRESIZE;
|
---|
| 102 | } else {
|
---|
| 103 | event->type = UI_EVENT_WINDOW;
|
---|
| 104 | }
|
---|
[f6521fb] | 105 | break;
|
---|
| 106 | case SDL_KEYDOWN:
|
---|
[5a23277] | 107 | event->type = UI_EVENT_KEYDOWN;
|
---|
| 108 | event->key.keycode = e.key.keysym.scancode;
|
---|
| 109 | break;
|
---|
| 110 | case SDL_KEYUP:
|
---|
| 111 | event->type = UI_EVENT_KEYUP;
|
---|
[f6521fb] | 112 | event->key.keycode = e.key.keysym.scancode;
|
---|
| 113 | break;
|
---|
| 114 | case SDL_MOUSEBUTTONDOWN:
|
---|
| 115 | case SDL_FINGERDOWN:
|
---|
| 116 | event->type = UI_EVENT_MOUSEBUTTONDOWN;
|
---|
| 117 | break;
|
---|
| 118 | case SDL_MOUSEBUTTONUP:
|
---|
| 119 | case SDL_FINGERUP:
|
---|
| 120 | event->type = UI_EVENT_MOUSEBUTTONUP;
|
---|
| 121 | break;
|
---|
| 122 | case SDL_MOUSEMOTION:
|
---|
| 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:
|
---|
[c61323a] | 130 | case SDL_TEXTEDITING: // TODO: Research this one later
|
---|
[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() {
|
---|
| 151 | SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | int GameGui_SDL::getWindowWidth() {
|
---|
| 155 | return windowWidth;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | int GameGui_SDL::getWindowHeight() {
|
---|
| 159 | return windowHeight;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[4eb4d0a] | 162 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 163 |
|
---|
[7fc5e27] | 164 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[c6fec84] | 165 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 166 | }
|
---|
[8667f76] | 167 |
|
---|
[7fc5e27] | 168 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 169 | uint32_t extensionCount = 0;
|
---|
| 170 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
| 171 |
|
---|
| 172 | vector<const char*> extensions(extensionCount);
|
---|
| 173 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
| 174 |
|
---|
| 175 | return extensions;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[a6f6833] | 178 | #endif
|
---|