v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
Public Member Functions | Static Public Member Functions | List of all members
Sifteo::TimeDelta Class Reference

Represents a difference between two SystemTimes, with moderate resolution. More...

#include <sifteo/time.h>

Public Member Functions

unsigned frames (TimeDelta duration) const
 Return the number of frames, of a particular duration, that are represented by this time delta, rounding down. More...
 
bool isNegative () const
 Is this time value negative? More...
 
bool isPositive () const
 Is this time value positive?
 
int32_t milliseconds () const
 Return the delta in milliseconds.
 
int64_t nanoseconds () const
 Return the delta in nanoseconds.
 
 operator double () const
 Explicit converstion to double.
 
 operator float () const
 Explicit conversion to float.
 
unsigned pullFrames (TimeDelta duration)
 Return a frame count, and subtract (pull) the time corresponding with those frames from this TimeDelta. More...
 
float seconds () const
 Return the delta in seconds, as a floating point value.
 
 TimeDelta (float sec)
 Construct a new TimeDelta from a floating point time, in seconds. More...
 

Static Public Member Functions

static TimeDelta fromMillisec (int32_t m)
 Construct a new TimeDelta from an integer time, in milliseconds.
 
static TimeDelta hz (float h)
 Construct a TimeDelta representing the period which corresponds with a given frequency. More...
 

Detailed Description

Represents a difference between two SystemTimes, with moderate resolution.

Deltas are internally stored as 32-bit millisecond counts, but they can be converted to many other units.

TimeDeltas can be directly compared to floating point values efficiently. In cases where the comparison is made against a constant, we can convert the entire operation to integer math at compile-time.

Constructor & Destructor Documentation

Sifteo::TimeDelta::TimeDelta ( float  sec)
inline

Construct a new TimeDelta from a floating point time, in seconds.

These constructors also provide an implicit conversion that makes tests like "delta < 1.5" work.

Member Function Documentation

unsigned Sifteo::TimeDelta::frames ( TimeDelta  duration) const
inline

Return the number of frames, of a particular duration, that are represented by this time delta, rounding down.

This is integer division with truncation.

You can use this to implement animations which start at a particular reference point. At each frame, look at the current time, subtract the reference SystemTime to yield a TimeDelta, then use frames() to convert that value to an integer frame count.

static TimeDelta Sifteo::TimeDelta::hz ( float  h)
inlinestatic

Construct a TimeDelta representing the period which corresponds with a given frequency.

This can be used, for example, with frames() in order to specify the frame rate in HZ (FPS) rather than in seconds.

bool Sifteo::TimeDelta::isNegative ( ) const
inline

Is this time value negative?

Time deltas are signed, but in many cases it makes sense to reject negative deltas. This predicate can be used quickly in ASSERTs.

unsigned Sifteo::TimeDelta::pullFrames ( TimeDelta  duration)
inline

Return a frame count, and subtract (pull) the time corresponding with those frames from this TimeDelta.

This leaves the remainder, i.e. the time since the beginning of the current frame, in this Time Delta.

This can be used as an alternative to frames() in cases where time is being accumulated frame-by-frame instead of being calculated based on a fixed reference point. This is also very handy for animating at a fixed rate or running a fixed timestep physics simulation, while saving the remainders for later.


The documentation for this class was generated from the following file: