use stb_image.h instead of SDL2_image

This commit is contained in:
veclav talica 2024-08-27 14:45:26 +03:00
parent e1cba136a3
commit 1952ab60ff
3 changed files with 60 additions and 20 deletions

View File

@ -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
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-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()

View File

@ -4,12 +4,14 @@
#include "townengine/context.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <physfs.h>
#include <physfsrwops.h>
#include <stb_ds.h>
#include <stb_rect_pack.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
@ -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);
}
}

View File

@ -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();
}