Fixed oversights in collision code. More sound control.
[luagame.git] / SoundEngine.cpp
blob8bcfcaef6878c31d3c9c9bdeda9a27e1c0097c07
1 /*
2 ===================
3 = SoundEngine.cpp =
4 ===================
6 The sound engine's implementation.
8 Copyright (c)2006 - Brett Lajzer
9 */
11 #include "SoundEngine.h"
12 #include <iostream>
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";
22 return;
25 //Allocate channels
26 Mix_AllocateChannels(_channels);
28 //init music to null
29 _music = 0;
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
36 if(_music != 0)
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);
42 _samples.clear();
44 Mix_CloseAudio();
47 //play a music file
48 void SoundEngine::play_music(const char *filename, int loops){
49 //free up music if not free
50 if(_music != 0){
51 if(Mix_PlayingMusic())
52 Mix_HaltMusic();
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
63 if(!_music)
64 std::cerr << "ERROR: Cannot load music file '" << filename << "' : " << Mix_GetError();
66 //play the music
67 Mix_PlayMusic(_music, loops);
70 //stop the music and unload the music file
71 void SoundEngine::stop_music(){
72 if(_music != 0){
73 Mix_HaltMusic();
74 Mix_FreeMusic(_music);
75 _m_filename = "";
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.
82 //it then plays it
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
88 //and try again
89 if(Mix_PlayChannel(-1, s->second, 0)==-1){
90 Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
91 Mix_PlayChannel(-1, s->second, 0);
93 }else{
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);
121 _samples.erase(s);
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);
132 _samples.clear();