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
30 #include "TextureLoader.h"
34 #include <SDL/SDL_mixer.h>
37 typedef map
<string
,Mix_Chunk
*> ChunkMap
;
38 vector
<string
> sounds
;
42 int audio_rate
= 22050;
43 int audio_channels
= 2;
44 int audio_buffers
= 1024; /* small enough buffer to get synchronized sound */
45 Uint16 audio_format
= AUDIO_S16
; /* 16-bit stereo */
46 int has_audio_available
= 0;
48 void Sound::initialize( void )
50 SDL_Init( SDL_INIT_AUDIO
);
51 if (Mix_OpenAudio(audio_rate
, audio_format
, audio_channels
, audio_buffers
)) {
52 cout
<< "WARNING *** Unable to open audio device!" << endl
;
55 Mix_QuerySpec(&audio_rate
, &audio_format
, &audio_channels
);
56 // Initialize some global variables
57 has_audio_available
= 1;
58 // Load chunks filenames
59 sounds
.push_back (GC_SOUND_COUNTDOWN
);
60 sounds
.push_back (GC_SOUND_BLOCK_FALLEN
);
61 sounds
.push_back (GC_SOUND_BLOCK_AWAKING
);
62 sounds
.push_back (GC_SOUND_BLOCK_DYING
);
63 sounds
.push_back (GC_SOUND_GARBAGE_FALLEN
);
64 sounds
.push_back (GC_SOUND_GARBAGE_SHATTERING
);
65 // Load chunk files to ChunkMap
68 for (size_t i
= 0; i
< sounds
.size(); i
++) {
70 cout
<< "Loading " << sounds
[i
];
72 // Try to load chunk in $HOME/.crack-attack/sounds/
73 char sound_dirname
[256];
74 TextureLoader::buildLocalDataFileName("sounds/", sound_dirname
);
75 File
= sound_dirname
+ sounds
[i
];
76 chunk
= Mix_LoadWAV (File
.c_str());
77 if ( chunk
== NULL
) { // Try to load chunk in DATA_DIRECTORY/sounds/
78 char *another_dir
= GC_DATA_DIRECTORY("sounds/");
79 File
= another_dir
+ sounds
[i
];
80 chunk
= Mix_LoadWAV (File
.c_str());
82 // If chunk is NULL there is no WAV available for this sound
83 if (!chunk
) cout
<< "WARNING *** Unable to open " << sounds
[i
] << endl
;
84 chunks
[sounds
[i
]] = chunk
;
91 void Sound::play( const char *file
, int vol
)
93 if (!has_audio_available
|| !chunks
[file
])
98 if (vol
> 10) vol
= 10;
99 // string File (file);
100 Mix_VolumeChunk( chunks
[file
], vol
* MIX_MAX_VOLUME
/ 10 );
101 channel
= Mix_PlayChannel( -1, chunks
[file
], 0 );
104 int Sound::audio_available( )
106 return has_audio_available
;
109 void Sound::cleanup( )
111 if (!has_audio_available
)