Fixed some little errors with the drawing functions.
[luagame.git] / funcs_sound.cpp
blob997fb1ff8c4b19a08336a6c5226aee58be0e88cb
1 /*
2 Copyright (c)2006-2008 - 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 = 44100; //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 = 256; //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);
53 if(s == NULL){
54 return luaL_error(L, "ERROR: Can't play sample, is NULL.");
57 if(Mix_PlayChannel(-1, s, 0)==-1){
58 Mix_AllocateChannels(Mix_AllocateChannels(-1)+1);
59 Mix_PlayChannel(-1, s, 0);
61 return 0;
64 //stop all samples
65 int l_stopsamples(lua_State *L){
66 Mix_HaltChannel(-1);
67 return 0;
70 //load a sample
71 int l_loadsample(lua_State *L){
72 Mix_Chunk *temp = 0;
73 std::map<std::string, Mix_Chunk *>::iterator s = sample_cache.find(std::string(luaL_checkstring(L,1)));
75 if( s == sample_cache.end()){
76 temp = Mix_LoadWAV(luaL_checkstring(L,1));
78 if(temp == NULL){
79 return luaL_error(L, "ERROR: Can't find sound file: \"%s\".", luaL_checkstring(L,1));
82 sample_cache[std::string(luaL_checkstring(L,1))] = temp;
85 lua_pushlightuserdata(L,temp);
86 return 1;
89 //unload a sample
90 int l_unloadsample(lua_State *L){
91 std::map<std::string, Mix_Chunk *>::iterator s = sample_cache.find(std::string(luaL_checkstring(L,1)));
92 if(s != sample_cache.end()){
93 Mix_FreeChunk(s->second);
94 sample_cache.erase(s);
96 return 0;
99 //clear the sample cache
100 int l_clearsamples(lua_State *L){
101 std::map<std::string, Mix_Chunk *>::iterator s = sample_cache.begin();
102 for( ; s != sample_cache.end(); s++){
103 Mix_FreeChunk(s->second);
105 sample_cache.clear();
106 return 0;
110 //play music
111 int l_playmusic(lua_State *L){
112 std::string temp = std::string(luaL_checkstring(L,1));
113 //free up music if not free
114 if(_music != 0){
115 if(Mix_PlayingMusic())
116 Mix_HaltMusic();
117 if(temp != _music_filename){ //only free if not loaded
118 Mix_FreeMusic(_music);
121 if(temp != _music_filename){ //only load if not loaded
122 _music = Mix_LoadMUS(temp.c_str());
123 _music_filename = temp;
126 //don't halt on error, just output it
127 if(!_music)
128 return luaL_error(L, "ERROR: Can't load music file \"%s\" : %s.",luaL_checkstring(L,1), Mix_GetError());
130 //play the music
131 Mix_PlayMusic(_music, lua_tointeger(L,2));
133 return 0;
136 //stop music
137 int l_stopmusic(lua_State *L){
138 if(_music != 0){
139 Mix_HaltMusic();
140 Mix_FreeMusic(_music);
141 _music_filename = "";
143 return 0;