[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 |
|
---|
| 64 | return window;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[7fc5e27] | 67 | void GameGui_SDL::destroyWindow() {
|
---|
[0e6ecf3] | 68 | // TODO: This function can throw some errors. They should be handled
|
---|
| 69 | SDL_DestroyWindow(window);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[27c40ce] | 72 | void GameGui_SDL::processEvents() {
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[f6521fb] | 75 | int GameGui_SDL::pollEvent(UIEvent* event) {
|
---|
| 76 | SDL_Event e;
|
---|
[27c40ce] | 77 |
|
---|
[f6521fb] | 78 | /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
|
---|
| 79 | * as mouse events since this game isn't targeting mobile devices
|
---|
| 80 | */
|
---|
| 81 |
|
---|
| 82 | if (SDL_PollEvent(&e)) {
|
---|
| 83 | switch(e.type) {
|
---|
| 84 | case SDL_QUIT:
|
---|
| 85 | event->type = UI_EVENT_QUIT;
|
---|
| 86 | break;
|
---|
| 87 | case SDL_WINDOWEVENT:
|
---|
| 88 | event->type = UI_EVENT_WINDOW;
|
---|
| 89 | break;
|
---|
| 90 | case SDL_KEYUP:
|
---|
| 91 | case SDL_KEYDOWN:
|
---|
| 92 | event->type = UI_EVENT_KEY;
|
---|
| 93 | event->key.keycode = e.key.keysym.scancode;
|
---|
| 94 | break;
|
---|
| 95 | case SDL_MOUSEBUTTONDOWN:
|
---|
| 96 | case SDL_FINGERDOWN:
|
---|
| 97 | event->type = UI_EVENT_MOUSEBUTTONDOWN;
|
---|
| 98 | break;
|
---|
| 99 | case SDL_MOUSEBUTTONUP:
|
---|
| 100 | case SDL_FINGERUP:
|
---|
| 101 | event->type = UI_EVENT_MOUSEBUTTONUP;
|
---|
| 102 | break;
|
---|
| 103 | case SDL_MOUSEMOTION:
|
---|
| 104 | case SDL_FINGERMOTION:
|
---|
| 105 | event->type = UI_EVENT_MOUSEMOTION;
|
---|
| 106 | break;
|
---|
| 107 | // Ignore the following events
|
---|
| 108 | case SDL_AUDIODEVICEADDED:
|
---|
| 109 | case SDL_AUDIODEVICEREMOVED:
|
---|
[c61323a] | 110 | case SDL_TEXTEDITING: // TODO: Research this one later
|
---|
[f6521fb] | 111 | event = nullptr;
|
---|
| 112 | return 0;
|
---|
| 113 | break;
|
---|
| 114 | default:
|
---|
| 115 | cout << "Unknown event type: 0x" << hex << e.type << dec << endl;
|
---|
| 116 | event = nullptr;
|
---|
| 117 | return 0;
|
---|
| 118 | }
|
---|
[27c40ce] | 119 |
|
---|
[f6521fb] | 120 | return 1;
|
---|
[27c40ce] | 121 | }
|
---|
| 122 |
|
---|
[f6521fb] | 123 | event = nullptr;
|
---|
| 124 | return 0;
|
---|
[27c40ce] | 125 | }
|
---|
| 126 |
|
---|
[4eb4d0a] | 127 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
| 128 |
|
---|
[7fc5e27] | 129 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
[0e6ecf3] | 130 | return SDL_Vulkan_CreateSurface(window, instance, surface) ?
|
---|
| 131 | RTWO_SUCCESS : RTWO_ERROR;
|
---|
| 132 | }
|
---|
[8667f76] | 133 |
|
---|
[4eb4d0a] | 134 | #endif
|
---|
| 135 |
|
---|
[7fc5e27] | 136 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
[8667f76] | 137 | uint32_t extensionCount = 0;
|
---|
| 138 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
| 139 |
|
---|
| 140 | vector<const char*> extensions(extensionCount);
|
---|
| 141 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
| 142 |
|
---|
| 143 | return extensions;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[7fc5e27] | 146 | void GameGui_SDL::getWindowSize(int* width, int* height) {
|
---|
[8667f76] | 147 | SDL_GetWindowSize(window, width, height);
|
---|
| 148 | }
|
---|