summaryrefslogtreecommitdiff
path: root/src/textures.c
blob: 5b23e7a9f2f6a14948170775a663f99d04775dc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}