sound module added

This commit is contained in:
p.kosyh 2011-04-15 07:23:46 +00:00
parent 1b1fa9f03c
commit d5cdf74613
9 changed files with 140 additions and 19 deletions

1
debian/changelog vendored
View file

@ -11,6 +11,7 @@ instead (1.3.5) unstable; urgency=low
* -appdata parameter;
* -chunksize parameter;
* show dir to be deleted while remove game;
* theme reset features;
-- Peter Kosyh <p.kosyh@gmail.com> Tue, 29 Mar 2011 14:08:00 +0300

View file

@ -60,6 +60,7 @@
".\symbian_gamedata\stead\theme.lua" -"!:\data\instead\stead\theme.lua"
".\symbian_gamedata\stead\prefs.lua" -"!:\data\instead\stead\prefs.lua"
".\symbian_gamedata\stead\sprites.lua" -"!:\data\instead\stead\sprites.lua"
".\symbian_gamedata\stead\sound.lua" -"!:\data\instead\stead\sound.lua"
".\symbian_gamedata\lang\ru.ini" -"!:\data\instead\lang\ru.ini"
".\symbian_gamedata\lang\en.ini" -"!:\data\instead\lang\en.ini"
".\symbian_gamedata\themes\fantasy\down.png" -"!:\data\instead\themes\fantasy\down.png"

View file

@ -778,8 +778,6 @@ void free_last_music(void)
last_music = NULL;
}
static void sounds_free(void);
void free_last(void)
{
if (last_pict)
@ -1334,6 +1332,7 @@ typedef struct {
struct list_head list;
char *fname;
wav_t wav;
int loaded;
} _snd_t;
static _snd_t *channels[SND_CHANNELS] = {};
@ -1354,6 +1353,26 @@ static int sound_playing(_snd_t *snd)
return -1;
}
const char *sound_channel(int i)
{
_snd_t *sn;
sn = channels[i];
if (!sn)
return NULL;
return sn->fname;
}
static void sound_free(_snd_t *sn)
{
if (!sn)
return;
free(sn->fname);
snd_free_wav(sn->wav);
list_del(&sn->list);
free(sn);
sounds_nr --;
}
static void sounds_shrink(void)
{
struct list_head *pos, *pos2;
@ -1362,36 +1381,29 @@ static void sounds_shrink(void)
// fprintf(stderr,"shrink try\n");
while (pos != &sounds && sounds_nr > MAX_WAVS) {
sn = (_snd_t*)pos;
if (sound_playing(sn) != -1) {
if (sound_playing(sn) != -1 || sn->loaded) {
pos = pos->next;
continue;
}
pos2 = pos->next;
// fprintf(stderr,"to %p\n", pos2);
free(sn->fname);
snd_free_wav(sn->wav);
list_del(pos);
free(sn);
sound_free(sn);
pos = pos2;
sounds_nr --;
// fprintf(stderr,"shrink by 1\n");
}
}
static void sounds_free(void)
void sounds_free(void)
{
int i = 0;
// int i = 0;
while (!list_empty(&sounds)) {
_snd_t *sn = (_snd_t*)(sounds.next);
free(sn->fname);
snd_free_wav(sn->wav);
list_del(&sn->list);
free(sn);
if (sound_playing(sn) == -1) {
sound_free(sn);
}
}
for (i = 0; i < SND_CHANNELS; i++)
channels[i] = NULL;
sounds_nr = 0;
// for (i = 0; i < SND_CHANNELS; i++)
// channels[i] = NULL;
// sounds_nr = 0;
}
static _snd_t *sound_find(const char *fname)
@ -1428,6 +1440,7 @@ static _snd_t *sound_add(const char *fname)
return NULL;
INIT_LIST_HEAD(&sn->list);
sn->fname = strdup(fname);
sn->loaded = 0;
if (!sn->fname) {
free(sn);
return NULL;
@ -1459,6 +1472,34 @@ static void sounds_reload(void)
}
}
int sound_load(const char *fname)
{
_snd_t *sn;
sn = sound_find(fname);
if (sn) {
sn->loaded ++; /* to pin */
return 0;
}
sn = sound_add(fname);
if (!sn)
return -1;
sn->loaded = 1;
return 0;
}
void sound_unload(const char *fname)
{
_snd_t *sn;
sn = sound_find(fname);
if (!sn || !sn->loaded)
return;
sn->loaded --;
if (!sn->loaded && sound_playing(sn) == -1)
sound_free(sn);
return;
}
static int _play_combined_snd(char *filename, int chan, int loop)
{
char *str;

View file

@ -85,6 +85,11 @@ extern int game_pict_coord(int *x, int *y, int *w, int *h);
extern void menu_toggle(void);
extern void game_channel_finished(int channel);
extern int sound_load(const char *fname);
extern void sound_unload(const char *fname);
extern void sounds_free(void);
extern const char *sound_channel(int i);
#define CURSOR_CLEAR -1
#define CURSOR_OFF 0
#define CURSOR_ON 1

View file

@ -1222,6 +1222,43 @@ static int luaB_free_font(lua_State *L) {
return 1;
}
static int luaB_load_sound(lua_State *L) {
int rc;
const char *fname = luaL_optstring(L, 1, NULL);
if (!fname)
return 0;
rc = sound_load(fname);
if (rc)
return 0;
lua_pushstring(L, fname);
return 1;
}
static int luaB_free_sound(lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
if (!fname)
return 0;
sound_unload(fname);
return 0;
}
static int luaB_free_sounds(lua_State *L) {
sounds_free();
return 0;
}
static int luaB_channel_sound(lua_State *L) {
const char *s;
int ch = luaL_optnumber(L, 1, 0);
ch = ch % SND_CHANNELS;
s = sound_channel(ch);
if (s) {
lua_pushstring(L, s);
return 1;
}
return 0;
}
static const luaL_Reg base_funcs[] = {
{"doencfile", luaB_doencfile},
{"dofile", luaB_dofile},
@ -1235,6 +1272,11 @@ static const luaL_Reg base_funcs[] = {
{"readdir", dir_iter_factory},
{"menu_toggle", luaB_show_menu},
{"sound_load", luaB_load_sound},
{"sound_free", luaB_free_sound},
{"sound_channel", luaB_channel_sound},
{"sounds_free", luaB_free_sounds},
{"font_load", luaB_load_font},
{"font_free", luaB_free_font},
{"font_scaled_size", luaB_font_size_scaled},

View file

@ -27,6 +27,7 @@ install:
$(INSTALL) hideinv.lua $(STEADPATH)/hideinv.lua
$(INSTALL) theme.lua $(STEADPATH)/theme.lua
$(INSTALL) sprites.lua $(STEADPATH)/sprites.lua
$(INSTALL) sound.lua $(STEADPATH)/sound.lua
uninstall:
$(RM) $(STEADPATH)/stead.lua

View file

@ -24,3 +24,4 @@ install:
copy hideinv.lua ..\bin\stead
copy theme.lua ..\bin\stead
copy sprites.lua ..\bin\stead
copy sound.lua ..\bin\stead

28
stead/sound.lua Normal file
View file

@ -0,0 +1,28 @@
stead.sound_load = sound_load
stead.sound_free = sound_free
stead.sounds_free = sounds_free
stead.sound_channel = sound_channel
sound = {
nam = 'sound';
object_type = true;
system_type = true;
load = function(fname)
return stead.sound_load(fname);
end;
free = function(key)
return stead.sound_free(key);
end;
play = function(key, ...)
return stead.add_sound(key, ...)
end;
stop = function(...)
stead.stop_sound(...);
end;
channel = function(...)
return stead.sound_channel(...)
end
}
stead.module_init(function()
stead.sounds_free();
end)

View file

@ -2632,6 +2632,7 @@ if is_sound == nil then
return false -- sdl-instead export own function
end
end
stead.is_sound = is_sound
if get_savepath == nil then
function get_savepath()