naev 0.12.5
nlua_music.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
9#include "nlua_music.h"
10
11#include "music.h"
12#include "nluadef.h"
13
14/* Music methods. */
15static int musicL_choose( lua_State *L );
16static int musicL_play( lua_State *L );
17static int musicL_pause( lua_State *L );
18static int musicL_stop( lua_State *L );
19static int musicL_isPlaying( lua_State *L );
20static int musicL_current( lua_State *L );
21static int musicL_getVolume( lua_State *L );
22
23static const luaL_Reg music_methods[] = {
24 { "choose", musicL_choose },
25 { "play", musicL_play },
26 { "pause", musicL_pause },
27 { "stop", musicL_stop },
28 { "isPlaying", musicL_isPlaying },
29 { "current", musicL_current },
30 { "getVolume", musicL_getVolume },
31 { 0, 0 } };
32
52int nlua_loadMusic( nlua_env env )
53{
54 nlua_register( env, "music", music_methods, 0 );
55 return 0;
56}
57
66static int musicL_choose( lua_State *L )
67{
68 const char *situation = luaL_checkstring( L, 1 );
69 if ( music_choose( situation ) )
70 return NLUA_ERROR( L, "music.choose failed!" );
71 return 0;
72}
73
81static int musicL_play( lua_State *L )
82{
83 const char *str = luaL_optstring( L, 1, NULL );
84 if ( music_play( str ) )
85 return NLUA_ERROR( L, "music.play failed!" );
86 return 0;
87}
88
97static int musicL_pause( lua_State *L )
98{
99 int disable = lua_toboolean( L, 1 );
100 if ( music_pause( disable ) )
101 return NLUA_ERROR( L, "music.pause failed!" );
102 return 0;
103}
104
114static int musicL_stop( lua_State *L )
115{
116 int disable = lua_toboolean( L, 1 );
117 if ( music_stop( disable ) )
118 return NLUA_ERROR( L, "music.stop failed!" );
119 return 0;
120}
121
128static int musicL_isPlaying( lua_State *L )
129{
130 if ( music_disabled ) {
131 lua_pushboolean( L, 0 );
132 } else {
133 MusicInfo_t *minfo = music_info();
134 if ( minfo == NULL )
135 return NLUA_ERROR( L, "Failed to get music info!" );
136 lua_pushboolean( L, minfo->playing );
137 }
138 return 1;
139}
140
152static int musicL_current( lua_State *L )
153{
154 if ( music_disabled ) {
155 lua_pushstring( L, "none" );
156 lua_pushnumber( L, -1. );
157 } else {
158 MusicInfo_t *minfo = music_info();
159 if ( minfo == NULL )
160 return NLUA_ERROR( L, "Failed to get music info!" );
161 lua_pushstring( L, minfo->name );
162 lua_pushnumber( L, minfo->pos );
163 }
164 return 2;
165}
166
174static int musicL_getVolume( lua_State *L )
175{
176 if ( lua_toboolean( L, 1 ) )
177 lua_pushnumber( L, music_getVolumeLog() );
178 else
179 lua_pushnumber( L, music_getVolume() );
180 return 1;
181}
MusicInfo_t * music_info(void)
Gets information about the current music state.
Definition music.c:327
int music_play(const char *filename)
Plays the loaded music.
Definition music.c:241
int music_disabled
Definition music.c:29
int music_pause(int disable)
Pauses the music.
Definition music.c:287
double music_getVolumeLog(void)
Gets the current music volume (logarithmic).
Definition music.c:233
double music_getVolume(void)
Gets the current music volume (linear).
Definition music.c:223
int music_choose(const char *situation)
Actually runs the music stuff, based on situation.
Definition music.c:426
int music_stop(int disable)
Stops the loaded music.
Definition music.c:267
static int musicL_isPlaying(lua_State *L)
Checks to see if something is playing.
Definition nlua_music.c:128
static int musicL_choose(lua_State *L)
Delays a rechoose.
Definition nlua_music.c:66
static int musicL_play(lua_State *L)
Plays the loaded song. Will resume paused songs.
Definition nlua_music.c:81
static int musicL_stop(lua_State *L)
Stops playing the current song.
Definition nlua_music.c:114
int nlua_loadMusic(nlua_env env)
Music Lua module.
Definition nlua_music.c:52
static int musicL_current(lua_State *L)
Gets the name of the current playing song.
Definition nlua_music.c:152
static int musicL_pause(lua_State *L)
Pauses the music engine.
Definition nlua_music.c:97
static int musicL_getVolume(lua_State *L)
Gets the volume level of the music.
Definition nlua_music.c:174
static const luaL_Reg music_methods[]
Definition nlua_music.c:23