diff options
Diffstat (limited to 'engines/sword25/sfx/soundengine_script.cpp')
-rwxr-xr-x | engines/sword25/sfx/soundengine_script.cpp | 397 |
1 files changed, 397 insertions, 0 deletions
diff --git a/engines/sword25/sfx/soundengine_script.cpp b/engines/sword25/sfx/soundengine_script.cpp new file mode 100755 index 0000000000..9cc3fcb153 --- /dev/null +++ b/engines/sword25/sfx/soundengine_script.cpp @@ -0,0 +1,397 @@ +// ----------------------------------------------------------------------------- +// This file is part of Broken Sword 2.5 +// Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdörfer +// +// Broken Sword 2.5 is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// Broken Sword 2.5 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Broken Sword 2.5; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// Includes +// ----------------------------------------------------------------------------- + +#include "kernel/common.h" +#include "kernel/kernel.h" +#include "script/script.h" +#include "script/luabindhelper.h" + +#include "soundengine.h" + +// ----------------------------------------------------------------------------- + +static int Init(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + if (lua_gettop(L) == 0) + lua_pushbooleancpp(L, pSfx->Init(44100, 32)); + else if (lua_gettop(L) == 1) + lua_pushbooleancpp(L, pSfx->Init(static_cast<unsigned int>(luaL_checknumber(L, 1)), 32)); + else + lua_pushbooleancpp(L, pSfx->Init(static_cast<unsigned int>(luaL_checknumber(L, 1)), static_cast<unsigned int>(luaL_checknumber(L, 2)))); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static int Update(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->Update(); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int SetVolume(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->SetVolume(static_cast<float>(luaL_checknumber(L, 1)), + static_cast<BS_SoundEngine::SOUND_TYPES>(static_cast<unsigned int>(luaL_checknumber(L, 2)))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int GetVolume(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + lua_pushnumber(L, pSfx->GetVolume(static_cast<BS_SoundEngine::SOUND_TYPES>(static_cast<unsigned int>(luaL_checknumber(L, 1))))); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static int PauseAll(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->PauseAll(); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int ResumeAll(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->ResumeAll(); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int PauseLayer(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->PauseLayer(static_cast<int>(luaL_checknumber(L, 1))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int ResumeLayer(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->ResumeLayer(static_cast<int>(luaL_checknumber(L, 1))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static void ProcessPlayParams(lua_State * L, std::string & FileName, BS_SoundEngine::SOUND_TYPES & Type, float & Volume, float & Pan, bool & Loop, int & LoopStart, int & LoopEnd, unsigned int & Layer) +{ + FileName = luaL_checkstring(L, 1); + + Type = static_cast<BS_SoundEngine::SOUND_TYPES>(static_cast<unsigned int>(luaL_checknumber(L, 2))); + + if (lua_gettop(L) < 3 || lua_isnil(L, 3)) Volume = 1.0f; + else Volume = static_cast<float>(luaL_checknumber(L, 3)); + + if (lua_gettop(L) < 4 || lua_isnil(L, 4)) Pan = 0.0f; + else Pan = static_cast<float>(luaL_checknumber(L, 4)); + + if (lua_gettop(L) < 5 || lua_isnil(L, 5)) Loop = false; + else Loop = lua_tobooleancpp(L, 5); + + if (lua_gettop(L) < 6 || lua_isnil(L, 6)) LoopStart = -1; + else LoopStart = static_cast<int>(luaL_checknumber(L, 6)); + + if (lua_gettop(L) < 7 || lua_isnil(L, 7)) LoopEnd = -1; + else LoopEnd = static_cast<int>(luaL_checknumber(L, 7)); + + if (lua_gettop(L) < 8 || lua_isnil(L, 8)) Layer = 0; + else Layer = static_cast<unsigned int>(luaL_checknumber(L, 8)); +} + +static int PlaySound(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + std::string FileName; + BS_SoundEngine::SOUND_TYPES Type; + float Volume; + float Pan; + bool Loop; + int LoopStart; + int LoopEnd; + unsigned int Layer; + ProcessPlayParams(L, FileName, Type, Volume, Pan, Loop, LoopStart, LoopEnd, Layer); + + lua_pushbooleancpp(L, pSfx->PlaySound(FileName, Type, Volume, Pan, Loop, LoopStart, LoopEnd, Layer)); + + return 1; +} + +static int PlaySoundEx(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + std::string FileName; + BS_SoundEngine::SOUND_TYPES Type; + float Volume; + float Pan; + bool Loop; + int LoopStart; + int LoopEnd; + unsigned int Layer; + ProcessPlayParams(L, FileName, Type, Volume, Pan, Loop, LoopStart, LoopEnd, Layer); + + lua_pushnumber(L, pSfx->PlaySoundEx(FileName, Type, Volume, Pan, Loop, LoopStart, LoopEnd, Layer)); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static int SetSoundVolume(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->SetSoundVolume(static_cast<unsigned int>(luaL_checknumber(L, 1)), static_cast<float>(luaL_checknumber(L, 2))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int SetSoundPanning(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->SetSoundPanning(static_cast<unsigned int>(luaL_checknumber(L, 1)), static_cast<float>(luaL_checknumber(L, 2))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int PauseSound(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->PauseSound(static_cast<unsigned int>(luaL_checknumber(L, 1))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int ResumeSound(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->ResumeSound(static_cast<unsigned int>(luaL_checknumber(L, 1))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int StopSound(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + pSfx->StopSound(static_cast<unsigned int>(luaL_checknumber(L, 1))); + + return 0; +} + +// ----------------------------------------------------------------------------- + +static int IsSoundPaused(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + lua_pushbooleancpp(L, pSfx->IsSoundPaused(static_cast<unsigned int>(luaL_checknumber(L, 1)))); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static int IsSoundPlaying(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + lua_pushbooleancpp(L, pSfx->IsSoundPlaying(static_cast<unsigned int>(luaL_checknumber(L, 1)))); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static int GetSoundVolume(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + lua_pushnumber(L, pSfx->GetSoundVolume(static_cast<unsigned int>(luaL_checknumber(L, 1)))); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static int GetSoundPanning(lua_State * L) +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_SoundEngine * pSfx = static_cast<BS_SoundEngine *>(BS_Kernel::GetInstance()->GetService("sfx")); + BS_ASSERT(pSfx); + + lua_pushnumber(L, pSfx->GetSoundPanning(static_cast<unsigned int>(luaL_checknumber(L, 1)))); + + return 1; +} + +// ----------------------------------------------------------------------------- + +static const char * SFX_LIBRARY_NAME = "Sfx"; + +static const luaL_reg SFX_FUNCTIONS[] = +{ + "Init", Init, + "Update", Update, + "__SetVolume", SetVolume, + "__GetVolume", GetVolume, + "PauseAll", PauseAll, + "ResumeAll", ResumeAll, + "PauseLayer", PauseLayer, + "ResumeLayer", ResumeLayer, + "__PlaySound", PlaySound, + "__PlaySoundEx", PlaySoundEx, + "__SetSoundVolume", SetSoundVolume, + "__SetSoundPanning", SetSoundPanning, + "__PauseSound", PauseSound, + "__ResumeSound", ResumeSound, + "__StopSound", StopSound, + "__IsSoundPaused", IsSoundPaused, + "__IsSoundPlaying", IsSoundPlaying, + "__GetSoundVolume", GetSoundVolume, + "__GetSoundPanning", GetSoundPanning, + 0, 0, +}; + +static const lua_constant_reg SFX_CONSTANTS[] = +{ + "MUSIC", BS_SoundEngine::MUSIC, + "SPEECH", BS_SoundEngine::SPEECH, + "SFX", BS_SoundEngine::SFX, + 0, 0, +}; + +// ----------------------------------------------------------------------------- + +bool BS_SoundEngine::_RegisterScriptBindings() +{ + BS_Kernel * pKernel = BS_Kernel::GetInstance(); + BS_ASSERT(pKernel); + BS_ScriptEngine * pScript = static_cast<BS_ScriptEngine *>(pKernel->GetService("script")); + BS_ASSERT(pScript); + lua_State * L = static_cast<lua_State *>(pScript->GetScriptObject()); + BS_ASSERT(L); + + if (!BS_LuaBindhelper::AddFunctionsToLib(L, SFX_LIBRARY_NAME, SFX_FUNCTIONS)) return false; + if (!BS_LuaBindhelper::AddConstantsToLib(L, SFX_LIBRARY_NAME, SFX_CONSTANTS)) return false; + + return true; +} |