1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* Tuxánci 2 - A first person shooter
* Copyright (C) 2025-2026 Connor Thomson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdlib.h>
#include "ta_sdl.h"
#include "logging.h"
#include "main.h"
static void ta_sdl_handle_error(void) {
const char *error = SDL_GetError();
SDL_Log("SDL: %s\n", error);
ta_main_exit(1);
}
void ta_sdl_init(SDL_InitFlags flags) {
// TODO: Make it log what subsystem
ta_log("Initializing a SDL subsystem");
bool success = SDL_Init(flags);
if (!success) {
ta_sdl_handle_error();
}
}
void ta_sdl_shutdown(void) {
ta_log("Exiting SDL");
SDL_Quit();
}
SDL_Window *ta_sdl_create_window(const char *title, int width, int height) {
SDL_WindowFlags flags = { 0 };
ta_log("Initializing SDL window");
SDL_Window *window = SDL_CreateWindow(title, width, height, flags);
if (window) {
return window;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_IOStream *ta_sdl_io_from_file(const char *file, const char *mode) {
ta_log("Opening %s as %s", file, mode);
SDL_IOStream *io = SDL_IOFromFile(file, mode);
if (io) {
return io;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_IOStream *ta_sdl_io_from_mem(void *mem, size_t size) {
ta_log("Loading IO from memory");
SDL_IOStream *io = SDL_IOFromMem(mem, size);
if (io) {
return io;
} else {
ta_sdl_handle_error();
return NULL;
}
}
void ta_sdl_get_window_size(SDL_Window *window, int *width, int *height) {
bool success = SDL_GetWindowSize(window, width, height);
if (!success) {
ta_sdl_handle_error();
}
}
SDL_GPUDevice *ta_sdl_create_gpu_device(void) {
ta_log("Creating SDL_gpu device");
SDL_GPUDevice *device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL);
if (device) {
return device;
} else {
ta_sdl_handle_error();
return NULL;
}
}
void ta_sdl_claim_window_for_gpu_device(SDL_GPUDevice *device, SDL_Window *window) {
ta_log("Claiming SDL window for SDL_gpu device");
bool success = SDL_ClaimWindowForGPUDevice(device, window);
if (!success) {
ta_sdl_handle_error();
}
}
SDL_GPUShader *ta_sdl_create_gpu_shader(SDL_GPUDevice *device, const SDL_GPUShaderCreateInfo *createinfo) {
ta_log("Creating a SDL_gpu shader");
SDL_GPUShader *shader = SDL_CreateGPUShader(device, createinfo);
if (shader) {
return shader;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPUGraphicsPipeline *ta_sdl_create_gpu_graphics_pipeline(SDL_GPUDevice *device, const SDL_GPUGraphicsPipelineCreateInfo *createinfo) {
ta_log("Creating a SDL_gpu graphics pipeline");
SDL_GPUGraphicsPipeline *graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, createinfo);
if (graphics_pipeline) {
return graphics_pipeline;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPUTexture *ta_sdl_create_gpu_texture(SDL_GPUDevice *device, const SDL_GPUTextureCreateInfo *createinfo) {
SDL_GPUTexture *texture = SDL_CreateGPUTexture(device, createinfo);
if (texture) {
return texture;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPUSampler *ta_sdl_create_gpu_sampler(SDL_GPUDevice *device, const SDL_GPUSamplerCreateInfo *createinfo) {
SDL_GPUSampler *sampler = SDL_CreateGPUSampler(device, createinfo);
if (sampler) {
return sampler;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPUTransferBuffer *ta_sdl_create_gpu_transfer_buffer(SDL_GPUDevice *device, const SDL_GPUTransferBufferCreateInfo *createinfo) {
SDL_GPUTransferBuffer *transfer_buffer = SDL_CreateGPUTransferBuffer(device, createinfo);
if (transfer_buffer) {
return transfer_buffer;
} else {
ta_sdl_handle_error();
return NULL;
}
}
void *ta_sdl_map_gpu_transfer_buffer(SDL_GPUDevice *device, SDL_GPUTransferBuffer *transfer_buffer, bool cycle) {
void *mapped = SDL_MapGPUTransferBuffer(device, transfer_buffer, cycle);
if (mapped) {
return mapped;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPUCommandBuffer *ta_sdl_acquire_gpu_command_buffer(SDL_GPUDevice *device) {
SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device);
if (command_buffer) {
return command_buffer;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPUCopyPass *ta_sdl_begin_gpu_copy_pass(SDL_GPUCommandBuffer *command_buffer) {
SDL_GPUCopyPass *copy_pass = SDL_BeginGPUCopyPass(command_buffer);
if (copy_pass) {
return copy_pass;
} else {
ta_sdl_handle_error();
return NULL;
}
}
SDL_GPURenderPass *ta_sdl_begin_gpu_render_pass(SDL_GPUCommandBuffer *command_buffer, const SDL_GPUColorTargetInfo *color_target_infos, Uint32 num_color_targets, const SDL_GPUDepthStencilTargetInfo *depth_stencil_target_info) {
SDL_GPURenderPass *render_pass = SDL_BeginGPURenderPass(command_buffer, color_target_infos, num_color_targets, depth_stencil_target_info);
if (render_pass) {
return render_pass;
} else {
ta_sdl_handle_error();
return NULL;
}
}
void ta_sdl_wait_and_acquire_gpu_swapchain_texture(SDL_GPUCommandBuffer *commandbuffer, SDL_Window *window, SDL_GPUTexture **swapchain_texture) {
Uint32 *swapchain_texture_width = NULL;
Uint32 *swapchain_texture_height = NULL;
bool success = SDL_WaitAndAcquireGPUSwapchainTexture(commandbuffer, window, swapchain_texture, swapchain_texture_width, swapchain_texture_height);
if (!success) {
ta_sdl_handle_error();
}
}
void ta_sdl_submit_gpu_command_buffer(SDL_GPUCommandBuffer *command_buffer) {
bool success = SDL_SubmitGPUCommandBuffer(command_buffer);
if (!success) {
ta_sdl_handle_error();
}
}
SDL_Surface *ta_sdl_convert_surface(SDL_Surface *surface, SDL_PixelFormat format) {
SDL_Surface *converted_surface = SDL_ConvertSurface(surface, format);
if (converted_surface) {
return converted_surface;
} else {
ta_sdl_handle_error();
return NULL;
}
}
void ta_sdl_set_window_fullscreen(SDL_Window *window, bool fullscreen) {
bool success = SDL_SetWindowFullscreen(window, fullscreen);
if (!success) {
ta_sdl_handle_error();
}
}
|