snd player

This commit is contained in:
p.kosyh 2009-11-12 09:07:17 +00:00
parent df094db760
commit 88639df639
7 changed files with 84 additions and 42 deletions

View file

@ -67,7 +67,6 @@ int game_select(const char *name)
chdir(game_cwd);
for (i = 0; i<games_nr; i ++) {
if (!strcmp(games[i].dir, name)) {
char *p;
instead_done();
if (instead_init())
return -1;
@ -75,9 +74,7 @@ int game_select(const char *name)
return -1;
if (instead_load(MAIN_FILE))
return -1;
p = instead_eval("game:ini()");
if (p)
free(p);
instead_eval("game:ini()");
curgame_dir = games[i].dir;
return 0;
}
@ -993,7 +990,8 @@ static void dec_music(void *data)
char *mus;
if (!curgame_dir)
return;
mus = instead_eval("return dec_music_loop()");
instead_eval("return dec_music_loop()");
mus = instead_retval(1);
if (!mus)
return;
if (atoi(mus) == -1)
@ -1015,23 +1013,21 @@ void game_music_player(void)
return;
if (!opt_music)
return;
mus = instead_eval("return get_music_loop()");
instead_eval("return get_music()");
mus = instead_retval(1);
if (mus) {
loop = atoi(mus);
free(mus);
} else
loop = -1;
mus = instead_eval("return get_music()");
mus = instead_retval(2);
unix_path(mus);
if (mus && loop == -1) { /* disabled, 0 - forever, 1-n - loops */
free(mus);
mus = NULL;
}
if (!mus) {
if (last_music) {
game_stop_mus(500);
@ -1109,31 +1105,29 @@ void game_sound_player(void)
if (!snd_volume_mus(-1))
return;
snd = instead_eval("return get_sound_chan()");
instead_eval("return get_sound()");
snd = instead_retval(1);
if (snd) {
loop = atoi(snd);
free(snd);
}
snd = instead_retval(2);
if (snd) {
chan = atoi(snd);
free(snd);
}
snd = instead_eval("return get_sound_loop()");
if (snd) {
loop = atoi(snd);
free(snd);
}
snd = instead_eval("return get_sound()");
snd = instead_retval(3);
if (!snd) {
if (chan != -1) {
/* halt channel */
snd_halt_chan(chan);
snd_halt_chan(chan, 500);
}
return;
}
do { /* reset sound */
char *p = instead_eval("set_sound(nil, -1)");
if (p)
free(p);
} while(0);
instead_eval("set_sound(nil, -1)");
unix_path(snd);
w = sound_find(snd);
@ -1203,7 +1197,8 @@ int game_cmd(char *cmd)
if (!cmdstr)
goto inv; /* hackish? ok, yes it is... */
title = instead_eval("return get_title();");
instead_eval("return get_title();");
title = instead_retval(1);
if (title) {
snprintf(buf, sizeof(buf), "<b><c><a:look>%s</a></c></b>", title);
txt_layout_set(el_layout(el_title), buf);
@ -1215,7 +1210,8 @@ int game_cmd(char *cmd)
txt_layout_size(el_layout(el_title), NULL, &title_h);
title_h += game_theme.font_size / 2; // todo?
pict = instead_eval("return get_picture();");
instead_eval("return get_picture();");
pict = instead_retval(1);
unix_path(pict);
new_pict = check_new_pict(pict);
@ -1665,7 +1661,7 @@ int game_click(int x, int y, int action, int filter)
if (mouse_filter(filter))
return 0;
if (opt_click)
snd_play(game_theme.click, -1, 0);
snd_play(game_theme.click, 0, 0);
game_cmd(buf);
return 1;
@ -1688,7 +1684,7 @@ int game_click(int x, int y, int action, int filter)
disable_use();
if (opt_click)
snd_play(game_theme.click, -1, 0);
snd_play(game_theme.click, 0, 0);
game_cmd(buf);
return 1;

View file

@ -86,11 +86,22 @@ char *getstring(char *cmd)
}
char *instead_eval(char *s)
int instead_eval(char *s)
{
char *p;
p = getstring(s);
return p;
if (!L)
return -1;
if (dostring(L, s))
return -1;
return 0;
}
char *instead_retval(int n)
{
char *s;
s = (char*)lua_tostring(L, -n);
if (s)
s = fromgame(s);
return s;
}
char *instead_cmd(char *s)
@ -308,9 +319,22 @@ static int luaB_print (lua_State *L) {
return 0;
}
static int luaB_is_sound(lua_State *L) {
const char *chan = luaL_optstring(L, 1, NULL);
int c, r;
if (!chan)
c = -1;
else
c = atoi(chan);
r = snd_playing(c);
lua_pushboolean(L, (r != 0)); /* else not a number */
return 1;
}
static const luaL_Reg base_funcs[] = {
{"doencfile", luaB_doencfile},
{"print", luaB_print}, /* for some mystic, it is needed in win version (with -debug) */
{"is_sound", luaB_is_sound},
{NULL, NULL}
};

View file

@ -5,7 +5,8 @@ extern int instead_init(void);
extern int instead_load(char *game);
extern void instead_done(void);
extern char *instead_cmd(char *s);
extern char *instead_eval(char *s);
extern int instead_eval(char *s);
extern char *instead_retval(int n);
char *fromgame(const char *s);
extern int instead_encode(const char *s, const char *d);
#endif

View file

@ -356,9 +356,7 @@ int game_menu_act(const char *a)
// free_last();
game_select(curgame_dir);
game_menu_box(0, NULL);
s = instead_eval("game:ini()");
if (s)
free(s);
instead_eval("game:ini()");
game_cmd("look");
s = game_save_path(0, 0);
if (s && !access(s, R_OK) && opt_autosave)

View file

@ -94,11 +94,15 @@ int snd_volume_mus(int vol)
wav_t snd_load_wav(const char *fname)
{
wav_t r;
if (!sound_on)
return NULL;
if (!fname)
return NULL;
return (wav_t)Mix_LoadWAV(fname);
r = (wav_t)Mix_LoadWAV(fname);
if (!r)
fprintf(stderr,"Can't load '%s'.\n", fname);
return r;
}
void snd_free_wav(wav_t w)
@ -109,9 +113,12 @@ void snd_free_wav(wav_t w)
Mix_FreeChunk((Mix_Chunk*)w);
}
void snd_halt_chan(int han)
void snd_halt_chan(int han, int ms)
{
Mix_HaltChannel(han);
if (ms)
Mix_FadeOutChannel(han, ms);
else
Mix_HaltChannel(han);
}
Mix_Music *snd_load_mus(const char *fname)
@ -180,6 +187,15 @@ int snd_playing_mus(void)
return 0;
}
int snd_playing(int channel)
{
if (channel >= MIX_CHANNELS)
channel %= MIX_CHANNELS;
if (channel < 0)
channel = -1;
return Mix_Playing(channel);
}
void snd_free_mus(mus_t mus)
{
if (!sound_on)

View file

@ -17,7 +17,7 @@ extern void snd_free_mus(mus_t mus);
extern int snd_init(int hz);
extern int snd_hz(void);
extern void snd_play(wav_t chunk, int channel, int loop);
extern void snd_halt_chan(int han);
extern void snd_halt_chan(int han, int ms);
extern void snd_free_wav(wav_t chunk);
extern wav_t snd_load_wav(const char *fname);
@ -28,5 +28,6 @@ extern int snd_volume_mus(int vol);
extern void snd_done(void);
extern int snd_vol_from_pcn(int v);
extern int snd_vol_to_pcn(int v);
extern int snd_playing(int channel);
#endif

View file

@ -2015,7 +2015,7 @@ function get_title()
end
function get_music()
return game._music;
return game._music, game._music_loop;
end
function get_music_loop()
@ -2059,8 +2059,14 @@ function is_music()
return game._music ~= nil and game._music_loop ~= -1
end
if is_sound == nil then
function is_sound()
return false -- sdl-instead export own function
end
end
function get_sound()
return game._sound;
return game._sound, game._sound_channel, game._sound_loop;
end
function get_sound_chan()