Prechádzať zdrojové kódy

Added a quicker way to play audio clips without having to create scene objects and audio source components

BearishSun 9 rokov pred
rodič
commit
73e30ce934

+ 20 - 2
Source/BansheeCore/Include/BsAudio.h

@@ -4,6 +4,7 @@
 
 
 #include "BsCorePrerequisites.h"
 #include "BsCorePrerequisites.h"
 #include "BsModule.h"
 #include "BsModule.h"
+#include "BsVector3.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
@@ -21,7 +22,17 @@ namespace BansheeEngine
 	class BS_CORE_EXPORT Audio : public Module<Audio>
 	class BS_CORE_EXPORT Audio : public Module<Audio>
 	{
 	{
 	public:
 	public:
-		virtual ~Audio() { }
+		virtual ~Audio() {}
+
+		/** 
+		 * Starts playback of the provided audio clip. This can be used for a quicker way of creating audio sources if you
+		 * don't need the full control provided by creating AudioSource manually.
+		 *
+		 * @param[in]	clip		Audio clip to play.
+		 * @param[in]	position	Position in world space to play the clip at. Only relevant if the clip is 3D.
+		 * @param[in]	volume		Volume to play the clip at.
+		 */
+		void play(const HAudioClip& clip, const Vector3& position = Vector3::ZERO, float volume = 1.0f);
 
 
 		/** Sets global audio volume. In range [0, 1]. */
 		/** Sets global audio volume. In range [0, 1]. */
 		virtual void setVolume(float volume) = 0;
 		virtual void setVolume(float volume) = 0;
@@ -52,7 +63,7 @@ namespace BansheeEngine
 		 */
 		 */
 
 
 		/** Called once per frame. Queues streaming audio requests. */
 		/** Called once per frame. Queues streaming audio requests. */
-		virtual void _update() = 0;
+		virtual void _update();
 
 
 		/** @} */
 		/** @} */
 	protected:
 	protected:
@@ -77,6 +88,13 @@ namespace BansheeEngine
 
 
 		/** Creates a new AudioSource. */
 		/** Creates a new AudioSource. */
 		virtual SPtr<AudioSource> createSource() = 0;
 		virtual SPtr<AudioSource> createSource() = 0;
+
+		/** Stops playback of all sources started with Audio::play calls. */
+		void stopManualSources();
+
+	private:
+		Vector<SPtr<AudioSource>> mManualSources;
+		Vector<SPtr<AudioSource>> mTempSources;
 	};
 	};
 
 
 	/** Provides easier access to Audio. */
 	/** Provides easier access to Audio. */

+ 33 - 0
Source/BansheeCore/Source/BsAudio.cpp

@@ -1,9 +1,42 @@
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
 #include "BsAudio.h"
 #include "BsAudio.h"
+#include "BsAudioSource.h"
 
 
 namespace BansheeEngine
 namespace BansheeEngine
 {
 {
+	void Audio::play(const HAudioClip& clip, const Vector3& position, float volume)
+	{
+		SPtr<AudioSource> source = createSource();
+		source->setClip(clip);
+		source->setPosition(position);
+		source->setVolume(volume);
+		source->play();
+
+		mManualSources.push_back(source);
+	}
+
+	void Audio::stopManualSources()
+	{
+		for (auto& source : mManualSources)
+			source->stop();
+
+		mManualSources.clear();
+	}
+
+	void Audio::_update()
+	{
+		UINT32 numSources = (UINT32)mManualSources.size();
+		for(UINT32 i = 0; i < numSources; i++)
+		{
+			if (mManualSources[i]->getState() != AudioSourceState::Stopped)
+				mTempSources.push_back(mManualSources[i]);
+		}
+
+		std::swap(mTempSources, mManualSources);
+		mTempSources.clear();
+	}
+
 	Audio& gAudio()
 	Audio& gAudio()
 	{
 	{
 		return Audio::instance();
 		return Audio::instance();

+ 1 - 1
Source/BansheeEngine/Source/BsGUICanvas.cpp

@@ -67,7 +67,7 @@ namespace BansheeEngine
 		element.color = color;
 		element.color = color;
 
 
 		element.vertexStart = (UINT32)mVertexData.size();
 		element.vertexStart = (UINT32)mVertexData.size();
-		element.numVertices = vertices.size();
+		element.numVertices = (UINT32)vertices.size();
 
 
 		mVertexData.insert(mVertexData.end(), vertices.begin(), vertices.end());
 		mVertexData.insert(mVertexData.end(), vertices.begin(), vertices.end());
 		_markContentAsDirty();
 		_markContentAsDirty();

+ 1 - 1
Source/BansheeFMOD/Source/BsFMODAudio.cpp

@@ -112,7 +112,7 @@ namespace BansheeEngine
 	{
 	{
 		mFMOD->update();
 		mFMOD->update();
 
 
-		// TODO - Update streaming?
+		Audio::_update();
 	}
 	}
 
 
 	void FMODAudio::setActiveDevice(const AudioDevice& device)
 	void FMODAudio::setActiveDevice(const AudioDevice& device)

+ 2 - 0
Source/BansheeOpenAudio/Source/BsOAAudio.cpp

@@ -112,6 +112,8 @@ namespace BansheeEngine
 
 
 		mStreamingTask = Task::create("AudioStream", worker, TaskPriority::VeryHigh);
 		mStreamingTask = Task::create("AudioStream", worker, TaskPriority::VeryHigh);
 		TaskScheduler::instance().addTask(mStreamingTask);
 		TaskScheduler::instance().addTask(mStreamingTask);
+
+		Audio::_update();
 	}
 	}
 
 
 	void OAAudio::setActiveDevice(const AudioDevice& device)
 	void OAAudio::setActiveDevice(const AudioDevice& device)