Only redraw game score strings when necessary
[tennix.git] / sound.c
blobdbbddfaa7040962d440c309604cf43f6b1b55287
2 /**
4 * Tennix! SDL Port
5 * Copyright (C) 2003, 2007, 2008 Thomas Perl <thp@perli.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
22 **/
24 #include "tennix.h"
25 #include "sound.h"
26 #include "archive.h"
27 #include "graphics.h"
29 #include <stdarg.h>
30 #include <stdlib.h>
32 static Sound* sounds;
34 static char* samples[] = {
35 "racket1.ogg",
36 "racket2.ogg",
37 "rain.ogg",
38 "ground1.ogg",
39 "ground2.ogg",
40 "audience.ogg",
41 "applause.ogg",
42 "out.ogg",
43 "background.ogg",
44 "mouseover.ogg",
45 "mouseclick.ogg",
47 #ifdef HAVE_VOICE_FILES
48 "to.ogg",
49 "all.ogg",
50 "love-in.ogg",
51 "love-out.ogg",
52 "fifteen-in.ogg",
53 "fifteen-out.ogg",
54 "thirty-in.ogg",
55 "thirty-out.ogg",
56 "forty-in.ogg",
57 "forty-out.ogg",
58 "deuce.ogg",
59 "advantage-player-one.ogg",
60 "advantage-player-two.ogg",
62 "zero-in.ogg",
63 "zero-out.ogg",
64 "one-in.ogg",
65 "one-out.ogg",
66 "two-in.ogg",
67 "two-out.ogg",
68 "three-in.ogg",
69 "three-out.ogg",
70 "four-in.ogg",
71 "four-out.ogg",
72 "five-in.ogg",
73 "five-out.ogg",
74 "six-in.ogg",
75 "six-out.ogg",
76 "seven-in.ogg",
77 "seven-out.ogg",
79 "in-the-first-set.ogg",
80 "in-the-second-set.ogg",
81 "in-the-third-set.ogg",
82 "in-the-fourth-set.ogg",
83 "in-the-fifth-set.ogg",
85 "quit-it1.ogg",
86 "quit-it2.ogg",
87 "quit-it3.ogg",
88 "quit-it4.ogg",
90 "new-game1.ogg",
91 "new-game2.ogg",
92 "new-game3.ogg",
93 "new-game4.ogg",
94 "new-game5.ogg",
95 "new-game6.ogg",
97 "lets-go1.ogg",
98 "lets-go2.ogg",
99 "lets-go3.ogg",
100 "lets-go4.ogg",
101 #endif
104 static sound_id voice_queue[VOICE_QUEUE_MAX];
105 static int voice_queue_size = 0;
106 static int voice_queue_position = 0;
108 int voice_finished_flag = 1;
110 void init_sound() {
111 int i;
112 char *d;
113 Mix_Chunk* data;
114 TennixArchive* tnxar;
116 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
117 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
120 sounds = (Sound*)calloc(SOUND_MAX, sizeof(Sound));
122 draw_button(40, (HEIGHT-40)/2, WIDTH-80, 40, 100, 100, 100, 1);
123 store_screen();
124 updatescr();
126 tnxar = tnxar_open(ARCHIVE_FILE);
128 for( i=0; i<SOUND_MAX; i++) {
129 if (tnxar_set_current_filename(tnxar, samples[i]) != 0) {
130 d = tnxar_read_current(tnxar);
131 data = Mix_LoadWAV_RW(SDL_RWFromMem(d, tnxar_size_current(tnxar)), 0);
132 free(d);
133 } else {
134 fprintf(stderr, "Cannot find %s\n", samples[i]);
135 exit(EXIT_FAILURE);
137 if( !data) {
138 fprintf( stderr, "Error: %s\n", SDL_GetError());
139 continue;
142 sounds[i].data = data;
144 draw_button(40, (HEIGHT-40)/2, (WIDTH-80)*(GR_COUNT+i)/(SOUND_MAX+GR_COUNT), 40, 100, 250, 100, 0);
145 rectangle(40+BUTTON_BORDER, (HEIGHT-40)/2+20, (WIDTH-80)*(GR_COUNT+i)/(SOUND_MAX+GR_COUNT)-2*BUTTON_BORDER, 10, 170, 250, 170);
146 updatescr();
149 tnxar_close(tnxar);
151 clear_screen();
152 store_screen();
153 updatescr();
155 /* for Voice Queue processing */
156 Mix_ChannelFinished(voice_channel_finished);
159 void play_sample_n(sound_id id, int n)
161 if (id >= SOUND_MAX) {
162 fprintf(stderr, "Cannot play sound #%d.\n", id);
163 return;
166 if (n == -2) {
167 Mix_FadeInChannel(CHANNEL_BY_ID(id), sounds[id].data, -1, FADE_IN_MS);
168 } else {
169 Mix_PlayChannel(CHANNEL_BY_ID(id), sounds[id].data, n);
172 /* Audience stops clapping when ball is served */
173 if (id >= SOUND_RACKET_FIRST && id <= SOUND_RACKET_LAST) {
174 stop_sample(SOUND_APPLAUSE);
178 void stop_sample(sound_id id)
180 Mix_FadeOutChannel(CHANNEL_BY_ID(id), FADE_OUT_MS);
183 void pan_sample(sound_id id, float position)
185 if (position == 0.5) {
186 Mix_SetPanning(CHANNEL_BY_ID(id), 255, 255);
188 else {
189 Mix_SetPanning(CHANNEL_BY_ID(id), 255*(1.0-position), 255*(position));
193 void voice_clear()
195 voice_queue_size = 0;
196 Mix_HaltChannel(CHANNEL_VOICE);
199 void voice_enqueue(sound_id id)
201 if (voice_queue_size < VOICE_QUEUE_MAX) {
202 voice_queue[voice_queue_size++] = id;
203 } else {
204 fprintf(stderr, "Voice queue overflow. Skipping: %d\n", id);
208 void voice_say()
210 if (voice_queue_size > 0) {
211 voice_queue_position = 0;
212 voice_finished_flag = 0;
213 Mix_PlayChannel(CHANNEL_VOICE, sounds[voice_queue[voice_queue_position]].data, 0);
214 voice_queue_position++;
218 void voice_say_list(int n, ...)
220 va_list ap;
221 int i;
222 sound_id id;
224 voice_clear();
226 va_start(ap, n);
227 for (i=0; i<n; i++) {
228 id = (sound_id)va_arg(ap, int);
229 voice_enqueue(id);
231 va_end(ap);
232 voice_say();
235 void voice_channel_finished(int channel)
237 if (channel == CHANNEL_VOICE) {
238 if (voice_queue_position < voice_queue_size) {
239 Mix_PlayChannel(CHANNEL_VOICE, sounds[voice_queue[voice_queue_position]].data, 0);
240 voice_queue_position++;
241 } else {
242 voice_finished_flag = 1;
243 voice_queue_position = 0;