genesis-3d_engine/Engine/foundation/framesync/framesynctimer.h
zhongdaohuan 6e8fbca745 genesis-3d engine version 1.3.
match the genesis editor version 1.3.0.653.
2014-05-05 14:50:33 +08:00

191 lines
5.5 KiB
Objective-C

/****************************************************************************
Copyright (c) 2009, Radon Labs GmbH
Copyright (c) 2011-2013,WebJet Business Division,CYOU
http://www.genesis-3d.com.cn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#pragma once
//------------------------------------------------------------------------------
/**
@class FrameSync::FrameSyncTimer
A thread-local time source object which is synchronized with the
sync point in the FrameSyncHandlerThread. Time values are thread-locally
cached and thus no thread-synchronization is necessary when reading
time values. FrameSyncTimer objects are updated inside the
FrameSyncHandler::ArriveAtSyncPoint() method.
Threads interested in the master time create a FrameSyncTimer singleton
and register it with FrameSyncHandlerThread through the GraphicsInterface
singleton.
Please note that when calling the Start()/Stop()/Reset() methods,
that the ENTIRE master time will be affected (these methods go
straight through to the FrameSyncHandler object).
*/
#include "core/refcounted.h"
#include "core/singleton.h"
#include "timing/time.h"
//------------------------------------------------------------------------------
namespace FrameSync
{
class FrameSyncTimer : public Core::RefCounted
{
__DeclareClass(FrameSyncTimer);
__DeclareThreadSingleton(FrameSyncTimer);
public:
/// constructor
FrameSyncTimer();
/// destructor
virtual ~FrameSyncTimer();
/// setup the object
void Setup();
/// discard the object
void Discard();
/// return true if object is valid
bool IsValid() const;
/// update the time through polling, only necessary for threads other then render/game thread!
void UpdateTimePolling();
/// start the master time (DON'T CALL FREQUENTLY!)
void StartTime();
/// stop the master time (DON'T CALL FREQUENTLY!)
void StopTime();
/// reset the master time (DON'T CALL FREQUENTLY!)
void ResetTime();
/// return true if master time is running (DON'T CALL FREQUENTLY!)
bool IsTimeRunning() const;
/// get current time
Timing::Time GetTime() const;
/// get current time in ticks
Timing::Tick GetTicks() const;
/// get current frame time
Timing::Time GetFrameTime() const;
/// get current frame time ins ticks
Timing::Tick GetFrameTicks() const;
/// get current frame count
IndexT GetFrameCount() const;
/// set time factor
void SetTimeFactor(Timing::Time factor);
/// get time factor
Timing::Time GetTimeFactor() const;
/// get scaled time
Timing::Time GetScaledTime() const;
/// get scaled time
Timing::Time GetScaledFrameTime() const;
private:
friend class FrameSyncHandlerThread;
/// update the time (always called from local thread)
void Update(Timing::Time masterTime);
Timing::Time time;
Timing::Tick ticks;
Timing::Time frameTime;
Timing::Tick frameTicks;
Timing::Time scaledTime;
Timing::Time timeFactor;
bool isValid;
};
//------------------------------------------------------------------------------
/**
*/
inline bool
FrameSyncTimer::IsValid() const
{
return this->isValid;
}
//------------------------------------------------------------------------------
/**
*/
inline Timing::Time
FrameSyncTimer::GetTime() const
{
return this->time;
}
//------------------------------------------------------------------------------
/**
*/
inline Timing::Tick
FrameSyncTimer::GetTicks() const
{
return this->ticks;
}
//------------------------------------------------------------------------------
/**
*/
inline Timing::Time
FrameSyncTimer::GetFrameTime() const
{
return this->frameTime;
}
//------------------------------------------------------------------------------
/**
*/
inline Timing::Tick
FrameSyncTimer::GetFrameTicks() const
{
return this->frameTicks;
}
//------------------------------------------------------------------------------
/**
*/
inline void
FrameSyncTimer::SetTimeFactor(Timing::Time factor)
{
this->timeFactor = factor;
}
//------------------------------------------------------------------------------
/**
*/
inline Timing::Time
FrameSyncTimer::GetTimeFactor() const
{
return this->timeFactor;
}
//------------------------------------------------------------------------------
/**
*/
inline Timing::Time
FrameSyncTimer::GetScaledTime()const
{
return this->scaledTime;
}
} // namespace FrameSync
//------------------------------------------------------------------------------