diff --git a/binaries/data/mods/public/gui/common/music.js b/binaries/data/mods/public/gui/common/music.js index 9d0df5416c..3cdcd2bb48 100644 --- a/binaries/data/mods/public/gui/common/music.js +++ b/binaries/data/mods/public/gui/common/music.js @@ -46,8 +46,6 @@ function Music() this.currentState = 0; this.oldState = 0; - this.currentMusic = null; - // timer for delay between tracks this.timer = []; this.time = Date.now(); @@ -81,11 +79,7 @@ Music.prototype.updateState = function() switch (this.currentState) { case this.states.OFF: - if (this.isPlaying()) - { - var thePlayer = SoundPlayer(); - thePlayer.stopMusic(); - } + Engine.StopMusic(); break; case this.states.MENU: @@ -152,50 +146,34 @@ Music.prototype.getRandomTrack = function(tracks) Music.prototype.startPlayList = function(tracks, fadeInPeriod, isLooping) { - this.currentMusicList = new MusicList; + Engine.ClearPlaylist(); for (var i in tracks) { - this.currentMusicList.addItem( this.RELATIVE_MUSIC_PATH + tracks[i] ) + Engine.AddPlaylistItem( this.RELATIVE_MUSIC_PATH + tracks[i] ); } - if (this.currentMusicList) - { - if (isLooping) - this.currentMusicList.loop(); - else - this.currentMusicList.play(); - } + if (isLooping) + Engine.LoopPlaylist(); + else + Engine.PlayPlaylist(); }; Music.prototype.switchMusic = function(track, fadeInPeriod, isLooping) { - this.currentMusic = new MusicSound(this.RELATIVE_MUSIC_PATH + track); - - if (this.currentMusic) - { - if (isLooping) - this.currentMusic.loop(); - else - this.currentMusic.play(); - } + if (isLooping) + Engine.LoopMusic(this.RELATIVE_MUSIC_PATH + track); + else + Engine.PlayMusic(this.RELATIVE_MUSIC_PATH + track); }; Music.prototype.isPlaying = function() { - if (!this.currentMusic) - return false; - - // should return whether there is a valid handle; gain and fade do this also - // However, if looping is not set, then it always returns false because the - // handle is immediately cleared out -// return this.currentMusic.isPlaying(); - return true; + return Engine.MusicPlaying(); }; Music.prototype.start = function() { - var thePlayer = SoundPlayer(); - thePlayer.startMusic(); + Engine.StartMusic(); this.setState(this.states.PEACE); }; @@ -204,31 +182,3 @@ Music.prototype.stop = function() this.setState(this.states.OFF); }; -// ============================================================================= -// This allows for delays between tracks -// ============================================================================= -Music.prototype.setDelay = function(state, delay) -{ - this.timer = [this.time + delay, state]; -}; - -Music.prototype.stopTimer = function() -{ - this.timer = null; -}; - -// Needs to be called in onTick() to work -Music.prototype.updateTimer = function() -{ - this.time = Date.now(); - - if (this.timer && (this.timer[0] <= this.time)) - { - // Setting to OFF first guarantees that a state - // change will take place even if the current - // state is the same as the new state - this.reference.setState(this.states.OFF); - this.reference.setState(this.timer[1]); - this.stopTimer(); - } -}; \ No newline at end of file diff --git a/binaries/data/mods/public/gui/pregame/mainmenu.js b/binaries/data/mods/public/gui/pregame/mainmenu.js index d27e225d92..502b5dde39 100644 --- a/binaries/data/mods/public/gui/pregame/mainmenu.js +++ b/binaries/data/mods/public/gui/pregame/mainmenu.js @@ -6,7 +6,6 @@ const background = "hellenes1"; // Background type. Currently: 'hellenes1', 'per function init(initData) { initMusic(); - // Play main menu music global.music.setState(global.music.states.MENU); @@ -156,9 +155,6 @@ function onTick() // Animate submenu updateMenuPosition(tickLength); - // Update music state - global.music.updateTimer(); - if (Engine.IsUserReportEnabled()) { getGUIObjectByName("userReportEnabledText").caption = diff --git a/binaries/data/mods/public/gui/session/session.js b/binaries/data/mods/public/gui/session/session.js index aae80c3b99..bac1155cd6 100644 --- a/binaries/data/mods/public/gui/session/session.js +++ b/binaries/data/mods/public/gui/session/session.js @@ -299,9 +299,6 @@ function onTick() // Animate menu updateMenuPosition(tickLength); - // Update music state - global.music.updateTimer(); - // When training is blocked, flash population (alternates colour every 500msec) if (g_IsTrainingBlocked && (Date.now() % 1000) < 500) getGUIObjectByName("resourcePop").textcolor = POPULATION_ALERT_COLOR; @@ -639,12 +636,7 @@ function playRandomAmbient(type) // currentAmbient = newRandomSound("ambient", "temperate_", "dayscape"); const AMBIENT = "audio/ambient/dayscape/day_temperate_gen_03.ogg"; - currentAmbient = new AmbientSound(AMBIENT); - - if (currentAmbient) - { - currentAmbient.loop(); - } + Engine.LoopAmbientSound( AMBIENT ); break; default: diff --git a/binaries/data/mods/public/gui/summary/summary.js b/binaries/data/mods/public/gui/summary/summary.js index 59d71da4c0..81129a3060 100644 --- a/binaries/data/mods/public/gui/summary/summary.js +++ b/binaries/data/mods/public/gui/summary/summary.js @@ -308,6 +308,4 @@ function init(data) function onTick() { - // Update music state - global.music.updateTimer(); } diff --git a/build/premake/premake4.lua b/build/premake/premake4.lua index bbf673e473..dc6727ebe9 100644 --- a/build/premake/premake4.lua +++ b/build/premake/premake4.lua @@ -559,7 +559,7 @@ function setup_all_libs () "soundmanager", "soundmanager/data", "soundmanager/items", - "soundmanager/js", + "soundmanager/scripting", "scripting", "maths", "maths/scripting", diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp index b2e13741cd..66059610b4 100644 --- a/source/ps/Game.cpp +++ b/source/ps/Game.cpp @@ -46,7 +46,7 @@ #include "simulation2/Simulation2.h" #include "simulation2/components/ICmpPlayer.h" #include "simulation2/components/ICmpPlayerManager.h" -#include "soundmanager/SoundManager.h" +#include "soundmanager/ISoundManager.h" extern bool g_GameRestarted; diff --git a/source/ps/GameSetup/Config.cpp b/source/ps/GameSetup/Config.cpp index 13ddcb041e..19bde46389 100644 --- a/source/ps/GameSetup/Config.cpp +++ b/source/ps/GameSetup/Config.cpp @@ -24,7 +24,7 @@ #include "ps/CLogger.h" #include "ps/GameSetup/CmdLineArgs.h" #include "lib/timer.h" -#include "soundmanager/SoundManager.h" +#include "soundmanager/ISoundManager.h" // (these variables are documented in the header.) diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index 8ad227d9f8..5744e8ba76 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -88,7 +88,8 @@ #include "scriptinterface/ScriptInterface.h" #include "scriptinterface/ScriptStats.h" #include "simulation2/Simulation2.h" -#include "soundmanager/SoundManager.h" +#include "soundmanager/scripting/JSInterface_Sound.h" +#include "soundmanager/ISoundManager.h" #include "tools/atlas/GameInterface/GameLoop.h" #include "tools/atlas/GameInterface/View.h" @@ -321,9 +322,6 @@ static void RegisterJavascriptInterfaces() // maths JSI_Vector3D::init(); - // sound - CSoundManager::ScriptingInit(); - // graphics CGameView::ScriptingInit(); @@ -337,6 +335,7 @@ static void RegisterJavascriptInterfaces() CGUI::ScriptingInit(); GuiScriptingInit(g_ScriptingHost.GetScriptInterface()); + JSI_Sound::RegisterScriptFunctions(g_ScriptingHost.GetScriptInterface()); } @@ -885,7 +884,7 @@ void Init(const CmdLineArgs& args, int UNUSED(flags)) #if CONFIG2_AUDIO - CSoundManager::CreateSoundManager(); + ISoundManager::CreateSoundManager(); #endif // g_ConfigDB, command line args, globals @@ -957,7 +956,7 @@ void InitGraphics(const CmdLineArgs& args, int flags) // (OpenAL init will be skipped). // must be called before first snd_open. #if CONFIG2_AUDIO - CSoundManager::SetEnabled(false); + ISoundManager::SetEnabled(false); #endif } diff --git a/source/ps/Globals.cpp b/source/ps/Globals.cpp index 9174aab513..6fa4b3f0a2 100644 --- a/source/ps/Globals.cpp +++ b/source/ps/Globals.cpp @@ -21,7 +21,7 @@ #include "lib/external_libraries/libsdl.h" #include "network/NetClient.h" #include "ps/GameSetup/Config.h" -#include "soundmanager/SoundManager.h" +#include "soundmanager/ISoundManager.h" bool g_app_minimized = false; bool g_app_has_focus = true; diff --git a/source/scripting/ScriptGlue.cpp b/source/scripting/ScriptGlue.cpp index b5e878741b..4951e53642 100644 --- a/source/scripting/ScriptGlue.cpp +++ b/source/scripting/ScriptGlue.cpp @@ -53,7 +53,7 @@ #include "renderer/Renderer.h" #include "scriptinterface/ScriptInterface.h" #include "simulation2/Simulation2.h" -#include "soundmanager/SoundManager.h" +#include "soundmanager/ISoundManager.h" // rationale: the function table is now at the end of the source file to // avoid the need for forward declarations for every function. diff --git a/source/simulation2/components/CCmpSoundManager.cpp b/source/simulation2/components/CCmpSoundManager.cpp index 0ce1be3730..9fbf29264b 100644 --- a/source/simulation2/components/CCmpSoundManager.cpp +++ b/source/simulation2/components/CCmpSoundManager.cpp @@ -25,7 +25,7 @@ #include "simulation2/MessageTypes.h" #include "simulation2/components/ICmpPosition.h" #include "simulation2/components/ICmpRangeManager.h" -#include "soundmanager/js/SoundGroup.h" +#include "soundmanager/scripting/SoundGroup.h" class CCmpSoundManager : public ICmpSoundManager { diff --git a/source/soundmanager/ISoundManager.h b/source/soundmanager/ISoundManager.h new file mode 100644 index 0000000000..61ad59f34a --- /dev/null +++ b/source/soundmanager/ISoundManager.h @@ -0,0 +1,59 @@ +/* Copyright (C) 2013 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. 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. + * + * 0 A.D. 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 0 A.D. If not, see . + */ + +#ifndef INCLUDED_ISOUNDMANAGER_H +#define INCLUDED_ISOUNDMANAGER_H + +#include "lib/config2.h" + +#if CONFIG2_AUDIO + +class CSoundManagerWorker; + +class ISoundManager +{ + +public: + virtual ~ISoundManager() {}; + + virtual void IdleTask() = 0; + virtual void Pause(bool pauseIt) = 0; + virtual void SetMemoryUsage(long bufferSize, int bufferCount) = 0; + virtual void SetMasterGain(float gain) = 0; + virtual void SetMusicGain(float gain) = 0; + virtual void SetAmbientGain(float gain) = 0; + virtual void SetActionGain(float gain) = 0; + + static void CreateSoundManager(); + static void SetEnabled(bool doEnable); + + virtual bool InDistress() = 0; + virtual long GetBufferCount() = 0; + virtual long GetBufferSize() = 0; +}; + +#else // !CONFIG2_AUDIO + +class ISoundManager {}; + +#endif // !CONFIG2_AUDIO + + +extern ISoundManager* g_SoundManager; + +#endif // INCLUDED_ISOUNDMANAGER_H + diff --git a/source/soundmanager/SoundManager.cpp b/source/soundmanager/SoundManager.cpp index eca68d78e6..65b285a7ec 100644 --- a/source/soundmanager/SoundManager.cpp +++ b/source/soundmanager/SoundManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 Wildfire Games. + /* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -17,23 +17,19 @@ #include "precompiled.h" +#include "ISoundManager.h" #include "SoundManager.h" #include "soundmanager/data/SoundData.h" #include "soundmanager/items/CSoundItem.h" #include "soundmanager/items/CBufferItem.h" #include "soundmanager/items/CStreamItem.h" -#include "soundmanager/js/SoundPlayer.h" -#include "soundmanager/js/AmbientSound.h" -#include "soundmanager/js/MusicList.h" -#include "soundmanager/js/MusicSound.h" -#include "soundmanager/js/Sound.h" #include "lib/external_libraries/libsdl.h" #include "ps/CLogger.h" #include "ps/CStr.h" #include "ps/Profiler2.h" -CSoundManager* g_SoundManager = NULL; +ISoundManager* g_SoundManager = NULL; #define SOURCE_NUM 64 @@ -207,29 +203,15 @@ private: bool m_Enabled; bool m_Shutdown; - CSoundManagerWorker(CSoundManager* UNUSED(other)){}; + CSoundManagerWorker(ISoundManager* UNUSED(other)){}; }; -#endif -void CSoundManager::ScriptingInit() -{ - JAmbientSound::ScriptingInit(); - JMusicSound::ScriptingInit(); - JSound::ScriptingInit(); - JSoundPlayer::ScriptingInit(); - JMusicList::ScriptingInit(); -} - - -#if CONFIG2_AUDIO - - -void CSoundManager::CreateSoundManager() +void ISoundManager::CreateSoundManager() { g_SoundManager = new CSoundManager(); } -void CSoundManager::SetEnabled(bool doEnable) +void ISoundManager::SetEnabled(bool doEnable) { if ( g_SoundManager && !doEnable ) { @@ -237,7 +219,7 @@ void CSoundManager::SetEnabled(bool doEnable) } else if ( ! g_SoundManager && doEnable ) { - CSoundManager::CreateSoundManager(); + ISoundManager::CreateSoundManager(); } } @@ -466,9 +448,9 @@ long CSoundManager::GetBufferSize() return m_BufferSize; } -void CSoundManager::AddPlayListItem( const VfsPath& itemPath) +void CSoundManager::AddPlayListItem( const VfsPath* itemPath) { - m_PlayListItems->push_back( itemPath ); + m_PlayListItems->push_back( *itemPath ); } void CSoundManager::ClearPlayListItems() @@ -485,20 +467,23 @@ void CSoundManager::ClearPlayListItems() void CSoundManager::StartPlayList( bool doLoop ) { - if ( !m_PlayListItems->empty() ) - { - m_PlayingPlaylist = true; - m_LoopingPlaylist = doLoop; - m_RunningPlaylist = false; - - ISoundItem* aSnd = g_SoundManager->LoadItem( (m_PlayListItems->at( 0 )) ); - if ( aSnd ) - SetMusicItem( aSnd ); - else - { - SetMusicItem( NULL ); - } - } + if ( m_MusicEnabled ) + { + if ( m_PlayListItems->size() > 0 ) + { + m_PlayingPlaylist = true; + m_LoopingPlaylist = doLoop; + m_RunningPlaylist = false; + + ISoundItem* aSnd = LoadItem( (m_PlayListItems->at( 0 )) ); + if ( aSnd ) + SetMusicItem( aSnd ); + else + { + SetMusicItem( NULL ); + } + } + } } @@ -598,7 +583,7 @@ void CSoundManager::IdleTask() else nextPath = *it; - ISoundItem* aSnd = g_SoundManager->LoadItem( nextPath ); + ISoundItem* aSnd = LoadItem( nextPath ); if ( aSnd ) SetMusicItem( aSnd ); } @@ -746,7 +731,7 @@ void CSoundManager::PauseMusic (bool pauseIt) { m_CurrentTune->FadeAndPause( 1.0 ); } - else if ( m_CurrentTune && m_MusicPaused && !pauseIt ) + else if ( m_CurrentTune && m_MusicPaused && !pauseIt && m_MusicEnabled ) { m_CurrentTune->SetGain(0); m_CurrentTune->Resume(); diff --git a/source/soundmanager/SoundManager.h b/source/soundmanager/SoundManager.h index 810eb43b14..6e7f5f05f0 100644 --- a/source/soundmanager/SoundManager.h +++ b/source/soundmanager/SoundManager.h @@ -22,7 +22,11 @@ #if CONFIG2_AUDIO +#include "lib/external_libraries/openal.h" + + #include "lib/file/vfs/vfs_path.h" +#include "soundmanager/ISoundManager.h" #include "soundmanager/items/ISoundItem.h" #include "simulation2/system/Entity.h" #include "soundmanager/data/SoundData.h" @@ -34,13 +38,6 @@ #define AL_CHECK CSoundManager::al_check(__func__, __LINE__); -typedef std::vector PlayList; -typedef std::vector ItemsList; -typedef std::map ItemsMap; - -class CSoundManagerWorker; - - struct ALSourceHolder { /// Title of the column @@ -48,7 +45,14 @@ struct ALSourceHolder ISoundItem* SourceItem; }; -class CSoundManager +typedef std::vector PlayList; +typedef std::vector ItemsList; +typedef std::map ItemsMap; + +class CSoundManagerWorker; + + +class CSoundManager : public ISoundManager { NONCOPYABLE(CSoundManager); @@ -96,7 +100,7 @@ public: void ClearPlayListItems(); void StartPlayList( bool doLoop ); - void AddPlayListItem( const VfsPath& itemPath); + void AddPlayListItem( const VfsPath* itemPath); static void ScriptingInit(); static void CreateSoundManager(); @@ -153,15 +157,7 @@ private: #define AL_CHECK -class CSoundManager -{ -public: - static void ScriptingInit(); -}; #endif // !CONFIG2_AUDIO - -extern CSoundManager* g_SoundManager; - #endif // INCLUDED_SOUNDMANAGER_H diff --git a/source/soundmanager/items/CBufferItem.cpp b/source/soundmanager/items/CBufferItem.cpp index 2f4aa4dbac..cd96ac5804 100644 --- a/source/soundmanager/items/CBufferItem.cpp +++ b/source/soundmanager/items/CBufferItem.cpp @@ -60,7 +60,7 @@ void CBufferItem::ReleaseOpenALBuffer() delete[] al_buf; } alSourcei(m_ALSource, AL_BUFFER, NULL); - g_SoundManager->ReleaseALSource(m_ALSource); + ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource); AL_CHECK m_ALSource = 0; diff --git a/source/soundmanager/items/CSoundBase.cpp b/source/soundmanager/items/CSoundBase.cpp index c54287f93a..b7b8ab6f3e 100644 --- a/source/soundmanager/items/CSoundBase.cpp +++ b/source/soundmanager/items/CSoundBase.cpp @@ -46,7 +46,7 @@ void CSoundBase::ReleaseOpenAL() AL_CHECK alSourcei(m_ALSource, AL_BUFFER, NULL); AL_CHECK - g_SoundManager->ReleaseALSource(m_ALSource); + ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource); AL_CHECK m_ALSource = 0; } @@ -96,7 +96,7 @@ bool CSoundBase::Finished() bool CSoundBase::InitOpenAL() { alGetError(); /* clear error */ - m_ALSource = g_SoundManager->GetALSource( this ); + m_ALSource = ((CSoundManager*)g_SoundManager)->GetALSource( this ); AL_CHECK @@ -288,9 +288,9 @@ void CSoundBase::Play() if (err != AL_NO_ERROR) { if (err == AL_INVALID) - g_SoundManager->SetDistressThroughError(); + ((CSoundManager*)g_SoundManager)->SetDistressThroughError(); else - g_SoundManager->al_ReportError(err, __func__, __LINE__); + ((CSoundManager*)g_SoundManager)->al_ReportError(err, __func__, __LINE__); } } } @@ -348,12 +348,12 @@ void CSoundBase::FadeToIn(ALfloat newVolume, double fadeDuration) void CSoundBase::PlayAsMusic() { - g_SoundManager->SetMusicItem(this); + ((CSoundManager*)g_SoundManager)->SetMusicItem(this); } void CSoundBase::PlayAsAmbient() { - g_SoundManager->SetAmbientItem(this); + ((CSoundManager*)g_SoundManager)->SetAmbientItem(this); } void CSoundBase::Stop() diff --git a/source/soundmanager/items/CStreamItem.cpp b/source/soundmanager/items/CStreamItem.cpp index 7d77c0bf99..fd9e2e8d2b 100644 --- a/source/soundmanager/items/CStreamItem.cpp +++ b/source/soundmanager/items/CStreamItem.cpp @@ -57,7 +57,7 @@ void CStreamItem::ReleaseOpenALStream() } alSourcei(m_ALSource, AL_BUFFER, NULL); AL_CHECK - g_SoundManager->ReleaseALSource(m_ALSource); + ((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource); AL_CHECK m_ALSource = 0; } diff --git a/source/soundmanager/items/ISoundItem.h b/source/soundmanager/items/ISoundItem.h index dff07ccca8..912eb4facd 100644 --- a/source/soundmanager/items/ISoundItem.h +++ b/source/soundmanager/items/ISoundItem.h @@ -22,7 +22,6 @@ #if CONFIG2_AUDIO -#include "lib/external_libraries/openal.h" #include "maths/Vector3D.h" #include "ps/CStr.h" #include "soundmanager/data/SoundData.h" @@ -53,16 +52,16 @@ public: virtual void PlayAndDelete() = 0; virtual void StopAndDelete() = 0; - virtual void FadeToIn(ALfloat newVolume, double fadeDuration) = 0; + virtual void FadeToIn(float newVolume, double fadeDuration) = 0; virtual void FadeAndDelete(double fadeTime) = 0; virtual void FadeAndPause(double fadeTime) = 0; virtual void PlayLoop() = 0; - virtual void SetCone(ALfloat innerCone, ALfloat outerCone, ALfloat coneGain) = 0; - virtual void SetPitch(ALfloat pitch) = 0; - virtual void SetGain(ALfloat gain) = 0; + virtual void SetCone(float innerCone, float outerCone, float coneGain) = 0; + virtual void SetPitch(float pitch) = 0; + virtual void SetGain(float gain) = 0; virtual void SetLocation(const CVector3D& position) = 0; - virtual void SetRollOff(ALfloat gain) = 0; + virtual void SetRollOff(float gain) = 0; virtual void Pause() = 0; virtual void Resume() = 0; diff --git a/source/soundmanager/js/AmbientSound.cpp b/source/soundmanager/js/AmbientSound.cpp deleted file mode 100644 index 54bcecc1fe..0000000000 --- a/source/soundmanager/js/AmbientSound.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -#include "precompiled.h" - -#include "AmbientSound.h" - -#include "lib/config2.h" -#include "lib/utf8.h" -#include "maths/Vector3D.h" -#include "ps/CLogger.h" -#include "ps/CStr.h" -#include "ps/Filesystem.h" - -#include "soundmanager/SoundManager.h" - -JAmbientSound::JAmbientSound(const VfsPath& pathname) : m_FileName(pathname) -{ -} - -// start playing the sound, all ambient sounds loop -bool JAmbientSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) { - ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); - - if (aSnd) - aSnd->PlayAsAmbient(); - } -#endif // CONFIG2_AUDIO - return true; -} - -// start playing the sound, all ambient sounds loop -bool JAmbientSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) { - ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); - - if (aSnd) - aSnd->PlayAsAmbient(); - } -#endif // CONFIG2_AUDIO - return true; -} -bool JAmbientSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->SetAmbientItem(0L); -#endif // CONFIG2_AUDIO - - return true; -} - -// Script-bound functions - - -void JAmbientSound::ScriptingInit() -{ - AddMethod("toString", 0); - AddMethod("play", 0); - AddMethod("loop", 0); - AddMethod("free", 0); - - CJSObject::ScriptingInit("AmbientSound", &JAmbientSound::Construct, 1); -} - -CStr JAmbientSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ - std::ostringstream stringStream; - stringStream << "[object AmbientSound: "; - stringStream << m_FileName.string().c_str(); - - return stringStream.str(); -} - -JSBool JAmbientSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) -{ -// JSU_REQUIRE_MIN_PARAMS(1); - - CStrW filename; - if (! ToPrimitive(cx, JS_ARGV(cx, vp)[0], filename)) - return JS_FALSE; - - JAmbientSound* newObject = new JAmbientSound(filename); - newObject->m_EngineOwned = false; - - JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); - - return JS_TRUE; -} - diff --git a/source/soundmanager/js/AmbientSound.h b/source/soundmanager/js/AmbientSound.h deleted file mode 100644 index e20a2b12bc..0000000000 --- a/source/soundmanager/js/AmbientSound.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -#ifndef INCLUDED_JAMBIENTSOUND -#define INCLUDED_JAMBIENTSOUND - -#include "scripting/ScriptableObject.h" -#include "soundmanager/items/ISoundItem.h" - -class JAmbientSound : public CJSObject -{ -public: - JAmbientSound(const VfsPath& pathname); - - CStr ToString(JSContext* cx, uintN argc, jsval* argv); - - bool Play(JSContext* cx, uintN argc, jsval* argv); - - bool Loop(JSContext* cx, uintN argc, jsval* argv); - bool Free(JSContext* cx, uintN argc, jsval* argv); - - bool SetGain(JSContext* cx, uintN argc, jsval* argv); - bool SetPitch(JSContext* cx, uintN argc, jsval* argv); - bool Fade(JSContext* cx, uintN argc, jsval* argv); - - static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); - void clearSoundItem(); - static void ScriptingInit(); -protected: - - VfsPath m_FileName; - -}; - -#endif // #ifndef INCLUDED_JAMBIENTSOUND - diff --git a/source/soundmanager/js/MusicList.cpp b/source/soundmanager/js/MusicList.cpp deleted file mode 100644 index 55a3eafa20..0000000000 --- a/source/soundmanager/js/MusicList.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -#include "precompiled.h" - -#include "MusicList.h" - -#include "lib/config2.h" -#include "lib/utf8.h" -#include "maths/Vector3D.h" -#include "ps/CStr.h" -#include "ps/Filesystem.h" - #include "ps/CLogger.h" - -#include "soundmanager/SoundManager.h" -#include "gui/GUI.h" -#include - - -JMusicList::JMusicList() -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->ClearPlayListItems(); -#endif -} - -bool JMusicList::AddItem(JSContext* cx, uintN UNUSED(argc), jsval* vp) -{ - CStrW filename; - if (! ToPrimitive(cx, vp[0], filename)) - return false; - -#if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->AddPlayListItem( VfsPath( filename ) ); -#endif - - return true; -} - - -bool JMusicList::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ - #if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->StartPlayList( false ); - #endif // CONFIG2_AUDIO - - return true; -} - -// request the sound be played until free() is called. returns immediately. -bool JMusicList::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ - #if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->StartPlayList( true ); - - #endif // CONFIG2_AUDIO - - return true; -} - -void JMusicList::ScriptingInit() -{ - AddMethod("toString", 0); - AddMethod("play", 0); - AddMethod("loop", 0); - AddMethod("addItem", 1); - - CJSObject::ScriptingInit("MusicList", &JMusicList::Construct, 0); -} - -CStr JMusicList::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ - std::ostringstream stringStream; - stringStream << "[object MusicList]"; - - return stringStream.str(); -} - -JSBool JMusicList::Construct(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* vp) -{ - CStrW filename; - - JMusicList* newObject = new JMusicList(); - newObject->m_EngineOwned = false; - JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); - - return JS_TRUE; -} diff --git a/source/soundmanager/js/MusicList.h b/source/soundmanager/js/MusicList.h deleted file mode 100644 index 5d442ce4aa..0000000000 --- a/source/soundmanager/js/MusicList.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -// JS sound binding - -// interface rationale: -// - can't just expose fire and forget playSound to script code: -// we sometimes need to loop until a certain condition is met -// (e.g. building is complete) => need means of access (Handle) to sound. -// -// - the current 64-bit Handle can't be stored as-is by JS code; -// we could make it 32 bit, but that limits its usefulness -// (barely enough tag bits). -// -// - instead, we provide a thin class wrapper (using scriptableobject.h) -// on top of the snd API that encapsulates the Handle. - -#ifndef INCLUDED_MUSICLIST_H -#define INCLUDED_MUSICLIST_H - -#include "scripting/ScriptableObject.h" -#include "soundmanager/items/ISoundItem.h" - -class JMusicList : public CJSObject -{ -public: - JMusicList(); - - // Script-bound functions - - CStr ToString(JSContext* cx, uintN argc, jsval* argv); - - bool Play(JSContext* cx, uintN argc, jsval* argv); - bool Loop(JSContext* cx, uintN argc, jsval* argv); - bool AddItem(JSContext* cx, uintN argc, jsval* vp); - - static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); - - static void ScriptingInit(); - -protected: - -}; - -#endif // #ifndef INCLUDED_MUSICLIST_H \ No newline at end of file diff --git a/source/soundmanager/js/MusicSound.cpp b/source/soundmanager/js/MusicSound.cpp deleted file mode 100644 index 505b2173f7..0000000000 --- a/source/soundmanager/js/MusicSound.cpp +++ /dev/null @@ -1,91 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -#include "precompiled.h" - -#include "MusicSound.h" - -#include "lib/config2.h" -#include "lib/utf8.h" -#include "maths/Vector3D.h" -#include "ps/CStr.h" -#include "ps/Filesystem.h" -#include "soundmanager/SoundManager.h" - -#include - - -JMusicSound::JMusicSound(const VfsPath& pathname) : m_FileName(pathname) -{ -} - -bool JMusicSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) { - ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); - if (aSnd != NULL) - aSnd->PlayAsMusic(); - } -#endif // CONFIG2_AUDIO - return true; -} - -// request the sound be played until free() is called. returns immediately. -bool JMusicSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) { - ISoundItem* aSnd = g_SoundManager->LoadItem(m_FileName); - if (aSnd != NULL) - aSnd->PlayAsMusic(); - } -#endif // CONFIG2_AUDIO - return true; -} - -void JMusicSound::ScriptingInit() -{ - AddMethod("toString", 0); - AddMethod("play", 0); - AddMethod("loop", 0); - - CJSObject::ScriptingInit("MusicSound", &JMusicSound::Construct, 1); -} - -CStr JMusicSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ - std::ostringstream stringStream; - stringStream << "[object MusicSound: "; - stringStream << m_FileName.string().c_str(); - - return stringStream.str(); -} - -JSBool JMusicSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) -{ - CStrW filename; - if (! ToPrimitive(cx, JS_ARGV(cx, vp)[0], filename)) - return JS_FALSE; - - JMusicSound* newObject = new JMusicSound(filename); - newObject->m_EngineOwned = false; - JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); - - return JS_TRUE; -} - diff --git a/source/soundmanager/js/MusicSound.h b/source/soundmanager/js/MusicSound.h deleted file mode 100644 index 359de790f4..0000000000 --- a/source/soundmanager/js/MusicSound.h +++ /dev/null @@ -1,58 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -// JS sound binding - -// interface rationale: -// - can't just expose fire and forget playSound to script code: -// we sometimes need to loop until a certain condition is met -// (e.g. building is complete) => need means of access (Handle) to sound. -// -// - the current 64-bit Handle can't be stored as-is by JS code; -// we could make it 32 bit, but that limits its usefulness -// (barely enough tag bits). -// -// - instead, we provide a thin class wrapper (using scriptableobject.h) -// on top of the snd API that encapsulates the Handle. - -#ifndef INCLUDED_MUSICSOUND_H -#define INCLUDED_MUSICSOUND_H - -#include "scripting/ScriptableObject.h" -#include "soundmanager/items/ISoundItem.h" - -class JMusicSound : public CJSObject -{ -public: - JMusicSound(const VfsPath& pathname); - - // Script-bound functions - - CStr ToString(JSContext* cx, uintN argc, jsval* argv); - - bool Play(JSContext* cx, uintN argc, jsval* argv); - bool Loop(JSContext* cx, uintN argc, jsval* argv); - - static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); - - static void ScriptingInit(); - -protected: - VfsPath m_FileName; -}; - -#endif // #ifndef INCLUDED_MUSICSOUND_H diff --git a/source/soundmanager/js/Sound.cpp b/source/soundmanager/js/Sound.cpp deleted file mode 100644 index 35886b734d..0000000000 --- a/source/soundmanager/js/Sound.cpp +++ /dev/null @@ -1,213 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -#include "precompiled.h" - -#include "Sound.h" - -#include "lib/config2.h" -#include "lib/utf8.h" -#include "maths/Vector3D.h" -#include "ps/CStr.h" -#include "ps/Filesystem.h" -#include "soundmanager/SoundManager.h" - - -JSound::JSound(const VfsPath& pathname) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) - m_SndItem = g_SoundManager->LoadItem(pathname); -#else // !CONFIG2_AUDIO - UNUSED2(pathname); -#endif // !CONFIG2_AUDIO -} - -JSound::~JSound() -{ -#if CONFIG2_AUDIO - if (m_SndItem) - { - m_SndItem->FadeAndDelete(0.2); - m_SndItem = 0; - } -#endif // CONFIG2_AUDIO -} - -bool JSound::ClearSoundItem() -{ -#if CONFIG2_AUDIO - m_SndItem = 0L; -#endif - return true; -} - -bool JSound::SetGain(JSContext* cx, uintN UNUSED(argc), jsval* argv) -{ -#if CONFIG2_AUDIO - if (! m_SndItem) - return false; - - float gain; - if (! ToPrimitive(cx, argv[0], gain)) - return false; - - m_SndItem->SetGain(gain); -#else // !CONFIG2_AUDIO - UNUSED2(cx); - UNUSED2(argv); -#endif // !CONFIG2_AUDIO - return true; -} - -bool JSound::SetPitch(JSContext* cx, uintN UNUSED(argc), jsval* argv) -{ -#if CONFIG2_AUDIO - if (! m_SndItem) - return false; - - float pitch; - if (! ToPrimitive(cx, argv[0], pitch)) - return false; - - m_SndItem->SetPitch(pitch); -#else // !CONFIG2_AUDIO - UNUSED2(cx); - UNUSED2(argv); -#endif // CONFIG2_AUDIO - return true; -} - -bool JSound::SetPosition(JSContext* cx, uintN argc, jsval* argv) -{ -#if CONFIG2_AUDIO - if (! m_SndItem) - return false; - - ENSURE(argc >= 1); // FIXME - - CVector3D pos; - // absolute world coords - if (!ToPrimitive(cx, argv[0], pos)) - return false; - - m_SndItem->SetLocation(pos); -#else // !CONFIG2_AUDIO - UNUSED2(cx); - UNUSED2(argc); - UNUSED2(argv); -#endif // !CONFIG2_AUDIO - return true; -} - - -bool JSound::Fade(JSContext* cx, uintN UNUSED(argc), jsval* argv) -{ -#if CONFIG2_AUDIO - if (! m_SndItem) - return false; - -// ENSURE(argc >= 3); // FIXME - float initial_gain, final_gain; - float length; - if (! (ToPrimitive(cx, argv[0], initial_gain) - && ToPrimitive(cx, argv[1], final_gain) - && ToPrimitive(cx, argv[2], length))) - return false; - - m_SndItem->SetGain(initial_gain); - m_SndItem->FadeToIn(final_gain, length); -#else // !CONFIG2_AUDIO - UNUSED2(cx); - UNUSED2(argv); -#endif // !CONFIG2_AUDIO - return true; -} - -bool JSound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if (! m_SndItem) - return false; - - m_SndItem->Play(); -#endif // CONFIG2_AUDIO - return true; -} - -bool JSound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if (! m_SndItem) - return false; - - m_SndItem->PlayLoop(); -#endif // CONFIG2_AUDIO - return true; -} - -bool JSound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if (m_SndItem) - { - m_SndItem->FadeAndDelete(0.2); - m_SndItem = 0; - } -#endif // CONFIG2_AUDIO - return true; -} - -void JSound::ScriptingInit() -{ - AddMethod("toString", 0); - AddMethod("play", 0); - AddMethod("loop", 0); - AddMethod("free", 0); - AddMethod("setGain", 0); - AddMethod("setPitch", 0); - AddMethod("setPosition", 0); - AddMethod("fade", 0); - - CJSObject::ScriptingInit("Sound", &JSound::Construct, 1); -} - -CStr JSound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - CStrW titleString( m_SndItem->GetName()->string() ); - return "[object Sound: " + (m_SndItem ? titleString.ToUTF8() : "(null)") + "]"; -#else // !CONFIG2_AUDIO - return "[object Sound: audio disabled]"; -#endif // !CONFIG2_AUDIO -} - -JSBool JSound::Construct(JSContext* cx, uintN UNUSED(argc), jsval* vp) -{ -// JSU_REQUIRE_MIN_PARAMS(1); - - CStrW filename; - if (! ToPrimitive(cx, JS_ARGV(cx, vp)[0], filename)) - return JS_FALSE; - - JSound* newObject = new JSound(filename); - newObject->m_EngineOwned = false; - JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); - - return JS_TRUE; -} - diff --git a/source/soundmanager/js/Sound.h b/source/soundmanager/js/Sound.h deleted file mode 100644 index 984a200bed..0000000000 --- a/source/soundmanager/js/Sound.h +++ /dev/null @@ -1,71 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -// JS sound binding - -// interface rationale: -// - can't just expose fire and forget playSound to script code: -// we sometimes need to loop until a certain condition is met -// (e.g. building is complete) => need means of access (Handle) to sound. -// -// - the current 64-bit Handle can't be stored as-is by JS code; -// we could make it 32 bit, but that limits its usefulness -// (barely enough tag bits). -// -// - instead, we provide a thin class wrapper (using scriptableobject.h) -// on top of the snd API that encapsulates the Handle. - -#ifndef INCLUDED_JSOUND -#define INCLUDED_JSOUND - -#include "lib/config2.h" -#include "scripting/ScriptableObject.h" -#include "soundmanager/items/ISoundItem.h" - -class JSound : public CJSObject -{ -public: - - // note: filename is stored by handle manager; no need to keep a copy here. - - JSound(const VfsPath& pathname); - virtual ~JSound(); - - CStr ToString(JSContext* cx, uintN argc, jsval* argv); - - bool Play(JSContext* cx, uintN argc, jsval* argv); - bool Loop(JSContext* cx, uintN argc, jsval* argv); - - bool Free(JSContext* cx, uintN argc, jsval* argv); - bool SetGain(JSContext* cx, uintN argc, jsval* argv); - bool SetPitch(JSContext* cx, uintN argc, jsval* argv); - bool SetPosition(JSContext* cx, uintN argc, jsval* argv); - bool ClearSoundItem(); - - bool Fade(JSContext* cx, uintN argc, jsval* argv); - - static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); - static void ScriptingInit(); - -protected: -#if CONFIG2_AUDIO - ISoundItem* m_SndItem; -#endif -}; - -#endif // #ifndef INCLUDED_JSOUND - diff --git a/source/soundmanager/js/SoundPlayer.cpp b/source/soundmanager/js/SoundPlayer.cpp deleted file mode 100644 index 358461a7ad..0000000000 --- a/source/soundmanager/js/SoundPlayer.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ -#include "precompiled.h" - -#include "SoundPlayer.h" - -#include "lib/config2.h" -#include "lib/utf8.h" -#include "maths/Vector3D.h" -#include "ps/Filesystem.h" -#include "soundmanager/SoundManager.h" - -#include - - -JSoundPlayer::JSoundPlayer() -{ -} - -JSoundPlayer::~JSoundPlayer() -{ -} - -bool JSoundPlayer::StartMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->SetMusicEnabled(true); -#endif - return true; -} - -// request the sound be played until free() is called. returns immediately. -bool JSoundPlayer::StopMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ -#if CONFIG2_AUDIO - if ( g_SoundManager ) - g_SoundManager->SetMusicEnabled(false); -#endif - - return true; -} - -void JSoundPlayer::ScriptingInit() -{ - AddMethod("toString", 0); - AddMethod("startMusic", 0); - AddMethod("stopMusic", 0); - - CJSObject::ScriptingInit("SoundPlayer", &JSoundPlayer::Construct, 1); -} - -JSBool JSoundPlayer::Construct(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* vp) -{ - JSoundPlayer* newObject = new JSoundPlayer(); - newObject->m_EngineOwned = false; - JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(newObject->GetScript())); - - return JS_TRUE; -} - -CStr JSoundPlayer::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)) -{ - std::ostringstream stringStream; - stringStream << "[object MusicPlayer]"; - - return stringStream.str(); -} - diff --git a/source/soundmanager/js/SoundPlayer.h b/source/soundmanager/js/SoundPlayer.h deleted file mode 100644 index 399f1ee991..0000000000 --- a/source/soundmanager/js/SoundPlayer.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Copyright (C) 2012 Wildfire Games. - * This file is part of 0 A.D. - * - * 0 A.D. 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. - * - * 0 A.D. 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 0 A.D. If not, see . - */ - -// JS sound binding - -// interface rationale: -// - can't just expose fire and forget playSound to script code: -// we sometimes need to loop until a certain condition is met -// (e.g. building is complete) => need means of access (Handle) to sound. -// -// - the current 64-bit Handle can't be stored as-is by JS code; -// we could make it 32 bit, but that limits its usefulness -// (barely enough tag bits). -// -// - instead, we provide a thin class wrapper (using scriptableobject.h) -// on top of the snd API that encapsulates the Handle. - -#ifndef INCLUDED_JSOUNDPLAYER -#define INCLUDED_JSOUNDPLAYER - -#include "scripting/ScriptableObject.h" -#include "soundmanager/items/ISoundItem.h" - -class JSoundPlayer : public CJSObject -{ -public: - JSoundPlayer(); - virtual ~JSoundPlayer(); - - // Script-bound functions - - CStr ToString(JSContext* cx, uintN argc, jsval* argv); - - bool StartMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)); - bool StopMusic(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv)); - - static JSBool Construct(JSContext* cx, uintN argc, jsval* vp); - static void ScriptingInit(); - -protected: - VfsPath m_FileName; -}; - -#endif // #ifndef INCLUDED_JSOUNDPLAYER - diff --git a/source/soundmanager/scripting/JSInterface_Sound.cpp b/source/soundmanager/scripting/JSInterface_Sound.cpp new file mode 100644 index 0000000000..3a48d4cbcf --- /dev/null +++ b/source/soundmanager/scripting/JSInterface_Sound.cpp @@ -0,0 +1,149 @@ +/* Copyright (C) 2012 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. 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. + * + * 0 A.D. 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 0 A.D. If not, see . + */ +#include "precompiled.h" +#include "scriptinterface/ScriptInterface.h" + +#include "JSInterface_Sound.h" + +#include "lib/config2.h" +#include "lib/utf8.h" +#include "maths/Vector3D.h" +#include "ps/Filesystem.h" +#include "soundmanager/SoundManager.h" + +#include + +namespace JSI_Sound +{ + void StartMusic(void* UNUSED(cbdata)) + { + #if CONFIG2_AUDIO + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + if ( sndManager ) + sndManager->SetMusicEnabled(true); + #endif + } + void StopMusic(void* UNUSED(cbdata)) + { + #if CONFIG2_AUDIO + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + if ( sndManager ) + sndManager->SetMusicEnabled(false); + #endif + } + + void ClearPlaylist(void* UNUSED(cbdata)) + { + #if CONFIG2_AUDIO + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + if ( sndManager ) + sndManager->ClearPlayListItems(); + #endif + } + + void AddPlaylistItem(void* UNUSED(cbdata), std::wstring filename) + { + #if CONFIG2_AUDIO + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + if ( sndManager ) + sndManager->AddPlayListItem( new VfsPath( filename ) ); + #else + UNUSED2(filename); + #endif + } + + void LoopPlaylist(void* UNUSED(cbdata)) + { + #if CONFIG2_AUDIO + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + if ( sndManager ) + sndManager->StartPlayList( true ); + #endif + } + void PlayPlaylist(void* UNUSED(cbdata)) + { + #if CONFIG2_AUDIO + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + if ( sndManager ) + sndManager->StartPlayList( false ); + #endif + } + + void LoopMusic(void* UNUSED(cbdata), std::wstring filename) + { + #if CONFIG2_AUDIO + if ( g_SoundManager ) { + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + ISoundItem* aSnd = sndManager->LoadItem(filename); + if (aSnd != NULL) + aSnd->PlayAsMusic(); + } + #else + UNUSED2(filename); + #endif // CONFIG2_AUDIO + } + void PlayMusic(void* UNUSED(cbdata), std::wstring filename) + { + #if CONFIG2_AUDIO + if ( g_SoundManager ) { + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + ISoundItem* aSnd = sndManager->LoadItem(filename); + if (aSnd != NULL) + aSnd->PlayAsMusic(); + } + #else + UNUSED2(filename); + #endif // CONFIG2_AUDIO + } + + void LoopAmbientSound(void* UNUSED(cbdata), std::wstring filename) + { + #if CONFIG2_AUDIO + if ( g_SoundManager ) { + CSoundManager* sndManager = (CSoundManager*)g_SoundManager; + ISoundItem* aSnd = sndManager->LoadItem(filename); + if (aSnd != NULL) + aSnd->PlayAsAmbient(); + } + #else + UNUSED2(filename); + #endif // CONFIG2_AUDIO + } + + bool MusicPlaying(void* UNUSED(cbdata)) + { + #if CONFIG2_AUDIO + return true; + #else + return false; + #endif // CONFIG2_AUDIO + } + + void RegisterScriptFunctions(ScriptInterface& scriptInterface) + { + scriptInterface.RegisterFunction("StartMusic"); + scriptInterface.RegisterFunction("StopMusic"); + scriptInterface.RegisterFunction("ClearPlaylist"); + scriptInterface.RegisterFunction("AddPlaylistItem"); + scriptInterface.RegisterFunction("LoopPlaylist"); + scriptInterface.RegisterFunction("PlayPlaylist"); + scriptInterface.RegisterFunction("LoopMusic"); + scriptInterface.RegisterFunction("PlayMusic"); + scriptInterface.RegisterFunction("LoopAmbientSound"); + scriptInterface.RegisterFunction("MusicPlaying"); + } +} \ No newline at end of file diff --git a/source/soundmanager/scripting/JSInterface_Sound.h b/source/soundmanager/scripting/JSInterface_Sound.h new file mode 100644 index 0000000000..43bc4efd63 --- /dev/null +++ b/source/soundmanager/scripting/JSInterface_Sound.h @@ -0,0 +1,29 @@ +/* Copyright (C) 2012 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. 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. + * + * 0 A.D. 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 0 A.D. If not, see . + */ + + +#ifndef INCLUDED_SOUNDSCRIPTINTERFACE +#define INCLUDED_SOUNDSCRIPTINTERFACE + +class ScriptInterface; + +namespace JSI_Sound +{ + void RegisterScriptFunctions(ScriptInterface& scriptInterface); +} + +#endif // #ifndef INCLUDED_SOUNDSCRIPTINTERFACE diff --git a/source/soundmanager/js/SoundGroup.cpp b/source/soundmanager/scripting/SoundGroup.cpp similarity index 98% rename from source/soundmanager/js/SoundGroup.cpp rename to source/soundmanager/scripting/SoundGroup.cpp index 8e05917722..9f2706ae21 100644 --- a/source/soundmanager/js/SoundGroup.cpp +++ b/source/soundmanager/scripting/SoundGroup.cpp @@ -181,7 +181,7 @@ void CSoundGroup::UploadPropertiesAndPlay(size_t theIndex, const CVector3D& posi if ( (sndDist * 2) < itemDist ) sndDist = itemDist; - ISoundItem* hSound = g_SoundManager->ItemForEntity( source, sndData); + ISoundItem* hSound = ((CSoundManager*)g_SoundManager)->ItemForEntity( source, sndData); if ( hSound ) { @@ -208,7 +208,7 @@ void CSoundGroup::UploadPropertiesAndPlay(size_t theIndex, const CVector3D& posi hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain); - g_SoundManager->PlayGroupItem(hSound, theGain); + ((CSoundManager*)g_SoundManager)->PlayGroupItem(hSound, theGain); } } } diff --git a/source/soundmanager/js/SoundGroup.h b/source/soundmanager/scripting/SoundGroup.h similarity index 100% rename from source/soundmanager/js/SoundGroup.h rename to source/soundmanager/scripting/SoundGroup.h