From 90cf3176d183aa1c434dc5d023039f79c465fdd8 Mon Sep 17 00:00:00 2001 From: Marianne Gagnon Date: Sun, 21 Jun 2015 18:31:16 -0400 Subject: [PATCH] Start adding an audio scripting API, and also fix a deadlock in sfx_manager due to improper release of locks --- src/audio/sfx_manager.cpp | 4 +-- src/scriptengine/script_audio.cpp | 55 ++++++++++++++++++++++++++++++ src/scriptengine/script_audio.hpp | 33 ++++++++++++++++++ src/scriptengine/script_engine.cpp | 4 ++- 4 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/scriptengine/script_audio.cpp create mode 100644 src/scriptengine/script_audio.hpp diff --git a/src/audio/sfx_manager.cpp b/src/audio/sfx_manager.cpp index 5078b9293..aebe6ac2c 100644 --- a/src/audio/sfx_manager.cpp +++ b/src/audio/sfx_manager.cpp @@ -939,7 +939,7 @@ SFXBase* SFXManager::quickSound(const std::string &sound_type) { if (!sfxAllowed()) return NULL; - m_quick_sounds.lock(); + MutexLockerHelper lock(m_quick_sounds); std::map::iterator sound = m_quick_sounds.getData().find(sound_type); @@ -950,14 +950,12 @@ SFXBase* SFXManager::quickSound(const std::string &sound_type) if (new_sound == NULL) return NULL; new_sound->play(); m_quick_sounds.getData()[sound_type] = new_sound; - m_quick_sounds.unlock(); return new_sound; } else { SFXBase *base_sound = sound->second; base_sound->play(); - m_quick_sounds.unlock(); return base_sound; } diff --git a/src/scriptengine/script_audio.cpp b/src/scriptengine/script_audio.cpp new file mode 100644 index 000000000..4d712aceb --- /dev/null +++ b/src/scriptengine/script_audio.cpp @@ -0,0 +1,55 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart Team +// +// This program 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 3 +// 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "script_audio.hpp" +#include "audio/sfx_manager.hpp" + +#include +#include + +/** \cond DOXYGEN_IGNORE */ +namespace Scripting +{ + /** \endcond */ + + namespace Audio + { + /** \addtogroup Scripting + * @{ + */ + /** \addtogroup Audio + * @{ + */ + void playSound(const std::string* sound_name) + { + SFXManager::get()->quickSound(*sound_name); + } + /** @}*/ + /** @}*/ + + void registerScriptFunctions(asIScriptEngine *engine) + { + int r; // of type asERetCodes + engine->SetDefaultNamespace("Audio"); + r = engine->RegisterGlobalFunction("void playSound(const string &in)", asFUNCTION(playSound), asCALL_CDECL); assert(r >= 0); + } + } + +/** \cond DOXYGEN_IGNORE */ +} +/** \endcond */ diff --git a/src/scriptengine/script_audio.hpp b/src/scriptengine/script_audio.hpp new file mode 100644 index 000000000..139988e08 --- /dev/null +++ b/src/scriptengine/script_audio.hpp @@ -0,0 +1,33 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2014-2015 SuperTuxKart Team +// +// This program 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 3 +// 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef HEADER_SCRIPT_AUDIO_HPP +#define HEADER_SCRIPT_AUDIO_HPP + +#include + +#include + +namespace Scripting +{ + namespace Audio + { + void registerScriptFunctions(asIScriptEngine *engine); + } +} +#endif diff --git a/src/scriptengine/script_engine.cpp b/src/scriptengine/script_engine.cpp index 1e94e64c3..77fc84ceb 100644 --- a/src/scriptengine/script_engine.cpp +++ b/src/scriptengine/script_engine.cpp @@ -21,6 +21,7 @@ #include "io/file_manager.hpp" #include "karts/kart.hpp" #include "modes/world.hpp" +#include "scriptengine/script_audio.hpp" #include "scriptengine/script_challenges.hpp" #include "scriptengine/script_kart.hpp" #include "scriptengine/script_engine.hpp" @@ -336,7 +337,8 @@ namespace Scripting Scripting::Utils::registerScriptFunctions(m_engine); Scripting::GUI::registerScriptFunctions(m_engine); Scripting::GUI::registerScriptEnums(m_engine); - + Scripting::Audio::registerScriptFunctions(m_engine); + // It is possible to register the functions, properties, and types in // configuration groups as well. When compiling the scripts it can then // be defined which configuration groups should be available for that