FPS Limiting for the main menu display code
[tennix.git] / sound.c
blobf5f142011a33b91fda94704507fee69475e6fef7
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(ground1),
34 RESOURCE(ground2),
35 RESOURCE(audience),
36 RESOURCE(applause),
37 RESOURCE(out),
38 RESOURCE(background),
39 RESOURCE(mouseover),
40 RESOURCE(mouseclick)
43 void init_sound() {
44 int i;
45 Mix_Chunk* data;
47 if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 1024) < 0) {
48 fprintf( stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
51 sounds = (Sound*)calloc(SOUND_MAX, sizeof(Sound));
53 for( i=0; i<SOUND_MAX; i++) {
54 data = Mix_LoadWAV_RW(SDL_RWFromConstMem(samples[i].data, samples[i].size), 1);
55 if( !data) {
56 fprintf( stderr, "Error: %s\n", SDL_GetError());
57 continue;
60 sounds[i].data = data;
65 void play_sample_n(sound_id id, int n)
67 if (id >= SOUND_MAX) {
68 fprintf(stderr, "Cannot play sound #%d.\n", id);
69 return;
72 if (n == -2) {
73 Mix_FadeInChannel(id%8, sounds[id].data, -1, FADE_IN_MS);
74 } else {
75 Mix_PlayChannel(id%8, sounds[id].data, n);
78 if (id == SOUND_RACKET) {
79 stop_sample(SOUND_APPLAUSE);
83 void stop_sample(sound_id id)
85 Mix_FadeOutChannel(id%8, FADE_OUT_MS);
88 void pan_sample(sound_id id, float position)
90 if (position == 0.5) {
91 Mix_SetPanning(id%8, 255, 255);
93 else {
94 Mix_SetPanning(id%8, 255*(1.0-position), 255*(position));