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

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

Add processEvents() and pollEvent() to GameGui, implement them for GameGui_SDL, and add a union for different event types, similar to the SDL_Event union

  • 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
10/*
11// Temporary to allow the program using this class to receive events other than keyboard events
12// Remove once I add a better game-gui wrapper for doing that
13queue<SDL_Event> events;
14
15queue<MouseEvent> mouseEvents;
16*/
17
18string GameGui_SDL::s_errorMessage;
19
20string& GameGui_SDL::getError() {
21 GameGui_SDL::s_errorMessage = SDL_GetError();
22
23 return GameGui_SDL::s_errorMessage;
24}
25
26bool GameGui_SDL::init() {
27 // may want to define SDL_INIT_NOPARACHUTE when I start handling crashes since it
28 // prevents SDL from setting up its own handlers for SIG_SEGV and stuff like that
29
30 GameGui_SDL::s_errorMessage = "No error";
31
32 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
33 return RTWO_ERROR;
34 }
35
36 int imgFlags = IMG_INIT_PNG;
37 if (!(IMG_Init(imgFlags) & imgFlags)) {
38 return RTWO_ERROR;
39 }
40
41 if (TTF_Init() == -1) {
42 return RTWO_ERROR;
43 }
44
45 return RTWO_SUCCESS;
46}
47
48void GameGui_SDL::shutdown() {
49 SDL_Quit();
50}
51
52void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
53 // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
54
55 // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
56 // otherwise you will not receive a High DPI OpenGL canvas.
57
58 SDL_DisplayMode dm;
59 SDL_GetCurrentDisplayMode(0, &dm);
60
61 if (fullscreen) {
62 width = dm.w;
63 height = dm.h;
64 }
65
66 uint32_t flags = SDL_WINDOW_VULKAN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
67
68 window = SDL_CreateWindow(title.c_str(),
69 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
70 width, height, flags);
71
72 return window;
73}
74
75void GameGui_SDL::destroyWindow() {
76 // TODO: This function can throw some errors. They should be handled
77 SDL_DestroyWindow(window);
78}
79
80void GameGui_SDL::processEvents() {
81}
82
83int GameGui_SDL::pollEvent(UIEvent* event) {
84 SDL_Event e;
85
86 /* The trackpad on OSX triggers both SDL_MOUSE and SDL_FINGER events, so just treat them both
87 * as mouse events since this game isn't targeting mobile devices
88 */
89
90 if (SDL_PollEvent(&e)) {
91 switch(e.type) {
92 case SDL_QUIT:
93 event->type = UI_EVENT_QUIT;
94 break;
95 case SDL_WINDOWEVENT:
96 event->type = UI_EVENT_WINDOW;
97 break;
98 case SDL_KEYUP:
99 case SDL_KEYDOWN:
100 event->type = UI_EVENT_KEY;
101 event->key.keycode = e.key.keysym.scancode;
102 break;
103 case SDL_MOUSEBUTTONDOWN:
104 case SDL_FINGERDOWN:
105 event->type = UI_EVENT_MOUSEBUTTONDOWN;
106 break;
107 case SDL_MOUSEBUTTONUP:
108 case SDL_FINGERUP:
109 event->type = UI_EVENT_MOUSEBUTTONUP;
110 break;
111 case SDL_MOUSEMOTION:
112 case SDL_FINGERMOTION:
113 event->type = UI_EVENT_MOUSEMOTION;
114 break;
115 // Ignore the following events
116 case SDL_AUDIODEVICEADDED:
117 case SDL_AUDIODEVICEREMOVED:
118 event = nullptr;
119 return 0;
120 break;
121 default:
122 cout << "Unknown event type: 0x" << hex << e.type << dec << endl;
123 event = nullptr;
124 return 0;
125 }
126
127 return 1;
128 }
129
130 event = nullptr;
131 return 0;
132}
133
134#ifdef GAMEGUI_INCLUDE_VULKAN
135
136bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
137 return SDL_Vulkan_CreateSurface(window, instance, surface) ?
138 RTWO_SUCCESS : RTWO_ERROR;
139}
140
141#endif
142
143vector<const char*> GameGui_SDL::getRequiredExtensions() {
144 uint32_t extensionCount = 0;
145 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
146
147 vector<const char*> extensions(extensionCount);
148 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
149
150 return extensions;
151}
152
153void GameGui_SDL::getWindowSize(int* width, int* height) {
154 SDL_GetWindowSize(window, width, height);
155}
Note: See TracBrowser for help on using the repository browser.