aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-05-11 01:39:27 +0200
committerkdx <kikoodx@paranoici.org>2023-05-11 01:59:07 +0200
commitba11ae247bf6d234de29feec5fc59e7f3b96d632 (patch)
tree1e6bcce496e320aff32faee72953f59746022866
parent378f0a3965c917b0491e2b30938684584bb754f5 (diff)
downloadtzr-ba11ae247bf6d234de29feec5fc59e7f3b96d632.tar.gz
optional stb_image loading
-rw-r--r--_head.c6
-rw-r--r--_head.h1
-rwxr-xr-xcreate_TZR.h.sh2
-rw-r--r--sources/TZR_DirectResourceLoad.c53
-rw-r--r--sources/TZR_LoadResource.c1
5 files changed, 62 insertions, 1 deletions
diff --git a/_head.c b/_head.c
index afa4308..5450be1 100644
--- a/_head.c
+++ b/_head.c
@@ -1 +1,7 @@
/* Licensing information can be found at the end of the file. */
+
+#ifdef TZR_STB_IMAGE
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+#undef STB_IMAGE_IMPLEMENTATION
+#endif
diff --git a/_head.h b/_head.h
new file mode 100644
index 0000000..afa4308
--- /dev/null
+++ b/_head.h
@@ -0,0 +1 @@
+/* Licensing information can be found at the end of the file. */
diff --git a/create_TZR.h.sh b/create_TZR.h.sh
index 85d2831..4d51d15 100755
--- a/create_TZR.h.sh
+++ b/create_TZR.h.sh
@@ -19,7 +19,7 @@ HEADER=TZR_render ProcessHeader
HEADER=TZR_keystate ProcessHeader
HEADER=TZR ProcessHeader
-cp _head.c "$BUILDDIR/out"
+cp _head.h "$BUILDDIR/out"
printf '#pragma once\n' >>"$BUILDDIR/out"
sort -u "$BUILDDIR/includes" >>"$BUILDDIR/out"
cat <<EOF >>"$BUILDDIR/out"
diff --git a/sources/TZR_DirectResourceLoad.c b/sources/TZR_DirectResourceLoad.c
index 9c7153d..6bbbc5c 100644
--- a/sources/TZR_DirectResourceLoad.c
+++ b/sources/TZR_DirectResourceLoad.c
@@ -6,6 +6,53 @@
#include <SDL2/SDL_rwops.h>
#include <string.h>
+#ifdef TZR_STB_IMAGE
+#define STB_IMAGE_IMPLEMENTATION
+#include "stb_image.h"
+
+SDL_Surface *
+stbi_load_surface(void *src, int size)
+{
+ int width, height, chans;
+ void *const data = stbi_load_from_memory(src, size, &width, &height,
+ &chans, 4);
+ const int pitch = (width * 4 + 3) & ~3;
+
+ if (data == NULL) {
+ fprintf(stderr, "stbi: %s\n", stbi_failure_reason());
+ return NULL;
+ }
+
+ if (chans != 4) {
+ fprintf(stderr, "TZR only supports RGBA images\n");
+ free(data);
+ return NULL;
+ }
+
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ const int rmask = 0x000000ff;
+ const int gmask = 0x0000ff00;
+ const int bmask = 0x00ff0000;
+ const int amask = 0xff000000 * (chans == 4);
+#else
+ const int s = 8 * (chans != 4);
+ const int rmask = 0xff000000 >> s;
+ const int gmask = 0x00ff0000 >> s;
+ const int bmask = 0x0000ff00 >> s;
+ const int amask = 0x000000ff >> s;
+#endif
+
+ SDL_Surface *const surf = SDL_CreateRGBSurfaceFrom(data, width, height, chans * 8, pitch, rmask, gmask, bmask, amask);
+ if (surf == NULL) {
+ sdl_error(0);
+ free(data);
+ return NULL;
+ }
+
+ return surf;
+}
+#endif
+
int
TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
{
@@ -20,12 +67,18 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
memcpy(res->raw.data, data, size);
break;
case TZR_RES_IMAGE: {
+#ifdef TZR_STB_IMAGE
+ SDL_Surface *const surf = stbi_load_surface((void*)data, size);
+ if (surf == NULL)
+ return -1;
+#else
SDL_RWops *const rw = SDL_RWFromConstMem(data, size);
if (rw == NULL)
return sdl_error(-1);
SDL_Surface *const surf = SDL_LoadBMP_RW(rw, 1);
if (surf == NULL)
return sdl_error(-1);
+#endif
SDL_Texture *const tex =
SDL_CreateTextureFromSurface(___tzr_renderer, surf);
SDL_FreeSurface(surf);
diff --git a/sources/TZR_LoadResource.c b/sources/TZR_LoadResource.c
index 4f5312e..c1c73ce 100644
--- a/sources/TZR_LoadResource.c
+++ b/sources/TZR_LoadResource.c
@@ -10,6 +10,7 @@ deduce_type(const char *path)
return TZR_RES_RAW;
if (strcasecmp(path_extension, ".bmp") == 0 ||
strcasecmp(path_extension, ".png") == 0 ||
+ strcasecmp(path_extension, ".jpg") == 0 ||
strcasecmp(path_extension, ".qoi") == 0)
return TZR_RES_IMAGE;
if (strcasecmp(path_extension, ".wav") == 0)