Skip to content

Commit

Permalink
Merge pull request #335 from capnlove/Clean_Positional
Browse files Browse the repository at this point in the history
Positional Audio Update
  • Loading branch information
greenfire27 committed Mar 10, 2016
2 parents 88a43b5 + 3ecd6cc commit b4f6c75
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 2 deletions.
2 changes: 2 additions & 0 deletions engine/compilers/VisualStudio 2015/Torque 2D.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@
<ClCompile Include="..\..\source\assets\declaredAssets.cc" />
<ClCompile Include="..\..\source\assets\referencedAssets.cc" />
<ClCompile Include="..\..\source\audio\AudioAsset.cc" />
<ClCompile Include="..\..\source\audio\audioDescriptions.cc" />
<ClCompile Include="..\..\source\audio\audio_ScriptBinding.cc" />
<ClCompile Include="..\..\source\audio\vorbisStreamSource.cc" />
<ClCompile Include="..\..\source\bitmapFont\BitmapFont.cc" />
Expand Down Expand Up @@ -763,6 +764,7 @@
<ClInclude Include="..\..\source\assets\tamlAssetReferencedUpdateVisitor.h" />
<ClInclude Include="..\..\source\assets\tamlAssetReferencedVisitor.h" />
<ClInclude Include="..\..\source\audio\AudioAsset.h" />
<ClInclude Include="..\..\source\audio\audioDescriptions.h" />
<ClInclude Include="..\..\source\audio\vorbisStreamSource.h" />
<ClInclude Include="..\..\source\bitmapFont\BitmapFont.h" />
<ClInclude Include="..\..\source\bitmapFont\BitmapFontCharacter.h" />
Expand Down
6 changes: 6 additions & 0 deletions engine/compilers/VisualStudio 2015/Torque 2D.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,9 @@
<ClCompile Include="..\..\source\bitmapFont\BitmapFontCharacter.cc">
<Filter>bitmapFont</Filter>
</ClCompile>
<ClCompile Include="..\..\source\audio\audioDescriptions.cc">
<Filter>audio</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\source\audio\audio.h">
Expand Down Expand Up @@ -3192,6 +3195,9 @@
<ClInclude Include="..\..\source\bitmapFont\BitmapFontLineInfo.h">
<Filter>bitmapFont</Filter>
</ClInclude>
<ClInclude Include="..\..\source\audio\audioDescriptions.h">
<Filter>audio</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\source\math\mMath_ASM.asm">
Expand Down
32 changes: 30 additions & 2 deletions engine/source/2d/gui/SceneWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ SceneWindow::SceneWindow() : mpScene(NULL),
mUseObjectInputEvents(false),
mInputEventGroupMaskFilter(MASK_ALL),
mInputEventLayerMaskFilter(MASK_ALL),
mInputEventInvisibleFilter( true )
mInputEventInvisibleFilter( true ),
mProcessAudioListener(false)
{
// Set Vector Associations.
VECTOR_SET_ASSOCIATION( mCameraQueue );
Expand Down Expand Up @@ -419,6 +420,13 @@ void SceneWindow::setCameraInterpolationMode( const CameraInterpolationMode inte

//-----------------------------------------------------------------------------

void SceneWindow::setProcessAudioListener(bool mval)
{
mProcessAudioListener = mval;
}

//-----------------------------------------------------------------------------

void SceneWindow::startCameraMove( const F32 interpolationTime )
{
// Are we mounted to an object and trying to move?
Expand Down Expand Up @@ -1462,6 +1470,8 @@ void SceneWindow::processTick( void )
// Debug Profiling.
PROFILE_SCOPE(SceneWindow_ProcessTick);

Point2F PreMove_CameraPos = mCameraCurrent.mSourceArea.centre();
F32 PreMove_CameraZoom = mCameraCurrent.mCameraZoom;
// Are we moving the camera.
if ( mMovingCamera )
{
Expand Down Expand Up @@ -1505,7 +1515,25 @@ void SceneWindow::processTick( void )
// No, so stop shake.
stopCameraShake();
}
}
}
if (mProcessAudioListener)
{
F32 listenervelocity[] = { 0.f, 0.f, 0.f };

Point2F campos = mCameraCurrent.mSourceArea.centre();

if (campos != PreMove_CameraPos)
{
listenervelocity[0] = (campos.x - PreMove_CameraPos.x) * Tickable::smTickSec;
listenervelocity[1] = (campos.y - PreMove_CameraPos.y) * Tickable::smTickSec;
listenervelocity[2] = (mCameraCurrent.mCameraZoom - PreMove_CameraZoom) * Tickable::smTickSec;
}

F32 listenerpos[] = { campos.x, campos.y, 5.f };

alListenerfv(AL_POSITION, listenerpos);
alListenerfv(AL_VELOCITY, listenervelocity);
}
}

//-----------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions engine/source/2d/gui/SceneWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class SceneWindow : public GuiControl, public virtual Tickable
U32 mRenderLayerMask;
U32 mRenderGroupMask;

//Audio Listener
bool mProcessAudioListener;

char mDebugText[256];

/// Handling Input Events.
Expand Down Expand Up @@ -264,6 +267,9 @@ class SceneWindow : public GuiControl, public virtual Tickable
void setCameraInterpolationTime( const F32 interpolationTime );
void setCameraInterpolationMode( const CameraInterpolationMode interpolationMode );

/// Audio Listener
void setProcessAudioListener(bool mval);

/// Camera Movement.
void startCameraMove( const F32 interpolationTime );
void stopCameraMove( void );
Expand All @@ -281,6 +287,9 @@ class SceneWindow : public GuiControl, public virtual Tickable
inline bool isCameraMounted( void ) const { return mCameraMounted; }
inline bool isCameraMoving( void ) const { return mMovingCamera; }

/// Audio Listener
inline bool isProcessAudioListener(void) const { return mProcessAudioListener; }

/// Camera Shake.
void startCameraShake( const F32 magnitude, const F32 time );
void stopCameraShake( void );
Expand Down
12 changes: 12 additions & 0 deletions engine/source/2d/gui/SceneWindow_ScriptBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -1545,4 +1545,16 @@ ConsoleMethodWithDocs(SceneWindow, getIsWindowPoint, ConsoleBool, 3, 4, (X / Y))
return object->mBounds.pointInRect( Point2I( S32(mFloor(dstPoint.x)+object->mBounds.point.x), S32(mFloor(dstPoint.y)+object->mBounds.point.y )) );
}

/*! Sets this Scenewindow to process the AudioListener's position and velocity
WARNING : The code does not verify if multiple SceneWindows have this function enabled.
User must make sure to manage this manually. Multiple listeners may lead to crashes/bugs/unexpected behaviors.
@param status - Whether to enable or disable AudioListener processing (optional, default to true)
*/
ConsoleMethodWithDocs(SceneWindow, setAudioListener, ConsoleVoid, 2, 3, ())
{
bool onoff = true;
if ((argc > 2) && !dAtob(argv[2])) onoff = false;
object->setProcessAudioListener(onoff);
}

ConsoleMethodGroupEndWithDocs(SceneWindow)
1 change: 1 addition & 0 deletions engine/source/2d/scene/Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5272,6 +5272,7 @@ static EnumTable::Enums DebugOptionsLookup[] =
{ Scene::SCENE_DEBUG_CONTROLLERS, "controllers" },
{ Scene::SCENE_DEBUG_JOINTS, "joints" },
{ Scene::SCENE_DEBUG_WIREFRAME_RENDER, "wireframe" },
{ Scene::SCENE_DEBUG_AUDIO_SOURCES, "audio"},
///
{ Scene::SCENE_DEBUG_AABB, "aabb" },
{ Scene::SCENE_DEBUG_OOBB, "oobb" },
Expand Down
1 change: 1 addition & 0 deletions engine/source/2d/scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Scene :
SCENE_DEBUG_CONTROLLERS = BIT(2),
SCENE_DEBUG_JOINTS = BIT(3),
SCENE_DEBUG_WIREFRAME_RENDER = BIT(4),
SCENE_DEBUG_AUDIO_SOURCES = BIT(5),
///
SCENE_DEBUG_AABB = BIT(16),
SCENE_DEBUG_OOBB = BIT(17),
Expand Down
94 changes: 94 additions & 0 deletions engine/source/2d/sceneobject/SceneObject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ SceneObject::SceneObject() :
VECTOR_SET_ASSOCIATION( mCollisionFixtureDefs );
VECTOR_SET_ASSOCIATION( mCollisionFixtures );
VECTOR_SET_ASSOCIATION( mAttachedCtrls );
VECTOR_SET_ASSOCIATION( mAudioHandles );
VECTOR_SET_ASSOCIATION( mHandleDeletionList );

// Assign scene-object index.
mSerialId = ++sSceneObjectMasterSerialId;
Expand Down Expand Up @@ -256,6 +258,16 @@ SceneObject::~SceneObject()
mpScene->removeFromScene( this );
}

if (mAudioHandles.size())
{
for (typeAudioHandleVector::iterator itr = mAudioHandles.begin(); itr != mAudioHandles.end(); ++itr)
{
U32 handle = *itr;
alxStop(handle);
}
mAudioHandles.clear();
}

// Decrease scene-object count.
--sGlobalSceneObjectCount;
}
Expand Down Expand Up @@ -563,6 +575,9 @@ void SceneObject::preIntegrate( const F32 totalTime, const F32 elapsedTime, Debu
updateSize(elapsedTime);
}

if (mAudioHandles.size())
refreshsources();

// Finish if nothing is dirty.
if ( !mSpatialDirty )
return;
Expand Down Expand Up @@ -640,6 +655,17 @@ void SceneObject::integrateObject( const F32 totalTime, const F32 elapsedTime, D
{
updateBlendColor( elapsedTime );
}

if (mAudioHandles.size())
{
for (typeAudioHandleVector::iterator itr = mAudioHandles.begin(); itr != mAudioHandles.end(); ++itr)
{
U32 handle = *itr;
Point2F vel = getLinearVelocity();
alxSource3f(handle, AL_POSITION, position.x, position.y, 0.f);
alxSource3f(handle, AL_VELOCITY, vel.x, vel.y, 0.f);
}
}
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -861,6 +887,24 @@ void SceneObject::sceneRenderOverlay( const SceneRenderState* sceneRenderState )
{
pScene->mDebugDraw.DrawSortPoint( getRenderPosition(), getSize(), mSortPoint );
}

if (debugMask & Scene::SCENE_DEBUG_AUDIO_SOURCES)
{
if (mAudioHandles.size())
{
for (typeAudioHandleVector::iterator itr = mAudioHandles.begin(); itr != mAudioHandles.end(); ++itr)
{
U32 handle = *itr;
ALfloat MaxDistance = 0.f;
ALfloat RefDistance = 0.f;
alxGetSourcef(handle, AL_MAX_DISTANCE, &MaxDistance);
alxGetSourcef(handle, AL_REFERENCE_DISTANCE, &RefDistance);
pScene->mDebugDraw.DrawCircle(getRenderPosition(), MaxDistance, ColorF(1.f, 0.2f, 0.2f));
pScene->mDebugDraw.DrawCircle(getRenderPosition(), RefDistance, ColorF(1.f, 0.0f, 0.0f));
}
}

}
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -4030,6 +4074,56 @@ bool SceneObject::writeField(StringTableEntry fieldname, const char* value)
return true;
}

void SceneObject::addAudioHandle(AUDIOHANDLE handle)
{
mAudioHandles.push_back_unique(handle);
Con::printf("New Vector size : %i", mAudioHandles.size());
}

S32 SceneObject::getSoundsCount(void)
{
return mAudioHandles.size();
}

U32 SceneObject::getSound(S32 index)
{
if (mAudioHandles.size() - 1 < index)
return NULL_AUDIOHANDLE;
U32 handle = mAudioHandles[index];
return handle;
}

void SceneObject::refreshsources()
{
if (mAudioHandles.size())
{
S32 index = 0;
for (typeAudioHandleVector::iterator itr = mAudioHandles.begin(); itr != mAudioHandles.end(); ++itr)
{
U32 handle = *itr;

if (handle)
{
if (!alxIsValidHandle(handle))
mHandleDeletionList.push_back(index);

index++;
}
}

if (mHandleDeletionList.size())
{

for (Vector<S32>::iterator delitr = mHandleDeletionList.begin(); delitr != mHandleDeletionList.end(); ++delitr)
{
mAudioHandles.erase(*delitr);
}
mHandleDeletionList.clear();
}
}
}


//------------------------------------------------------------------------------

S32 QSORT_CALLBACK SceneObject::sceneObjectLayerDepthSort(const void* a, const void* b)
Expand Down
9 changes: 9 additions & 0 deletions engine/source/2d/sceneobject/SceneObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct tDestroyNotification
typedef VectorPtr<b2FixtureDef*> typeCollisionFixtureDefVector;
typedef VectorPtr<b2Fixture*> typeCollisionFixtureVector;
typedef Vector<tDestroyNotification> typeDestroyNotificationVector;
typedef Vector<U32> typeAudioHandleVector;

//-----------------------------------------------------------------------------

Expand Down Expand Up @@ -189,6 +190,8 @@ class SceneObject :
/// General collision shape access.
typeCollisionFixtureDefVector mCollisionFixtureDefs;
typeCollisionFixtureVector mCollisionFixtures;
typeAudioHandleVector mAudioHandles;
Vector<S32> mHandleDeletionList; //Used for Audio source maintenance & deletion

/// Render visibility.
bool mVisible;
Expand Down Expand Up @@ -607,6 +610,12 @@ class SceneObject :
static U32 getGlobalSceneObjectCount( void );
inline U32 getSerialId( void ) const { return mSerialId; }

// Audio
void addAudioHandle(AUDIOHANDLE handle);
U32 getSound(S32 index);
S32 getSoundsCount();
void refreshsources();

// Read / Write fields.
virtual bool writeField(StringTableEntry fieldname, const char* value);

Expand Down
Loading

0 comments on commit b4f6c75

Please sign in to comment.