twn_textures.c: use SDL memory allocation, use SDL_LoadFileRW in texture file loading
This commit is contained in:
parent
fc8b953655
commit
1430a13832
@ -13,8 +13,6 @@
|
|||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
|
|
||||||
static SDL_Surface *image_to_surface(const char *path) {
|
static SDL_Surface *image_to_surface(const char *path) {
|
||||||
@ -22,12 +20,9 @@ static SDL_Surface *image_to_surface(const char *path) {
|
|||||||
if (handle == NULL)
|
if (handle == NULL)
|
||||||
goto ERR_CANNOT_OPEN_FILE;
|
goto ERR_CANNOT_OPEN_FILE;
|
||||||
|
|
||||||
/* TODO: try using callbacks so that less memory is used */
|
/* TODO: try using callbacks so that no loading of whole files is needed */
|
||||||
Sint64 file_size = handle->size(handle);
|
size_t file_size;
|
||||||
SDL_assert_always(file_size != -1);
|
void *file_mem = SDL_LoadFile_RW(handle, &file_size, 0);
|
||||||
void *file_mem = malloc(file_size);
|
|
||||||
size_t read = handle->read(handle, file_mem, 1, file_size);
|
|
||||||
SDL_assert_always(read == (size_t)file_size);
|
|
||||||
SDL_FreeRW(handle);
|
SDL_FreeRW(handle);
|
||||||
|
|
||||||
if (!file_mem)
|
if (!file_mem)
|
||||||
@ -38,7 +33,7 @@ static SDL_Surface *image_to_surface(const char *path) {
|
|||||||
if (!image_mem)
|
if (!image_mem)
|
||||||
goto ERR_CANNOT_READ_IMAGE;
|
goto ERR_CANNOT_READ_IMAGE;
|
||||||
|
|
||||||
free(file_mem);
|
SDL_free(file_mem);
|
||||||
|
|
||||||
Uint32 rmask, gmask, bmask, amask;
|
Uint32 rmask, gmask, bmask, amask;
|
||||||
|
|
||||||
@ -70,11 +65,9 @@ ERR_CANNOT_CREATE_SURFACE:
|
|||||||
stbi_image_free(image_mem);
|
stbi_image_free(image_mem);
|
||||||
|
|
||||||
ERR_CANNOT_READ_IMAGE:
|
ERR_CANNOT_READ_IMAGE:
|
||||||
free(file_mem);
|
SDL_free(file_mem);
|
||||||
|
|
||||||
ERR_CANNOT_ALLOCATE_MEM:
|
ERR_CANNOT_ALLOCATE_MEM:
|
||||||
SDL_FreeRW(handle);
|
|
||||||
|
|
||||||
ERR_CANNOT_OPEN_FILE:
|
ERR_CANNOT_OPEN_FILE:
|
||||||
CRY(path, "Failed to load image. Aborting...");
|
CRY(path, "Failed to load image. Aborting...");
|
||||||
die_abruptly();
|
die_abruptly();
|
||||||
@ -251,7 +244,7 @@ void textures_cache_init(TextureCache *cache, SDL_Window *window) {
|
|||||||
cache->window = window;
|
cache->window = window;
|
||||||
sh_new_arena(cache->hash);
|
sh_new_arena(cache->hash);
|
||||||
|
|
||||||
cache->node_buffer = cmalloc(sizeof *cache->node_buffer * TEXTURE_ATLAS_SIZE);
|
cache->node_buffer = SDL_calloc(TEXTURE_ATLAS_SIZE, sizeof *cache->node_buffer);
|
||||||
|
|
||||||
add_new_atlas(cache);
|
add_new_atlas(cache);
|
||||||
recreate_current_atlas_texture(cache);
|
recreate_current_atlas_texture(cache);
|
||||||
@ -278,7 +271,7 @@ void textures_cache_deinit(TextureCache *cache) {
|
|||||||
}
|
}
|
||||||
shfree(cache->hash);
|
shfree(cache->hash);
|
||||||
|
|
||||||
free(cache->node_buffer);
|
SDL_free(cache->node_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user