[f898c5f] | 1 | #include "game-gui-sdl.hpp"
|
---|
| 2 |
|
---|
[27c40ce] | 3 | #include <map>
|
---|
| 4 | #include <queue>
|
---|
| 5 |
|
---|
[9546928] | 6 | #include "consts.hpp"
|
---|
| 7 |
|
---|
[27c40ce] | 8 | using namespace std;
|
---|
| 9 |
|
---|
[d5f2b42] | 10 | string GameGui_SDL::s_errorMessage;
|
---|
| 11 |
|
---|
[7fc5e27] | 12 | string& GameGui_SDL::getError() {
|
---|
[d5f2b42] | 13 | GameGui_SDL::s_errorMessage = SDL_GetError();
|
---|
| 14 |
|
---|
| 15 | return GameGui_SDL::s_errorMessage;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[7fc5e27] | 18 | bool GameGui_SDL::init() {
|
---|
[f898c5f] | 19 | // may want to define SDL_INIT_NOPARACHUTE when I start handling crashes since it
|
---|
| 20 | // prevents SDL from setting up its own handlers for SIG_SEGV and stuff like that
|
---|
| 21 |
|
---|
[d5f2b42] | 22 | GameGui_SDL::s_errorMessage = "No error";
|
---|
[f898c5f] | 23 |
|
---|
[5f3dba8] | 24 | if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
---|
| 25 | return RTWO_ERROR;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | int imgFlags = IMG_INIT_PNG;
|
---|
| 29 | if (!(IMG_Init(imgFlags) & imgFlags)) {
|
---|
| 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 |
|
---|
| 58 | uint32_t flags = SDL_WINDOW_VULKAN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
|
---|
[7fc5e27] | 59 |
|
---|
[0e6ecf3] | 60 | window = SDL_CreateWindow(title.c_str(),
|
---|
| 61 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
[7fc5e27] | 62 | width, height, flags);
|
---|
[0e6ecf3] | 63 |
|
---|
[a6f6833] | 64 | refreshWindowSize();
|
---|
| 65 |
|
---|
[0e6ecf3] | 66 | return window;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[7fc5e27] | 69 | void GameGui_SDL::destroyWindow() {
|
---|
[0e6ecf3] | 70 | // TODO: This function can throw some errors. They should be handled
|
---|
| 71 | SDL_DestroyWindow(window);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[27c40ce] | 74 | void GameGui_SDL::processEvents() {
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[f6521fb] | 77 | int GameGui_SDL::pollEvent(UIEvent* event) {
|
---|
| 78 | SDL_Event e;
|
---|
[27c40ce] | 79 |
|
---|
[f6521fb] | 80 | /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
|
---|
| 81 | * as mouse events since this game isn't targeting mobile devices
|
---|
| 82 | */
|
---|
| 83 |
|
---|
| 84 | if (SDL_PollEvent(&e)) {
|
---|
| 85 | switch(e.type) {
|
---|
| 86 | case SDL_QUIT:
|
---|
| 87 | event->type = UI_EVENT_QUIT;
|
---|
| 88 | break;
|
---|
| 89 | case SDL_WINDOWEVENT:
|
---|
| 90 | event->type = UI_EVENT_WINDOW;
|
---|
| 91 | break;
|
---|
| 92 | case SDL_KEYUP:
|
---|
| 93 | case SDL_KEYDOWN:
|
---|
| 94 | event->type = UI_EVENT_KEY;
|
---|
| 95 | event->key.keycode = e.key.keysym.scancode;
|
---|
| 96 | break;
|
---|
| 97 | case SDL_MOUSEBUTTONDOWN:
|
---|
| 98 | case SDL_FINGERDOWN:
|
---|
| 99 | event->type = UI_EVENT_MOUSEBUTTONDOWN;
|
---|
| 100 | break;
|
---|
| 101 | case SDL_MOUSEBUTTONUP:
|
---|
| 102 | case SDL_FINGERUP:
|
---|
| 103 | event->type = UI_EVENT_MOUSEBUTTONUP;
|
---|
| 104 | break;
|
---|
| 105 | case SDL_MOUSEMOTION:
|
---|
| 106 | case SDL_FINGERMOTION:
|
---|
| 107 | event->type = UI_EVENT_MOUSEMOTION;
|
---|
| 108 | break;
|
---|
| 109 | // Ignore the following events
|
---|
| 110 | case SDL_AUDIODEVICEADDED:
|
---|
| 111 | case SDL_AUDIODEVICEREMOVED:
|
---|
[c61323a] | 112 | case SDL_TEXTEDITING: // TODO: Research this one later
|
---|
[f6521fb] | 113 | event = nullptr;
|
---|
| 114 | return 0;
|
---|
| 115 | break;
|
---|
| 116 | default:
|
---|
| 117 | cout << "Unknown event type: 0x" << hex << e.type << dec << endl;
|
---|
| 118 | event = nullptr;
|
---|
| 119 | return 0;
|
---|
| 120 | }
|
---|
[27c40ce] | 121 |
|
---|
[f6521fb] | 122 | return 1;
|
---|
[27c40ce] | 123 | }
|
---|
| 124 |
|
---|
[f6521fb] | 125 | event = nullptr;
|
---|
| 126 | return 0;
|
---|
[27c40ce] | 127 | }
|
---|
| 128 |
|
---|
[a6f6833] | 129 | void GameGui_SDL::refreshWindowSize() {
|
---|
| 130 | SDL_GetWindowSize(window, &windowWidth, &windowHeight);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | int GameGui_SDL::getWindowWidth() {
|
---|
| 134 | return windowWidth;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | int GameGui_SDL::getWindowHeight() {
|
---|
| 138 | return windowHeight;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[4eb4d0a] | 141 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 142 |
|
---|
[7fc5e27] | 143 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[c6fec84] | 144 | return SDL_Vulkan_CreateSurface(window, instance, surface) ? RTWO_SUCCESS : RTWO_ERROR;
|
---|
[0e6ecf3] | 145 | }
|
---|
[8667f76] | 146 |
|
---|
[7fc5e27] | 147 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 148 | uint32_t extensionCount = 0;
|
---|
| 149 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
| 150 |
|
---|
| 151 | vector<const char*> extensions(extensionCount);
|
---|
| 152 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
| 153 |
|
---|
| 154 | return extensions;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[a6f6833] | 157 | #endif
|
---|