/*
* 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 .
*/
#include
#include
#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();
}
}