Skip to content

Commit

Permalink
Add music_is_playing()
Browse files Browse the repository at this point in the history
  • Loading branch information
binji committed Dec 26, 2023
1 parent 093245e commit 840b47b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions dev/src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ nfr("music_volume", "mus_id,vol", "IF", "",
return NilVal();
});

nfr("music_is_playing", "mus_id", "I", "B",
"returns whether the music with the given id has not yet finished. Paused music is still considered to be playing",
[](StackPtr &, VM &, Value &mus_id) {
auto is_playing = SDLMusicIsPlaying(mus_id.intval());
return Value(is_playing);
});

nfr("music_set_general_volume", "vol", "F", "",
"set the general music volume in the range 0..1.",
[](StackPtr &, VM &, Value &vol) {
Expand Down
1 change: 1 addition & 0 deletions dev/src/lobster/sdlinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ extern void SDLHaltMusic(int mus_id);
extern void SDLPauseMusic(int mus_id);
extern void SDLResumeMusic(int mus_id);
extern void SDLSetMusicVolume(int mus_id, float vol);
extern bool SDLMusicIsPlaying(int mus_id);
extern void SDLSetGeneralMusicVolume(float vol);

extern int64_t SDLLoadFile(string_view_nt absfilename, string *dest, int64_t start, int64_t len);
Expand Down
7 changes: 7 additions & 0 deletions dev/src/sdlaudiosfxr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ void SDLSetMusicVolume(int mus_id, float vol) {
Mix_VolumeMusicStream(mus, (int)(MIX_MAX_VOLUME * vol));
}

bool SDLMusicIsPlaying(int mus_id) {
if (mus_id <= 0 || mus_id - 1 >= (int)playing_music.size()) {
return false;
}
return !*playing_music[mus_id - 1].finished;
}

void SDLSetGeneralMusicVolume(float vol) {
Mix_VolumeMusicGeneral((int)(MIX_MAX_VOLUME * vol));
}
Expand Down
3 changes: 2 additions & 1 deletion docs/builtin_functions_reference.html
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ <h3>sound</h3>
<tr class="a" valign=top><td class="a"><tt><b>music_pause</b>(mus_id<font color="#666666">:int</font>)</tt></td><td class="a">pause music with the given id.</td></tr>
<tr class="a" valign=top><td class="a"><tt><b>music_resume</b>(mus_id<font color="#666666">:int</font>)</tt></td><td class="a">resume music with the given id.</td></tr>
<tr class="a" valign=top><td class="a"><tt><b>music_volume</b>(mus_id<font color="#666666">:int</font>, vol<font color="#666666">:float</font>)</tt></td><td class="a">set the music volume in the range 0..1.</td></tr>
<tr class="a" valign=top><td class="a"><tt><b>music_is_playing</b>(mus_id<font color="#666666">:int</font>) -> <font color="#666666">int</font></tt></td><td class="a">returns whether the music with the given id has not yet finished. Paused music is still considered to be playing</td></tr>
<tr class="a" valign=top><td class="a"><tt><b>music_set_general_volume</b>(vol<font color="#666666">:float</font>)</tt></td><td class="a">set the general music volume in the range 0..1.</td></tr>
</table>
<h3>physics</h3>
Expand Down Expand Up @@ -621,7 +622,7 @@ <h3>imgui</h3>
<tr class="a" valign=top><td class="a"><tt><b>im.sliderfloat4</b>(label<font color="#666666">:string</font>, F4<font color="#666666">:float4</font>, min<font color="#666666">:float</font>, max<font color="#666666">:float</font>) -> <font color="#666666">float4</font></tt></td><td class="a"></td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.coloredit</b>(label<font color="#666666">:string</font>, color<font color="#666666">:vec_f</font>) -> <font color="#666666">any</font></tt></td><td class="a"></td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.image</b>(tex<font color="#666666">:resource&lt;texture&gt;</font>, size<font color="#666666">:float2</font>, flip<font color="#666666">:bool</font> = false)</tt></td><td class="a"></td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.image_button</b>(label<font color="#666666">:string</font>, tex<font color="#666666">:resource&lt;texture&gt;</font>, size<font color="#666666">:float2</font>, bgcol<font color="#666666">:float4</font> = nil) -> <font color="#666666">int</font></tt></td><td class="a"></td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.image_button</b>(label<font color="#666666">:string</font>, tex<font color="#666666">:resource&lt;texture&gt;</font>, size<font color="#666666">:float2</font>, bgcol<font color="#666666">:float4</font> = nil, flip<font color="#666666">:bool</font> = false) -> <font color="#666666">int</font></tt></td><td class="a"></td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.image_mouseclick</b>(tex<font color="#666666">:resource&lt;texture&gt;</font>, size<font color="#666666">:float2</font>) -> <font color="#666666">float2</font>, <font color="#666666">int</font></tt></td><td class="a"></td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.treenode_start</b>(label<font color="#666666">:string</font>, flags<font color="#666666">:int</font>) -> <font color="#666666">int</font></tt></td><td class="a">(use im.treenode instead)</td></tr>
<tr class="a" valign=top><td class="a"><tt><b>im.treenode_end</b>()</tt></td><td class="a"></td></tr>
Expand Down

0 comments on commit 840b47b

Please sign in to comment.