6 The sound engine's implementation.
8 Copyright (c)2006 - Brett Lajzer
11 #include "SoundEngine.h"
14 Mix_Music
* SoundEngine::_music
; // the music pointer
15 std::string
SoundEngine::_m_filename
; //the music filename
16 std::map
<std::string
, Mix_Chunk
*> SoundEngine::_samples
; //the sample cache
18 void SoundEngine::open(){
19 //Attempt to open the audio device in this format
20 if(Mix_OpenAudio(_rate
, _format
, _op_channels
, _chunksize
)==-1){
21 std::cerr
<< "ERROR: Couldn't Initialize SoundEngine: " << Mix_GetError() << "\n";
26 Mix_AllocateChannels(_channels
);
32 //this should only be called right before closing the program
33 //really, don't do it unless you want to lose all your samples
34 void SoundEngine::close(){
35 //free music if loaded
37 Mix_FreeMusic(_music
);
39 //free up the samples in the sample cache
40 for(std::map
<std::string
, Mix_Chunk
*>::iterator it
= _samples
.begin(); it
!= _samples
.end(); it
++)
41 Mix_FreeChunk(it
->second
);
48 void SoundEngine::play_music(const char *filename
, int loops
){
49 //free up music if not free
51 if(Mix_PlayingMusic())
53 if(std::string(filename
) != _m_filename
){ //only free if not loaded
54 Mix_FreeMusic(_music
);
57 if(std::string(filename
) != _m_filename
){ //only load if not loaded
58 _music
= Mix_LoadMUS(filename
);
59 _m_filename
= std::string(filename
);
62 //don't halt on error, just output it
64 std::cerr
<< "ERROR: Cannot load music file '" << filename
<< "' : " << Mix_GetError();
67 Mix_PlayMusic(_music
, loops
);
70 //stop the music and unload the music file
71 void SoundEngine::stop_music(){
74 Mix_FreeMusic(_music
);
79 //this function looks first in the sample cache to see
80 //if the sample is already loaded. If not, then it
81 //loads it and adds it to the sample cache.
83 void SoundEngine::play_sample(const char *filename
){
84 std::map
<std::string
, Mix_Chunk
*>::iterator s
= _samples
.find(std::string(filename
));
86 if( s
!= _samples
.end()){
87 //if it can't be played, then allocate another channel
89 if(Mix_PlayChannel(-1, s
->second
, 0)==-1){
90 Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
91 Mix_PlayChannel(-1, s
->second
, 0);
94 Mix_Chunk
* temp
= Mix_LoadWAV(filename
);
95 _samples
[std::string(filename
)] = temp
;
97 if(Mix_PlayChannel(-1, temp
, 0)==-1){
98 Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
99 Mix_PlayChannel(-1, temp
, 0);
105 //this function looks first in the sample cache to see
106 //if the sample is already loaded. If not, then it
107 //loads it and adds it to the sample cache.
108 void SoundEngine::load_sample(const char *filename
){
109 std::map
<std::string
, Mix_Chunk
*>::iterator s
= _samples
.find(std::string(filename
));
111 if( s
== _samples
.end()){
112 Mix_Chunk
* temp
= Mix_LoadWAV(filename
);
113 _samples
[std::string(filename
)] = temp
;
116 //unloads a (theoretically) prevoiously loaded sample
117 void SoundEngine::unload_sample(const char *filename
){
118 std::map
<std::string
, Mix_Chunk
*>::iterator s
= _samples
.begin();
119 if(s
!= _samples
.end()){
120 Mix_FreeChunk(s
->second
);
125 //clears the sample cache
126 void SoundEngine::clear_samples(){
127 std::map
<std::string
, Mix_Chunk
*>::iterator s
= _samples
.begin();
129 for( ; s
!= _samples
.end(); s
++){
130 Mix_FreeChunk(s
->second
);