diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2010-09-17 20:35:58 +0000 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2010-09-17 20:35:58 +0000 |
commit | 1a759599045dab619b1e0f7a72ab852eb1ca3445 (patch) | |
tree | 7cc5c89a24cb0636a957e0fc928b821db02765d7 /sdl-test/music_and_sound | |
parent | f3d32ab694109eade818ba692fd6c4c1173c4db8 (diff) | |
download | 2010-netlemmings-1a759599045dab619b1e0f7a72ab852eb1ca3445.tar.gz 2010-netlemmings-1a759599045dab619b1e0f7a72ab852eb1ca3445.tar.bz2 2010-netlemmings-1a759599045dab619b1e0f7a72ab852eb1ca3445.zip |
Déplacement du test de muisque et sons dans un sous dossier pour ranger un peu
git-svn-id: file:///var/svn/2010-netlemmings/trunk@26 077b3477-7977-48bd-8428-443f22f7bfda
Diffstat (limited to 'sdl-test/music_and_sound')
-rwxr-xr-x | sdl-test/music_and_sound/compil.sh | 2 | ||||
-rw-r--r-- | sdl-test/music_and_sound/main.c | 335 |
2 files changed, 337 insertions, 0 deletions
diff --git a/sdl-test/music_and_sound/compil.sh b/sdl-test/music_and_sound/compil.sh new file mode 100755 index 0000000..21cb9a3 --- /dev/null +++ b/sdl-test/music_and_sound/compil.sh @@ -0,0 +1,2 @@ +#/bin/sh +gcc -I/usr/include/SDL -lSDL_mixer -lSDL_image -g -o main main.c diff --git a/sdl-test/music_and_sound/main.c b/sdl-test/music_and_sound/main.c new file mode 100644 index 0000000..b769bb2 --- /dev/null +++ b/sdl-test/music_and_sound/main.c @@ -0,0 +1,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); +} + |