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.

diff --git a/doc/class_eventmanager.html b/doc/class_eventmanager.html index 91106d5..5234288 100644 --- a/doc/class_eventmanager.html +++ b/doc/class_eventmanager.html @@ -752,7 +752,7 @@ evman.quit = diff --git a/doc/class_fpsmanager.html b/doc/class_fpsmanager.html index e63da73..a342724 100644 --- a/doc/class_fpsmanager.html +++ b/doc/class_fpsmanager.html @@ -505,7 +505,7 @@ http://www.gnu.org/software/src-highlite --> diff --git a/doc/class_object.html b/doc/class_object.html index b3a4bae..ae95749 100644 --- a/doc/class_object.html +++ b/doc/class_object.html @@ -622,7 +622,7 @@ MyObj.attr2 = diff --git a/doc/class_objectlist.html b/doc/class_objectlist.html index effeb2d..14073cc 100644 --- a/doc/class_objectlist.html +++ b/doc/class_objectlist.html @@ -578,7 +578,7 @@ http://www.gnu.org/software/src-highlite --> diff --git a/doc/fun_ref.html b/doc/fun_ref.html index 2d647ac..55becc0 100644 --- a/doc/fun_ref.html +++ b/doc/fun_ref.html @@ -444,11 +444,20 @@ results.

5. Sound

-

5.1. play_sample(path<string>)

-

Plays the sample specified by path. The sample will play until completion.

-

5.2. play_music(path<string>, loops<int>)

-

Plays music specified by path. Music will play loops times. If loops is -1 then it will loop forever.

-

5.3. stop_music()

+

5.1. sample<Mix_Chunk*> load_sample(path<string>)

+

Loads a sample, specified by path, and returns a pointer to it.

+

5.2. unload_sample(path<string>)

+

Unloads a previously loaded sample specified by path.

+

5.3. play_sample(sample<Mix_Chunk*>)

+

Plays the sample specified by sample. The sample will play until completion. +Sample needs to have been previously loaded using load_sample().

+

5.4. clear_samples()

+

Unloads all loaded samples from memory.

+

5.5. stop_samples()

+

Stops the playback of all currently playing samples.

+

5.6. play_music(path<string>, loops<int>)

+

Plays music specified by path. Music will play loops times. If loops is -1 then it will loop continuously until stopped.

+

5.7. stop_music()

Stops the currently playing music.

6. Drawing

@@ -483,7 +492,7 @@ results.

diff --git a/doc/fun_ref.txt b/doc/fun_ref.txt index 4b7f34c..666fcb4 100644 --- a/doc/fun_ref.txt +++ b/doc/fun_ref.txt @@ -76,11 +76,24 @@ results. == Sound == -=== play_sample(path) === -Plays the sample specified by *path*. The sample will play until completion. +=== sample load_sample(path) === +Loads a sample, specified by *path*, and returns a pointer to it. + +=== unload_sample(path) === +Unloads a previously loaded sample specified by *path*. + +=== play_sample(sample) === +Plays the sample specified by *sample*. The sample will play until completion. +Sample needs to have been previously loaded using load_sample(). + +=== clear_samples() === +Unloads all loaded samples from memory. + +=== stop_samples() === +Stops the playback of all currently playing samples. === play_music(path, loops) === -Plays music specified by *path*. Music will play *loops* times. If *loops* is -1 then it will loop forever. +Plays music specified by *path*. Music will play *loops* times. If *loops* is -1 then it will loop continuously until stopped. === stop_music() === Stops the currently playing music. diff --git a/doc/class_index.html b/doc/index.html similarity index 90% rename from doc/class_index.html rename to doc/index.html index a6addca..0f0ec5d 100644 --- a/doc/class_index.html +++ b/doc/index.html @@ -370,22 +370,23 @@ function generateToc(toclevels) { } /*]]>*/ -LuaGame Class Reference +LuaGame Documentation -

1. Introduction

+

1. Tutorials

-

This set of documents details the functionality offered by the base classes of -LuaGame.

+

1.1. Basic Tutorial

+

The basic tutorial is a light introduction to the concepts that +are central to programming with LuaGame.

-

2. Classes

+

2. Class Reference

2.1. Object

The Object class is the basic class that all @@ -416,9 +417,14 @@ the Object class.

2.9. Rectangle

2.10. Circle

+

3. Function Reference

+
+

The function reference contains information about all of the +exported functions from C++ to Lua.

+
diff --git a/doc/class_index.txt b/doc/index.txt similarity index 79% rename from doc/class_index.txt rename to doc/index.txt index e0233e2..609e29b 100644 --- a/doc/class_index.txt +++ b/doc/index.txt @@ -1,12 +1,11 @@ -LuaGame Class Reference -======================= += LuaGame Documentation = -== Introduction == -This set of documents details the functionality offered by the base classes of -LuaGame. - -== Classes == +== Tutorials == +=== Basic Tutorial === +The link:basic_totrial.html[basic tutorial] is a light introduction to the concepts that +are central to programming with LuaGame. +== Class Reference == === Object === The link:class_object.html[Object] class is the basic class that all in-game entities should descend from. @@ -44,3 +43,8 @@ the link:class_object.html[Object] class. === Rectangle === === Circle === + + +== Function Reference == +The link:fun_ref.html[function reference] contains information about all of the +exported functions from C++ to Lua. diff --git a/funcs_sound.cpp b/funcs_sound.cpp index bf11d15..4593ccd 100644 --- a/funcs_sound.cpp +++ b/funcs_sound.cpp @@ -11,16 +11,22 @@ See LICENSE for license information. //play a sample int l_playsample(lua_State *L){ - SoundEngine::play_sample(luaL_checkstring(L,1)); + SoundEngine::play_sample((Mix_Chunk *)lua_touserdata(L,1)); return 0; } -//preload a sample -int l_loadsample(lua_State *L){ - SoundEngine::load_sample(luaL_checkstring(L,1)); +//stop all samples +int l_stopsamples(lua_State *L){ + SoundEngine::stop_samples(); return 0; } +//load a sample +int l_loadsample(lua_State *L){ + lua_pushlightuserdata(L,SoundEngine::load_sample(luaL_checkstring(L,1))); + return 1; +} + //unload a sample int l_unloadsample(lua_State *L){ SoundEngine::unload_sample(luaL_checkstring(L,1)); diff --git a/funcs_sound.h b/funcs_sound.h index 7644167..39f8d5b 100644 --- a/funcs_sound.h +++ b/funcs_sound.h @@ -11,6 +11,9 @@ See LICENSE for license information. //play a sample int l_playsample(lua_State *L); +//stop all samples +int l_stopsamples(lua_State *L); + //preload a sample int l_loadsample(lua_State *L); diff --git a/main.cpp b/main.cpp index 1acfe2c..0e61566 100644 --- a/main.cpp +++ b/main.cpp @@ -105,6 +105,9 @@ int main(int argc, char *argv[]){ lua_pushcfunction(L, l_playsample); lua_setglobal(L, "play_sample"); + lua_pushcfunction(L, l_stopsamples); + lua_setglobal(L, "stop_samples"); + lua_pushcfunction(L, l_loadsample); lua_setglobal(L, "load_sample"); -- 2.11.4.GIT