1 /*
2 * See Licensing and Copyright notice in naev.h
3 */
4
5 /**
6 * @file nlua_music.c
7 *
8 * @brief Lua music playing module.
9 */
10
11
12 #include "nlua_music.h"
13
14 #include "naev.h"
15
16 #include "SDL.h"
17
18 #include "nluadef.h"
19 #include "music.h"
20 #include "log.h"
21 #include "ndata.h"
22
23
24 /* Music methods. */
25 static int musicL_delay( lua_State* L );
26 static int musicL_load( lua_State* L );
27 static int musicL_play( lua_State* L );
28 static int musicL_stop( lua_State* L );
29 static int musicL_isPlaying( lua_State* L );
30 static int musicL_current( lua_State* L );
31 static const luaL_reg music_methods[] = {
32 { "delay", musicL_delay },
33 { "load", musicL_load },
34 { "play", musicL_play },
35 { "stop", musicL_stop },
36 { "isPlaying", musicL_isPlaying },
37 { "current", musicL_current },
38 {0,0}
39 }; /**< Music specific methods. */
40
41
42 /**
43 * @brief Music Lua module.
44 *
45 * Typical usage would be something like:
46 * @code
47 * music.load( "intro" ) -- Load the song
48 * music.play() -- Play it
49 * @endcode
50 *
51 * @luamod music
52 */
53
54
55 /**
56 * @brief Loads the music functions into a lua_State.
57 *
58 * @param env Lua environment to load the music functions into.
59 * @return 0 on success.
60 */
nlua_loadMusic(nlua_env env)61 int nlua_loadMusic( nlua_env env )
62 {
63 nlua_register(env, "music", music_methods, 0);
64 return 0;
65 }
66
67
68 /**
69 * @brief Delays a rechoose.
70 *
71 * @usage music.delay( "ambient", 5.0 ) -- Rechooses ambient in 5 seconds
72 *
73 * @luatparam string situation Situation to choose.
74 * @luatparam number delay Delay in seconds.
75 * @luafunc delay( situation, delay )
76 */
musicL_delay(lua_State * L)77 static int musicL_delay( lua_State* L )
78 {
79 const char *situation;
80 double delay;
81
82 /* Get parameters. */
83 situation = luaL_checkstring(L,1);
84 delay = luaL_checknumber(L,2);
85
86 /* Delay. */
87 music_chooseDelay( situation, delay );
88 return 0;
89 }
90
91
92 /**
93 * @brief Loads a song.
94 *
95 * @luatparam string name Name of the song to load.
96 * @luafunc load( name )
97 */
musicL_load(lua_State * L)98 static int musicL_load( lua_State *L )
99 {
100 const char* str;
101
102 /* check parameters */
103 str = luaL_checkstring(L,1);
104 if (music_load( str )) {
105 NLUA_ERROR(L,"Music '%s' invalid or failed to load.", str );
106 return 0;
107 }
108
109 return 0;
110 }
111
112
113 /**
114 * @brief Plays the loaded song.
115 *
116 * @luafunc play()
117 */
musicL_play(lua_State * L)118 static int musicL_play( lua_State *L )
119 {
120 (void)L;
121 music_play();
122 return 0;
123 }
124
125
126 /**
127 * @brief Stops playing the current song.
128 *
129 * @luafunc stop()
130 */
musicL_stop(lua_State * L)131 static int musicL_stop( lua_State *L )
132 {
133 (void)L;
134 music_stop();
135 return 0;
136 }
137
138
139 /**
140 * @brief Checks to see if something is playing.
141 *
142 * @luatreturn boolean true if something is playing.
143 * @luafunc isPlaying()
144 */
musicL_isPlaying(lua_State * L)145 static int musicL_isPlaying( lua_State* L )
146 {
147 lua_pushboolean(L, music_isPlaying());
148 return 1;
149 }
150
151
152 /**
153 * @brief Gets the name of the current playing song.
154 *
155 * @usage songname, songplayed = music.current()
156 *
157 * @luatreturn string The name of the current playing song or "none" if no song is playing.
158 * @luatreturn number The current offset inside the song (0. if music is none).
159 * @luafunc current()
160 */
musicL_current(lua_State * L)161 static int musicL_current( lua_State* L )
162 {
163 const char *music_name;
164
165 music_name = music_playingName();
166
167 lua_pushstring(L, (music_name != NULL) ? music_name : "none" );
168 lua_pushnumber(L, music_playingTime() );
169
170 return 2;
171 }
172
173
174
175