refactor SDL out of core files

This commit is contained in:
asrael 2025-10-17 17:54:33 -05:00
parent 82ed6b4ea9
commit 9d183341ae
21 changed files with 1419 additions and 1028 deletions

View file

@ -8,7 +8,6 @@
#define MINIZ_NO_DEFLATE_APIS
#include <miniz.h>
#include <SDL3/SDL.h>
#include "pxl8_ase.h"
#include "pxl8_io.h"
@ -57,7 +56,7 @@ static pxl8_result parse_old_palette_chunk(pxl8_stream* stream, pxl8_ase_palette
palette->entry_count = total_colors;
palette->first_color = 0;
palette->last_color = total_colors - 1;
palette->colors = (u32*)SDL_malloc(total_colors * sizeof(u32));
palette->colors = (u32*)malloc(total_colors * sizeof(u32));
if (!palette->colors) {
return PXL8_ERROR_OUT_OF_MEMORY;
}
@ -96,7 +95,7 @@ static pxl8_result parse_layer_chunk(pxl8_stream* stream, pxl8_ase_layer* layer)
u16 name_len = pxl8_read_u16(stream);
if (name_len > 0) {
layer->name = (char*)SDL_malloc(name_len + 1);
layer->name = (char*)malloc(name_len + 1);
if (!layer->name) return PXL8_ERROR_OUT_OF_MEMORY;
pxl8_read_bytes(stream, layer->name, name_len);
layer->name[name_len] = '\0';
@ -119,7 +118,7 @@ static pxl8_result parse_palette_chunk(pxl8_stream* stream, pxl8_ase_palette* pa
return PXL8_ERROR_ASE_MALFORMED_CHUNK;
}
palette->colors = (u32*)SDL_malloc(color_count * sizeof(u32));
palette->colors = (u32*)malloc(color_count * sizeof(u32));
if (!palette->colors) {
return PXL8_ERROR_OUT_OF_MEMORY;
}
@ -167,7 +166,7 @@ static pxl8_result parse_cel_chunk(pxl8_stream* stream, u32 chunk_size, pxl8_ase
u32 pixel_data_size = cel->width * cel->height;
u32 compressed_data_size = chunk_size - 20;
cel->pixel_data = (u8*)SDL_malloc(pixel_data_size);
cel->pixel_data = (u8*)malloc(pixel_data_size);
if (!cel->pixel_data) {
return PXL8_ERROR_OUT_OF_MEMORY;
}
@ -177,7 +176,7 @@ static pxl8_result parse_cel_chunk(pxl8_stream* stream, u32 chunk_size, pxl8_ase
i32 result = mz_uncompress(cel->pixel_data, &dest_len, compressed_data, compressed_data_size);
if (result != MZ_OK) {
pxl8_error("Failed to decompress cel data: miniz error %d", result);
SDL_free(cel->pixel_data);
free(cel->pixel_data);
cel->pixel_data = NULL;
return PXL8_ERROR_ASE_MALFORMED_CHUNK;
}
@ -218,7 +217,7 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
}
ase_file->frame_count = ase_file->header.frames;
ase_file->frames = (pxl8_ase_frame*)SDL_calloc(ase_file->frame_count, sizeof(pxl8_ase_frame));
ase_file->frames = (pxl8_ase_frame*)calloc(ase_file->frame_count, sizeof(pxl8_ase_frame));
if (!ase_file->frames) {
pxl8_io_free_binary_data(file_data);
return PXL8_ERROR_OUT_OF_MEMORY;
@ -256,7 +255,7 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
frame->duration = frame_header.duration;
u32 pixel_count = frame->width * frame->height;
frame->pixels = (u8*)SDL_calloc(pixel_count, sizeof(u8));
frame->pixels = (u8*)calloc(pixel_count, sizeof(u8));
if (!frame->pixels) {
result = PXL8_ERROR_OUT_OF_MEMORY;
break;
@ -285,7 +284,7 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
case PXL8_ASE_CHUNK_LAYER: {
ase_file->layers =
(pxl8_ase_layer*)SDL_realloc(ase_file->layers,
(pxl8_ase_layer*)realloc(ase_file->layers,
(ase_file->layer_count + 1) * sizeof(pxl8_ase_layer));
if (!ase_file->layers) {
result = PXL8_ERROR_OUT_OF_MEMORY;
@ -325,14 +324,14 @@ pxl8_result pxl8_ase_load(const char* filepath, pxl8_ase_file* ase_file) {
}
}
}
SDL_free(cel.pixel_data);
free(cel.pixel_data);
}
break;
}
case PXL8_ASE_CHUNK_PALETTE:
if (ase_file->palette.colors) {
SDL_free(ase_file->palette.colors);
free(ase_file->palette.colors);
ase_file->palette.colors = NULL;
}
result = parse_palette_chunk(&stream, &ase_file->palette);
@ -367,23 +366,23 @@ void pxl8_ase_destroy(pxl8_ase_file* ase_file) {
if (ase_file->frames) {
for (u32 i = 0; i < ase_file->frame_count; i++) {
if (ase_file->frames[i].pixels) {
SDL_free(ase_file->frames[i].pixels);
free(ase_file->frames[i].pixels);
}
}
SDL_free(ase_file->frames);
free(ase_file->frames);
}
if (ase_file->palette.colors) {
SDL_free(ase_file->palette.colors);
free(ase_file->palette.colors);
}
if (ase_file->layers) {
for (u32 i = 0; i < ase_file->layer_count; i++) {
if (ase_file->layers[i].name) {
SDL_free(ase_file->layers[i].name);
free(ase_file->layers[i].name);
}
}
SDL_free(ase_file->layers);
free(ase_file->layers);
}
memset(ase_file, 0, sizeof(pxl8_ase_file));