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"
32 #include <SDL/SDL_mixer.h>
36 typedef map
<string
,Mix_Chunk
*> ChunkMap
;
37 vector
<string
> sounds
;
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
;
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
67 for (int i
= 0; i
< sounds
.size(); i
++) {
69 cout
<< "Loading " << sounds
[i
];
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
;
90 void Sound::play( const char *file
, int vol
)
92 if (!has_audio_available
|| !chunks
[file
])
97 if (vol
> 10) vol
= 10;
99 cout
<< "Playing sound: " << File
.c_str() << endl
;
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
)