Correct sounds (enable the racket/ground sounds)
[tennix.git] / sound.c
blob7ce9d42e306c2f7284c8478115061ef0e3645576
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"
27 static Sound* sounds;
29 #include "data/sounds_data.c"
30 static const ResourceData samples[] = {
31 RESOURCE(racket1),
32 RESOURCE(racket2),
33 RESOURCE(rain),
34 RESOURCE(ground1),
35 RESOURCE(ground2),
36 RESOURCE(audience),
37 RESOURCE(applause),
38 RESOURCE(out),
39 RESOURCE(background),
40 RESOURCE(mouseover),
41 RESOURCE(mouseclick)
44 void init_sound() {
45 int i;
46 Mix_Chunk* data;
48 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
49 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
52 sounds = (Sound*)calloc(SOUND_MAX, sizeof(Sound));
54 for( i=0; i<SOUND_MAX; i++) {
55 data = Mix_LoadWAV_RW(SDL_RWFromConstMem(samples[i].data, samples[i].size), 1);
56 if( !data) {
57 fprintf( stderr, "Error: %s\n", SDL_GetError());
58 continue;
61 sounds[i].data = data;
66 void play_sample_n(sound_id id, int n)
68 if (id >= SOUND_MAX) {
69 fprintf(stderr, "Cannot play sound #%d.\n", id);
70 return;
73 if (n == -2) {
74 Mix_FadeInChannel(id%8, sounds[id].data, -1, FADE_IN_MS);
75 } else {
76 Mix_PlayChannel(id%8, sounds[id].data, n);
79 /* Audience stops clapping when ball is served */
80 if (id >= SOUND_RACKET_FIRST && id <= SOUND_RACKET_LAST) {
81 stop_sample(SOUND_APPLAUSE);
85 void stop_sample(sound_id id)
87 Mix_FadeOutChannel(id%8, FADE_OUT_MS);
90 void pan_sample(sound_id id, float position)
92 if (position == 0.5) {
93 Mix_SetPanning(id%8, 255, 255);
95 else {
96 Mix_SetPanning(id%8, 255*(1.0-position), 255*(position));