瀏覽代碼

Merge pull request #2220 from irei1as/development

Changes for SFXSound::setPosition(time)
Areloch 6 年之前
父節點
當前提交
1ec048f41d
共有 3 個文件被更改,包括 29 次插入2 次删除
  1. 13 0
      Engine/source/platform/async/asyncPacketStream.h
  2. 13 2
      Engine/source/sfx/sfxSound.cpp
  3. 3 0
      Engine/source/sfx/sfxSound.h

+ 13 - 0
Engine/source/platform/async/asyncPacketStream.h

@@ -281,9 +281,22 @@ void AsyncPacketBufferedInputStream< Stream, Packet >::_requestNext()
       IResettable* resettable = dynamic_cast< IResettable* >( s );
       IResettable* resettable = dynamic_cast< IResettable* >( s );
       if( resettable )
       if( resettable )
       {
       {
+         IPositionable< U32 >* positionable = dynamic_cast< IPositionable< U32 >* >( &Deref( stream ) );
+         U32 pos;
+         if(positionable)
+            pos = positionable->getPosition();
+         
          resettable->reset();
          resettable->reset();
          isEOS = false;
          isEOS = false;
          this->mNumRemainingSourceElements = mNumTotalSourceElements;
          this->mNumRemainingSourceElements = mNumTotalSourceElements;
+         
+         if( positionable )
+         {
+            positionable->setPosition(pos);
+            U32 dur = stream->getDuration();
+            if(dur != 0) //avoiding division by zero? not needed, probably
+               this->mNumRemainingSourceElements -= (U32)(mNumTotalSourceElements*(F32)pos/dur);
+         }
       }
       }
    }
    }
    else if( isEOS )
    else if( isEOS )

+ 13 - 2
Engine/source/sfx/sfxSound.cpp

@@ -81,6 +81,7 @@ SFXSound::SFXSound( SFXProfile *profile, SFXDescription* desc )
    :  Parent( profile, desc ),
    :  Parent( profile, desc ),
       mVoice( NULL )
       mVoice( NULL )
 {
 {
+   mSetPositionValue = 0;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -411,6 +412,9 @@ void SFXSound::_play()
          Platform::outputDebugString( "[SFXSound] virtualizing playback of source '%i'", getId() );
          Platform::outputDebugString( "[SFXSound] virtualizing playback of source '%i'", getId() );
       #endif
       #endif
    }
    }
+   if(getPosition() != mSetPositionValue)
+      setPosition(mSetPositionValue);
+   mSetPositionValue = 0; //Non looping sounds need this to reset.
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -421,6 +425,7 @@ void SFXSound::_stop()
    
    
    if( mVoice )
    if( mVoice )
       mVoice->stop();
       mVoice->stop();
+   mSetPositionValue = 0;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -431,6 +436,7 @@ void SFXSound::_pause()
    
    
    if( mVoice )
    if( mVoice )
       mVoice->pause();
       mVoice->pause();
+   mSetPositionValue = getPosition();
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -511,6 +517,8 @@ void SFXSound::_updatePriority()
 
 
 U32 SFXSound::getPosition() const
 U32 SFXSound::getPosition() const
 {
 {
+   if( getLastStatus() == SFXStatusStopped)
+      return mSetPositionValue;
    if( mVoice )
    if( mVoice )
       return mVoice->getFormat().getDuration( mVoice->getPosition() );
       return mVoice->getFormat().getDuration( mVoice->getPosition() );
    else
    else
@@ -522,6 +530,8 @@ U32 SFXSound::getPosition() const
 void SFXSound::setPosition( U32 ms )
 void SFXSound::setPosition( U32 ms )
 {
 {
    AssertFatal( ms < getDuration(), "SFXSound::setPosition() - position out of range" );
    AssertFatal( ms < getDuration(), "SFXSound::setPosition() - position out of range" );
+   mSetPositionValue = ms;
+
    if( mVoice )
    if( mVoice )
       mVoice->setPosition( mVoice->getFormat().getSampleCount( ms ) );
       mVoice->setPosition( mVoice->getFormat().getSampleCount( ms ) );
    else
    else
@@ -693,8 +703,9 @@ DefineEngineMethod( SFXSound, setPosition, void, ( F32 position ),,
    "playback will resume at the given position when play() is called.\n\n"
    "playback will resume at the given position when play() is called.\n\n"
    "@param position The new position of the play cursor (in seconds).\n" )
    "@param position The new position of the play cursor (in seconds).\n" )
 {
 {
-   if( position >= 0 && position <= object->getDuration() )
-      object->setPosition( position * 1000.0f );
+   position *= 1000.0f;
+   if( position >= 0 && position < object->getDuration() )
+      object->setPosition( position );
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------

+ 3 - 0
Engine/source/sfx/sfxSound.h

@@ -81,6 +81,9 @@ class SFXSound : public SFXSource,
       /// _initBuffer() used for managing virtual sources.
       /// _initBuffer() used for managing virtual sources.
       U32 mDuration;
       U32 mDuration;
 
 
+      ///Used for setPosition (time in miliseconds)
+      U32 mSetPositionValue;
+
       /// Create a new voice for this source.
       /// Create a new voice for this source.
       bool _allocVoice( SFXDevice* device );
       bool _allocVoice( SFXDevice* device );