diff options
Diffstat (limited to 'src/resources/image.c')
-rw-r--r-- | src/resources/image.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/resources/image.c b/src/resources/image.c new file mode 100644 index 0000000..cd2c317 --- /dev/null +++ b/src/resources/image.c @@ -0,0 +1,48 @@ +#include <SDL.h> +#include <SDL_image.h> +#include "../logging.h" +#include "image.h" +#include "resource.h" + +struct resource_table img_res; + +struct image * +img_png_get(const char *path) +{ + SDL_RWops *rwops; + struct image *img; + + img = (struct image *) resource_get(&img_res, path); + if (img != NULL) { + resource_use((struct resource *) img); + return img; + } + + img = resource_alloc(path, sizeof(*img)); + rwops = SDL_RWFromFile(path, "rb"); + img->image = IMG_LoadPNG_RW(rwops); + if (!img->image) { + err(1, "Failed to load image \"%s\" (%s)", + path, IMG_GetError()); + } + + resource_use((struct resource *) img); + resource_add(&img_res, path, (struct resource *) img); + + return img; +} + +void +img_png_free(struct image *image) +{ + SDL_Surface *data; + + if (image == NULL) { + return; + } + + data = image->image; + if (resource_free(&img_res, (struct resource *) image)) { + SDL_FreeSurface(data); + } +} |