From d38deeef3af2316a666f8fc0173940bd769b748e Mon Sep 17 00:00:00 2001 From: onelin Date: Sat, 1 Nov 2025 00:55:42 +0100 Subject: Flatten project structure This will make it easier to break up the code into smaller chunks again later. One would think doing this seems fun to me at this point. --- src/textures.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/textures.c (limited to 'src/textures.c') diff --git a/src/textures.c b/src/textures.c new file mode 100644 index 0000000..5b23e7a --- /dev/null +++ b/src/textures.c @@ -0,0 +1,41 @@ +#include +#include +#include + +/* Uses stb_image to load an image, and passes it to the renderer for + * backend-specific texture creation. */ +Texture load_texture(const Asset_TextureSpec *restrict ts) { + int width; + int height; + int components_per_pixel; + unsigned char* img; + Texture t; + const Texture err = (Texture){.id = 0, .width = 0, .height = 0}; + + if (ts == NULL) { + ERROR("Invalid Asset_TextureSpec\n"); + return err; + } + + if (ts->path == NULL) { + ERROR("Missing path in Asset_TextureSpec\n"); + return err; + } + + img = stbi_load(ts->path, &width, &height, &components_per_pixel, 0); + + if (img == NULL) { + ERROR("Failed to load image %s", ts->path); + return err; + } else { + t = createTextureFromImageData(img, width, height, (u8)components_per_pixel); + stbi_image_free(img); + } + + if (t.id == 0) { + ERROR("Failed to create texture %s!", ts->path); + return err; + } + + return t; +} -- cgit v1.3