Removed SoundEngine. ObjectList can now hold and update other ObjectLists.
[luagame.git] / funcs_sound.cpp
bloba7f1b6cbdc96bf8d51eda4dcd1d557507bc68fba
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include "lua.hpp"
8 #include "globals.h"
9 #include "funcs_sound.h"
10 #include <iostream>
12 static const int mix_channels = 16; //the number of mixing channels
13 static const int mix_rate = MIX_DEFAULT_FREQUENCY; //the mix rate in Hz
14 static const int mix_format = MIX_DEFAULT_FORMAT; //the output format
15 static const int mix_op_channels = 2; //output channels, 2 == stereo
16 static const int mix_chunksize = 512; //output chunk size
19 // open sound device
20 void sound_open(){
21 //Attempt to open the audio device in this format
22 if(Mix_OpenAudio(mix_rate, mix_format, mix_op_channels, mix_chunksize)==-1){
23 std::cerr << "ERROR: Couldn't Initialize SoundEngine: " << Mix_GetError() << "\n";
24 return;
27 //Allocate channels
28 Mix_AllocateChannels(mix_channels);
30 //init music to null
31 _music = 0;
34 // close sound device
35 void sound_close(){
36 //free music if loaded
37 if(_music != 0)
38 Mix_FreeMusic(_music);
40 //free up the samples in the sample cache
41 for(std::map<std::string, Mix_Chunk*>::iterator it = sample_cache.begin(); it != sample_cache.end(); it++)
42 Mix_FreeChunk(it->second);
43 sample_cache.clear();
45 Mix_CloseAudio();
49 //play a sample
50 int l_playsample(lua_State *L){
51 Mix_Chunk *s = (Mix_Chunk *)lua_touserdata(L,1);
52 if(Mix_PlayChannel(-1, s, 0)==-1){
53 Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
54 Mix_PlayChannel(-1, s, 0);
56 return 0;
59 //stop all samples
60 int l_stopsamples(lua_State *L){
61 Mix_HaltChannel(-1);
62 return 0;
65 //load a sample
66 int l_loadsample(lua_State *L){
67 Mix_Chunk *temp = 0;
68 std::map<std::string, Mix_Chunk *>::iterator s = sample_cache.find(std::string(luaL_checkstring(L,1)));
70 if( s == sample_cache.end()){
71 temp = Mix_LoadWAV(luaL_checkstring(L,1));
72 sample_cache[std::string(luaL_checkstring(L,1))] = temp;
75 lua_pushlightuserdata(L,temp);
76 return 1;
79 //unload a sample
80 int l_unloadsample(lua_State *L){
81 std::map<std::string, Mix_Chunk *>::iterator s = sample_cache.find(std::string(luaL_checkstring(L,1)));
82 if(s != sample_cache.end()){
83 Mix_FreeChunk(s->second);
84 sample_cache.erase(s);
86 return 0;
89 //clear the sample cache
90 int l_clearsamples(lua_State *L){
91 std::map<std::string, Mix_Chunk *>::iterator s = sample_cache.begin();
92 for( ; s != sample_cache.end(); s++){
93 Mix_FreeChunk(s->second);
95 sample_cache.clear();
96 return 0;
100 //play music
101 int l_playmusic(lua_State *L){
102 std::string temp = std::string(luaL_checkstring(L,1));
103 //free up music if not free
104 if(_music != 0){
105 if(Mix_PlayingMusic())
106 Mix_HaltMusic();
107 if(temp != _music_filename){ //only free if not loaded
108 Mix_FreeMusic(_music);
111 if(temp != _music_filename){ //only load if not loaded
112 _music = Mix_LoadMUS(temp.c_str());
113 _music_filename = temp;
116 //don't halt on error, just output it
117 if(!_music)
118 std::cerr << "ERROR: Cannot load music file '" << luaL_checkstring(L,1) << "' : " << Mix_GetError();
120 //play the music
121 Mix_PlayMusic(_music, lua_tointeger(L,2));
123 return 0;
126 //stop music
127 int l_stopmusic(lua_State *L){
128 if(_music != 0){
129 Mix_HaltMusic();
130 Mix_FreeMusic(_music);
131 _music_filename = "";
133 return 0;