#include #include #include #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;isrc = 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; iw; 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); }