summaryrefslogtreecommitdiff
path: root/src/textures.c
diff options
context:
space:
mode:
authoronelin <oscar@nelin.dk>2025-10-31 23:55:42 +0000
committeronelin <oscar@nelin.dk>2025-11-02 22:07:17 +0000
commitd38deeef3af2316a666f8fc0173940bd769b748e (patch)
tree6e30d4a9eea18daa5705c894f28cd99ff047e8f9 /src/textures.c
parent6c077751982ea2c7bd2d9262b01b9f8602f80dc8 (diff)
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.
Diffstat (limited to 'src/textures.c')
-rw-r--r--src/textures.c41
1 files changed, 41 insertions, 0 deletions
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 <daw/logging.h>
+#include <daw/resources.h>
+#include <stb/stb_image.h>
+
+/* 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;
+}