From 840b47b2b662bc6ac16614227078e68448e34bc1 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Tue, 26 Dec 2023 15:41:00 -0800 Subject: [PATCH] Add music_is_playing() --- dev/src/audio.cpp | 7 +++++++ dev/src/lobster/sdlinterface.h | 1 + dev/src/sdlaudiosfxr.cpp | 7 +++++++ docs/builtin_functions_reference.html | 3 ++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dev/src/audio.cpp b/dev/src/audio.cpp index 4a0d676dc..360d3684d 100644 --- a/dev/src/audio.cpp +++ b/dev/src/audio.cpp @@ -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) { diff --git a/dev/src/lobster/sdlinterface.h b/dev/src/lobster/sdlinterface.h index 48f35937a..660553915 100644 --- a/dev/src/lobster/sdlinterface.h +++ b/dev/src/lobster/sdlinterface.h @@ -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); diff --git a/dev/src/sdlaudiosfxr.cpp b/dev/src/sdlaudiosfxr.cpp index b8201445c..bb46ff9a6 100644 --- a/dev/src/sdlaudiosfxr.cpp +++ b/dev/src/sdlaudiosfxr.cpp @@ -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)); } diff --git a/docs/builtin_functions_reference.html b/docs/builtin_functions_reference.html index ef4f60dfd..9c3473004 100644 --- a/docs/builtin_functions_reference.html +++ b/docs/builtin_functions_reference.html @@ -438,6 +438,7 @@

sound

music_pause(mus_id:int)pause music with the given id. music_resume(mus_id:int)resume music with the given id. music_volume(mus_id:int, vol:float)set the music volume in the range 0..1. +music_is_playing(mus_id:int) -> intreturns whether the music with the given id has not yet finished. Paused music is still considered to be playing music_set_general_volume(vol:float)set the general music volume in the range 0..1.

physics

@@ -621,7 +622,7 @@

imgui

im.sliderfloat4(label:string, F4:float4, min:float, max:float) -> float4 im.coloredit(label:string, color:vec_f) -> any im.image(tex:resource<texture>, size:float2, flip:bool = false) -im.image_button(label:string, tex:resource<texture>, size:float2, bgcol:float4 = nil) -> int +im.image_button(label:string, tex:resource<texture>, size:float2, bgcol:float4 = nil, flip:bool = false) -> int im.image_mouseclick(tex:resource<texture>, size:float2) -> float2, int im.treenode_start(label:string, flags:int) -> int(use im.treenode instead) im.treenode_end()