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