naev 0.11.5
nlua_music.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include "SDL.h"
11
12#include "naev.h"
15#include "nlua_music.h"
16
17#include "log.h"
18#include "music.h"
19#include "ndata.h"
20#include "nluadef.h"
21
22/* Music methods. */
23static int musicL_choose( lua_State *L );
24static int musicL_play( lua_State *L );
25static int musicL_pause( lua_State *L );
26static int musicL_stop( lua_State *L );
27static int musicL_isPlaying( lua_State *L );
28static int musicL_current( lua_State *L );
29static int musicL_getVolume( lua_State *L );
30static const luaL_Reg music_methods[] = {
31 { "choose", musicL_choose },
32 { "play", musicL_play },
33 { "pause", musicL_pause },
34 { "stop", musicL_stop },
35 { "isPlaying", musicL_isPlaying },
36 { "current", musicL_current },
37 { "getVolume", musicL_getVolume },
38 {0,0}
39};
58int nlua_loadMusic( nlua_env env )
59{
60 nlua_register(env, "music", music_methods, 0);
61 return 0;
62}
63
72static int musicL_choose( lua_State* L )
73{
74 const char *situation = luaL_checkstring(L,1);
75 if (music_choose( situation ))
76 return NLUA_ERROR(L,"music.choose failed!");
77 return 0;
78}
79
85static int musicL_play( lua_State *L )
86{
87 const char *str = luaL_optstring(L,1,NULL);
88 if (music_play( str ))
89 return NLUA_ERROR(L,"music.play failed!");
90 return 0;
91}
92
99static int musicL_pause( lua_State* L )
100{
101 int disable = lua_toboolean(L,1);
102 if (music_pause(disable))
103 return NLUA_ERROR(L,"music.pause failed!");
104 return 0;
105}
106
113static int musicL_stop( lua_State *L )
114{
115 int nodisable = lua_toboolean(L,1);
116 if (music_stop(!nodisable))
117 return NLUA_ERROR(L,"music.stop failed!");
118 return 0;
119}
120
127static int musicL_isPlaying( lua_State* L )
128{
129 if (music_disabled) {
130 lua_pushboolean(L, 0);
131 }
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
150static int musicL_current( lua_State* L )
151{
152 if (music_disabled) {
153 lua_pushstring(L, "none");
154 lua_pushnumber(L, -1.);
155 }
156 else {
157 MusicInfo_t *minfo = music_info();
158 if (minfo==NULL)
159 return NLUA_ERROR(L,"Failed to get music info!");
160 lua_pushstring(L, minfo->name);
161 lua_pushnumber(L, minfo->pos);
162 }
163 return 2;
164}
165
173static int musicL_getVolume( lua_State *L )
174{
175 if (lua_toboolean(L,1))
176 lua_pushnumber(L, music_getVolumeLog() );
177 else
178 lua_pushnumber(L, music_getVolume() );
179 return 1;
180}
MusicInfo_t * music_info(void)
Gets information about the current music state.
Definition music.c:314
int music_play(const char *filename)
Plays the loaded music.
Definition music.c:236
int music_disabled
Definition music.c:32
int music_pause(int disable)
Pauses the music.
Definition music.c:278
double music_getVolumeLog(void)
Gets the current music volume (logarithmic).
Definition music.c:228
double music_getVolume(void)
Gets the current music volume (linear).
Definition music.c:218
int music_choose(const char *situation)
Actually runs the music stuff, based on situation.
Definition music.c:412
int music_stop(int disable)
Stops the loaded music.
Definition music.c:260
Header file with generic functions and naev-specifics.
static int musicL_isPlaying(lua_State *L)
Checks to see if something is playing.
Definition nlua_music.c:127
static int musicL_choose(lua_State *L)
Delays a rechoose.
Definition nlua_music.c:72
static int musicL_play(lua_State *L)
Plays the loaded song. Will resume paused songs.
Definition nlua_music.c:85
static int musicL_stop(lua_State *L)
Stops playing the current song.
Definition nlua_music.c:113
int nlua_loadMusic(nlua_env env)
Music Lua module.
Definition nlua_music.c:58
static int musicL_current(lua_State *L)
Gets the name of the current playing song.
Definition nlua_music.c:150
static int musicL_pause(lua_State *L)
Pauses the music engine.
Definition nlua_music.c:99
static int musicL_getVolume(lua_State *L)
Gets the volume level of the music.
Definition nlua_music.c:173
static const luaL_Reg music_methods[]
Definition nlua_music.c:30