Code cleanups so this may actually work cross-platfor
[crack-attack.git] / src / Sound.cxx
blobb6e059af340a0d7d29d4dd270b92db473a8d9892
1 /*
2 * Sound.cxx
3 * Miguel Ángel Vilela García - 8/29/03
5 * Copyright (C) 2003 Miguel Ángel Vilela García
6 * Copyright (C) 2005 See COPYRIGHT
7 * Crack Attack! is the legal property of its developers, whose names
8 * are too numerous to list here. Please refer to the COPYRIGHT file
9 * distributed with this source distribution for a full listing.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Miguel Ángel Vilela García - www.miguev.net
28 #include "TextureLoader.h"
29 #include "Random.h"
30 #include "Sound.h"
31 #include <SDL/SDL.h>
32 #include <SDL/SDL_mixer.h>
34 using namespace std;
36 typedef map <string,Mix_Chunk*> ChunkMap;
37 vector<string> sounds;
38 string sound_dirname;
39 ChunkMap chunks;
41 int audio_rate = 22050;
42 int audio_channels = 2;
43 int audio_buffers = 1024; /* small enough buffer to get synchronized sound */
44 Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
45 int has_audio_available = 0;
47 void Sound::initialize( void )
49 SDL_Init( SDL_INIT_AUDIO );
50 if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
51 cout << "WARNING *** Unable to open audio device!" << endl;
52 return;
54 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
55 // Initialize some global variables
56 has_audio_available = 1;
57 // Load chunks filenames
58 sounds.push_back (GC_SOUND_COUNTDOWN);
59 sounds.push_back (GC_SOUND_BLOCK_FALLEN);
60 sounds.push_back (GC_SOUND_BLOCK_AWAKING);
61 sounds.push_back (GC_SOUND_BLOCK_DYING);
62 sounds.push_back (GC_SOUND_GARBAGE_FALLEN);
63 sounds.push_back (GC_SOUND_GARBAGE_SHATTERING);
64 // Load chunk files to ChunkMap
65 Mix_Chunk *chunk;
66 string File;
67 for (int i = 0; i < sounds.size(); i++) {
68 #ifndef NDEBUG
69 cout << "Loading " << sounds[i];
70 #endif
71 // Try to load chunk in $HOME/.crack-attack/sounds/
72 char sound_dirname[256];
73 TextureLoader::buildLocalDataFileName("sounds/", sound_dirname);
74 File = sound_dirname + sounds[i];
75 chunk = Mix_LoadWAV (File.c_str());
76 if ( chunk == NULL ) { // Try to load chunk in DATA_DIRECTORY/sounds/
77 char *another_dir = GC_DATA_DIRECTORY("sounds/");
78 File = another_dir + sounds[i];
79 chunk = Mix_LoadWAV (File.c_str());
81 // If chunk is NULL there is no WAV available for this sound
82 if (!chunk) cout << "WARNING *** Unable to open " << sounds[i] << endl;
83 chunks[sounds[i]] = chunk;
84 #ifndef NDEBUG
85 cout << endl;
86 #endif
90 void Sound::play( const char *file, int vol )
92 if (!has_audio_available || !chunks[file])
93 return;
95 int channel, i;
97 if (vol > 10) vol = 10;
98 #ifndef NDEBUG
99 cout << "Playing sound: " << File.c_str() << endl;
100 #endif
101 // string File (file);
102 Mix_VolumeChunk( chunks[file], vol * MIX_MAX_VOLUME / 10 );
103 channel = Mix_PlayChannel( -1, chunks[file], 0 );
106 int Sound::audio_available( )
108 return has_audio_available;
111 void Sound::cleanup( )
113 if (!has_audio_available)
114 return;
116 Mix_CloseAudio();
117 SDL_Quit();