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