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

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

Fix the logic for creating a fullscreen SDL window

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include "game-gui-sdl.hpp"
2
3#include <map>
4#include <queue>
5
6#include "consts.hpp"
7
8map<unsigned int, unsigned char> s_keyState;
9map<unsigned int, bool> s_keyDown;
10
11using namespace std;
12
13/*
14// Temporary to allow the program using this class to receive events other than keyboard events
15// Remove once I add a better game-gui wrapper for doing that
16queue<SDL_Event> events;
17
18queue<MouseEvent> mouseEvents;
19*/
20
21string GameGui_SDL::s_errorMessage;
22
23string& GameGui_SDL::getError() {
24 GameGui_SDL::s_errorMessage = SDL_GetError();
25
26 return GameGui_SDL::s_errorMessage;
27}
28
29bool GameGui_SDL::init() {
30 // may want to define SDL_INIT_NOPARACHUTE when I start handling crashes since it
31 // prevents SDL from setting up its own handlers for SIG_SEGV and stuff like that
32
33 GameGui_SDL::s_errorMessage = "No error";
34
35 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
36 return RTWO_ERROR;
37 }
38
39 int imgFlags = IMG_INIT_PNG;
40 if (!(IMG_Init(imgFlags) & imgFlags)) {
41 return RTWO_ERROR;
42 }
43
44 if (TTF_Init() == -1) {
45 return RTWO_ERROR;
46 }
47
48 return RTWO_SUCCESS;
49}
50
51void GameGui_SDL::shutdown() {
52 SDL_Quit();
53}
54
55void* GameGui_SDL::createWindow(const string& title, int width, int height, bool fullscreen) {
56 // TODO: Make an OpenGL version of the SDL_CreateWindow call as well
57
58 // On Apple's OS X you must set the NSHighResolutionCapable Info.plist property to YES,
59 // otherwise you will not receive a High DPI OpenGL canvas.
60
61 SDL_DisplayMode dm;
62 SDL_GetCurrentDisplayMode(0, &dm);
63
64 if (fullscreen) {
65 width = dm.w;
66 height = dm.h;
67 }
68
69 uint32_t flags = SDL_WINDOW_VULKAN | (fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE);
70
71 window = SDL_CreateWindow(title.c_str(),
72 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
73 width, height, flags);
74
75 return window;
76}
77
78void GameGui_SDL::destroyWindow() {
79 // TODO: This function can throw some errors. They should be handled
80 SDL_DestroyWindow(window);
81}
82
83/*
84void GameGui_SDL::processEvents() {
85 SDL_Event e;
86
87 s_keyState.clear();
88 while (SDL_PollEvent(&e)) {
89 if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
90 if (e.type == SDL_KEYDOWN && !e.key.repeat) {
91 s_keyState[e.key.keysym.sym] = RTWO_KEY_EVENT_PRESSED;
92 } else if (e.type == SDL_KEYUP) {
93 s_keyState[e.key.keysym.sym] = RTWO_KEY_EVENT_RELEASED;
94 } else {
95 s_keyState.erase(e.key.keysym.sym);
96 }
97
98 s_keyDown[e.key.keysym.sym] = e.type == SDL_KEYDOWN;
99 } else if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) {
100 MouseEvent mouseEvent { 0, 0, e.button.x, e.button.y };
101
102 mouseEvents.push(mouseEvent);
103 } else {
104 events.push(e);
105 }
106 }
107}
108
109int GameGui_SDL::pollMouseEvent(MouseEvent* event) {
110 if (mouseEvents.empty()) {
111 return 0;
112 }
113
114 *event = mouseEvents.front();
115 mouseEvents.pop();
116
117 return 1;
118}
119
120unsigned char GameGui_SDL::getKeyEvent(unsigned int key) {
121 if (s_keyDown.count(key)) {
122 return s_keyState[key];
123 } else {
124 return RTWO_KEY_EVENT_NONE;
125 }
126}
127
128bool GameGui_SDL::isKeyPressed(unsigned int key) {
129 if (s_keyDown.count(key)) {
130 return s_keyDown[key];
131 } else {
132 return false;
133 }
134}
135
136int GameGui_SDL::pollEvent(SDL_Event* event) {
137 if (events.empty()) {
138 return 0;
139 }
140
141 *event = events.front();
142 events.pop();
143
144 return 1;
145}
146*/
147
148#ifdef GAMEGUI_INCLUDE_VULKAN
149
150bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
151 return SDL_Vulkan_CreateSurface(window, instance, surface) ?
152 RTWO_SUCCESS : RTWO_ERROR;
153}
154
155#endif
156
157vector<const char*> GameGui_SDL::getRequiredExtensions() {
158 uint32_t extensionCount = 0;
159 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
160
161 vector<const char*> extensions(extensionCount);
162 SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
163
164 return extensions;
165}
166
167void GameGui_SDL::getWindowSize(int* width, int* height) {
168 SDL_GetWindowSize(window, width, height);
169}
Note: See TracBrowser for help on using the repository browser.