Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for Game-Specific Menu Music #11241

Merged
merged 10 commits into from Nov 22, 2021
3 changes: 1 addition & 2 deletions builtin/mainmenu/init.lua
Expand Up @@ -36,6 +36,7 @@ dofile(menupath .. DIR_DELIM .. "common.lua")
dofile(menupath .. DIR_DELIM .. "pkgmgr.lua")
dofile(menupath .. DIR_DELIM .. "serverlistmgr.lua")
dofile(menupath .. DIR_DELIM .. "textures.lua")
dofile(menupath .. DIR_DELIM .. "music_player.lua")

dofile(menupath .. DIR_DELIM .. "dlg_config_world.lua")
dofile(menupath .. DIR_DELIM .. "dlg_settings_advanced.lua")
Expand Down Expand Up @@ -121,8 +122,6 @@ local function init_globals()
tv_main:show()

ui.update()

core.sound_play("main_menu", true)
end

init_globals()
33 changes: 33 additions & 0 deletions builtin/mainmenu/music_player.lua
@@ -0,0 +1,33 @@
--Minetest
--Copyright (C) 2021 ExeVirus
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
--the Free Software Foundation; either version 2.1 of the License, or
--(at your option) any later version.
--
--This program is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU Lesser General Public License for more details.
--
--You should have received a copy of the GNU Lesser General Public License along
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

--local static variables
local current_file = ""
local current_music_handle = nil

-- Stops current music, if not the same as the requested file, then plays the new file on repeat
function menu_music_play(file)
if file == current_file then
return
end
if current_music_handle ~= nil then
core.sound_stop(current_music_handle)
end
minetest.log("playing: " .. file)
current_music_handle = core.sound_play(file, true)
current_file = file
end
6 changes: 6 additions & 0 deletions builtin/mainmenu/tab_local.lua
Expand Up @@ -56,6 +56,8 @@ if enable_gamebar then
if ("game_btnbar_" .. pkgmgr.games[j].id == key) then
mm_texture.update("singleplayer", pkgmgr.games[j])
core.set_topleft_text(pkgmgr.games[j].name)
-- Switch game theme, if found, and current game theme is not already playing
menu_music_play(pkgmgr.games[j].path .. DIR_DELIM .. "menu" .. DIR_DELIM .. "theme")
core.settings:set("menu_last_game",pkgmgr.games[j].id)
menudata.worldlist:set_filtercriteria(pkgmgr.games[j].id)
local index = filterlist.get_current_index(menudata.worldlist,
Expand Down Expand Up @@ -376,6 +378,9 @@ if enable_gamebar then
menudata.worldlist:set_filtercriteria(game.id)
core.set_topleft_text(game.name)
mm_texture.update("singleplayer",game)
if old_tab ~= new_tab then
menu_music_play(game.path .. DIR_DELIM .. "menu" .. DIR_DELIM .. "theme")
end
end

singleplayer_refresh_gamebar()
Expand All @@ -387,6 +392,7 @@ if enable_gamebar then
gamebar:hide()
end
core.set_topleft_text("")
menu_music_play("main_menu")
mm_texture.update(new_tab,nil)
end
end
Expand Down
10 changes: 10 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -113,8 +113,16 @@ If you want to specify multiple images for one identifier, add additional
images named like `$identifier.$n.png`, with an ascending number $n starting
with 1, and a random image will be chosen from the provided ones.

Menu music
-----------

Games can provide custom main menu music. They are put inside a `menu`
directory inside the game directory.

The music files are named `theme.ogg`.
If you want to specify multiple music files for one game, add additional
images named like `theme.$n.ogg`, with an ascending number $n starting
with 1 (max 10), and a random music file will be chosen from the provided ones.

Mods
====
Expand Down Expand Up @@ -5608,6 +5616,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
29 changes: 18 additions & 11 deletions src/gui/guiEngine.cpp
Expand Up @@ -104,16 +104,23 @@ void MenuMusicFetcher::fetchSounds(const std::string &name,
if(m_fetched.count(name))
return;
m_fetched.insert(name);
std::string base;
base = porting::path_share + DIR_DELIM + "sounds";
dst_paths.insert(base + 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");
std::vector<fs::DirListNode> list;
// Reusable local function
auto add_paths = [&dst_paths](const std::string name, const std::string base = "") {
dst_paths.insert(base + name + ".ogg");
for (int i = 0; i < 10; i++)
dst_paths.insert(base + name + "." + itos(i) +
".ogg");
ShadowNinja marked this conversation as resolved.
Show resolved Hide resolved
};
// Allow full paths
if (name.find(DIR_DELIM_CHAR) != std::string::npos) {
add_paths(name);
} else {
std::string share_prefix = porting::path_share + DIR_DELIM;
add_paths(name, share_prefix + "sounds" + DIR_DELIM);
std::string user_prefix = porting::path_user + DIR_DELIM;
add_paths(name, user_prefix + "sounds" + DIR_DELIM);
}
}

/******************************************************************************/
Expand Down Expand Up @@ -626,4 +633,4 @@ s32 GUIEngine::playSound(const SimpleSoundSpec &spec, bool looped)
void GUIEngine::stopSound(s32 handle)
{
m_sound_manager->stopSound(handle);
}
ShadowNinja marked this conversation as resolved.
Show resolved Hide resolved
}