1 | #include "game-gui-sdl.hpp"
|
---|
2 |
|
---|
3 | #include <map>
|
---|
4 | #include <queue>
|
---|
5 |
|
---|
6 | #include "consts.hpp"
|
---|
7 |
|
---|
8 | map<unsigned int, unsigned char> s_keyState;
|
---|
9 | map<unsigned int, bool> s_keyDown;
|
---|
10 |
|
---|
11 | using 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
|
---|
16 | queue<SDL_Event> events;
|
---|
17 |
|
---|
18 | queue<MouseEvent> mouseEvents;
|
---|
19 | */
|
---|
20 |
|
---|
21 | string GameGui_SDL::s_errorMessage;
|
---|
22 |
|
---|
23 | string& GameGui_SDL::getError() {
|
---|
24 | GameGui_SDL::s_errorMessage = SDL_GetError();
|
---|
25 |
|
---|
26 | return GameGui_SDL::s_errorMessage;
|
---|
27 | }
|
---|
28 |
|
---|
29 | bool 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 |
|
---|
51 | void GameGui_SDL::shutdown() {
|
---|
52 | SDL_Quit();
|
---|
53 | }
|
---|
54 |
|
---|
55 | void* 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 | uint32_t flags = SDL_WINDOW_VULKAN;
|
---|
62 |
|
---|
63 | flags |= fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
|
---|
64 |
|
---|
65 | window = SDL_CreateWindow(title.c_str(),
|
---|
66 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
---|
67 | width, height, flags);
|
---|
68 |
|
---|
69 | return window;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void GameGui_SDL::destroyWindow() {
|
---|
73 | // TODO: This function can throw some errors. They should be handled
|
---|
74 | SDL_DestroyWindow(window);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /*
|
---|
78 | void GameGui_SDL::processEvents() {
|
---|
79 | SDL_Event e;
|
---|
80 |
|
---|
81 | s_keyState.clear();
|
---|
82 | while (SDL_PollEvent(&e)) {
|
---|
83 | if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
|
---|
84 | if (e.type == SDL_KEYDOWN && !e.key.repeat) {
|
---|
85 | s_keyState[e.key.keysym.sym] = RTWO_KEY_EVENT_PRESSED;
|
---|
86 | } else if (e.type == SDL_KEYUP) {
|
---|
87 | s_keyState[e.key.keysym.sym] = RTWO_KEY_EVENT_RELEASED;
|
---|
88 | } else {
|
---|
89 | s_keyState.erase(e.key.keysym.sym);
|
---|
90 | }
|
---|
91 |
|
---|
92 | s_keyDown[e.key.keysym.sym] = e.type == SDL_KEYDOWN;
|
---|
93 | } else if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) {
|
---|
94 | MouseEvent mouseEvent { 0, 0, e.button.x, e.button.y };
|
---|
95 |
|
---|
96 | mouseEvents.push(mouseEvent);
|
---|
97 | } else {
|
---|
98 | events.push(e);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | int GameGui_SDL::pollMouseEvent(MouseEvent* event) {
|
---|
104 | if (mouseEvents.empty()) {
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | *event = mouseEvents.front();
|
---|
109 | mouseEvents.pop();
|
---|
110 |
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | unsigned char GameGui_SDL::getKeyEvent(unsigned int key) {
|
---|
115 | if (s_keyDown.count(key)) {
|
---|
116 | return s_keyState[key];
|
---|
117 | } else {
|
---|
118 | return RTWO_KEY_EVENT_NONE;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | bool GameGui_SDL::isKeyPressed(unsigned int key) {
|
---|
123 | if (s_keyDown.count(key)) {
|
---|
124 | return s_keyDown[key];
|
---|
125 | } else {
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | int GameGui_SDL::pollEvent(SDL_Event* event) {
|
---|
131 | if (events.empty()) {
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | *event = events.front();
|
---|
136 | events.pop();
|
---|
137 |
|
---|
138 | return 1;
|
---|
139 | }
|
---|
140 | */
|
---|
141 |
|
---|
142 | #ifdef GAMEGUI_INCLUDE_VULKAN
|
---|
143 |
|
---|
144 | bool GameGui_SDL::createVulkanSurface(VkInstance instance, VkSurfaceKHR* surface) {
|
---|
145 | return SDL_Vulkan_CreateSurface(window, instance, surface) ?
|
---|
146 | RTWO_SUCCESS : RTWO_ERROR;
|
---|
147 | }
|
---|
148 |
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | vector<const char*> GameGui_SDL::getRequiredExtensions() {
|
---|
152 | uint32_t extensionCount = 0;
|
---|
153 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, nullptr);
|
---|
154 |
|
---|
155 | vector<const char*> extensions(extensionCount);
|
---|
156 | SDL_Vulkan_GetInstanceExtensions(window, &extensionCount, extensions.data());
|
---|
157 |
|
---|
158 | return extensions;
|
---|
159 | }
|
---|
160 |
|
---|
161 | void GameGui_SDL::getWindowSize(int* width, int* height) {
|
---|
162 | SDL_GetWindowSize(window, width, height);
|
---|
163 | }
|
---|