[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 |
|
---|
[7fc5e27] | 13 | string& GameGui_SDL::getError() {
|
---|
[d5f2b42] | 14 | GameGui_SDL::s_errorMessage = SDL_GetError();
|
---|
| 15 |
|
---|
| 16 | return GameGui_SDL::s_errorMessage;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
[7fc5e27] | 19 | bool GameGui_SDL::init() {
|
---|
[f898c5f] | 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 |
|
---|
[d5f2b42] | 23 | GameGui_SDL::s_errorMessage = "No error";
|
---|
[f898c5f] | 24 |
|
---|
[5f3dba8] | 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;
|
---|
[f898c5f] | 39 | }
|
---|
| 40 |
|
---|
[7fc5e27] | 41 | void GameGui_SDL::shutdown() {
|
---|
[f898c5f] | 42 | SDL_Quit();
|
---|
[826df16] | 43 | }
|
---|
[0e6ecf3] | 44 |
|
---|
[7fc5e27] | 45 | void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
|
---|
[1ce9afe] | 46 | // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
|
---|
| 47 |
|
---|
[0e6ecf3] | 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.
|
---|
[7fc5e27] | 50 |
|
---|
[d2f607c] | 51 | SDL_DisplayMode dm;
|
---|
| 52 | SDL_GetCurrentDisplayMode(0, &dm);
|
---|
[7fc5e27] | 53 |
|
---|
[d2f607c] | 54 | if (fullscreen) {
|
---|
| 55 | width = dm.w;
|
---|
| 56 | height = dm.h;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[054d9ed] | 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
|
---|
[7fc5e27] | 64 |
|
---|
[0e6ecf3] | 65 | window = SDL_CreateWindow(title.c_str(),
|
---|
| 66 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
[7fc5e27] | 67 | width, height, flags);
|
---|
[0e6ecf3] | 68 |
|
---|
[a6f6833] | 69 | refreshWindowSize();
|
---|
| 70 |
|
---|
[0e6ecf3] | 71 | return window;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[7fc5e27] | 74 | void GameGui_SDL::destroyWindow() {
|
---|
[0e6ecf3] | 75 | // TODO: This function can throw some errors. They should be handled
|
---|
| 76 | SDL_DestroyWindow(window);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[27c40ce] | 79 | void GameGui_SDL::processEvents() {
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[f6521fb] | 82 | int GameGui_SDL::pollEvent(UIEvent* event) {
|
---|
| 83 | SDL_Event e;
|
---|
[27c40ce] | 84 |
|
---|
[f6521fb] | 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:
|
---|
[0e09340] | 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 | }
|
---|
[f6521fb] | 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:
|
---|
[c61323a] | 123 | case SDL_TEXTEDITING: // TODO: Research this one later
|
---|
[a0da009] | 124 | event->type = UI_EVENT_UNKNOWN;
|
---|
| 125 | event->unknown.eventType = e.type;
|
---|
[f6521fb] | 126 | break;
|
---|
| 127 | default:
|
---|
[a0da009] | 128 | event->type = UI_EVENT_UNKNOWN;
|
---|
| 129 | event->unknown.eventType = 0;
|
---|
[f6521fb] | 130 | }
|
---|
[27c40ce] | 131 |
|
---|
[f6521fb] | 132 | return 1;
|
---|
[27c40ce] | 133 | }
|
---|
| 134 |
|
---|
[f6521fb] | 135 | event = nullptr;
|
---|
| 136 | return 0;
|
---|
[27c40ce] | 137 | }
|
---|
| 138 |
|
---|
[a6f6833] | 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 |
|
---|
[4eb4d0a] | 151 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 152 |
|
---|
[7fc5e27] | 153 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[c6fec84] | 154 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 155 | }
|
---|
[8667f76] | 156 |
|
---|
[7fc5e27] | 157 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 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 |
|
---|
[a6f6833] | 167 | #endif
|
---|