summaryrefslogtreecommitdiff
path: root/src/loader.c
blob: fdf2a6e72297cf6b4ddfb375ed49b7c0ee0dd0c6 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "loader.h"
#include "utils.h" /* MPL_CHECK */

#include <SDL_image.h>

int loadSprite(SDL_Renderer *rend, char *giffilepath, int frames, sprite_t *sprite);
int loadSurface(SDL_Renderer *rend, char *giffilepath, SDL_Surface **surf);

int loadLevel(char data_basepath[], char packId[], char levelId[], gameIni_t *gIni) {
	int res;
	char filepath[MAX_PATH_LEN];

	SDL_strlcpy(gIni->data_basepath, data_basepath, MAX_PATH_LEN);

	SDL_snprintf(filepath, MAX_PATH_LEN, "%s/level/%s/levelpack.ini", data_basepath, packId);
	res=loadIni(ini_levelpack, filepath, gIni);
	if (res!=0) return res;

	SDL_strlcpy(gIni->levelPack.id, packId, MAX_NAMELEN);

	SDL_snprintf(filepath, MAX_PATH_LEN, "%s/level/%s/%s.ini", gIni->data_basepath, packId, levelId);
	res=loadIni(ini_level, filepath, gIni);
	if (res!=0) return res;

	// Check if we found a "style =" line in level ini file
	MPL_CHECK(
			gIni->level.style[0] != '\0',
			{ return 10; },
			SDL_LOG_PRIORITY_CRITICAL,
			"No valid style detected in level ini file"
	);

	// Loading style ini file
	SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%s.ini", gIni->data_basepath, gIni->level.style, gIni->level.style);
	res=loadIni(ini_style, filepath, gIni);
	if (res!=0) return res;


	return 0;
}


void unloadLevel(gameIni_t *gIni, gameRess_t *gRess) {
	//FIXME Not yet implemented
}

/* Load all textures needed for style from gIni */
int loadStyleRes(SDL_Renderer *rend, gameIni_t *gIni, gameRess_t *gRess) {
	int i, res;
	char filepath[MAX_PATH_LEN+1];
	char *stylename=gIni->level.style;

	for(i=0; i < gIni->style.tiles; i++) {
		(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%s_%d.gif", gIni->data_basepath, stylename, stylename, i);
		res=loadSurface(rend,filepath,&gRess->tiles[i]);
		if (res != 0) return res;
	}

	for(i=0; i < gIni->style.objectCount; i++) {
		(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%so_%d.gif", gIni->data_basepath, stylename, stylename, i);
		res=loadSprite(rend,filepath,gIni->style.frames[i],&gRess->objects[i]);
		if (res != 0) return res;

		/* Some object types needs a mask */
		if ( gIni->style.type[i] >= 5 && gIni->style.type[i] <= 8 ) {
			(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/style/%s/%som_%d.gif", gIni->data_basepath, stylename, stylename, i);
			res=loadSurface(rend,filepath,&gRess->objectMasks[i]);
			if (res != 0) return res;
		}
	}
	return 0;
}


void unloadStyleRes(gameRess_t *gRess) {
	int i;
	
	/* Nullpointer checked for surface but not for textures in SDL code */
	for(i=0; i<MAX_TILES_COUNT;i++) {
		DESTROYSURFACE_SAFE(gRess->tiles[i]);
	}
	for(i=0; i<MAX_OBJECTS_COUNT;i++) {
		DESTROYTEXTURE_SAFE(gRess->objects[i].t);
		DESTROYSURFACE_SAFE(gRess->objectMasks[i]);
	}
}

/* Load all misc textures (lemmings, fonts, cursor...) */
int loadMiscRes(SDL_Renderer *rend, gameIni_t *gIni, gameRess_t *gRess) {

	int lemmanim_frames[MAX_LEMMANIM_COUNT] = {8,4,8,8,10,16,16,16,14,8,16,16,8,16,32,24,4};
	int lemmanim_hasmask[6] = {6,10,11,13,14,15};
	int lemmanim_hasimask[3] = {13,14,15};

	char filepath[MAX_PATH_LEN+1];
	int i,ii,res;

	for(i=0; i<MAX_LEMMANIM_COUNT; i++) {
		(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemm_%d.gif", gIni->data_basepath, i);
		res=loadSprite(rend,filepath,lemmanim_frames[i],&gRess->lemmingAnims[i]);
		if (res != 0) return res;
	}

	for(ii=0; ii<6; ii++) {
		i=lemmanim_hasmask[ii];
		(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/mask_%d.gif", gIni->data_basepath, i);
		res=loadSurface(rend,filepath,&gRess->lemmingMasks[i]);
		if (res != 0) return res;
	}

	for(ii=0; ii<3; ii++) {
		i=lemmanim_hasimask[ii];
		(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/imask_%d.gif", gIni->data_basepath, i);
		res=loadSurface(rend,filepath,&gRess->lemmingIMasks[i]);
		if (res != 0) return res;
	}

	(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/cursor.gif", gIni->data_basepath);
	res=loadSprite(rend,filepath,8,&gRess->cursor);
	if (res != 0) return res;

	(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/countdown.gif", gIni->data_basepath);
	res=loadSprite(rend,filepath,5,&gRess->countdown);
	if (res != 0) return res;

	(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemmfont.gif", gIni->data_basepath);
	res=loadSprite(rend,filepath,94,&gRess->font1);
	if (res != 0) return res;
	
	(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/lemmfont2.gif", gIni->data_basepath);
	res=loadSprite(rend,filepath,94,&gRess->font2);
	if (res != 0) return res;

	return 0; 
}

void unloadMiscRes(gameRess_t *gRess) {
	int i;
	for(i=0; i<MAX_LEMMANIM_COUNT; i++) {
		DESTROYTEXTURE_SAFE(gRess->lemmingAnims[i].t);
		DESTROYSURFACE_SAFE(gRess->lemmingMasks[i]);
		DESTROYSURFACE_SAFE(gRess->lemmingIMasks[i]);
	}
	DESTROYTEXTURE_SAFE(gRess->cursor.t);
	DESTROYTEXTURE_SAFE(gRess->countdown.t);
	DESTROYTEXTURE_SAFE(gRess->font1.t);
	DESTROYTEXTURE_SAFE(gRess->font2.t);
}

/* frames is for animated sprites and is the number of images. Assumed to be all in a column */
int loadSprite(SDL_Renderer *rend, char *giffilepath, int frames, sprite_t *sprite) {
	int res;
	SDL_Surface *surf;

	MPL_CHECK(
			frames > 0,
			{return 1;},
			SDL_LOG_PRIORITY_WARN,
			"loadSprite(rend, \"%s\", frames, sprite) failed : invalid frames value %i",
			giffilepath, frames
		 );
	
	res = loadSurface(rend, giffilepath, &surf);
	if (res !=0) return res;

	sprite->t = SDL_CreateTextureFromSurface(rend, surf);
	MPL_CHECK(
			sprite->t,
			{SDL_FreeSurface(surf); return 4;},
			SDL_LOG_PRIORITY_WARN,
			"loadSprite(rend, \"%s\", frames, sprite) failed : can't convert surface to texture", giffilepath
		 );

	sprite->size.x=0;
	sprite->size.y=0;
	sprite->size.w=surf->w;
	sprite->size.h=surf->h / frames;
	sprite->frames = frames;

	/* Surface no longer useful (everything was copied in the texture) */
	SDL_FreeSurface(surf);
	return 0;
}

int loadSurface(SDL_Renderer *rend, char *giffilepath, SDL_Surface **surf) {
	SDL_RWops *rwop;
	//int res;

	rwop = SDL_RWFromFile(giffilepath,"r");
	MPL_CHECK(
			rwop,
			{return 2;},
			SDL_LOG_PRIORITY_WARN,
			"loadSurface(rend, \"%s\", frames, sprite) failed : problem opening file", giffilepath
		 );
	*surf = IMG_LoadGIF_RW(rwop);
	SDL_RWclose(rwop);

	MPL_CHECK(
			*surf,
			{return 3;},
			SDL_LOG_PRIORITY_WARN,
			"loadSurface(rend, \"%s\", frames, sprite) failed : can't decode file", giffilepath
		 );

	*surf = SDL_ConvertSurfaceFormat(*surf, SDL_PIXELFORMAT_ARGB8888, 0);
	// TODO : conversion en fonction du renderer ou bien systématiquement en ARGB8888, a voir.
	return 0;
}