1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include "SDL.h" |
---|
4 | #include "SDL_mixer.h" |
---|
5 | |
---|
6 | /* Mix_Chunk is like Mix_Music, only it's for ordinary sounds. */ |
---|
7 | Mix_Chunk *phaser = NULL; |
---|
8 | |
---|
9 | /* Every sound that gets played is assigned to a channel. Note that |
---|
10 | this is different from the number of channels you request when you |
---|
11 | open the audio device; a channel in SDL_mixer holds information |
---|
12 | about a sound sample that is playing, while the number of channels |
---|
13 | you request when opening the device is dependant on what sort of |
---|
14 | sound you want (1 channel = mono, 2 = stereo, etc) */ |
---|
15 | int phaserChannel = -1; |
---|
16 | |
---|
17 | void handleKey(SDL_KeyboardEvent key); |
---|
18 | |
---|
19 | int main(void) { |
---|
20 | |
---|
21 | SDL_Surface *screen; |
---|
22 | SDL_Event event; |
---|
23 | int done = 0; |
---|
24 | |
---|
25 | /* Same setup as before */ |
---|
26 | int audio_rate = 22050; |
---|
27 | Uint16 audio_format = AUDIO_S16; |
---|
28 | int audio_channels = 2; |
---|
29 | int audio_buffers = 4096; |
---|
30 | |
---|
31 | SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); |
---|
32 | |
---|
33 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { |
---|
34 | printf("Unable to open audio!\n"); |
---|
35 | exit(1); |
---|
36 | } |
---|
37 | |
---|
38 | /* We're going to pre-load the sound effects that we need right here |
---|
39 | */ |
---|
40 | phaser = Mix_LoadWAV("phaser.wav"); |
---|
41 | |
---|
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 | SDL_Delay(50); |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | Mix_CloseAudio(); |
---|
61 | SDL_Quit(); |
---|
62 | |
---|
63 | } |
---|
64 | |
---|
65 | void handleKey(SDL_KeyboardEvent key) { |
---|
66 | switch(key.keysym.sym) { |
---|
67 | case SDLK_p: |
---|
68 | |
---|
69 | /* We're going to have the phaser continually fire as long as |
---|
70 | the user is holding the button down */ |
---|
71 | if(key.type == SDL_KEYDOWN) { |
---|
72 | |
---|
73 | if(phaserChannel < 0) { |
---|
74 | |
---|
75 | /* Mix_PlayChannel takes, as its arguments, the channel that |
---|
76 | the given sound should be played on, the sound itself, and |
---|
77 | the number of times it should be looped. If you don't care |
---|
78 | what channel the sound plays on, just pass in -1. Looping |
---|
79 | works like Mix_PlayMusic. This function returns the channel |
---|
80 | that the sound was assigned to, which you'll need later. */ |
---|
81 | phaserChannel = Mix_PlayChannel(-1, phaser, -1); |
---|
82 | |
---|
83 | } |
---|
84 | } else { |
---|
85 | /* Mix_HaltChannel stops a certain channel from playing - this |
---|
86 | is one of the reasons we kept track of which channel the |
---|
87 | phaser had been assigned to */ |
---|
88 | Mix_HaltChannel(phaserChannel); |
---|
89 | |
---|
90 | phaserChannel = -1; |
---|
91 | } |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|