Selaa lähdekoodia

Added C++ components for AudioListener and AudioSource

BearishSun 9 vuotta sitten
vanhempi
sitoutus
28e8be2526

+ 7 - 0
Source/BansheeCore/CMakeSources.cmake

@@ -14,6 +14,8 @@ set(BS_BANSHEECORE_INC_COMPONENTS
 	"Include/BsCSphericalJoint.h"
 	"Include/BsCSphericalJoint.h"
 	"Include/BsCD6Joint.h"
 	"Include/BsCD6Joint.h"
 	"Include/BsCCharacterController.h"
 	"Include/BsCCharacterController.h"
+	"Include/BsCAudioSource.h"
+	"Include/BsCAudioListener.h"
 )
 )
 
 
 set(BS_BANSHEECORE_INC_PHYSICS
 set(BS_BANSHEECORE_INC_PHYSICS
@@ -244,6 +246,8 @@ set(BS_BANSHEECORE_SRC_COMPONENTS
 	"Source/BsCSphericalJoint.cpp"
 	"Source/BsCSphericalJoint.cpp"
 	"Source/BsCD6Joint.cpp"
 	"Source/BsCD6Joint.cpp"
 	"Source/BsCCharacterController.cpp"
 	"Source/BsCCharacterController.cpp"
+	"Source/BsCAudioSource.cpp"
+	"Source/BsCAudioListener.cpp"	
 )
 )
 
 
 set(BS_BANSHEECORE_SRC_PLATFORM
 set(BS_BANSHEECORE_SRC_PLATFORM
@@ -330,6 +334,9 @@ set(BS_BANSHEECORE_INC_RTTI
 	"Include/BsCCharacterControllerRTTI.h"
 	"Include/BsCCharacterControllerRTTI.h"
 	"Include/BsShaderImportOptionsRTTI.h"
 	"Include/BsShaderImportOptionsRTTI.h"
 	"Include/BsPhysicsMeshRTTI.h"
 	"Include/BsPhysicsMeshRTTI.h"
+	"Include/BsAudioClipRTTI.h"
+	"Include/BsCAudioSourceRTTI.h"
+	"Include/BsCAudioListenerRTTI.h"
 )
 )
 
 
 set(BS_BANSHEECORE_SRC_RENDERER
 set(BS_BANSHEECORE_SRC_RENDERER

+ 90 - 0
Source/BansheeCore/Include/BsCAudioListener.h

@@ -0,0 +1,90 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsAudioListener.h"
+#include "BsComponent.h"
+
+namespace BansheeEngine 
+{
+	/** @addtogroup Components-Core
+	 *  @{
+	 */
+
+	/**
+	 * @copydoc	AudioListener
+	 *
+	 * Wraps AudioListener as a Component.
+	 */
+    class BS_CORE_EXPORT CAudioListener : public Component
+    {
+    public:
+		CAudioListener(const HSceneObject& parent);
+		virtual ~CAudioListener() {}
+		
+		/** @name Internal
+		 *  @{
+		 */
+
+		/** Returns the AudioListener implementation wrapped by this component. */
+		AudioListener* _getInternal() const { return mInternal.get(); }
+
+		/** @} */
+
+		/************************************************************************/
+		/* 						COMPONENT OVERRIDES                      		*/
+		/************************************************************************/
+	protected:
+		friend class SceneObject;
+
+		/** @copydoc Component::onInitialized() */
+		void onInitialized() override;
+
+		/** @copydoc Component::onDestroyed() */
+		void onDestroyed() override;
+
+		/** @copydoc Component::onDisabled() */
+		void onDisabled() override;
+
+		/** @copydoc Component::onEnabled() */
+		void onEnabled() override;
+
+		/** @copydoc Component::onTransformChanged() */
+		void onTransformChanged(TransformChangedFlags flags) override;
+
+		/** @copydoc Component::update() */
+		void update() override;
+    protected:
+		using Component::destroyInternal;
+
+		/** Creates the internal representation of the AudioListener and restores the values saved by the Component. */
+		void restoreInternal();
+
+		/** Destroys the internal AudioListener representation. */
+		void destroyInternal();
+
+		/** 
+		 * Updates the transform of the internal AudioListener representation from the transform of the component's scene
+		 * object.
+		 */
+		void updateTransform();
+
+		SPtr<AudioListener> mInternal;
+		Vector3 mLastPosition;
+		Vector3 mVelocity;
+
+		/************************************************************************/
+		/* 								RTTI		                     		*/
+		/************************************************************************/
+	public:
+		friend class CAudioListenerRTTI;
+		static RTTITypeBase* getRTTIStatic();
+		RTTITypeBase* getRTTI() const override;
+
+	protected:
+		CAudioListener() {} // Serialization only
+     };
+
+	 /** @} */
+}

+ 42 - 0
Source/BansheeCore/Include/BsCAudioListenerRTTI.h

@@ -0,0 +1,42 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsRTTIType.h"
+#include "BsCAudioListener.h"
+#include "BsGameObjectRTTI.h"
+
+namespace BansheeEngine
+{
+	/** @cond RTTI */
+	/** @addtogroup RTTI-Impl-Core
+	 *  @{
+	 */
+
+	class BS_CORE_EXPORT CAudioListenerRTTI : public RTTIType<CAudioListener, Component, CAudioListenerRTTI>
+	{
+	public:
+		CAudioListenerRTTI()
+		{ }
+
+		const String& getRTTIName() override
+		{
+			static String name = "CAudioListener";
+			return name;
+		}
+
+		UINT32 getRTTIId() override
+		{
+			return TID_CAudioListener;
+		}
+
+		SPtr<IReflectable> newRTTIObject() override
+		{
+			return GameObjectRTTI::createGameObject<CAudioListener>();
+		}
+	};
+
+	/** @} */
+	/** @endcond */
+}

+ 155 - 0
Source/BansheeCore/Include/BsCAudioSource.h

@@ -0,0 +1,155 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsAudioSource.h"
+#include "BsComponent.h"
+
+namespace BansheeEngine 
+{
+	/** @addtogroup Components-Core
+	 *  @{
+	 */
+
+	/**
+	 * @copydoc	AudioSource
+	 *
+	 * Wraps AudioSource as a Component.
+	 */
+    class BS_CORE_EXPORT CAudioSource : public Component
+    {
+    public:
+		CAudioSource(const HSceneObject& parent);
+		virtual ~CAudioSource() {}
+		
+		/** @copydoc AudioSource::setClip */
+		void setClip(const HAudioClip& clip);
+
+		/** @copydoc AudioSource::getClip */
+		HAudioClip getClip() const { return mAudioClip; }
+
+		/** @copydoc AudioSource::setVolume */
+		void setVolume(float volume);
+
+		/** @copydoc AudioSource::getVolume */
+		float getVolume() const { return mVolume; }
+
+		/** @copydoc AudioSource::setPitch */
+		void setPitch(float pitch);
+
+		/** @copydoc AudioSource::getPitch */
+		float getPitch() const { return mPitch; }
+
+		/** @copydoc AudioSource::setIsLooping */
+		void setIsLooping(bool loop);
+
+		/** @copydoc AudioSource::getIsLooping */
+		bool getIsLooping() const { return mLoop; }
+
+		/** @copydoc AudioSource::setPriority */
+		void setPriority(UINT32 priority);
+
+		/** @copydoc AudioSource::getPriority */
+		UINT32 getPriority() const { return mPriority; }
+
+		/** @copydoc AudioSource::setMinDistance */
+		void setMinDistance(float distance);
+
+		/** @copydoc AudioSource::getMinDistance */
+		float getMinDistance() const { return mMinDistance; }
+
+		/** @copydoc AudioSource::setAttenuation */
+		void setAttenuation(float attenuation);
+
+		/** @copydoc AudioSource::getAttenuation */
+		float getAttenuation() const { return mAttenuation; }
+
+		/** @copydoc AudioSource::play */
+		void play();
+
+		/** @copydoc AudioSource::pause */
+		void pause();
+
+		/** @copydoc AudioSource::stop */
+		void stop();
+
+		/** @copydoc AudioSource::seek */
+		void seek(float position);
+
+		/** @copydoc AudioSource::getState */
+		AudioSourceState getState() const;
+
+		/** @name Internal
+		 *  @{
+		 */
+
+		/** Returns the AudioSource implementation wrapped by this component. */
+		AudioSource* _getInternal() const { return mInternal.get(); }
+
+		/** @} */
+
+		/************************************************************************/
+		/* 						COMPONENT OVERRIDES                      		*/
+		/************************************************************************/
+	protected:
+		friend class SceneObject;
+
+		/** @copydoc Component::onInitialized() */
+		void onInitialized() override;
+
+		/** @copydoc Component::onDestroyed() */
+		void onDestroyed() override;
+
+		/** @copydoc Component::onDisabled() */
+		void onDisabled() override;
+
+		/** @copydoc Component::onEnabled() */
+		void onEnabled() override;
+
+		/** @copydoc Component::onTransformChanged() */
+		void onTransformChanged(TransformChangedFlags flags) override;
+
+		/** @copydoc Component::update() */
+		void update() override;
+    protected:
+		using Component::destroyInternal;
+
+		/** Creates the internal representation of the AudioSource and restores the values saved by the Component. */
+		void restoreInternal();
+
+		/** Destroys the internal AudioSource representation. */
+		void destroyInternal();
+
+		/** 
+		 * Updates the transform of the internal AudioSource representation from the transform of the component's scene
+		 * object.
+		 */
+		void updateTransform();
+
+		SPtr<AudioSource> mInternal;
+		Vector3 mLastPosition;
+		Vector3 mVelocity;
+
+		HAudioClip mAudioClip;
+		float mVolume;
+		float mPitch;
+		bool mLoop;
+		UINT32 mPriority;
+		float mMinDistance;
+		float mAttenuation;
+
+		/************************************************************************/
+		/* 								RTTI		                     		*/
+		/************************************************************************/
+	public:
+		friend class CAudioSourceRTTI;
+		static RTTITypeBase* getRTTIStatic();
+		RTTITypeBase* getRTTI() const override;
+
+	protected:
+		CAudioSource() {} // Serialization only
+     };
+
+	 /** @} */
+}

+ 52 - 0
Source/BansheeCore/Include/BsCAudioSourceRTTI.h

@@ -0,0 +1,52 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsRTTIType.h"
+#include "BsCAudioSource.h"
+#include "BsGameObjectRTTI.h"
+
+namespace BansheeEngine
+{
+	/** @cond RTTI */
+	/** @addtogroup RTTI-Impl-Core
+	 *  @{
+	 */
+
+	class BS_CORE_EXPORT CAudioSourceRTTI : public RTTIType<CAudioSource, Component, CAudioSourceRTTI>
+	{
+		BS_BEGIN_RTTI_MEMBERS
+			BS_RTTI_MEMBER_REFL(mAudioClip, 0)
+			BS_RTTI_MEMBER_PLAIN(mVolume, 1)
+			BS_RTTI_MEMBER_PLAIN(mPitch, 2)
+			BS_RTTI_MEMBER_PLAIN(mLoop, 3)
+			BS_RTTI_MEMBER_PLAIN(mPriority, 4)
+			BS_RTTI_MEMBER_PLAIN(mMinDistance, 5)
+			BS_RTTI_MEMBER_PLAIN(mAttenuation, 6)
+		BS_END_RTTI_MEMBERS
+	public:
+		CAudioSourceRTTI()
+			:mInitMembers(this)
+		{ }
+
+		const String& getRTTIName() override
+		{
+			static String name = "CAudioSource";
+			return name;
+		}
+
+		UINT32 getRTTIId() override
+		{
+			return TID_CAudioSource;
+		}
+
+		SPtr<IReflectable> newRTTIObject() override
+		{
+			return GameObjectRTTI::createGameObject<CAudioSource>();
+		}
+	};
+
+	/** @} */
+	/** @endcond */
+}

+ 3 - 1
Source/BansheeCore/Include/BsCorePrerequisites.h

@@ -499,7 +499,9 @@ namespace BansheeEngine
 		TID_FPhysicsMesh = 1109,
 		TID_FPhysicsMesh = 1109,
 		TID_ShaderImportOptions = 1110,
 		TID_ShaderImportOptions = 1110,
 		TID_AudioClip = 1111,
 		TID_AudioClip = 1111,
-		TID_AudioClipImportOptions = 1112
+		TID_AudioClipImportOptions = 1112,
+		TID_CAudioListener = 1113,
+		TID_CAudioSource = 1114
 	};
 	};
 }
 }
 
 

+ 87 - 0
Source/BansheeCore/Source/BsCAudioListener.cpp

@@ -0,0 +1,87 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#include "BsCAudioListener.h"
+#include "BsSceneObject.h"
+#include "BsTime.h"
+#include "BsCAudioListenerRTTI.h"
+
+using namespace std::placeholders;
+
+namespace BansheeEngine
+{
+	CAudioListener::CAudioListener(const HSceneObject& parent)
+		: Component(parent)
+	{
+		setName("AudioListener");
+
+		mNotifyFlags = TCF_Transform;
+	}
+	
+	void CAudioListener::onInitialized()
+	{
+
+	}
+
+	void CAudioListener::onDestroyed()
+	{
+		destroyInternal();
+	}
+
+	void CAudioListener::onDisabled()
+	{
+		destroyInternal();
+	}
+
+	void CAudioListener::onEnabled()
+	{
+		restoreInternal();
+	}
+
+	void CAudioListener::onTransformChanged(TransformChangedFlags flags)
+	{
+		if (!SO()->getActive())
+			return;
+
+		if ((flags & (TCF_Parent | TCF_Transform)) != 0)
+			updateTransform();
+	}
+
+	void CAudioListener::update()
+	{
+		Vector3 worldPos = SO()->getWorldPosition();
+		mVelocity = (worldPos - mLastPosition) / gTime().getFrameDelta();
+		mLastPosition = worldPos;
+	}
+
+	void CAudioListener::restoreInternal()
+	{
+		if (mInternal == nullptr)
+			mInternal = AudioListener::create();
+
+		updateTransform();
+	}
+
+	void CAudioListener::destroyInternal()
+	{
+		// This should release the last reference and destroy the internal listener
+		mInternal = nullptr;
+	}
+	
+	void CAudioListener::updateTransform()
+	{
+		mInternal->setPosition(SO()->getWorldPosition());
+		mInternal->setDirection(SO()->getForward());
+		mInternal->setUp(SO()->getUp());
+		mInternal->setVelocity(mVelocity);
+	}
+	
+	RTTITypeBase* CAudioListener::getRTTIStatic()
+	{
+		return CAudioListenerRTTI::instance();
+	}
+
+	RTTITypeBase* CAudioListener::getRTTI() const
+	{
+		return CAudioListener::getRTTIStatic();
+	}
+}

+ 195 - 0
Source/BansheeCore/Source/BsCAudioSource.cpp

@@ -0,0 +1,195 @@
+//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
+//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
+#include "BsCAudioSource.h"
+#include "BsSceneObject.h"
+#include "BsTime.h"
+#include "BsCAudioSourceRTTI.h"
+
+using namespace std::placeholders;
+
+namespace BansheeEngine
+{
+	CAudioSource::CAudioSource(const HSceneObject& parent)
+		: Component(parent)
+	{
+		setName("AudioSource");
+
+		mNotifyFlags = TCF_Transform;
+	}
+
+	void CAudioSource::setClip(const HAudioClip& clip)
+	{
+		if (mAudioClip == clip)
+			return;
+
+		mAudioClip = clip;
+
+		if (mInternal != nullptr)
+			mInternal->setClip(clip);
+	}
+
+	void CAudioSource::setVolume(float volume)
+	{
+		if (mVolume == volume)
+			return;
+
+		mVolume = volume;
+
+		if (mInternal != nullptr)
+			mInternal->setVolume(volume);
+	}
+
+	void CAudioSource::setPitch(float pitch)
+	{
+		if (mPitch == pitch)
+			return;
+
+		mPitch = pitch;
+
+		if (mInternal != nullptr)
+			mInternal->setPitch(pitch);
+	}
+
+	void CAudioSource::setIsLooping(bool loop)
+	{
+		if (mLoop == loop)
+			return;
+
+		mLoop = loop;
+
+		if (mInternal != nullptr)
+			mInternal->setIsLooping(loop);
+	}
+
+	void CAudioSource::setPriority(UINT32 priority)
+	{
+		if (mPriority == priority)
+			return;
+
+		mPriority = priority;
+
+		if (mInternal != nullptr)
+			mInternal->setPriority(priority);
+	}
+
+	void CAudioSource::setMinDistance(float distance)
+	{
+		if (mMinDistance == distance)
+			return;
+
+		mMinDistance = distance;
+
+		if (mInternal != nullptr)
+			mInternal->setMinDistance(distance);
+	}
+
+	void CAudioSource::setAttenuation(float attenuation)
+	{
+		if (mAttenuation == attenuation)
+			return;
+
+		mAttenuation = attenuation;
+
+		if (mInternal != nullptr)
+			mInternal->setAttenuation(attenuation);
+	}
+
+	void CAudioSource::play()
+	{
+		if (mInternal != nullptr)
+			mInternal->play();
+	}
+
+	void CAudioSource::pause()
+	{
+		if (mInternal != nullptr)
+			mInternal->pause();
+	}
+
+	void CAudioSource::stop()
+	{
+		if (mInternal != nullptr)
+			mInternal->stop();
+	}
+
+	void CAudioSource::seek(float position)
+	{
+		if (mInternal != nullptr)
+			mInternal->seek(position);
+	}
+
+	void CAudioSource::onInitialized()
+	{
+
+	}
+
+	void CAudioSource::onDestroyed()
+	{
+		destroyInternal();
+	}
+
+	void CAudioSource::onDisabled()
+	{
+		destroyInternal();
+	}
+
+	void CAudioSource::onEnabled()
+	{
+		restoreInternal();
+	}
+
+	void CAudioSource::onTransformChanged(TransformChangedFlags flags)
+	{
+		if (!SO()->getActive())
+			return;
+
+		if ((flags & (TCF_Parent | TCF_Transform)) != 0)
+			updateTransform();
+	}
+
+	void CAudioSource::update()
+	{
+		Vector3 worldPos = SO()->getWorldPosition();
+		mVelocity = (worldPos - mLastPosition) / gTime().getFrameDelta();
+		mLastPosition = worldPos;
+	}
+
+	void CAudioSource::restoreInternal()
+	{
+		if (mInternal == nullptr)
+			mInternal = AudioSource::create();
+
+		// Note: Merge into one call to avoid many virtual function calls
+		mInternal->setClip(mAudioClip);
+		mInternal->setVolume(mVolume);
+		mInternal->setPitch(mPitch);
+		mInternal->setIsLooping(mLoop);
+		mInternal->setPriority(mPriority);
+		mInternal->setMinDistance(mMinDistance);
+		mInternal->setAttenuation(mAttenuation);
+
+		updateTransform();
+	}
+
+	void CAudioSource::destroyInternal()
+	{
+		// This should release the last reference and destroy the internal listener
+		mInternal = nullptr;
+	}
+
+	void CAudioSource::updateTransform()
+	{
+		mInternal->setPosition(SO()->getWorldPosition());
+		mInternal->setVelocity(mVelocity);
+	}
+
+	RTTITypeBase* CAudioSource::getRTTIStatic()
+	{
+		return CAudioSourceRTTI::instance();
+	}
+
+	RTTITypeBase* CAudioSource::getRTTI() const
+	{
+		return CAudioSource::getRTTIStatic();
+	}
+}