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

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

Implement processEvents() and pollEvent() for GameGui_GLFW and re-implement the main loop in opengl-game using those functions

  • Property mode set to 100644
File size: 3.8 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 return window;
65}
66
67void GameGui_SDL::destroyWindow() {
68 // TODO: This function can throw some errors. They should be handled
69 SDL_DestroyWindow(window);
70}
71
72void GameGui_SDL::processEvents() {
73}
74
75int GameGui_SDL::pollEvent(UIEvent* event) {
76 SDL_Event e;
77
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:
110 case SDL_TEXTEDITING: // TODO: Research this one later
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 }
119
120 return 1;
121 }
122
123 event = nullptr;
124 return 0;
125}
126
127#ifdef GAMEGUI_INCLUDE_VULKAN
128
129bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
130 return SDL_Vulkan_CreateSurface(window, instance, surface) ?
131 RTWO_SUCCESS : RTWO_ERROR;
132}
133
134#endif
135
136vector<const char*> GameGui_SDL::getRequiredExtensions() {
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
146void GameGui_SDL::getWindowSize(int* width, int* height) {
147 SDL_GetWindowSize(window, width, height);
148}
Note: See TracBrowser for help on using the repository browser.