#include #include #include #include #include "SDL.h" #include "SDL_mixer.h" Mix_Chunk* blaster = NULL; Mix_Chunk* r2 = NULL; Mix_Chunk* sabre = NULL; Mix_Music* music = NULL; int bChannel = -1; int rChannel = -1; int sChannel = -1; SDL_Event event; int volume = SDL_MIX_MAXVOLUME; int track_number = 1; int done = 0; void handleKey(SDL_KeyboardEvent key); void musicDone(); int main(int argc, char *argv[]) { SDL_Surface *screen; //setup parameters int audio_rate = 44100, audio_channels = 2, audio_buffers = 4096, bits = 0; Uint16 audio_format = AUDIO_S16; //initializing sound and calling Mix_OpenAudio if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)<0){ printf("Unable to init sound or video!\n"); exit(1); } if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ printf("Unable to open audio!\n"); exit(1); } // Print some info Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); bits=audio_format&0xFF; printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); // Pre-load sound effects blaster = Mix_LoadWAV("blaster.wav"); r2 = Mix_LoadWAV("artoo.wav"); sabre = Mix_LoadWAV("sabre_on.wav"); Mix_VolumeMusic(volume); //screen buildup screen = SDL_SetVideoMode(320, 240, 0, 0); while(!done) { //possible extension to looping with: if(Mix_PlayMusic(music, 1)==-1){} while(SDL_PollEvent(&event)) { switch(event.type){ case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: case SDL_KEYUP: handleKey(event.key); break; default: break; } } SDL_Delay(50); } Mix_CloseAudio(); SDL_Quit(); return 0; } void handleKey(SDL_KeyboardEvent key) { switch(event.key.keysym.sym){ case SDLK_ESCAPE: done = 1; break; case SDLK_q: done = 1; break; case SDLK_LEFT: Mix_RewindMusic(); break; case SDLK_RIGHT: switch(Mix_GetMusicType(NULL)){ case MUS_MP3: Mix_SetMusicPosition(+5); break; default: printf("no forwarding with this type of music.\n"); break; } break; case SDLK_UP: volume = (volume + 1) << 1; if(volume > SDL_MIX_MAXVOLUME) volume = SDL_MIX_MAXVOLUME; Mix_VolumeMusic(volume); break; case SDLK_DOWN: volume >>= 1; Mix_VolumeMusic(volume); break; case SDLK_SPACE: if(Mix_PausedMusic()) Mix_ResumeMusic(); else Mix_PauseMusic(); break; case SDLK_r: if(event.key.type == SDL_KEYDOWN) { if(rChannel < 0) { rChannel = Mix_PlayChannel(-1, r2, -1); } } else { Mix_HaltChannel(rChannel); rChannel = -1; } break; case SDLK_b: if(event.key.type == SDL_KEYDOWN) { if(bChannel < 0) { bChannel = Mix_PlayChannel(-1, blaster, -1); } } else { Mix_HaltChannel(bChannel); bChannel = -1; } break; case SDLK_s: if(event.key.type == SDL_KEYDOWN) { if(sChannel < 0) { sChannel = Mix_PlayChannel(-1, sabre, -1); } } else { Mix_HaltChannel(bChannel); sChannel = -1; } break; case SDLK_m: if(event.key.state == SDL_PRESSED) { if(music == NULL) { switch (track_number){ case 1: music = Mix_LoadMUS("luke_grey_orxonox1.ogg"); Mix_PlayMusic(music, 0); Mix_HookMusicFinished(musicDone); track_number++; break; case 2: music = Mix_LoadMUS("luke_grey_orxonox2.ogg"); Mix_PlayMusic(music, 0); Mix_HookMusicFinished(musicDone); track_number++; break; case 3: music = Mix_LoadMUS("luke_grey_orxonox3.ogg"); Mix_PlayMusic(music, 0); Mix_HookMusicFinished(musicDone); track_number++; break; case 4: music = Mix_LoadMUS("luke_grey_and aquarius_orxonox.ogg"); Mix_PlayMusic(music, 0); Mix_HookMusicFinished(musicDone); track_number++; break; case 5: music = Mix_LoadMUS("nomenes_orxonox.ogg"); Mix_PlayMusic(music, 0); Mix_HookMusicFinished(musicDone); track_number = 1; break; } } else { Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; } } break; default: break; } } void musicDone() { Mix_HaltMusic(); Mix_FreeMusic(music); music = NULL; }