From 1952ab60ff50cd18b82f914067420f262e178e98 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 27 Aug 2024 14:45:26 +0300 Subject: [PATCH] use stb_image.h instead of SDL2_image --- CMakeLists.txt | 3 +- townengine/textures/textures.c | 70 ++++++++++++++++++++++++++++------ townengine/twn_loop.c | 7 ---- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8006118..5296c4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,6 @@ project(townengine LANGUAGES C) # SDL dependencies find_package(SDL2 REQUIRED GLOBAL) -find_package(SDL2_image REQUIRED GLOBAL) # CMake actually has no default configuration and it's toolchain file dependent, set debug if not specified if(NOT CMAKE_BUILD_TYPE) @@ -164,6 +163,7 @@ function(give_options target) -Wconversion -Wno-sign-conversion -Werror=vla -Wno-missing-field-initializers + -Wunused-result $<$:-Wcast-align=strict>) target_compile_options(${target} PRIVATE @@ -194,7 +194,6 @@ endfunction() function(link_deps target) target_link_libraries(${target} PUBLIC SDL2::SDL2 - SDL2_image::SDL2_image physfs-static xms) endfunction() diff --git a/townengine/textures/textures.c b/townengine/textures/textures.c index 5d727ed..6f896d5 100644 --- a/townengine/textures/textures.c +++ b/townengine/textures/textures.c @@ -4,12 +4,14 @@ #include "townengine/context.h" #include -#include #include #include #include #include +#define STB_IMAGE_IMPLEMENTATION +#include + #include #include #include @@ -18,18 +20,62 @@ static SDL_Surface *image_to_surface(const char *path) { SDL_RWops *handle = PHYSFSRWOPS_openRead(path); if (handle == NULL) - goto fail; - - SDL_Surface *result = IMG_Load_RW(handle, true); - if (result == NULL) - goto fail; + goto ERR_CANNOT_OPEN_FILE; - SDL_SetSurfaceBlendMode(result, SDL_BLENDMODE_NONE); - SDL_SetSurfaceRLE(result, true); + /* 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); + SDL_FreeRW(handle); - return result; + if (!file_mem) + goto ERR_CANNOT_ALLOCATE_MEM; -fail: + int width, height, channels; + void *image_mem = stbi_load_from_memory(file_mem, (int)file_size, &width, &height, &channels, 4); + if (!image_mem) + goto ERR_CANNOT_READ_IMAGE; + + free(file_mem); + + Uint32 rmask, gmask, bmask, amask; + + #if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; + #else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; + #endif + + SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(image_mem, width, height, + channels * 8, + width * channels, + rmask, gmask, bmask, amask); + if (surface == NULL) + goto ERR_CANNOT_CREATE_SURFACE; + + SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE); + SDL_SetSurfaceRLE(surface, true); + + return surface; + +ERR_CANNOT_CREATE_SURFACE: + stbi_image_free(image_mem); + +ERR_CANNOT_READ_IMAGE: + free(file_mem); + +ERR_CANNOT_ALLOCATE_MEM: + SDL_FreeRW(handle); + +ERR_CANNOT_OPEN_FILE: CRY(path, "Failed to load image. Aborting..."); die_abruptly(); } @@ -276,7 +322,9 @@ void textures_dump_atlases(struct texture_cache *cache) { return; } - IMG_SavePNG_RW(cache->atlas_surfaces[i], handle, true); + /* TODO: */ + // IMG_SavePNG_RW(cache->atlas_surfaces[i], handle, true); + CRY("Unimplemented", "textures_dump_atlases dumping is not there, sorry"); log_info("Dumped atlas %s", buf); } } diff --git a/townengine/twn_loop.c b/townengine/twn_loop.c index a75d985..fbbb406 100644 --- a/townengine/twn_loop.c +++ b/townengine/twn_loop.c @@ -283,12 +283,6 @@ static bool initialize(void) { } - /* images */ - if (IMG_Init(IMG_INIT_PNG) == 0) { - CRY_SDL("SDL_image initialization failed."); - goto fail; - } - /* filesystem time */ /* TODO: ANDROID: see the warning in physicsfs PHYSFS_init docs/header */ if (!PHYSFS_init(ctx.argv[0]) || @@ -375,7 +369,6 @@ static void clean_up(void) { textures_cache_deinit(&ctx.texture_cache); PHYSFS_deinit(); - IMG_Quit(); SDL_Quit(); }