mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Sound refactor and improvements (#12764)
This commit is contained in:
parent
8e1af25738
commit
edcbfa31c9
52 changed files with 2802 additions and 1211 deletions
|
@ -104,10 +104,10 @@ void read_item_definition(lua_State* L, int index,
|
|||
if (!lua_isnil(L, -1)) {
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
lua_getfield(L, -1, "place");
|
||||
read_soundspec(L, -1, def.sound_place);
|
||||
read_simplesoundspec(L, -1, def.sound_place);
|
||||
lua_pop(L, 1);
|
||||
lua_getfield(L, -1, "place_failed");
|
||||
read_soundspec(L, -1, def.sound_place_failed);
|
||||
read_simplesoundspec(L, -1, def.sound_place_failed);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
@ -117,10 +117,10 @@ void read_item_definition(lua_State* L, int index,
|
|||
if (!lua_isnil(L, -1)) {
|
||||
luaL_checktype(L, -1, LUA_TTABLE);
|
||||
lua_getfield(L, -1, "punch_use");
|
||||
read_soundspec(L, -1, def.sound_use);
|
||||
read_simplesoundspec(L, -1, def.sound_use);
|
||||
lua_pop(L, 1);
|
||||
lua_getfield(L, -1, "punch_use_air");
|
||||
read_soundspec(L, -1, def.sound_use_air);
|
||||
read_simplesoundspec(L, -1, def.sound_use_air);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
@ -187,9 +187,9 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
|
|||
}
|
||||
push_groups(L, i.groups);
|
||||
lua_setfield(L, -2, "groups");
|
||||
push_soundspec(L, i.sound_place);
|
||||
push_simplesoundspec(L, i.sound_place);
|
||||
lua_setfield(L, -2, "sound_place");
|
||||
push_soundspec(L, i.sound_place_failed);
|
||||
push_simplesoundspec(L, i.sound_place_failed);
|
||||
lua_setfield(L, -2, "sound_place_failed");
|
||||
lua_pushstring(L, i.node_placement_prediction.c_str());
|
||||
lua_setfield(L, -2, "node_placement_prediction");
|
||||
|
@ -821,13 +821,13 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
|
|||
lua_getfield(L, index, "sounds");
|
||||
if(lua_istable(L, -1)){
|
||||
lua_getfield(L, -1, "footstep");
|
||||
read_soundspec(L, -1, f.sound_footstep);
|
||||
read_simplesoundspec(L, -1, f.sound_footstep);
|
||||
lua_pop(L, 1);
|
||||
lua_getfield(L, -1, "dig");
|
||||
read_soundspec(L, -1, f.sound_dig);
|
||||
read_simplesoundspec(L, -1, f.sound_dig);
|
||||
lua_pop(L, 1);
|
||||
lua_getfield(L, -1, "dug");
|
||||
read_soundspec(L, -1, f.sound_dug);
|
||||
read_simplesoundspec(L, -1, f.sound_dug);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
|
@ -965,11 +965,11 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
|
|||
push_nodebox(L, c.collision_box);
|
||||
lua_setfield(L, -2, "collision_box");
|
||||
lua_newtable(L);
|
||||
push_soundspec(L, c.sound_footstep);
|
||||
push_simplesoundspec(L, c.sound_footstep);
|
||||
lua_setfield(L, -2, "sound_footstep");
|
||||
push_soundspec(L, c.sound_dig);
|
||||
push_simplesoundspec(L, c.sound_dig);
|
||||
lua_setfield(L, -2, "sound_dig");
|
||||
push_soundspec(L, c.sound_dug);
|
||||
push_simplesoundspec(L, c.sound_dug);
|
||||
lua_setfield(L, -2, "sound_dug");
|
||||
lua_setfield(L, -2, "sounds");
|
||||
lua_pushboolean(L, c.legacy_facedir_simple);
|
||||
|
@ -1067,10 +1067,11 @@ void read_server_sound_params(lua_State *L, int index,
|
|||
if(index < 0)
|
||||
index = lua_gettop(L) + 1 + index;
|
||||
|
||||
if(lua_istable(L, index)){
|
||||
if (lua_istable(L, index)) {
|
||||
// Functional overlap: this may modify SimpleSoundSpec contents
|
||||
getfloatfield(L, index, "fade", params.spec.fade);
|
||||
getfloatfield(L, index, "pitch", params.spec.pitch);
|
||||
getfloatfield(L, index, "start_time", params.spec.start_time);
|
||||
getboolfield(L, index, "loop", params.spec.loop);
|
||||
|
||||
getfloatfield(L, index, "gain", params.gain);
|
||||
|
@ -1101,7 +1102,7 @@ void read_server_sound_params(lua_State *L, int index,
|
|||
}
|
||||
|
||||
/******************************************************************************/
|
||||
void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
|
||||
void read_simplesoundspec(lua_State *L, int index, SoundSpec &spec)
|
||||
{
|
||||
if(index < 0)
|
||||
index = lua_gettop(L) + 1 + index;
|
||||
|
@ -1118,7 +1119,7 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
|
|||
}
|
||||
}
|
||||
|
||||
void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
|
||||
void push_simplesoundspec(lua_State *L, const SoundSpec &spec)
|
||||
{
|
||||
lua_createtable(L, 0, 3);
|
||||
lua_pushstring(L, spec.name.c_str());
|
||||
|
|
|
@ -53,7 +53,7 @@ struct ItemStack;
|
|||
struct ItemDefinition;
|
||||
struct ToolCapabilities;
|
||||
struct ObjectProperties;
|
||||
struct SimpleSoundSpec;
|
||||
struct SoundSpec;
|
||||
struct ServerPlayingSound;
|
||||
class Inventory;
|
||||
class InventoryList;
|
||||
|
@ -87,8 +87,8 @@ void push_palette (lua_State *L,
|
|||
TileDef read_tiledef (lua_State *L, int index,
|
||||
u8 drawtype, bool special);
|
||||
|
||||
void read_soundspec (lua_State *L, int index,
|
||||
SimpleSoundSpec &spec);
|
||||
void read_simplesoundspec (lua_State *L, int index,
|
||||
SoundSpec &spec);
|
||||
NodeBox read_nodebox (lua_State *L, int index);
|
||||
|
||||
void read_server_sound_params (lua_State *L, int index,
|
||||
|
@ -167,8 +167,8 @@ std::vector<ItemStack> read_items (lua_State *L,
|
|||
int index,
|
||||
IGameDef* gdef);
|
||||
|
||||
void push_soundspec (lua_State *L,
|
||||
const SimpleSoundSpec &spec);
|
||||
void push_simplesoundspec (lua_State *L,
|
||||
const SoundSpec &spec);
|
||||
|
||||
bool string_to_enum (const EnumString *spec,
|
||||
int &result,
|
||||
|
|
|
@ -28,10 +28,11 @@ set(common_SCRIPT_LUA_API_SRCS
|
|||
set(client_SCRIPT_LUA_API_SRCS
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_camera.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_client.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_client_sound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_localplayer.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu_sound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_minimap.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_particles_local.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_sound.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/l_storage.cpp
|
||||
PARENT_SCOPE)
|
||||
|
|
|
@ -260,63 +260,6 @@ int ModApiClient::l_get_meta(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
// sound_play(spec, parameters)
|
||||
int ModApiClient::l_sound_play(lua_State *L)
|
||||
{
|
||||
ISoundManager *sound = getClient(L)->getSoundManager();
|
||||
|
||||
SimpleSoundSpec spec;
|
||||
read_soundspec(L, 1, spec);
|
||||
|
||||
SoundLocation type = SoundLocation::Local;
|
||||
float gain = 1.0f;
|
||||
v3f position;
|
||||
|
||||
if (lua_istable(L, 2)) {
|
||||
getfloatfield(L, 2, "gain", gain);
|
||||
getfloatfield(L, 2, "pitch", spec.pitch);
|
||||
getboolfield(L, 2, "loop", spec.loop);
|
||||
|
||||
lua_getfield(L, 2, "pos");
|
||||
if (!lua_isnil(L, -1)) {
|
||||
position = read_v3f(L, -1) * BS;
|
||||
type = SoundLocation::Position;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
spec.gain *= gain;
|
||||
|
||||
s32 handle;
|
||||
if (type == SoundLocation::Local)
|
||||
handle = sound->playSound(spec);
|
||||
else
|
||||
handle = sound->playSoundAt(spec, position);
|
||||
|
||||
lua_pushinteger(L, handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// sound_stop(handle)
|
||||
int ModApiClient::l_sound_stop(lua_State *L)
|
||||
{
|
||||
s32 handle = luaL_checkinteger(L, 1);
|
||||
|
||||
getClient(L)->getSoundManager()->stopSound(handle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// sound_fade(handle, step, gain)
|
||||
int ModApiClient::l_sound_fade(lua_State *L)
|
||||
{
|
||||
s32 handle = luaL_checkinteger(L, 1);
|
||||
float step = readParam<float>(L, 2);
|
||||
float gain = readParam<float>(L, 3);
|
||||
getClient(L)->getSoundManager()->fadeSound(handle, step, gain);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get_server_info()
|
||||
int ModApiClient::l_get_server_info(lua_State *L)
|
||||
{
|
||||
|
@ -433,9 +376,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
|
|||
API_FCT(get_node_or_nil);
|
||||
API_FCT(disconnect);
|
||||
API_FCT(get_meta);
|
||||
API_FCT(sound_play);
|
||||
API_FCT(sound_stop);
|
||||
API_FCT(sound_fade);
|
||||
API_FCT(get_server_info);
|
||||
API_FCT(get_item_def);
|
||||
API_FCT(get_node_def);
|
||||
|
|
|
@ -78,15 +78,6 @@ private:
|
|||
// get_meta(pos)
|
||||
static int l_get_meta(lua_State *L);
|
||||
|
||||
// sound_play(spec, parameters)
|
||||
static int l_sound_play(lua_State *L);
|
||||
|
||||
// sound_stop(handle)
|
||||
static int l_sound_stop(lua_State *L);
|
||||
|
||||
// sound_fade(handle, step, gain)
|
||||
static int l_sound_fade(lua_State *L);
|
||||
|
||||
// get_server_info()
|
||||
static int l_get_server_info(lua_State *L);
|
||||
|
||||
|
|
150
src/script/lua_api/l_client_sound.cpp
Normal file
150
src/script/lua_api/l_client_sound.cpp
Normal file
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
Minetest
|
||||
Copyright (C) 2023 DS
|
||||
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "l_client_sound.h"
|
||||
#include "l_internal.h"
|
||||
#include "common/c_content.h"
|
||||
#include "common/c_converter.h"
|
||||
#include "client/client.h"
|
||||
#include "client/sound.h"
|
||||
|
||||
/* ModApiClientSound */
|
||||
|
||||
// sound_play(spec, parameters)
|
||||
int ModApiClientSound::l_sound_play(lua_State *L)
|
||||
{
|
||||
ISoundManager *sound_manager = getClient(L)->getSoundManager();
|
||||
|
||||
SoundSpec spec;
|
||||
read_simplesoundspec(L, 1, spec);
|
||||
|
||||
SoundLocation type = SoundLocation::Local;
|
||||
float gain = 1.0f;
|
||||
v3f position;
|
||||
|
||||
if (lua_istable(L, 2)) {
|
||||
getfloatfield(L, 2, "gain", gain);
|
||||
getfloatfield(L, 2, "pitch", spec.pitch);
|
||||
getboolfield(L, 2, "loop", spec.loop);
|
||||
|
||||
lua_getfield(L, 2, "pos");
|
||||
if (!lua_isnil(L, -1)) {
|
||||
position = read_v3f(L, -1);
|
||||
type = SoundLocation::Position;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
}
|
||||
|
||||
spec.gain *= gain;
|
||||
|
||||
sound_handle_t handle = sound_manager->allocateId(2);
|
||||
|
||||
if (type == SoundLocation::Local)
|
||||
sound_manager->playSound(handle, spec);
|
||||
else
|
||||
sound_manager->playSoundAt(handle, spec, position, v3f(0.0f));
|
||||
|
||||
ClientSoundHandle::create(L, handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ModApiClientSound::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(sound_play);
|
||||
}
|
||||
|
||||
/* ClientSoundHandle */
|
||||
|
||||
ClientSoundHandle *ClientSoundHandle::checkobject(lua_State *L, int narg)
|
||||
{
|
||||
luaL_checktype(L, narg, LUA_TUSERDATA);
|
||||
void *ud = luaL_checkudata(L, narg, className);
|
||||
if (!ud)
|
||||
luaL_typerror(L, narg, className);
|
||||
return *(ClientSoundHandle**)ud; // unbox pointer
|
||||
}
|
||||
|
||||
int ClientSoundHandle::gc_object(lua_State *L)
|
||||
{
|
||||
ClientSoundHandle *o = *(ClientSoundHandle **)(lua_touserdata(L, 1));
|
||||
if (getClient(L) && getClient(L)->getSoundManager())
|
||||
getClient(L)->getSoundManager()->freeId(o->m_handle);
|
||||
delete o;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// :stop()
|
||||
int ClientSoundHandle::l_stop(lua_State *L)
|
||||
{
|
||||
ClientSoundHandle *o = checkobject(L, 1);
|
||||
getClient(L)->getSoundManager()->stopSound(o->m_handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// :fade(step, gain)
|
||||
int ClientSoundHandle::l_fade(lua_State *L)
|
||||
{
|
||||
ClientSoundHandle *o = checkobject(L, 1);
|
||||
float step = readParam<float>(L, 2);
|
||||
float gain = readParam<float>(L, 3);
|
||||
getClient(L)->getSoundManager()->fadeSound(o->m_handle, step, gain);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ClientSoundHandle::create(lua_State *L, sound_handle_t handle)
|
||||
{
|
||||
ClientSoundHandle *o = new ClientSoundHandle(handle);
|
||||
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
||||
luaL_getmetatable(L, className);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
void ClientSoundHandle::Register(lua_State *L)
|
||||
{
|
||||
lua_newtable(L);
|
||||
int methodtable = lua_gettop(L);
|
||||
luaL_newmetatable(L, className);
|
||||
int metatable = lua_gettop(L);
|
||||
|
||||
lua_pushliteral(L, "__metatable");
|
||||
lua_pushvalue(L, methodtable);
|
||||
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
|
||||
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, methodtable);
|
||||
lua_settable(L, metatable);
|
||||
|
||||
lua_pushliteral(L, "__gc");
|
||||
lua_pushcfunction(L, gc_object);
|
||||
lua_settable(L, metatable);
|
||||
|
||||
lua_pop(L, 1); // drop metatable
|
||||
|
||||
luaL_register(L, nullptr, methods); // fill methodtable
|
||||
lua_pop(L, 1); // drop methodtable
|
||||
}
|
||||
|
||||
const char ClientSoundHandle::className[] = "ClientSoundHandle";
|
||||
const luaL_Reg ClientSoundHandle::methods[] = {
|
||||
luamethod(ClientSoundHandle, stop),
|
||||
luamethod(ClientSoundHandle, fade),
|
||||
{0,0}
|
||||
};
|
66
src/script/lua_api/l_client_sound.h
Normal file
66
src/script/lua_api/l_client_sound.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
Minetest
|
||||
Copyright (C) 2023 DS
|
||||
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lua_api/l_base.h"
|
||||
#include "util/basic_macros.h"
|
||||
|
||||
using sound_handle_t = int;
|
||||
|
||||
class ModApiClientSound : public ModApiBase
|
||||
{
|
||||
private:
|
||||
// sound_play(spec, parameters)
|
||||
static int l_sound_play(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
};
|
||||
|
||||
class ClientSoundHandle final : public ModApiBase
|
||||
{
|
||||
private:
|
||||
sound_handle_t m_handle;
|
||||
|
||||
static const char className[];
|
||||
static const luaL_Reg methods[];
|
||||
|
||||
ClientSoundHandle(sound_handle_t handle) : m_handle(handle) {}
|
||||
|
||||
DISABLE_CLASS_COPY(ClientSoundHandle)
|
||||
|
||||
static ClientSoundHandle *checkobject(lua_State *L, int narg);
|
||||
|
||||
static int gc_object(lua_State *L);
|
||||
|
||||
// :stop()
|
||||
static int l_stop(lua_State *L);
|
||||
|
||||
// :fade(step, gain)
|
||||
static int l_fade(lua_State *L);
|
||||
|
||||
public:
|
||||
~ClientSoundHandle() = default;
|
||||
|
||||
static void create(lua_State *L, sound_handle_t handle);
|
||||
static void Register(lua_State *L);
|
||||
};
|
116
src/script/lua_api/l_mainmenu_sound.cpp
Normal file
116
src/script/lua_api/l_mainmenu_sound.cpp
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
Minetest
|
||||
Copyright (C) 2023 DS
|
||||
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "l_mainmenu_sound.h"
|
||||
#include "l_internal.h"
|
||||
#include "common/c_content.h"
|
||||
#include "gui/guiEngine.h"
|
||||
|
||||
/* ModApiMainMenuSound */
|
||||
|
||||
// sound_play(spec, loop)
|
||||
int ModApiMainMenuSound::l_sound_play(lua_State *L)
|
||||
{
|
||||
SoundSpec spec;
|
||||
read_simplesoundspec(L, 1, spec);
|
||||
spec.loop = readParam<bool>(L, 2);
|
||||
|
||||
ISoundManager &sound_manager = *getGuiEngine(L)->m_sound_manager;
|
||||
|
||||
sound_handle_t handle = sound_manager.allocateId(2);
|
||||
sound_manager.playSound(handle, spec);
|
||||
|
||||
MainMenuSoundHandle::create(L, handle);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ModApiMainMenuSound::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(sound_play);
|
||||
}
|
||||
|
||||
/* MainMenuSoundHandle */
|
||||
|
||||
MainMenuSoundHandle *MainMenuSoundHandle::checkobject(lua_State *L, int narg)
|
||||
{
|
||||
luaL_checktype(L, narg, LUA_TUSERDATA);
|
||||
void *ud = luaL_checkudata(L, narg, className);
|
||||
if (!ud)
|
||||
luaL_typerror(L, narg, className);
|
||||
return *(MainMenuSoundHandle**)ud; // unbox pointer
|
||||
}
|
||||
|
||||
int MainMenuSoundHandle::gc_object(lua_State *L)
|
||||
{
|
||||
MainMenuSoundHandle *o = *(MainMenuSoundHandle **)(lua_touserdata(L, 1));
|
||||
if (getGuiEngine(L) && getGuiEngine(L)->m_sound_manager)
|
||||
getGuiEngine(L)->m_sound_manager->freeId(o->m_handle);
|
||||
delete o;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// :stop()
|
||||
int MainMenuSoundHandle::l_stop(lua_State *L)
|
||||
{
|
||||
MainMenuSoundHandle *o = checkobject(L, 1);
|
||||
getGuiEngine(L)->m_sound_manager->stopSound(o->m_handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MainMenuSoundHandle::create(lua_State *L, sound_handle_t handle)
|
||||
{
|
||||
MainMenuSoundHandle *o = new MainMenuSoundHandle(handle);
|
||||
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
||||
luaL_getmetatable(L, className);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
void MainMenuSoundHandle::Register(lua_State *L)
|
||||
{
|
||||
lua_newtable(L);
|
||||
int methodtable = lua_gettop(L);
|
||||
luaL_newmetatable(L, className);
|
||||
int metatable = lua_gettop(L);
|
||||
|
||||
lua_pushliteral(L, "__metatable");
|
||||
lua_pushvalue(L, methodtable);
|
||||
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
|
||||
|
||||
lua_pushliteral(L, "__index");
|
||||
lua_pushvalue(L, methodtable);
|
||||
lua_settable(L, metatable);
|
||||
|
||||
lua_pushliteral(L, "__gc");
|
||||
lua_pushcfunction(L, gc_object);
|
||||
lua_settable(L, metatable);
|
||||
|
||||
lua_pop(L, 1); // drop metatable
|
||||
|
||||
luaL_register(L, nullptr, methods); // fill methodtable
|
||||
lua_pop(L, 1); // drop methodtable
|
||||
}
|
||||
|
||||
const char MainMenuSoundHandle::className[] = "MainMenuSoundHandle";
|
||||
const luaL_Reg MainMenuSoundHandle::methods[] = {
|
||||
luamethod(MainMenuSoundHandle, stop),
|
||||
{0,0}
|
||||
};
|
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
Minetest
|
||||
Copyright (C) 2023 DS
|
||||
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
|
||||
|
@ -21,13 +22,42 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#pragma once
|
||||
|
||||
#include "lua_api/l_base.h"
|
||||
#include "util/basic_macros.h"
|
||||
|
||||
class ModApiSound : public ModApiBase
|
||||
using sound_handle_t = int;
|
||||
|
||||
class ModApiMainMenuSound : public ModApiBase
|
||||
{
|
||||
private:
|
||||
// sound_play(spec, loop)
|
||||
static int l_sound_play(lua_State *L);
|
||||
static int l_sound_stop(lua_State *L);
|
||||
|
||||
public:
|
||||
static void Initialize(lua_State *L, int top);
|
||||
};
|
||||
|
||||
class MainMenuSoundHandle final : public ModApiBase
|
||||
{
|
||||
private:
|
||||
sound_handle_t m_handle;
|
||||
|
||||
static const char className[];
|
||||
static const luaL_Reg methods[];
|
||||
|
||||
MainMenuSoundHandle(sound_handle_t handle) : m_handle(handle) {}
|
||||
|
||||
DISABLE_CLASS_COPY(MainMenuSoundHandle)
|
||||
|
||||
static MainMenuSoundHandle *checkobject(lua_State *L, int narg);
|
||||
|
||||
static int gc_object(lua_State *L);
|
||||
|
||||
// :stop()
|
||||
static int l_stop(lua_State *L);
|
||||
|
||||
public:
|
||||
~MainMenuSoundHandle() = default;
|
||||
|
||||
static void create(lua_State *L, sound_handle_t handle);
|
||||
static void Register(lua_State *L);
|
||||
};
|
|
@ -503,7 +503,7 @@ int ModApiServer::l_sound_play(lua_State *L)
|
|||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
ServerPlayingSound params;
|
||||
read_soundspec(L, 1, params.spec);
|
||||
read_simplesoundspec(L, 1, params.spec);
|
||||
read_server_sound_params(L, 2, params);
|
||||
bool ephemeral = lua_gettop(L) > 2 && readParam<bool>(L, 3);
|
||||
if (ephemeral) {
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
Minetest
|
||||
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
||||
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "l_sound.h"
|
||||
#include "l_internal.h"
|
||||
#include "common/c_content.h"
|
||||
#include "gui/guiEngine.h"
|
||||
|
||||
|
||||
int ModApiSound::l_sound_play(lua_State *L)
|
||||
{
|
||||
SimpleSoundSpec spec;
|
||||
read_soundspec(L, 1, spec);
|
||||
spec.loop = readParam<bool>(L, 2);
|
||||
|
||||
s32 handle = getGuiEngine(L)->playSound(spec);
|
||||
|
||||
lua_pushinteger(L, handle);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ModApiSound::l_sound_stop(lua_State *L)
|
||||
{
|
||||
u32 handle = luaL_checkinteger(L, 1);
|
||||
|
||||
getGuiEngine(L)->stopSound(handle);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void ModApiSound::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(sound_play);
|
||||
API_FCT(sound_stop);
|
||||
}
|
|
@ -29,13 +29,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "lua_api/l_modchannels.h"
|
||||
#include "lua_api/l_particles_local.h"
|
||||
#include "lua_api/l_storage.h"
|
||||
#include "lua_api/l_sound.h"
|
||||
#include "lua_api/l_util.h"
|
||||
#include "lua_api/l_item.h"
|
||||
#include "lua_api/l_nodemeta.h"
|
||||
#include "lua_api/l_localplayer.h"
|
||||
#include "lua_api/l_camera.h"
|
||||
#include "lua_api/l_settings.h"
|
||||
#include "lua_api/l_client_sound.h"
|
||||
|
||||
ClientScripting::ClientScripting(Client *client):
|
||||
ScriptApiBase(ScriptingType::Client)
|
||||
|
@ -75,6 +75,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
|
|||
LuaCamera::Register(L);
|
||||
ModChannelRef::Register(L);
|
||||
LuaSettings::Register(L);
|
||||
ClientSoundHandle::Register(L);
|
||||
|
||||
ModApiUtil::InitializeClient(L, top);
|
||||
ModApiClient::Initialize(L, top);
|
||||
|
@ -83,6 +84,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
|
|||
ModApiEnvMod::InitializeClient(L, top);
|
||||
ModApiChannels::Initialize(L, top);
|
||||
ModApiParticlesLocal::Initialize(L, top);
|
||||
ModApiClientSound::Initialize(L, top);
|
||||
}
|
||||
|
||||
void ClientScripting::on_client_ready(LocalPlayer *localplayer)
|
||||
|
|
|
@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "lua_api/l_base.h"
|
||||
#include "lua_api/l_http.h"
|
||||
#include "lua_api/l_mainmenu.h"
|
||||
#include "lua_api/l_sound.h"
|
||||
#include "lua_api/l_mainmenu_sound.h"
|
||||
#include "lua_api/l_util.h"
|
||||
#include "lua_api/l_settings.h"
|
||||
#include "log.h"
|
||||
|
@ -66,7 +66,7 @@ void MainMenuScripting::initializeModApi(lua_State *L, int top)
|
|||
// Initialize mod API modules
|
||||
ModApiMainMenu::Initialize(L, top);
|
||||
ModApiUtil::Initialize(L, top);
|
||||
ModApiSound::Initialize(L, top);
|
||||
ModApiMainMenuSound::Initialize(L, top);
|
||||
ModApiHttp::Initialize(L, top);
|
||||
|
||||
asyncEngine.registerStateInitializer(registerLuaClasses);
|
||||
|
@ -83,6 +83,7 @@ void MainMenuScripting::initializeModApi(lua_State *L, int top)
|
|||
void MainMenuScripting::registerLuaClasses(lua_State *L, int top)
|
||||
{
|
||||
LuaSettings::Register(L);
|
||||
MainMenuSoundHandle::Register(L);
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue