summaryrefslogtreecommitdiff
path: root/src/loader.c
blob: b2ea9bf0be00865192beee3b84299ee5de9f5643 (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
#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 loadStyleRes(SDL_Renderer *rend, gameIni_t *gIni, char data_basepath[], 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", 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", 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", data_basepath, stylename, stylename, i);
			res=loadSprite(rend,filepath,1,&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++) {
		SDL_FreeSurface(gRess->tiles[i]);
		gRess->tiles[i] = NULL;
	}
	for(i=0; i<MAX_OBJECTS_COUNT;i++) {
		DESTROYTEXTURE_SAFE(gRess->objects[i].t);
		DESTROYTEXTURE_SAFE(gRess->objectMasks[i].t);
	}
}

int loadMiscRes(SDL_Renderer *rend, char data_basepath[], 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", 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", data_basepath, i);
		res=loadSprite(rend,filepath,1,&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", data_basepath, i);
		res=loadSprite(rend,filepath,1,&gRess->lemmingIMasks[i]);
		if (res != 0) return res;
	}

	(void) SDL_snprintf(filepath, MAX_PATH_LEN, "%s/misc/cursor.gif", 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", 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", 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", 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);
		DESTROYTEXTURE_SAFE(gRess->lemmingMasks[i].t);
		DESTROYTEXTURE_SAFE(gRess->lemmingIMasks[i].t);
	}
	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
		 );

	// TODO : conversion en fonction du renderer ou bien systématiquement en ARGB8888, a voir.
	return 0;
}