summaryrefslogtreecommitdiff
path: root/sdl-test/music_and_sound/main.c
blob: b769bb2b3e888a83e31885f9103cfe1aa544ca62 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"

#define RESS_PATH "../"
#define	FPS 12
#define	NUM_WAVES 24
#define	NUM_MIDIS 27 

char midi_names[NUM_MIDIS][8] = {
"awesome","beastii","beasti","cancan","doggie","lemming1","lemming2","lemming3",
"menace","mountain","tenlemms","tim1","tim2","tim3","tim4","tim5","tim6","tim7",
"tim8","tim9","tim10","tune1","tune2","tune3","tune4","tune5","tune6"
};
char sound_names[NUM_WAVES][8] = {
"BANG","CHAIN","CHANGEOP","CHINK","DIE","DOOR","ELECTRIC","EXPLODE",
"FIRE","GLUG","LETSGO","MANTRAP","MOUSEPRE","OHNO","OING","SCRAPE",
"SLICER","SPLASH","SPLAT","TENTON","THUD","THUNK","TING","YIPPEE"
};

SDL_Surface *screen;
SDL_Surface *background;
/*
#define MAX_UPDATES	3*(1+MAX_SHOTS+MAX_ALIENS)
int numupdates;
SDL_Rect srcupdate[MAX_UPDATES];
SDL_Rect dstupdate[MAX_UPDATES];
struct blit {
	SDL_Surface *src;
	SDL_Rect *srcrect;
	SDL_Rect *dstrect;
} blits[MAX_UPDATES];
*/

Mix_Music *music;
Mix_Chunk *sounds[NUM_WAVES];


SDL_Surface *LoadImage(char *datafile, int transparent) {
	SDL_Surface *image, *surface;

	image = IMG_Load(datafile);
	if ( image == NULL ) {
		fprintf(stderr, "Couldn't load image %s: %s\n",
					datafile, IMG_GetError());
		return(NULL);
	}
	if ( transparent ) {
		/* Assuming 8-bit BMP image */
		SDL_SetColorKey(image, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
						*(Uint8 *)image->pixels);
	}
	surface = SDL_DisplayFormat(image);
	SDL_FreeSurface(image);
	return(surface);
}

void path(char *filepath, char type[], char name[], char ext[]) {
	//XXX : ajouter une variable pour la taille de la chaine de sortie pour eviter les buffer overflow
	strcpy(filepath,RESS_PATH);
	strcat(filepath,type);
	strcat(filepath,"/");
	strcat(filepath,name);
	strcat(filepath,".");
	strcat(filepath,ext);
}


int LoadData(int musicnum, int theme)
{
	int i;
	char filepath[255];

	//SDL_Surface *firstShot;

	/* Load sounds */

	/* load support for the OGG and MOD sample/music formats
	int flags=MIX_INIT_OGG|MIX_INIT_MOD;
	int initted=Mix_Init(flags);
	if(initted&flags != flags) {
	    printf("Mix_Init: Failed to init required ogg and mod support!\n");
	    printf("Mix_Init: %s\n", Mix_GetError());
	    // TODO handle error
	}*/

	path(filepath,"music",midi_names[musicnum],"mod");
	music = Mix_LoadMUS(filepath);
	if ( music == NULL ) {
		fprintf(stderr, "Warning: Couldn't load music: %s\n",
							Mix_GetError());
	}
	
	for (i=0;i<NUM_WAVES;i++) {
		path(filepath,"sound",sound_names[i],"WAV");
		sounds[i] = Mix_LoadWAV(filepath);
		//FIXME : test retour

		//Iutile si Mix volume par Channel
		//Mix_VolumeChunk(sound[i],sfxvolume);
	}
	//Mix_Quit(); // Unload libs loaded with Mix_Init(...)

	/* Load graphics */
	background = LoadImage("../jeu-test/aliens-1.0.2/data/background.gif", 0);

	/* Set up the update rectangle pointers
	for ( i=0; i<MAX_UPDATES; ++i ) {
		blits[i].srcrect = &srcupdate[i];
		blits[i].dstrect = &dstupdate[i];
	}
	*/

	return 0;
}

void FreeData(void)
{
	int i;

	/* Free sounds */
	Mix_FreeMusic(music);
	for ( i=0; i<NUM_WAVES; ++i ) {
		Mix_FreeChunk(sounds[i]);
	}

	/* Free graphics */
	SDL_FreeSurface(background);
}
/*
void DrawObject(object *sprite)
{
	struct blit *update;

	update = &blits[numupdates++];
	update->src = sprite->image;
	update->srcrect->x = 0;
	update->srcrect->y = 0;
	update->srcrect->w = sprite->image->w;
	update->srcrect->h = sprite->image->h;
	update->dstrect->x = sprite->x;
	update->dstrect->y = sprite->y;
	update->dstrect->w = sprite->image->w;
	update->dstrect->h = sprite->image->h;
}
void EraseObject(object *sprite)
{
	struct blit *update;
	int wrap;

	// The background wraps horizontally across the screen
	update = &blits[numupdates++];
	update->src = background;
	update->srcrect->x = sprite->x%background->w;
	update->srcrect->y = sprite->y;
	update->srcrect->w = sprite->image->w;
	update->srcrect->h = sprite->image->h;
	wrap = (update->srcrect->x+update->srcrect->w)-(background->w);
	if ( wrap > 0 ) {
		update->srcrect->w -= wrap;
	}
	update->dstrect->x = sprite->x;
	update->dstrect->y = sprite->y;
	update->dstrect->w = update->srcrect->w;
	update->dstrect->h = update->srcrect->h;

	// Assuming sprites can only wrap across one background tile
	if ( wrap > 0 ) {
		update = &blits[numupdates++];
		update->src = background;
		update->srcrect->x = 0;
		update->srcrect->y = sprite->y;
		update->srcrect->w = wrap;
		update->srcrect->h = sprite->image->h;
		update->dstrect->x =((sprite->x/background->w)+1)*background->w;
		update->dstrect->y = sprite->y;
		update->dstrect->w = update->srcrect->w;
		update->dstrect->h = update->srcrect->h;
	}
}
void UpdateScreen(void)
{
	int i;

	for ( i=0; i<numupdates; ++i ) {
		SDL_LowerBlit(blits[i].src, blits[i].srcrect,
						screen, blits[i].dstrect);
	}
	SDL_UpdateRects(screen, numupdates, dstupdate);
	numupdates = 0;
}
*/

void WaitFrame(void)
{
	static Uint32 next_tick = 0;
	Uint32 this_tick;

	/* Wait for the next frame */
	this_tick = SDL_GetTicks(); 
	if ( this_tick < next_tick ) {
		SDL_Delay(next_tick-this_tick);
	}
	next_tick = this_tick + (1000/FPS);
}

/* This of course can be optimized :-) */
void RunGame(void)
{
	int i,j,k;
	SDL_Event event;
	Uint8 *keys;

	/* Paint the background */
	//numupdates = 0;
	for ( i=0; i<screen->w; i += background->w ) {
		SDL_Rect dst;

		dst.x = i;
		dst.y = 0;
		dst.w = background->w;
		dst.h = background->h;
		SDL_BlitSurface(background, NULL, screen, &dst);
	}
	SDL_UpdateRect(screen, 0, 0, 0, 0);

	/* Start the music */
	Mix_PlayMusic(music, -1);

	/* Initialize the objects */


	//UpdateScreen();

	while ( 1 ) {
		/* Wait for the next frame */
		WaitFrame();

		/* Poll input queue, run keyboard loop */
		while ( SDL_PollEvent(&event) ) {
			switch(event.type) {
				case SDL_QUIT:
					return;
				break;
				case SDL_KEYDOWN:
				case SDL_KEYUP:
					// TODO : procédure qui traite les events clavier et qui met à jour l'état du jeu
					switch( event.key.keysym.sym ){
						case SDLK_q:
							return;
						break;
						default:
						break;
					}
				default:
				break;
			}
		}


		//UpdateScreen();

	}

	Mix_PlayChannel(-1, sounds[NUM_WAVES-1], 0);

	while (Mix_Playing(-1)>0) {
		SDL_delay(1000);
	}

	Mix_FadeOutMusic(1000);
	SDL_delay(1000);

	Mix_HaltChannel(-1);
	return;
}

main(int argc, char *argv[]) {
	int res;

	/* Initialize the SDL library */
	if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		exit(2);
	}
	atexit(SDL_Quit);


	/* Open the audio device */
	//if ( Mix_OpenAudio(11025, AUDIO_U8, 1, 512) < 0 ) {
	if ( Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 512) < 0 ) {
		fprintf(stderr,
		"Warning: Couldn't Mix_OpenAudio: %s\n",
							SDL_GetError());
	}

 	

	// allocate 16 mixing channels at 80% volume
	Mix_AllocateChannels(16);
	Mix_Volume(-1,MIX_MAX_VOLUME*0.8);


	/* Open the display device */
	//FIXME : mettre la bonne résolution !
	screen = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
							SDL_GetError());
		exit(2);
	}

	/* Initialize the random number generator */
	srandom(time(NULL));

	/* Load the music and artwork */
	res=LoadData(random()%NUM_MIDIS,0);
	if ( res != 0 ) { exit(res); }

	/* Run the game */
	RunGame();

	/* Free the music and artwork */
	FreeData();

	/* Quit */

	Mix_CloseAudio();
	exit(0);
}