source: opengl-game/game-gui-sdl.cpp@ a6f6833

feature/imgui-sdl points-test
Last change on this file since a6f6833 was a6f6833, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Remove getWindowSize() from game-gui and instead add getWindowWidth(), getWindowHeight(), and refreshWindowSize() (which updates the internal variables returned by the first two functions)

  • Property mode set to 100644
File size: 3.9 KB
Line 
1#include "game-gui-sdl.hpp"
2
3#include <map>
4#include <queue>
5
6#include "consts.hpp"
7
8using namespace std;
9
10string GameGui_SDL::s_errorMessage;
11
12string& GameGui_SDL::getError() {
13 GameGui_SDL::s_errorMessage = SDL_GetError();
14
15 return GameGui_SDL::s_errorMessage;
16}
17
18bool GameGui_SDL::init() {
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
22 GameGui_SDL::s_errorMessage = "No error";
23
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;
38}
39
40void GameGui_SDL::shutdown() {
41 SDL_Quit();
42}
43
44void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
45 // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
46
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.
49
50 SDL_DisplayMode dm;
51 SDL_GetCurrentDisplayMode(0, &dm);
52
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);
59
60 window = SDL_CreateWindow(title.c_str(),
61 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
62 width, height, flags);
63
64 refreshWindowSize();
65
66 return window;
67}
68
69void GameGui_SDL::destroyWindow() {
70 // TODO: This function can throw some errors. They should be handled
71 SDL_DestroyWindow(window);
72}
73
74void GameGui_SDL::processEvents() {
75}
76
77int GameGui_SDL::pollEvent(UIEvent* event) {
78 SDL_Event e;
79
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:
112 case SDL_TEXTEDITING: // TODO: Research this one later
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 }
121
122 return 1;
123 }
124
125 event = nullptr;
126 return 0;
127}
128
129void GameGui_SDL::refreshWindowSize() {
130 SDL_GetWindowSize(window, &windowWidth, &windowHeight);
131}
132
133int GameGui_SDL::getWindowWidth() {
134 return windowWidth;
135}
136
137int GameGui_SDL::getWindowHeight() {
138 return windowHeight;
139}
140
141#ifdef GAMEGUI_INCLUDE_VULKAN
142
143bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
144 return SDL_Vulkan_CreateSurface(window, instance, surface) ?
145 RTWO_SUCCESS : RTWO_ERROR;
146}
147
148vector<const char*> GameGui_SDL::getRequiredExtensions() {
149 uint32_t extensionCount = 0;
150 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
151
152 vector<const char*> extensions(extensionCount);
153 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
154
155 return extensions;
156}
157
158#endif
Note: See TracBrowser for help on using the repository browser.