Skip to content

Commit

Permalink
#5306: Add reload sounds command and corresponding signal to SoundMan…
Browse files Browse the repository at this point in the history
…ager.
  • Loading branch information
codereader committed Oct 3, 2020
1 parent f58f705 commit 0cfd312
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 27 deletions.
8 changes: 7 additions & 1 deletion include/isound.h
Expand Up @@ -5,6 +5,7 @@

#include <vector>
#include <functional>
#include <sigc++/signal.h>

// A list of sound files associated to a shader
typedef std::vector<std::string> SoundFileList;
Expand Down Expand Up @@ -91,7 +92,6 @@ class ISoundManager :
public RegisterableModule
{
public:

/// Invoke a function for each sound shader
virtual void forEachShader(std::function<void(const ISoundShader&)>) = 0;

Expand Down Expand Up @@ -120,6 +120,12 @@ class ISoundManager :
/** greebo: Stops the currently played sound.
*/
virtual void stopSound() = 0;

// Reloads all sound shader definitions from the VFS
virtual void reloadSounds() = 0;

// Fired after the sound shaders have been (re-)parsed from disk
virtual sigc::signal<void>& signal_soundShadersReloaded() = 0;
};

// Accessor method
Expand Down
1 change: 1 addition & 0 deletions install/menu.xml
Expand Up @@ -24,6 +24,7 @@
<menuItem name="refreshShaders" caption="Reload Materials" command="RefreshShaders" icon="texwindow_flushandreload.png" />
<menuItem name="reloadDefs" caption="Reload Defs" command="ReloadDefs" />
<menuItem name="reloadParticles" caption="Reload Particles" command="ReloadParticles" icon="particle16.png" />
<menuItem name="reloadSounds" caption="Reload Sounds" command="ReloadSounds" icon="icon_sound.png" />
<menuSeparator />
<menuItem name="gameSelector" caption="&amp;Game/Project Setup..." command="ProjectSettings" />
<menuSeparator />
Expand Down
2 changes: 1 addition & 1 deletion plugins/sound/SoundFileLoader.h
Expand Up @@ -96,7 +96,7 @@ class SoundFileLoader
{
parseShadersFromStream(is, fileInfo, file->getModName());
}
catch (parser::ParseException & ex)
catch (parser::ParseException& ex)
{
rError() << "[sound]: Error while parsing " << fileInfo.name <<
": " << ex.what() << std::endl;
Expand Down
41 changes: 28 additions & 13 deletions plugins/sound/SoundManager.cpp
Expand Up @@ -2,6 +2,7 @@
#include "SoundFileLoader.h"

#include "ifilesystem.h"
#include "icommandsystem.h"

#include "debugging/ScopedDebugTimer.h"

Expand All @@ -22,7 +23,7 @@ void SoundManager::forEachShader(std::function<void(const ISoundShader&)> f)
{
ensureShadersLoaded();

for (const ShaderMap::value_type& pair : _shaders)
for (const auto& pair : _shaders)
{
f(*pair.second);
}
Expand Down Expand Up @@ -94,11 +95,16 @@ void SoundManager::stopSound()
if (_soundPlayer) _soundPlayer->stop();
}

sigc::signal<void>& SoundManager::signal_soundShadersReloaded()
{
return _sigSoundShadersReloaded;
}

ISoundShaderPtr SoundManager::getSoundShader(const std::string& shaderName)
{
ensureShadersLoaded();

ShaderMap::const_iterator found = _shaders.find(shaderName);
auto found = _shaders.find(shaderName);

// If the name was found, return it, otherwise return an empty shader object
return found != _shaders.end() ? found->second : _emptyShader;
Expand All @@ -112,12 +118,9 @@ const std::string& SoundManager::getName() const

const StringSet& SoundManager::getDependencies() const
{
static StringSet _dependencies;

if (_dependencies.empty()) {
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
}

static StringSet _dependencies {
MODULE_VIRTUALFILESYSTEM, MODULE_COMMANDSYSTEM
};
return _dependencies;
}

Expand Down Expand Up @@ -147,23 +150,35 @@ void SoundManager::ensureShadersLoaded()

void SoundManager::initialiseModule(const IApplicationContext& ctx)
{
GlobalCommandSystem().addCommand("ReloadSounds",
std::bind(&SoundManager::reloadSoundsCmd, this, std::placeholders::_1));

// Create the SoundPlayer if sound is not disabled
const auto& args = ctx.getCmdLineArgs();
auto found = std::find(args.begin(), args.end(), "--disable-sound");

if (found == args.end())
{
rMessage() << "SoundManager: initialising sound playback"
<< std::endl;
_soundPlayer = std::shared_ptr<SoundPlayer>(new SoundPlayer);
rMessage() << "SoundManager: initialising sound playback" << std::endl;
_soundPlayer.reset(new SoundPlayer);
}
else
{
rMessage() << "SoundManager: sound output disabled"
<< std::endl;
rMessage() << "SoundManager: sound output disabled" << std::endl;
}

_defLoader.start();
}

void SoundManager::reloadSounds()
{
_defLoader.reset();
_defLoader.start();
}

void SoundManager::reloadSoundsCmd(const cmd::ArgumentList& args)
{
reloadSounds();
}

} // namespace sound
26 changes: 15 additions & 11 deletions plugins/sound/SoundManager.h
Expand Up @@ -4,20 +4,20 @@
#include "SoundPlayer.h"

#include "isound.h"
#include "icommandsystem.h"

#include "ThreadedDefLoader.h"
#include <map>

namespace sound {

/// SoundManager implementing class.
class SoundManager : public ISoundManager
class SoundManager :
public ISoundManager
{
public: /* TYPES */

// Map of named sound shaders
typedef std::map<std::string, SoundShader::Ptr> ShaderMap;
typedef std::shared_ptr<ShaderMap> ShaderMapPtr;

private: /* FIELDS */

Expand All @@ -31,27 +31,31 @@ class SoundManager : public ISoundManager
SoundShader::Ptr _emptyShader;

// The helper class for playing the sounds
std::shared_ptr<SoundPlayer> _soundPlayer;
std::unique_ptr<SoundPlayer> _soundPlayer;

sigc::signal<void> _sigSoundShadersReloaded;

private:
void loadShadersFromFilesystem();
void ensureShadersLoaded();
void reloadSoundsCmd(const cmd::ArgumentList& args);

public:
SoundManager();

// ISoundManager implementation
void forEachShader(std::function<void(const ISoundShader&)>) override;
ISoundShaderPtr getSoundShader(const std::string& shaderName) override;
virtual bool playSound(const std::string& fileName) override;
virtual bool playSound(const std::string& fileName, bool loopSound) override;
virtual void stopSound() override;
bool playSound(const std::string& fileName) override;
bool playSound(const std::string& fileName, bool loopSound) override;
void stopSound() override;
void reloadSounds() override;
sigc::signal<void>& signal_soundShadersReloaded() override;

// RegisterableModule implementation
virtual const std::string& getName() const override;
virtual const StringSet& getDependencies() const override;
virtual void initialiseModule(const IApplicationContext& ctx) override;
const std::string& getName() const override;
const StringSet& getDependencies() const override;
void initialiseModule(const IApplicationContext& ctx) override;
};
typedef std::shared_ptr<SoundManager> SoundManagerPtr;

}
1 change: 0 additions & 1 deletion plugins/sound/sound.cpp
@@ -1,6 +1,5 @@
#include "SoundManager.h"

#include "ifilesystem.h"
#include "imodule.h"

extern "C" void DARKRADIANT_DLLEXPORT RegisterModule(IModuleRegistry& registry)
Expand Down

0 comments on commit 0cfd312

Please sign in to comment.