-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdlfileutils.c
30 lines (25 loc) · 806 Bytes
/
sdlfileutils.c
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
#include "sdlfileutils.h"
#include <SDL2/SDL_image.h>
HASHMAP_SOURCE(TextureMap, const char*, SDL_Surface*, hashS)
void fileLoaderInit(fileLoader* loader){
loader->textures = TextureMapInit();
}
SDL_Surface* loadImage(fileLoader* loader, const char* src){
SDL_Surface* item = TextureMapGet(&(loader->textures), src).val;
if (item != NULL){
return item;
}
item = IMG_Load(src);
if (item == NULL){
printf("[!] Unable to load image from file \'%s\'\n%s\n", src, SDL_GetError());
}
TextureMapPush(&(loader->textures), src, item);
return item;
}
void fileLoaderClose(fileLoader* loader){
TextureMapIterator it = TextureMapIteratorInit(&(loader->textures));
while(TextureMapIteratorHasNext(&it)){
SDL_FreeSurface(TextureMapIteratorNext(&it).val);
}
TextureMapFree(&(loader->textures));
}