Skip to content

Commit

Permalink
Allow for Game-Specific Menu Music and Add sound_stop_all()
Browse files Browse the repository at this point in the history
Allows Menu music to be provided for indivudal games in games/gamename/menu/gamename.ogg

Add sound_stop_all() lua_api function, no handles required
  • Loading branch information
ExeVirus committed May 4, 2021
1 parent de85bc9 commit ccf67cf
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 9 deletions.
2 changes: 0 additions & 2 deletions builtin/mainmenu/init.lua
Expand Up @@ -121,8 +121,6 @@ local function init_globals()
tv_main:show()

ui.update()

core.sound_play("main_menu", true)
end

init_globals()
11 changes: 11 additions & 0 deletions builtin/mainmenu/tab_local.lua
Expand Up @@ -52,6 +52,11 @@ if enable_gamebar then
core.set_topleft_text(pkgmgr.games[j].name)
core.settings:set("menu_last_game",pkgmgr.games[j].id)
menudata.worldlist:set_filtercriteria(pkgmgr.games[j].id)
--switch game sounds, if found
core.sound_stop_all()
local _,_,technical_game_name = string.find(pkgmgr.games[j].gamemods_path, ".*\\(.*)\\.*")
core.sound_play(technical_game_name, true)
core.log("Playing sound: " .. technical_game_name)
local index = filterlist.get_current_index(menudata.worldlist,
tonumber(core.settings:get("mainmenu_last_selected_world")))
if not index or index < 1 then
Expand Down Expand Up @@ -308,6 +313,9 @@ if enable_gamebar then
menudata.worldlist:set_filtercriteria(game.id)
core.set_topleft_text(game.name)
mm_texture.update("singleplayer",game)
core.sound_stop_all()
local _,_,technical_game_name = string.find(game.gamemods_path, ".*\\(.*)\\.*")
core.sound_play(technical_game_name, true)
end

singleplayer_refresh_gamebar()
Expand All @@ -319,6 +327,9 @@ if enable_gamebar then
gamebar:hide()
end
core.set_topleft_text("")
core.sound_stop_all()
core.sound_play("main_menu", true)
core.log("Playing sound: " .. "main_menu")
mm_texture.update(new_tab,nil)
end
end
Expand Down
2 changes: 2 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -5444,6 +5444,8 @@ Sounds
player actions (e.g. door closing).
* `minetest.sound_stop(handle)`
* `handle` is a handle returned by `minetest.sound_play`
* `minetest.sound_stop_all()`
Stops all currently playing non-ephermeral sounds.
* `minetest.sound_fade(handle, step, gain)`
* `handle` is a handle returned by `minetest.sound_play`
* `step` determines how fast a sound will fade.
Expand Down
2 changes: 2 additions & 0 deletions src/client/sound.h
Expand Up @@ -56,6 +56,7 @@ class ISoundManager
virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
float pitch = 1.0f) = 0;
virtual void stopSound(int sound) = 0;
virtual void stopAllSounds() = 0;
virtual bool soundExists(int sound) = 0;
virtual void updateSoundPosition(int sound, v3f pos) = 0;
virtual bool updateSoundGain(int id, float gain) = 0;
Expand Down Expand Up @@ -99,6 +100,7 @@ class DummySoundManager : public ISoundManager
return 0;
}
void stopSound(int sound) {}
void stopAllSounds() {}
bool soundExists(int sound) { return false; }
void updateSoundPosition(int sound, v3f pos) {}
bool updateSoundGain(int id, float gain) { return false; }
Expand Down
16 changes: 16 additions & 0 deletions src/client/sound_openal.cpp
Expand Up @@ -487,6 +487,16 @@ class OpenALSoundManager: public ISoundManager
m_sounds_playing.erase(id);
}

void deleteAllSounds()
{
for (auto it = m_sounds_playing.begin(); it != m_sounds_playing.end(); it++) {
PlayingSound *sound = it->second;
alDeleteSources(1, &sound->source_id);
delete sound;
}
m_sounds_playing.clear();
}

/* If buffer does not exist, consult the fetcher */
SoundBuffer* getFetchBuffer(const std::string &name)
{
Expand Down Expand Up @@ -613,6 +623,12 @@ class OpenALSoundManager: public ISoundManager
deleteSound(sound);
}

void stopAllSounds()
{
maintain();
deleteAllSounds();
}

void fadeSound(int soundid, float step, float gain)
{
// Ignore the command if step isn't valid.
Expand Down
38 changes: 31 additions & 7 deletions src/gui/guiEngine.cpp
Expand Up @@ -105,15 +105,33 @@ void MenuMusicFetcher::fetchSounds(const std::string &name,
return;
m_fetched.insert(name);
std::string base;
base = porting::path_share + DIR_DELIM + "sounds";
dst_paths.insert(base + DIR_DELIM + name + ".ogg");
std::vector<fs::DirListNode> list;
base = porting::path_share + DIR_DELIM;
// Add standard main menu sounds
dst_paths.insert(base + "sounds" + DIR_DELIM + name + ".ogg");
int i;
for(i=0; i<10; i++)
dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
base = porting::path_user + DIR_DELIM + "sounds";
dst_paths.insert(base + DIR_DELIM + name + ".ogg");
for(i=0; i<10; i++)
dst_paths.insert(base + DIR_DELIM + name + "."+itos(i)+".ogg");
dst_paths.insert(base + "sounds" + DIR_DELIM + name + "."+itos(i)+".ogg");
// Add game specific main menu sounds
dst_paths.insert(base + "games" + DIR_DELIM + name + DIR_DELIM + "menu" +
DIR_DELIM + name + ".ogg");
for (i = 0; i < 10; i++)
dst_paths.insert(base + "games" + DIR_DELIM + name + DIR_DELIM + "menu" +
DIR_DELIM + name +"." + itos(i) +
".ogg");

base = porting::path_user + DIR_DELIM;
// Add standard main menu sounds
dst_paths.insert(base + "sounds" + DIR_DELIM + name + ".ogg");
for (i = 0; i < 10; i++)
dst_paths.insert(base + "sounds" + DIR_DELIM + name + "." + itos(i) +
".ogg");
// Add game specific main menu sounds
dst_paths.insert(base + "games" + DIR_DELIM + name + DIR_DELIM + "menu" +
DIR_DELIM + name + ".ogg");
for (i = 0; i < 10; i++)
dst_paths.insert(base + "games" + DIR_DELIM + name + DIR_DELIM + "menu" +
DIR_DELIM + name + "." + itos(i) + ".ogg");
}

/******************************************************************************/
Expand Down Expand Up @@ -615,6 +633,12 @@ void GUIEngine::stopSound(s32 handle)
m_sound_manager->stopSound(handle);
}

/******************************************************************************/
void GUIEngine::stopAllSounds()
{
m_sound_manager->stopAllSounds();
}

/******************************************************************************/
unsigned int GUIEngine::queueAsync(const std::string &serialized_func,
const std::string &serialized_params)
Expand Down
2 changes: 2 additions & 0 deletions src/gui/guiEngine.h
Expand Up @@ -303,6 +303,8 @@ class GUIEngine {
s32 playSound(const SimpleSoundSpec &spec, bool looped);
/** stop playing a sound started with playSound() */
void stopSound(s32 handle);
/** stop playing all playing sounds started with playSound() */
void stopAllSounds();


};
9 changes: 9 additions & 0 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -305,6 +305,14 @@ int ModApiClient::l_sound_stop(lua_State *L)
return 0;
}

// sound_stop(handle)
int ModApiClient::l_sound_stop_all(lua_State *L)
{
getClient(L)->getSoundManager()->stopAllSounds();

return 0;
}

// sound_fade(handle, step, gain)
int ModApiClient::l_sound_fade(lua_State *L)
{
Expand Down Expand Up @@ -433,6 +441,7 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(get_meta);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_stop_all);
API_FCT(sound_fade);
API_FCT(get_server_info);
API_FCT(get_item_def);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_client.h
Expand Up @@ -84,6 +84,9 @@ class ModApiClient : public ModApiBase
// sound_stop(handle)
static int l_sound_stop(lua_State *L);

// sound_stop_all()
static int l_sound_stop_all(lua_State *L);

// sound_fade(handle, step, gain)
static int l_sound_fade(lua_State *L);

Expand Down
9 changes: 9 additions & 0 deletions src/script/lua_api/l_server.cpp
Expand Up @@ -441,6 +441,14 @@ int ModApiServer::l_sound_stop(lua_State *L)
return 0;
}

// sound_stop_all()
int ModApiServer::l_sound_stop_all(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
getServer(L)->stopAllSounds();
return 0;
}

int ModApiServer::l_sound_fade(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
Expand Down Expand Up @@ -542,6 +550,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(show_formspec);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_stop_all);
API_FCT(sound_fade);
API_FCT(dynamic_add_media_raw);

Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_server.h
Expand Up @@ -67,6 +67,9 @@ class ModApiServer : public ModApiBase
// sound_stop(handle)
static int l_sound_stop(lua_State *L);

// sound_stop_all()
static int l_sound_stop_all(lua_State *L);

// sound_fade(handle, step, gain)
static int l_sound_fade(lua_State *L);

Expand Down
7 changes: 7 additions & 0 deletions src/script/lua_api/l_sound.cpp
Expand Up @@ -46,8 +46,15 @@ int ModApiSound::l_sound_stop(lua_State *L)
return 1;
}

int ModApiSound::l_sound_stop_all(lua_State *L)
{
getGuiEngine(L)->stopAllSounds();
return 0;
}

void ModApiSound::Initialize(lua_State *L, int top)
{
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_stop_all);
}
1 change: 1 addition & 0 deletions src/script/lua_api/l_sound.h
Expand Up @@ -27,6 +27,7 @@ class ModApiSound : public ModApiBase
private:
static int l_sound_play(lua_State *L);
static int l_sound_stop(lua_State *L);
static int l_sound_stop_all(lua_State *L);

public:
static void Initialize(lua_State *L, int top);
Expand Down
21 changes: 21 additions & 0 deletions src/server.cpp
Expand Up @@ -2132,6 +2132,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
}
return id;
}

void Server::stopSound(s32 handle)
{
// Get sound reference
Expand All @@ -2153,6 +2154,26 @@ void Server::stopSound(s32 handle)
m_playing_sounds.erase(i);
}

void Server::stopAllSounds()
{
// Loop through references
for (auto it = m_playing_sounds.begin(); it != m_playing_sounds.end(); it++) {
ServerPlayingSound &psound = it->second;

NetworkPacket pkt(TOCLIENT_STOP_SOUND, 4);
pkt << it->first;

for (std::unordered_set<session_t>::const_iterator si =
psound.clients.begin();
si != psound.clients.end(); ++si) {
// Send as reliable
m_clients.send(*si, 0, &pkt, true);
}
}
// Remove sound references
m_playing_sounds.clear();
}

void Server::fadeSound(s32 handle, float step, float gain)
{
// Get sound reference
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Expand Up @@ -233,6 +233,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
s32 playSound(const SimpleSoundSpec &spec, const ServerSoundParams &params,
bool ephemeral=false);
void stopSound(s32 handle);
void stopAllSounds();
void fadeSound(s32 handle, float step, float gain);

// Envlock
Expand Down

0 comments on commit ccf67cf

Please sign in to comment.