From da45f29315822d768056e06791141cf7db74e5e5 Mon Sep 17 00:00:00 2001
From: Brett Lajzer
Date: Wed, 22 Aug 2007 11:12:26 -0400
Subject: [PATCH] Sound system is now analgous in operation to the image
system.
The sound system now returns a pointer to be passed to the
play_sample() function. This works in the same way that the
image system does, mostly for idealogical consistency.
There are also some slight documentation updates/fixes.
---
SoundEngine.cpp | 42 +++++++++++++-----------------------
SoundEngine.h | 11 +++++-----
doc/basic_tutorial.html | 2 +-
doc/class_eventmanager.html | 2 +-
doc/class_fpsmanager.html | 2 +-
doc/class_object.html | 2 +-
doc/class_objectlist.html | 2 +-
doc/fun_ref.html | 21 ++++++++++++------
doc/fun_ref.txt | 19 +++++++++++++---
doc/{class_index.html => index.html} | 20 +++++++++++------
doc/{class_index.txt => index.txt} | 18 ++++++++++------
funcs_sound.cpp | 14 ++++++++----
funcs_sound.h | 3 +++
main.cpp | 3 +++
14 files changed, 97 insertions(+), 64 deletions(-)
rename doc/{class_index.html => index.html} (90%)
rename doc/{class_index.txt => index.txt} (79%)
diff --git a/SoundEngine.cpp b/SoundEngine.cpp
index 8bcfcae..4351eac 100644
--- a/SoundEngine.cpp
+++ b/SoundEngine.cpp
@@ -75,43 +75,31 @@ void SoundEngine::stop_music(){
_m_filename = "";
}
}
-
-//this function looks first in the sample cache to see
-//if the sample is already loaded. If not, then it
-//loads it and adds it to the sample cache.
-//it then plays it
-void SoundEngine::play_sample(const char *filename){
- std::map::iterator s = _samples.find(std::string(filename));
-
- if( s != _samples.end()){
- //if it can't be played, then allocate another channel
- //and try again
- if(Mix_PlayChannel(-1, s->second, 0)==-1){
- Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
- Mix_PlayChannel(-1, s->second, 0);
- }
- }else{
- Mix_Chunk * temp = Mix_LoadWAV(filename);
- _samples[std::string(filename)] = temp;
-
- if(Mix_PlayChannel(-1, temp, 0)==-1){
- Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
- Mix_PlayChannel(-1, temp, 0);
- }
+//stops all samples
+void SoundEngine::stop_samples(){
+ Mix_HaltChannel(-1);
+}
+
+//plays samples that have been loaded (spec'd by pointer)
+void SoundEngine::play_sample(Mix_Chunk *s){
+ if(Mix_PlayChannel(-1, s, 0)==-1){
+ Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
+ Mix_PlayChannel(-1, s, 0);
}
-
}
//this function looks first in the sample cache to see
//if the sample is already loaded. If not, then it
//loads it and adds it to the sample cache.
-void SoundEngine::load_sample(const char *filename){
+Mix_Chunk *SoundEngine::load_sample(const char *filename){
+ Mix_Chunk *temp = 0;
std::map::iterator s = _samples.find(std::string(filename));
if( s == _samples.end()){
- Mix_Chunk * temp = Mix_LoadWAV(filename);
+ temp = Mix_LoadWAV(filename);
_samples[std::string(filename)] = temp;
- }
+ }
+ return temp;
}
//unloads a (theoretically) prevoiously loaded sample
void SoundEngine::unload_sample(const char *filename){
diff --git a/SoundEngine.h b/SoundEngine.h
index f8b3dff..2107614 100644
--- a/SoundEngine.h
+++ b/SoundEngine.h
@@ -26,9 +26,10 @@ public:
static void open();
static void close();
static void play_music(const char *filename, int loops); //loops = -1 -> loops forever
- static void stop_music();
- static void play_sample(const char *filename);
- static void load_sample(const char *filename);
+ static void stop_music();
+ static void stop_samples();
+ static void play_sample(Mix_Chunk *s);
+ static Mix_Chunk *load_sample(const char *filename);
static void unload_sample(const char *filename);
static void clear_samples();
@@ -37,9 +38,9 @@ private:
static std::string _m_filename; //the music filename
static std::map _samples; //the sample cache
static const int _channels = 16; //the number of mixing channels
- static const int _rate = 44100; //the mix rate in Hz
+ static const int _rate = MIX_DEFAULT_FREQUENCY; //the mix rate in Hz
static const int _format = MIX_DEFAULT_FORMAT; //the output format
static const int _op_channels = 2; //output channels, 2 == stereo
- static const int _chunksize = 2048; //output chunk size
+ static const int _chunksize = 512; //output chunk size
};
#endif
diff --git a/doc/basic_tutorial.html b/doc/basic_tutorial.html
index 1c63b6b..0c3fb39 100644
--- a/doc/basic_tutorial.html
+++ b/doc/basic_tutorial.html
@@ -968,7 +968,7 @@ for a simple game.