Last change
on this file since 3119 was
2777,
checked in by simon, 20 years ago
|
/branches/sound: brought up sound_control.cc which doesn't do anything at the moment.
|
File size:
1.6 KB
|
Line | |
---|
1 | |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include "SDL.h" |
---|
5 | #include "SDL_mixer.h" |
---|
6 | |
---|
7 | Mix_Chunk *phaser = NULL; |
---|
8 | Mix_Music *music = NULL; |
---|
9 | |
---|
10 | int phaserChannel = -1; |
---|
11 | |
---|
12 | void handleKey(SDL_KeyboardEvent key); |
---|
13 | void musicDone(); |
---|
14 | |
---|
15 | int main(void) { |
---|
16 | |
---|
17 | SDL_Surface *screen; |
---|
18 | SDL_Event event; |
---|
19 | int done = 0; |
---|
20 | |
---|
21 | /* Same setup as before */ |
---|
22 | int audio_rate = 22050; |
---|
23 | Uint16 audio_format = AUDIO_S16; |
---|
24 | int audio_channels = 2; |
---|
25 | int audio_buffers = 4096; |
---|
26 | |
---|
27 | SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); |
---|
28 | |
---|
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 | /* Pre-load sound effects */ |
---|
35 | phaser = Mix_LoadWAV("phaser.wav"); |
---|
36 | |
---|
37 | screen = SDL_SetVideoMode(320, 240, 0, 0); |
---|
38 | |
---|
39 | while(!done) { |
---|
40 | while(SDL_PollEvent(&event)) { |
---|
41 | switch(event.type) { |
---|
42 | case SDL_QUIT: |
---|
43 | done = 1; |
---|
44 | break; |
---|
45 | case SDL_KEYDOWN: |
---|
46 | case SDL_KEYUP: |
---|
47 | handleKey(event.key); |
---|
48 | break; |
---|
49 | } |
---|
50 | } |
---|
51 | SDL_Delay(50); |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | Mix_CloseAudio(); |
---|
56 | SDL_Quit(); |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | void handleKey(SDL_KeyboardEvent key) { |
---|
61 | switch(key.keysym.sym) { |
---|
62 | case SDLK_p: |
---|
63 | |
---|
64 | if(key.type == SDL_KEYDOWN) { |
---|
65 | |
---|
66 | if(phaserChannel < 0) { |
---|
67 | phaserChannel = Mix_PlayChannel(-1, phaser, -1); |
---|
68 | } |
---|
69 | } else { |
---|
70 | Mix_HaltChannel(phaserChannel); |
---|
71 | |
---|
72 | phaserChannel = -1; |
---|
73 | } |
---|
74 | break; |
---|
75 | case SDLK_m: |
---|
76 | if(key.state == SDL_PRESSED) { |
---|
77 | |
---|
78 | if(music == NULL) { |
---|
79 | |
---|
80 | music = Mix_LoadMUS("music.ogg"); |
---|
81 | Mix_PlayMusic(music, 0); |
---|
82 | Mix_HookMusicFinished(musicDone); |
---|
83 | |
---|
84 | } else { |
---|
85 | Mix_HaltMusic(); |
---|
86 | Mix_FreeMusic(music); |
---|
87 | music = NULL; |
---|
88 | } |
---|
89 | } |
---|
90 | break; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void musicDone() { |
---|
95 | Mix_HaltMusic(); |
---|
96 | Mix_FreeMusic(music); |
---|
97 | music = NULL; |
---|
98 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.