1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <stdarg.h> |
---|
4 | #include <string.h> |
---|
5 | |
---|
6 | #include "SDL.h" |
---|
7 | #include "SDL_mixer.h" |
---|
8 | |
---|
9 | Mix_Chunk* blaster = NULL; |
---|
10 | Mix_Chunk* r2 = NULL; |
---|
11 | Mix_Chunk* sabre = NULL; |
---|
12 | Mix_Music* music = NULL; |
---|
13 | |
---|
14 | int bChannel = -1; |
---|
15 | int rChannel = -1; |
---|
16 | int sChannel = -1; |
---|
17 | |
---|
18 | SDL_Event event; |
---|
19 | |
---|
20 | int volume = SDL_MIX_MAXVOLUME; |
---|
21 | int track_number = 1; |
---|
22 | int done = 0; |
---|
23 | |
---|
24 | void handleKey(SDL_KeyboardEvent key); |
---|
25 | void musicDone(); |
---|
26 | |
---|
27 | int main(int argc, char *argv[]) { |
---|
28 | |
---|
29 | SDL_Surface *screen; |
---|
30 | |
---|
31 | |
---|
32 | //setup parameters |
---|
33 | int audio_rate = 44100, audio_channels = 2, audio_buffers = 4096, bits = 0; |
---|
34 | Uint16 audio_format = AUDIO_S16; |
---|
35 | |
---|
36 | //initializing sound and calling Mix_OpenAudio |
---|
37 | if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)<0){ |
---|
38 | printf("Unable to init sound or video!\n"); |
---|
39 | exit(1); |
---|
40 | } |
---|
41 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ |
---|
42 | printf("Unable to open audio!\n"); |
---|
43 | exit(1); |
---|
44 | } |
---|
45 | |
---|
46 | // Print some info |
---|
47 | Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); |
---|
48 | bits=audio_format&0xFF; |
---|
49 | printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, |
---|
50 | bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); |
---|
51 | |
---|
52 | // Pre-load sound effects |
---|
53 | blaster = Mix_LoadWAV("blaster.wav"); |
---|
54 | r2 = Mix_LoadWAV("artoo.wav"); |
---|
55 | sabre = Mix_LoadWAV("sabre_on.wav"); |
---|
56 | |
---|
57 | Mix_VolumeMusic(volume); |
---|
58 | |
---|
59 | //screen buildup |
---|
60 | screen = SDL_SetVideoMode(320, 240, 0, 0); |
---|
61 | |
---|
62 | while(!done) { |
---|
63 | //possible extension to looping with: if(Mix_PlayMusic(music, 1)==-1){} |
---|
64 | while(SDL_PollEvent(&event)) { |
---|
65 | switch(event.type){ |
---|
66 | case SDL_QUIT: |
---|
67 | done = 1; |
---|
68 | break; |
---|
69 | case SDL_KEYDOWN: |
---|
70 | case SDL_KEYUP: |
---|
71 | handleKey(event.key); |
---|
72 | break; |
---|
73 | default: |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | SDL_Delay(50); |
---|
78 | } |
---|
79 | Mix_CloseAudio(); |
---|
80 | SDL_Quit(); |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | void handleKey(SDL_KeyboardEvent key) { |
---|
85 | switch(event.key.keysym.sym){ |
---|
86 | case SDLK_ESCAPE: |
---|
87 | done = 1; |
---|
88 | break; |
---|
89 | case SDLK_q: |
---|
90 | done = 1; |
---|
91 | break; |
---|
92 | case SDLK_LEFT: |
---|
93 | Mix_RewindMusic(); |
---|
94 | break; |
---|
95 | case SDLK_RIGHT: |
---|
96 | switch(Mix_GetMusicType(NULL)){ |
---|
97 | case MUS_MP3: |
---|
98 | Mix_SetMusicPosition(+5); |
---|
99 | break; |
---|
100 | default: |
---|
101 | printf("no forwarding with this type of music.\n"); |
---|
102 | break; |
---|
103 | } |
---|
104 | break; |
---|
105 | case SDLK_UP: |
---|
106 | volume = (volume + 1) << 1; |
---|
107 | if(volume > SDL_MIX_MAXVOLUME) |
---|
108 | volume = SDL_MIX_MAXVOLUME; |
---|
109 | Mix_VolumeMusic(volume); |
---|
110 | break; |
---|
111 | case SDLK_DOWN: |
---|
112 | volume >>= 1; |
---|
113 | Mix_VolumeMusic(volume); |
---|
114 | break; |
---|
115 | case SDLK_SPACE: |
---|
116 | if(Mix_PausedMusic()) |
---|
117 | Mix_ResumeMusic(); |
---|
118 | else |
---|
119 | Mix_PauseMusic(); |
---|
120 | break; |
---|
121 | case SDLK_r: |
---|
122 | if(event.key.type == SDL_KEYDOWN) { |
---|
123 | if(rChannel < 0) { |
---|
124 | rChannel = Mix_PlayChannel(-1, r2, -1); |
---|
125 | } |
---|
126 | } else { |
---|
127 | Mix_HaltChannel(rChannel); |
---|
128 | rChannel = -1; |
---|
129 | } |
---|
130 | break; |
---|
131 | case SDLK_b: |
---|
132 | if(event.key.type == SDL_KEYDOWN) { |
---|
133 | if(bChannel < 0) { |
---|
134 | bChannel = Mix_PlayChannel(-1, blaster, -1); |
---|
135 | } |
---|
136 | } else { |
---|
137 | Mix_HaltChannel(bChannel); |
---|
138 | bChannel = -1; |
---|
139 | } |
---|
140 | break; |
---|
141 | case SDLK_s: |
---|
142 | if(event.key.type == SDL_KEYDOWN) { |
---|
143 | if(sChannel < 0) { |
---|
144 | sChannel = Mix_PlayChannel(-1, sabre, -1); |
---|
145 | } |
---|
146 | } else { |
---|
147 | Mix_HaltChannel(bChannel); |
---|
148 | sChannel = -1; |
---|
149 | } |
---|
150 | break; |
---|
151 | case SDLK_m: |
---|
152 | if(event.key.state == SDL_PRESSED) { |
---|
153 | if(music == NULL) { |
---|
154 | switch (track_number){ |
---|
155 | case 1: |
---|
156 | music = Mix_LoadMUS("luke_grey_orxonox1.ogg"); |
---|
157 | Mix_PlayMusic(music, 0); |
---|
158 | Mix_HookMusicFinished(musicDone); |
---|
159 | track_number++; |
---|
160 | break; |
---|
161 | case 2: |
---|
162 | music = Mix_LoadMUS("luke_grey_orxonox2.ogg"); |
---|
163 | Mix_PlayMusic(music, 0); |
---|
164 | Mix_HookMusicFinished(musicDone); |
---|
165 | track_number++; |
---|
166 | break; |
---|
167 | case 3: |
---|
168 | music = Mix_LoadMUS("luke_grey_orxonox3.ogg"); |
---|
169 | Mix_PlayMusic(music, 0); |
---|
170 | Mix_HookMusicFinished(musicDone); |
---|
171 | track_number++; |
---|
172 | break; |
---|
173 | case 4: |
---|
174 | music = Mix_LoadMUS("luke_grey_and aquarius_orxonox.ogg"); |
---|
175 | Mix_PlayMusic(music, 0); |
---|
176 | Mix_HookMusicFinished(musicDone); |
---|
177 | track_number++; |
---|
178 | break; |
---|
179 | case 5: |
---|
180 | music = Mix_LoadMUS("nomenes_orxonox.ogg"); |
---|
181 | Mix_PlayMusic(music, 0); |
---|
182 | Mix_HookMusicFinished(musicDone); |
---|
183 | track_number = 1; |
---|
184 | break; |
---|
185 | } |
---|
186 | } else { |
---|
187 | Mix_HaltMusic(); |
---|
188 | Mix_FreeMusic(music); |
---|
189 | music = NULL; |
---|
190 | } |
---|
191 | } |
---|
192 | break; |
---|
193 | default: |
---|
194 | break; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | void musicDone() { |
---|
199 | Mix_HaltMusic(); |
---|
200 | Mix_FreeMusic(music); |
---|
201 | music = NULL; |
---|
202 | } |
---|