diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/crepe/Resource.h | 33 | ||||
| -rw-r--r-- | src/crepe/api/AudioSource.cpp | 22 | ||||
| -rw-r--r-- | src/crepe/api/AudioSource.h | 51 | ||||
| -rw-r--r-- | src/crepe/facade/Sound.cpp | 29 | ||||
| -rw-r--r-- | src/crepe/facade/Sound.h | 14 | ||||
| -rw-r--r-- | src/crepe/facade/SoundContext.cpp | 5 | ||||
| -rw-r--r-- | src/crepe/facade/SoundContext.h | 6 | ||||
| -rw-r--r-- | src/crepe/system/AudioSystem.cpp | 21 | ||||
| -rw-r--r-- | src/crepe/system/AudioSystem.h | 19 | ||||
| -rw-r--r-- | src/crepe/system/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/crepe/system/System.h | 2 | ||||
| -rw-r--r-- | src/test/AudioTest.cpp | 26 | ||||
| -rw-r--r-- | src/test/CMakeLists.txt | 1 | 
13 files changed, 200 insertions, 31 deletions
diff --git a/src/crepe/Resource.h b/src/crepe/Resource.h new file mode 100644 index 0000000..dcf3dbd --- /dev/null +++ b/src/crepe/Resource.h @@ -0,0 +1,33 @@ +#pragma once + +#include <memory> + +namespace crepe { + +class ResourceManager; +class Asset; + +/** + * Resource is an interface class used to represent a (deserialized) game + * resource (e.g. textures, sounds). + */ +class Resource { +private: +	/** +	 * \brief Prototype pattern clone function. +	 * +	 * \param src Source file of new resource (abstraction for file saved on +	 * disk) +	 * +	 * \returns New instance of concrete resource +	 */ +	virtual std::unique_ptr<Resource> clone(const Asset & src) const = 0; +	/** +	 * The resource manager uses \c clone to create new instances of the concrete +	 * resource class. This may be used to inherit references to classes that +	 * would otherwise need to be implemented as singletons. +	 */ +	friend class ResourceManager; +}; + +} // namespace crepe diff --git a/src/crepe/api/AudioSource.cpp b/src/crepe/api/AudioSource.cpp new file mode 100644 index 0000000..b0cf28c --- /dev/null +++ b/src/crepe/api/AudioSource.cpp @@ -0,0 +1,22 @@ +#include <memory> + +#include "AudioSource.h" + +using namespace crepe; +using namespace std; + +AudioSource::AudioSource(game_object_id_t id, unique_ptr<Asset> audio_clip) : +	Component(id), +	audio_clip(std::move(audio_clip)) +{ } + +void AudioSource::play(bool looping) { +	this->loop = looping; +	this->playing = true; +} + +void AudioSource::stop() { +	this->playing = false; +	this->rewind = true; +} + diff --git a/src/crepe/api/AudioSource.h b/src/crepe/api/AudioSource.h new file mode 100644 index 0000000..5bc70f9 --- /dev/null +++ b/src/crepe/api/AudioSource.h @@ -0,0 +1,51 @@ +#pragma once + +#include <memory> + +#include "../Asset.h" +#include "../Component.h" +#include "../types.h" + +namespace crepe { + +//! Audio source component +class AudioSource : public Component { +public: +	AudioSource(game_object_id_t id, const Asset & source); +	virtual ~AudioSource() = default; + +public: +	//! Start or resume this audio source +	void play(bool looping = false); +	//! Stop this audio source +	void stop(); + +public: +	//! Sample file location +	const std::unique_ptr<Asset> audio_clip; +	//! Play when this component becomes active +	bool play_on_awake = false; +	//! Repeat the current audio clip during playback +	bool loop = false; +	//! Normalized volume (0.0 - 1.0) +	float volume = 1.0; + +private: +	//! If this source is playing audio +	bool playing = false; +	//! Rewind the sample location +	bool rewind = false; + +private: +	//! Value of \c active after last system update +	bool last_active = false; +	//! Value of \c playing after last system update +	bool last_playing = false; +	//! Value of \c volume after last system update +	float last_volume = 1.0; +	//! Value of \c loop after last system update +	bool last_loop = false; +}; + +} // namespace crepe + diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp index a65b052..5cd31e8 100644 --- a/src/crepe/facade/Sound.cpp +++ b/src/crepe/facade/Sound.cpp @@ -1,3 +1,6 @@ +#include <memory> + +#include "../Asset.h"  #include "../util/Log.h"  #include "Sound.h" @@ -6,22 +9,16 @@  using namespace crepe;  using namespace std; -Sound::Sound(unique_ptr<Asset> res) { -	dbg_trace(); -	this->load(std::move(res)); -} - -Sound::Sound(const char * src) { -	dbg_trace(); -	this->load(make_unique<Asset>(src)); -} +Sound::Sound(SoundContext & ctx) : context(ctx) { dbg_trace(); } -void Sound::load(unique_ptr<Asset> res) { -	this->sample.load(res->get_canonical().c_str()); +unique_ptr<Resource> Sound::clone(const Asset & src) const { +	auto instance = make_unique<Sound>(*this); +	instance->sample.load(src.get_canonical().c_str()); +	return instance;  }  void Sound::play() { -	SoundContext & ctx = SoundContext::get_instance(); +	SoundContext & ctx = this->context;  	if (ctx.engine.getPause(this->handle)) {  		// resume if paused  		ctx.engine.setPause(this->handle, false); @@ -33,13 +30,13 @@ void Sound::play() {  }  void Sound::pause() { -	SoundContext & ctx = SoundContext::get_instance(); +	SoundContext & ctx = this->context;  	if (ctx.engine.getPause(this->handle)) return;  	ctx.engine.setPause(this->handle, true);  }  void Sound::rewind() { -	SoundContext & ctx = SoundContext::get_instance(); +	SoundContext & ctx = this->context;  	if (!ctx.engine.isValidVoiceHandle(this->handle)) return;  	ctx.engine.seek(this->handle, 0);  } @@ -47,7 +44,7 @@ void Sound::rewind() {  void Sound::set_volume(float volume) {  	this->volume = volume; -	SoundContext & ctx = SoundContext::get_instance(); +	SoundContext & ctx = this->context;  	if (!ctx.engine.isValidVoiceHandle(this->handle)) return;  	ctx.engine.setVolume(this->handle, this->volume);  } @@ -55,7 +52,7 @@ void Sound::set_volume(float volume) {  void Sound::set_looping(bool looping) {  	this->looping = looping; -	SoundContext & ctx = SoundContext::get_instance(); +	SoundContext & ctx = this->context;  	if (!ctx.engine.isValidVoiceHandle(this->handle)) return;  	ctx.engine.setLooping(this->handle, this->looping);  } diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h index 402482f..8342b46 100644 --- a/src/crepe/facade/Sound.h +++ b/src/crepe/facade/Sound.h @@ -4,17 +4,19 @@  #include <soloud/soloud.h>  #include <soloud/soloud_wav.h> -#include "../Asset.h" +#include "../Resource.h"  namespace crepe { +class SoundContext; +  /**   * \brief Sound resource facade   *   * This class is a wrapper around a \c SoLoud::Wav instance, which holds a   * single sample. It is part of the sound facade.   */ -class Sound { +class Sound : public Resource {  public:  	/**  	 * \brief Pause this sample @@ -70,15 +72,13 @@ public:  	bool get_looping() const { return this->looping; }  public: -	Sound(const char * src); -	Sound(std::unique_ptr<Asset> res); - -private: -	void load(std::unique_ptr<Asset> res); +	Sound(SoundContext & ctx); +	std::unique_ptr<Resource> clone(const Asset & src) const override;  private:  	SoLoud::Wav sample;  	SoLoud::handle handle; +	SoundContext & context;  	float volume = 1.0f;  	bool looping = false; diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp index deb2b62..b65dfb2 100644 --- a/src/crepe/facade/SoundContext.cpp +++ b/src/crepe/facade/SoundContext.cpp @@ -4,11 +4,6 @@  using namespace crepe; -SoundContext & SoundContext::get_instance() { -	static SoundContext instance; -	return instance; -} -  SoundContext::SoundContext() {  	dbg_trace();  	engine.init(); diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h index d703c16..d22ff7a 100644 --- a/src/crepe/facade/SoundContext.h +++ b/src/crepe/facade/SoundContext.h @@ -13,18 +13,18 @@ namespace crepe {   * the methods for playing \c Sound instances. It is part of the sound facade.   */  class SoundContext { -private: -	// singleton +public:  	SoundContext();  	virtual ~SoundContext(); +  	SoundContext(const SoundContext &) = delete;  	SoundContext(SoundContext &&) = delete;  	SoundContext & operator=(const SoundContext &) = delete;  	SoundContext & operator=(SoundContext &&) = delete;  private: -	static SoundContext & get_instance();  	SoLoud::Soloud engine; +	//! Sound directly calls methods on \c engine  	friend class Sound;  }; diff --git a/src/crepe/system/AudioSystem.cpp b/src/crepe/system/AudioSystem.cpp new file mode 100644 index 0000000..67967ef --- /dev/null +++ b/src/crepe/system/AudioSystem.cpp @@ -0,0 +1,21 @@ +#include "AudioSystem.h" +#include "ComponentManager.h" + +#include "../api/AudioSource.h" + +using namespace crepe; +using namespace std; + +void AudioSystem::update() { +	ComponentManager & mgr = this->component_manager; +	vector<reference_wrapper<AudioSource>> components = mgr.get_components_by_type<AudioSource>(); + +	for (auto component_ref : components) { +		AudioSource & component = component_ref.get(); +		if (!component.active) continue; + +		// TODO: fetch Sound instance from resourcemanager +		// TODO: lots of state diffing +	} +} + diff --git a/src/crepe/system/AudioSystem.h b/src/crepe/system/AudioSystem.h new file mode 100644 index 0000000..e037f51 --- /dev/null +++ b/src/crepe/system/AudioSystem.h @@ -0,0 +1,19 @@ +#pragma once + +#include "../facade/SoundContext.h" + +#include "System.h" + +namespace crepe { + +class AudioSystem : public System { +public: +	using System::System; +	void update() override; + +private: +	SoundContext context {}; +}; + +} // namespace crepe + diff --git a/src/crepe/system/CMakeLists.txt b/src/crepe/system/CMakeLists.txt index d658b25..f507b90 100644 --- a/src/crepe/system/CMakeLists.txt +++ b/src/crepe/system/CMakeLists.txt @@ -5,6 +5,7 @@ target_sources(crepe PUBLIC  	PhysicsSystem.cpp  	CollisionSystem.cpp  	RenderSystem.cpp +	AudioSystem.cpp  	AnimatorSystem.cpp  ) @@ -14,5 +15,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES  	PhysicsSystem.h  	CollisionSystem.h  	RenderSystem.h +	AudioSystem.h  	AnimatorSystem.h  ) diff --git a/src/crepe/system/System.h b/src/crepe/system/System.h index 28ea20e..36f7edc 100644 --- a/src/crepe/system/System.h +++ b/src/crepe/system/System.h @@ -1,5 +1,7 @@  #pragma once +#include "../ComponentManager.h" +  namespace crepe {  class ComponentManager; diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp new file mode 100644 index 0000000..6e2706c --- /dev/null +++ b/src/test/AudioTest.cpp @@ -0,0 +1,26 @@ +#include "system/AudioSystem.h" +#include <gtest/gtest.h> + +#include <crepe/ComponentManager.h> +#include <crepe/api/AudioSource.h> +#include <crepe/api/GameObject.h> + +using namespace std; +using namespace crepe; +using namespace testing; + +class AudioTest : public Test { +public: +	ComponentManager component_manager{}; +	AudioSystem system {component_manager}; + +	void SetUp() override { +		auto & mgr = this->component_manager; +		GameObject entity = mgr.new_object("name"); +		entity.add_component<AudioSource>("../mwe/audio/sfx1.wav"); +	} +}; + +TEST_F(AudioTest, Default) { +} + diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 49c8151..1a986bd 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,5 +3,6 @@ target_sources(test_main PUBLIC  	PhysicsTest.cpp  	ScriptTest.cpp  	ParticleTest.cpp +	AudioTest.cpp  )  |