summaryrefslogtreecommitdiff
path: root/src/loader.c
blob: bd8d8c4af921de3100f6db5a2d1469cb0cb47afc (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "SDL/SDL_stdinc.h"
#include "SDL/SDL_image.h"
#include "loader.h"
#include "utils.h"

#define PATH_STYLE "../styles"

int loadRessources(gameIni_t *gIni, gameRess_t *gRess) {
	int i, filenamelen;
	char *filepath;

	filenamelen = sizeof(char)*(strlen(PATH_STYLE)+2*strlen(gIni->style.name)+strlen("//om_00.gif")+1);
	filepath = malloc(filenamelen);

	gRess->style.tiles=malloc(sizeof(SDL_Surface *)*gIni->style.tiles);
	if (gRess->style.tiles==NULL) {
		logp(LOG_ERROR, "loadRessources(), malloc()");
		return 1;
	}
	for(i=0; i < gIni->style.tiles ; ++i) {
		snprintf(filepath, filenamelen, "%s/%s/%s_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i);
		gRess->style.tiles[i] = IMG_Load(filepath);
		if(gRess->style.tiles[i]==NULL) {
			logs2(LOG_WARN, "loadRessources(), IMG_Load() error for ", filepath);
			return 2;
		}
	}

	gRess->style.objects=malloc(sizeof(SDL_Surface *)*gIni->style.objectCount);
	if (gRess->style.objects==NULL) {
		logp(LOG_ERROR, "loadRessources(), malloc()");
		return 1;
	}

	gRess->style.objectMasks=malloc(sizeof(SDL_Surface *)*gIni->style.objectCount);
	if (gRess->style.objectMasks==NULL) {
		logp(LOG_ERROR, "loadRessources(), malloc()");
		return 1;
	}


	for(i=0; i < gIni->style.objectCount ; ++i) {
		snprintf(filepath, filenamelen, "%s/%s/%so_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i);
		gRess->style.objects[i] = IMG_Load(filepath);
		if (gRess->style.objects[i]==NULL) {
			logs2(LOG_WARN, "loadRessources(), IMG_Load() error for ", filepath);
			return 2;
		}

		switch(gIni->style.objects[i].type) {
			case 5:
			case 6:
			case 7:
			case 8:
				snprintf(filepath, filenamelen, "%s/%s/%som_%d.gif", PATH_STYLE, gIni->style.name, gIni->style.name, i);
				gRess->style.objectMasks[i] = IMG_Load(filepath);
				if (gRess->style.objectMasks[i]==NULL) {
					logs2(LOG_WARN, "loadRessources(), IMG_Load() error for ", filepath);
					return 2;
				}
				break;
			default:
				/* No mask for other types */
				break;
		}
	}

	free(filepath);
	return 0;
}