[2777] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include "SDL.h" |
---|
| 4 | #include "SDL_mixer.h" |
---|
| 5 | |
---|
| 6 | /* Mix_Music actually holds the music information. */ |
---|
| 7 | Mix_Music *music = NULL; |
---|
| 8 | |
---|
| 9 | void handleKey(SDL_KeyboardEvent key); |
---|
| 10 | void musicDone(); |
---|
| 11 | |
---|
| 12 | int main(void) { |
---|
| 13 | |
---|
| 14 | SDL_Surface *screen; |
---|
| 15 | SDL_Event event; |
---|
| 16 | int done = 0; |
---|
| 17 | |
---|
| 18 | /* We're going to be requesting certain things from our audio |
---|
| 19 | device, so we set them up beforehand */ |
---|
| 20 | int audio_rate = 22050; |
---|
| 21 | Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ |
---|
| 22 | int audio_channels = 2; |
---|
| 23 | int audio_buffers = 4096; |
---|
| 24 | |
---|
| 25 | SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); |
---|
| 26 | |
---|
| 27 | /* This is where we open up our audio device. Mix_OpenAudio takes |
---|
| 28 | as its parameters the audio format we'd /like/ to have. */ |
---|
| 29 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { |
---|
| 30 | printf("Unable to open audio!\n"); |
---|
| 31 | exit(1); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /* If we actually care about what we got, we can ask here. In this |
---|
| 35 | program we don't, but I'm showing the function call here anyway |
---|
| 36 | in case we'd want to know later. */ |
---|
| 37 | Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); |
---|
| 38 | |
---|
| 39 | /* We're going to be using a window onscreen to register keypresses |
---|
| 40 | in. We don't really care what it has in it, since we're not |
---|
| 41 | doing graphics, so we'll just throw something up there. */ |
---|
| 42 | screen = SDL_SetVideoMode(320, 240, 0, 0); |
---|
| 43 | |
---|
| 44 | while(!done) { |
---|
| 45 | while(SDL_PollEvent(&event)) { |
---|
| 46 | switch(event.type) { |
---|
| 47 | case SDL_QUIT: |
---|
| 48 | done = 1; |
---|
| 49 | break; |
---|
| 50 | case SDL_KEYDOWN: |
---|
| 51 | case SDL_KEYUP: |
---|
| 52 | handleKey(event.key); |
---|
| 53 | break; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /* So we don't hog the CPU */ |
---|
| 58 | SDL_Delay(50); |
---|
| 59 | |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /* This is the cleaning up part */ |
---|
| 63 | Mix_CloseAudio(); |
---|
| 64 | SDL_Quit(); |
---|
| 65 | |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void handleKey(SDL_KeyboardEvent key) { |
---|
| 69 | switch(key.keysym.sym) { |
---|
| 70 | case SDLK_m: |
---|
| 71 | if(key.state == SDL_PRESSED) { |
---|
| 72 | |
---|
| 73 | /* Here we're going to have the 'm' key toggle the music on and |
---|
| 74 | off. When it's on, it'll be loaded and 'music' will point to |
---|
| 75 | something valid. If it's off, music will be NULL. */ |
---|
| 76 | |
---|
| 77 | if(music == NULL) { |
---|
| 78 | |
---|
| 79 | /* Actually loads up the music */ |
---|
| 80 | music = Mix_LoadMUS("music.ogg"); |
---|
| 81 | |
---|
| 82 | /* This begins playing the music - the first argument is a |
---|
| 83 | pointer to Mix_Music structure, and the second is how many |
---|
| 84 | times you want it to loop (use -1 for infinite, and 0 to |
---|
| 85 | have it just play once) */ |
---|
| 86 | Mix_PlayMusic(music, 0); |
---|
| 87 | |
---|
| 88 | /* We want to know when our music has stopped playing so we |
---|
| 89 | can free it up and set 'music' back to NULL. SDL_Mixer |
---|
| 90 | provides us with a callback routine we can use to do |
---|
| 91 | exactly that */ |
---|
| 92 | Mix_HookMusicFinished(musicDone); |
---|
| 93 | |
---|
| 94 | } else { |
---|
| 95 | /* Stop the music from playing */ |
---|
| 96 | Mix_HaltMusic(); |
---|
| 97 | |
---|
| 98 | /* Unload the music from memory, since we don't need it |
---|
| 99 | anymore */ |
---|
| 100 | Mix_FreeMusic(music); |
---|
| 101 | |
---|
| 102 | music = NULL; |
---|
| 103 | } |
---|
| 104 | break; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* This is the function that we told SDL_Mixer to call when the music |
---|
| 110 | was finished. In our case, we're going to simply unload the music |
---|
| 111 | as though the player wanted it stopped. In other applications, a |
---|
| 112 | different music file might be loaded and played. */ |
---|
| 113 | void musicDone() { |
---|
| 114 | Mix_HaltMusic(); |
---|
| 115 | Mix_FreeMusic(music); |
---|
| 116 | music = NULL; |
---|
| 117 | } |
---|