summaryrefslogtreecommitdiff
path: root/src/loader.c
blob: d3cce84d61fca53497664f69d54b9f922b886382 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "loader.h"

#include "SDL/SDL_stdinc.h"
#include "SDL/SDL_image.h"

#include "parser.h"
#include "graphic.h"
#include "utils.h"

#define PATH_STYLE "../styles"

extern FILE *yyin;

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] = loadGif(filepath);
		if(gRess->style.tiles[i]==NULL) {
			logs2(LOG_WARN, "loadRessources(), loadGif() 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] = loadGif(filepath);
		if (gRess->style.objects[i]==NULL) {
			logs2(LOG_WARN, "loadRessources(), loadGif() 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] = loadGif(filepath);
				if (gRess->style.objectMasks[i]==NULL) {
					logs2(LOG_WARN, "loadRessources(), loadGif() error for ", filepath);
					return 2;
				}
				break;
			default:
				/* No mask for other types */
				break;
		}
	}

	free(filepath);
	return 0;
}

int loadIni(gameIni_t *gIni, char *filepath) {
	int res;

	yyin=fopen(filepath, "r");
	if (yyin == NULL ) {
		fprintf(stderr, "load_ini(), Could not open '%s'\n", filepath);
		return 1;
	}
	res=parse(gIni);
	fclose(yyin);
	return res;
}