use stb_image.h instead of SDL2_image
This commit is contained in:
parent
e1cba136a3
commit
1952ab60ff
@ -4,7 +4,6 @@ project(townengine LANGUAGES C)
|
|||||||
|
|
||||||
# SDL dependencies
|
# SDL dependencies
|
||||||
find_package(SDL2 REQUIRED GLOBAL)
|
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
|
# CMake actually has no default configuration and it's toolchain file dependent, set debug if not specified
|
||||||
if(NOT CMAKE_BUILD_TYPE)
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
@ -164,6 +163,7 @@ function(give_options target)
|
|||||||
-Wconversion -Wno-sign-conversion
|
-Wconversion -Wno-sign-conversion
|
||||||
-Werror=vla
|
-Werror=vla
|
||||||
-Wno-missing-field-initializers
|
-Wno-missing-field-initializers
|
||||||
|
-Wunused-result
|
||||||
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict>)
|
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict>)
|
||||||
|
|
||||||
target_compile_options(${target} PRIVATE
|
target_compile_options(${target} PRIVATE
|
||||||
@ -194,7 +194,6 @@ endfunction()
|
|||||||
function(link_deps target)
|
function(link_deps target)
|
||||||
target_link_libraries(${target} PUBLIC
|
target_link_libraries(${target} PUBLIC
|
||||||
SDL2::SDL2
|
SDL2::SDL2
|
||||||
SDL2_image::SDL2_image
|
|
||||||
physfs-static
|
physfs-static
|
||||||
xms)
|
xms)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
@ -4,12 +4,14 @@
|
|||||||
#include "townengine/context.h"
|
#include "townengine/context.h"
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_image.h>
|
|
||||||
#include <physfs.h>
|
#include <physfs.h>
|
||||||
#include <physfsrwops.h>
|
#include <physfsrwops.h>
|
||||||
#include <stb_ds.h>
|
#include <stb_ds.h>
|
||||||
#include <stb_rect_pack.h>
|
#include <stb_rect_pack.h>
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -18,18 +20,62 @@
|
|||||||
static SDL_Surface *image_to_surface(const char *path) {
|
static SDL_Surface *image_to_surface(const char *path) {
|
||||||
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
SDL_RWops *handle = PHYSFSRWOPS_openRead(path);
|
||||||
if (handle == NULL)
|
if (handle == NULL)
|
||||||
goto fail;
|
goto ERR_CANNOT_OPEN_FILE;
|
||||||
|
|
||||||
SDL_Surface *result = IMG_Load_RW(handle, true);
|
/* TODO: try using callbacks so that less memory is used */
|
||||||
if (result == NULL)
|
Sint64 file_size = handle->size(handle);
|
||||||
goto fail;
|
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);
|
||||||
|
|
||||||
SDL_SetSurfaceBlendMode(result, SDL_BLENDMODE_NONE);
|
if (!file_mem)
|
||||||
SDL_SetSurfaceRLE(result, true);
|
goto ERR_CANNOT_ALLOCATE_MEM;
|
||||||
|
|
||||||
return result;
|
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;
|
||||||
|
|
||||||
fail:
|
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...");
|
CRY(path, "Failed to load image. Aborting...");
|
||||||
die_abruptly();
|
die_abruptly();
|
||||||
}
|
}
|
||||||
@ -276,7 +322,9 @@ void textures_dump_atlases(struct texture_cache *cache) {
|
|||||||
return;
|
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);
|
log_info("Dumped atlas %s", buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 */
|
/* filesystem time */
|
||||||
/* TODO: ANDROID: see the warning in physicsfs PHYSFS_init docs/header */
|
/* TODO: ANDROID: see the warning in physicsfs PHYSFS_init docs/header */
|
||||||
if (!PHYSFS_init(ctx.argv[0]) ||
|
if (!PHYSFS_init(ctx.argv[0]) ||
|
||||||
@ -375,7 +369,6 @@ static void clean_up(void) {
|
|||||||
textures_cache_deinit(&ctx.texture_cache);
|
textures_cache_deinit(&ctx.texture_cache);
|
||||||
|
|
||||||
PHYSFS_deinit();
|
PHYSFS_deinit();
|
||||||
IMG_Quit();
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user