linux fixes, bugfixes, comments
This commit is contained in:
@@ -19,10 +19,10 @@
|
||||
#ifndef B3_DRAW_H
|
||||
#define B3_DRAW_H
|
||||
|
||||
#include <bounce\common\math\math.h>
|
||||
#include <bounce\collision\shapes\aabb3.h>
|
||||
#include <bounce/common/math/math.h>
|
||||
#include <bounce/collision/shapes/aabb3.h>
|
||||
|
||||
// Color channels used by the debug b3Draw interface.
|
||||
// Color channels used by the debug draw interface.
|
||||
struct b3Color
|
||||
{
|
||||
b3Color() { }
|
||||
@@ -32,11 +32,11 @@ struct b3Color
|
||||
float32 r, g, b, a;
|
||||
};
|
||||
|
||||
// Implement this interface and set to a world so it can b3Draw the physics entities.
|
||||
// Implement this interface and set to a world so it can draw the physics entities.
|
||||
class b3Draw
|
||||
{
|
||||
public :
|
||||
// Bit flags to tell the world what needs to be b3Draw.
|
||||
// Bit flags to tell the world what needs to be draw.
|
||||
enum b3Flags
|
||||
{
|
||||
e_shapesFlag = 0x0001,
|
||||
@@ -88,10 +88,10 @@ public :
|
||||
// Draw a AABB.
|
||||
virtual void DrawAABB(const b3AABB3& aabb, const b3Color& color) = 0;
|
||||
|
||||
// Draw a b3Transform.
|
||||
// Draw a transform.
|
||||
virtual void DrawTransform(const b3Transform& xf) = 0;
|
||||
|
||||
// Debug b3Draw flags.
|
||||
// Debug draw flags.
|
||||
u32 m_flags;
|
||||
};
|
||||
|
||||
|
@@ -19,8 +19,8 @@
|
||||
#ifndef B3_GEOMETRY_H
|
||||
#define B3_GEOMETRY_H
|
||||
|
||||
#include <bounce\common\math\math.h>
|
||||
#include <bounce\common\math\transform.h>
|
||||
#include <bounce/common/math/math.h>
|
||||
#include <bounce/common/math/transform.h>
|
||||
|
||||
// A triangle in indexed form.
|
||||
struct b3Triangle
|
||||
@@ -106,13 +106,13 @@ inline b3Plane b3Mul(const b3Transform& T, const b3Plane& plane)
|
||||
return b3Plane(normal, plane.offset + b3Dot(normal, T.position));
|
||||
}
|
||||
|
||||
// Compute the distance between a point and a plane.
|
||||
inline float32 b3Distance(const b3Vec3& P, const b3Plane& plane)
|
||||
{
|
||||
return b3Dot(plane.normal, P) - plane.offset;
|
||||
}
|
||||
|
||||
// Project a point onto a plane.
|
||||
// The plane must be normalized.
|
||||
// Project a point onto a normal plane.
|
||||
inline b3Vec3 b3Project(const b3Vec3& P, const b3Plane& plane)
|
||||
{
|
||||
float32 fraction = b3Distance(P, plane);
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_MAT_H
|
||||
#define B3_MAT_H
|
||||
|
||||
#include <bounce\common\math\math.h>
|
||||
#include <bounce/common/math/math.h>
|
||||
|
||||
// A vector stored in column-major order.
|
||||
template<u32 n>
|
||||
@@ -79,7 +79,10 @@ struct b3Mat
|
||||
};
|
||||
|
||||
// Solve Ax = b.
|
||||
// Warning: Make sure to pass a copy of A to the function. It will be invalidated.
|
||||
// It doesn't compute the inverse.
|
||||
// Therefore, is more efficient.
|
||||
// Returns false if the matrix is singular.
|
||||
// Warning: Make sure to pass a copy of the original matrix to the function. A will be invalidated.
|
||||
bool b3Solve(float32* b, float32* A, u32 n);
|
||||
|
||||
#endif
|
||||
#endif
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_MAT_22_H
|
||||
#define B3_MAT_22_H
|
||||
|
||||
#include <bounce\common\math\vec2.h>
|
||||
#include <bounce/common/math/vec2.h>
|
||||
|
||||
// A 2-by-2 matrix stored in column-major order.
|
||||
struct b3Mat22
|
||||
@@ -27,7 +27,7 @@ struct b3Mat22
|
||||
// Does nothing for performance.
|
||||
b3Mat22() { }
|
||||
|
||||
// Set this matrix from two vector elements.
|
||||
// Set this matrix from two vectors.
|
||||
b3Mat22(const b3Vec2& _x, const b3Vec2& _y) : x(_x), y(_y) { }
|
||||
|
||||
// Solve Ax = b.
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_MAT_33_H
|
||||
#define B3_MAT_33_H
|
||||
|
||||
#include <bounce\common\math\vec3.h>
|
||||
#include <bounce/common/math/vec3.h>
|
||||
|
||||
// A 3-by-3 matrix stored in column-major order.
|
||||
struct b3Mat33
|
||||
@@ -27,7 +27,7 @@ struct b3Mat33
|
||||
// Does nothing for performance.
|
||||
b3Mat33() { }
|
||||
|
||||
// Set this matrix from three elements.
|
||||
// Set this matrix from three column vectors.
|
||||
b3Mat33(const b3Vec3& _x, const b3Vec3& _y, const b3Vec3& _z) : x(_x), y(_y), z(_z) { }
|
||||
|
||||
// Read an indexed column vector from this matrix.
|
||||
@@ -42,6 +42,12 @@ struct b3Mat33
|
||||
return (&x)[i];
|
||||
}
|
||||
|
||||
// Read an indexed element from this matrix.
|
||||
float32 operator()(u32 i, u32 j) const
|
||||
{
|
||||
return (&x.x)[i + 3 * j];
|
||||
}
|
||||
|
||||
// Add a matrix to this matrix.
|
||||
void operator+=(const b3Mat33& B)
|
||||
{
|
||||
@@ -72,11 +78,6 @@ struct b3Mat33
|
||||
// Returns the zero vector if the matrix is singular.
|
||||
b3Vec3 Solve(const b3Vec3& b) const;
|
||||
|
||||
float32 operator()(u32 i, u32 j) const
|
||||
{
|
||||
return (&x.x)[i + 3 * j];
|
||||
}
|
||||
|
||||
b3Vec3 x, y, z;
|
||||
};
|
||||
|
||||
@@ -134,7 +135,7 @@ inline b3Mat33 b3Mul(const b3Mat33& A, const b3Mat33& B)
|
||||
|
||||
// Multiply the transpose of a matrix times a vector. If
|
||||
// the matrix represents a rotation frame this transforms the
|
||||
// vector from one frame to another (inverse b3Transform).
|
||||
// vector from one frame to another (inverse transform).
|
||||
inline b3Vec3 b3MulT(const b3Mat33& A, const b3Vec3& v)
|
||||
{
|
||||
return b3Vec3(b3Dot(A.x, v), b3Dot(A.y, v), b3Dot(A.z, v));
|
||||
|
@@ -20,7 +20,7 @@
|
||||
#define B3_MATH_H
|
||||
|
||||
#include <cmath>
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
inline bool b3IsValid(float32 fx)
|
||||
{
|
||||
@@ -30,13 +30,13 @@ inline bool b3IsValid(float32 fx)
|
||||
|
||||
inline float32 b3Sqrt(float32 x)
|
||||
{
|
||||
return sqrtf(x);
|
||||
return std::sqrt(x);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline T b3Abs(T x)
|
||||
{
|
||||
return abs(x);
|
||||
return std::abs(x);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
|
@@ -19,10 +19,10 @@
|
||||
#ifndef B3_QUAT_H
|
||||
#define B3_QUAT_H
|
||||
|
||||
#include <bounce\common\math\math.h>
|
||||
#include <bounce\common\math\mat33.h>
|
||||
#include <bounce/common/math/math.h>
|
||||
#include <bounce/common/math/mat33.h>
|
||||
|
||||
// A quaternion represents an orientation with 4 real numbers.
|
||||
// A quaternion can represent an orientation with 4 scalars.
|
||||
struct b3Quat
|
||||
{
|
||||
// Default constructor does nothing for performance.
|
||||
@@ -157,13 +157,13 @@ inline b3Quat b3Normalize(const b3Quat& q)
|
||||
return b3Quat(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
// Compute the dot poduct of two quaternions.
|
||||
// Perform the dot poduct of two quaternions.
|
||||
inline float b3Dot(const b3Quat& a, const b3Quat& b)
|
||||
{
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
||||
}
|
||||
|
||||
// Compute the conjugate of a quaternion.
|
||||
// Conjugate of a quaternion.
|
||||
inline b3Quat b3Conjugate(const b3Quat& q)
|
||||
{
|
||||
return b3Quat(-q.x, -q.y, -q.z, q.w);
|
||||
@@ -230,7 +230,6 @@ inline b3Quat b3Slerp(const b3Quat& a, const b3Quat& b, float32 fraction)
|
||||
b3Quat q1 = sin(w1 * angle) * a;
|
||||
b3Quat q2 = sin(w2 * angle) * b2;
|
||||
float32 invSin = 1.0f / sine;
|
||||
|
||||
return invSin * (q1 + q2);
|
||||
}
|
||||
|
||||
|
@@ -19,10 +19,10 @@
|
||||
#ifndef B3_TRANSFORM_H
|
||||
#define B3_TRANSFORM_H
|
||||
|
||||
#include <bounce\common\math\mat33.h>
|
||||
#include <bounce\common\math\quat.h>
|
||||
#include <bounce/common/math/mat33.h>
|
||||
#include <bounce/common/math/quat.h>
|
||||
|
||||
// A b3Transform represents a rigid frame.
|
||||
// A transform represents a rigid frame.
|
||||
// It has a translation representing a position
|
||||
// and a rotation representing an orientation.
|
||||
struct b3Transform
|
||||
@@ -30,7 +30,7 @@ struct b3Transform
|
||||
// Default ctor does nothing for performance.
|
||||
b3Transform() { }
|
||||
|
||||
// Set this b3Transform from a translation vector and an orientation
|
||||
// Set this transform from a translation vector and an orientation
|
||||
// quaternion.
|
||||
b3Transform(const b3Vec3& p, const b3Quat& q)
|
||||
{
|
||||
@@ -38,7 +38,7 @@ struct b3Transform
|
||||
rotation = b3ConvertQuatToRot(q);
|
||||
}
|
||||
|
||||
// Set this b3Transform to the identity.
|
||||
// Set this transform to the identity.
|
||||
void SetIdentity()
|
||||
{
|
||||
position.SetZero();
|
||||
@@ -61,7 +61,7 @@ struct b3Sweep
|
||||
b3Vec3 worldCenter; // world center
|
||||
b3Quat orientation; // world orientation
|
||||
|
||||
// Get this sweep b3Transform at a given time between [0, 1]
|
||||
// Get this sweep transform at a given time between [0, 1]
|
||||
b3Transform GetTransform(float32 t) const;
|
||||
|
||||
// Advance to a new initial state.
|
||||
@@ -90,7 +90,7 @@ inline void b3Sweep::Advance(float32 t)
|
||||
t0 = t;
|
||||
}
|
||||
|
||||
// Multiply a b3Transform times a vector. If the b3Transform
|
||||
// Multiply a transform times a vector. If the transform
|
||||
// represents a frame this returns the vector in terms
|
||||
// of the frame.
|
||||
inline b3Vec3 operator*(const b3Transform& T, const b3Vec3& v)
|
||||
@@ -98,7 +98,7 @@ inline b3Vec3 operator*(const b3Transform& T, const b3Vec3& v)
|
||||
return b3Mul(T.rotation, v) + T.position;
|
||||
}
|
||||
|
||||
// Multiply a b3Transform times another b3Transform (composed b3Transform).
|
||||
// Multiply a transform times another transform (composed transform).
|
||||
// [A y][B x] = [AB Ax+y]
|
||||
// [0 1][0 1] [0 1 ]
|
||||
inline b3Transform operator*(const b3Transform& A, const b3Transform& B)
|
||||
@@ -109,13 +109,13 @@ inline b3Transform operator*(const b3Transform& A, const b3Transform& B)
|
||||
return C;
|
||||
}
|
||||
|
||||
// Multiply a b3Transform times a vector.
|
||||
// Multiply a transform times a vector.
|
||||
inline b3Vec3 b3Mul(const b3Transform& T, const b3Vec3& v)
|
||||
{
|
||||
return b3Mul(T.rotation, v) + T.position;
|
||||
}
|
||||
|
||||
// Multiply a b3Transform times another b3Transform.
|
||||
// Multiply a transform times another transform.
|
||||
// [A y][B x] = [AB Ax+y]
|
||||
// [0 1][0 1] [0 1 ]
|
||||
inline b3Transform b3Mul(const b3Transform& A, const b3Transform& B)
|
||||
@@ -126,8 +126,8 @@ inline b3Transform b3Mul(const b3Transform& A, const b3Transform& B)
|
||||
return C;
|
||||
}
|
||||
|
||||
// Multiply the transpose of one b3Transform (inverse
|
||||
// b3Transform) times another b3Transform (composed b3Transform).
|
||||
// Multiply the transpose of one transform (inverse
|
||||
// transform) times another transform (composed transform).
|
||||
//[A^-1 -A^-1*y][B x] = [A^-1*B A^-1(x-y)]
|
||||
//[0 1 ][0 1] [0 1 ]
|
||||
inline b3Transform b3MulT(const b3Transform& A, const b3Transform& B)
|
||||
@@ -138,9 +138,9 @@ inline b3Transform b3MulT(const b3Transform& A, const b3Transform& B)
|
||||
return C;
|
||||
}
|
||||
|
||||
// Multiply the transpose of a b3Transform times a vector.
|
||||
// If the b3Transform represents a frame then this transforms
|
||||
// the vector from one frame to another (inverse b3Transform).
|
||||
// Multiply the transpose of a transform times a vector.
|
||||
// If the transform represents a frame then this transforms
|
||||
// the vector from one frame to another (inverse transform).
|
||||
//[A^-1 -A^-1*y][x] = A^-1*x - A^-1*y = A^-1 * (x - y)
|
||||
//[0 1 ][1]
|
||||
inline b3Vec3 b3MulT(const b3Transform& A, const b3Vec3& v)
|
||||
@@ -148,7 +148,7 @@ inline b3Vec3 b3MulT(const b3Transform& A, const b3Vec3& v)
|
||||
return b3MulT(A.rotation, v - A.position);
|
||||
}
|
||||
|
||||
// Inverse b3Transform.
|
||||
// Inverse transform.
|
||||
inline b3Transform b3Inverse(const b3Transform& T)
|
||||
{
|
||||
b3Transform B;
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_VEC2_H
|
||||
#define B3_VEC2_H
|
||||
|
||||
#include <bounce\common\math\math.h>
|
||||
#include <bounce/common/math/math.h>
|
||||
|
||||
// A 2D column vector.
|
||||
struct b3Vec2
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_VEC_3_H
|
||||
#define B3_VEC_3_H
|
||||
|
||||
#include <bounce\common\math\math.h>
|
||||
#include <bounce/common/math/math.h>
|
||||
|
||||
// A 3D column vector.
|
||||
struct b3Vec3
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_BLOCK_POOL_H
|
||||
#define B3_BLOCK_POOL_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// Number of blocks per chunk.
|
||||
const u32 b3_blockCount = 32;
|
||||
|
@@ -19,13 +19,13 @@
|
||||
#ifndef B3_STACK_ALLOCATOR_H
|
||||
#define B3_STACK_ALLOCATOR_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// Allocate 10 MiB from the stack.
|
||||
// Increase as you want.
|
||||
const u32 b3_maxStackSize = B3_MiB(10);
|
||||
|
||||
// An stack allocator.
|
||||
// A stack allocator.
|
||||
class b3StackAllocator
|
||||
{
|
||||
public :
|
||||
|
@@ -60,7 +60,7 @@ typedef float float32;
|
||||
#define B3_AABB_MULTIPLIER (2.0f)
|
||||
|
||||
// Collision and constraint tolerance.
|
||||
#define B3_LINEAR_SLOP (0.005f)
|
||||
#define B3_LINEAR_SLOP (0.01f)
|
||||
|
||||
// Collision and constraint tolerance.
|
||||
#define B3_ANGULAR_SLOP (2.0f / 180.0f * B3_PI)
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_ARRAY_POD_H
|
||||
#define B3_ARRAY_POD_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// An array for bytes (POD).
|
||||
template <typename T>
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_LIST_H
|
||||
#define B3_LIST_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// A singly-linked list.
|
||||
template<class T>
|
||||
@@ -28,7 +28,7 @@ class b3List1
|
||||
public:
|
||||
b3List1()
|
||||
{
|
||||
m_head = nullptr;
|
||||
m_head = NULL;
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ class b3List2
|
||||
public:
|
||||
b3List2()
|
||||
{
|
||||
m_head = nullptr;
|
||||
m_head = NULL;
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public:
|
||||
|
||||
void PushFront(T* link)
|
||||
{
|
||||
link->m_prev = nullptr;
|
||||
link->m_prev = NULL;
|
||||
link->m_next = m_head;
|
||||
if (m_head)
|
||||
{
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_OBJECT_ARRAY_H
|
||||
#define B3_OBJECT_ARRAY_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// An array for objects.
|
||||
template <typename T>
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#ifndef B3_STACK_H
|
||||
#define B3_STACK_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// A growable stack for plain-old-data (POD).
|
||||
template <typename T, u32 N>
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
{
|
||||
b3Free(m_elements);
|
||||
}
|
||||
m_elements = nullptr;
|
||||
m_elements = NULL;
|
||||
}
|
||||
|
||||
const T& Top() const
|
||||
|
@@ -19,45 +19,195 @@
|
||||
#ifndef B3_TIME_H
|
||||
#define B3_TIME_H
|
||||
|
||||
#include <bounce\common\settings.h>
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
#define B3_WINDOWS 1
|
||||
#define B3_MAC 2
|
||||
#define B3_UNIX 3
|
||||
|
||||
#if defined (_WIN32)
|
||||
#define B3_PLATFORM B3_WINDOWS
|
||||
#elif defined( __APPLE__ )
|
||||
#define B3_PLATFORM B3_MAC
|
||||
#else
|
||||
#define B3_PLATFORM B3_UNIX
|
||||
#endif
|
||||
|
||||
#if B3_PLATFORM == B3_WINDOWS
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
// A timer class that accumulates time.
|
||||
// Usefull for measuring elapsed times of
|
||||
// sections of code.
|
||||
// Usefull for measuring elapsed times between code sections.
|
||||
class b3Time
|
||||
{
|
||||
public :
|
||||
b3Time();
|
||||
public:
|
||||
b3Time()
|
||||
{
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceCounter(&c);
|
||||
m_c0 = c.QuadPart;
|
||||
m_t0 = 0.0;
|
||||
m_t = 0.0;
|
||||
}
|
||||
|
||||
// Get the accumulated time in miliseconds
|
||||
// from this timer.
|
||||
float64 GetCurMilis() const;
|
||||
// Get the accumulated time in miliseconds from this timer.
|
||||
float64 GetCurrentMilis() const
|
||||
{
|
||||
return m_t;
|
||||
}
|
||||
|
||||
// Get the elapsed time since this timer was updated.
|
||||
float64 GetElapsedMilis() const;
|
||||
float64 GetElapsedMilis() const
|
||||
{
|
||||
return m_t - m_t0;
|
||||
}
|
||||
|
||||
// Add the elapsed time since this function
|
||||
// was called to this timer.
|
||||
void Update();
|
||||
// Add the elapsed time since this function was called to this timer.
|
||||
void Update()
|
||||
{
|
||||
static float64 inv_frequency = 0.0;
|
||||
if (inv_frequency == 0.0)
|
||||
{
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceFrequency(&c);
|
||||
|
||||
float64 cycles_per_s = float64(c.QuadPart);
|
||||
float64 s_per_cycle = 1.0 / cycles_per_s;
|
||||
float64 ms_per_cycle = 1000.0 * s_per_cycle;
|
||||
inv_frequency = ms_per_cycle;
|
||||
}
|
||||
|
||||
LARGE_INTEGER c;
|
||||
QueryPerformanceCounter(&c);
|
||||
|
||||
float64 dt = inv_frequency * float64(c.QuadPart - m_c0);
|
||||
m_c0 = c.QuadPart;
|
||||
Add(dt);
|
||||
}
|
||||
|
||||
// Add time to this timer.
|
||||
void Add(float64 dt)
|
||||
{
|
||||
m_t0 = m_t;
|
||||
m_t += dt;
|
||||
}
|
||||
|
||||
// Add a given ammout of time to this timer.
|
||||
void UpdateBy(float64 dt);
|
||||
private:
|
||||
static float64 m_invFrequency;
|
||||
|
||||
u64 m_lastRealTime;
|
||||
float64 m_lastTime;
|
||||
float64 m_curTime;
|
||||
u64 m_c0;
|
||||
float64 m_t0;
|
||||
float64 m_t;
|
||||
};
|
||||
|
||||
inline float64 b3Time::GetCurMilis() const
|
||||
{
|
||||
return m_curTime;
|
||||
}
|
||||
#elif B3_PLATFORM == B3_MAC
|
||||
|
||||
inline float64 b3Time::GetElapsedMilis() const
|
||||
{
|
||||
return m_curTime - m_lastTime;
|
||||
}
|
||||
#include <mach/mach_time.h>
|
||||
|
||||
// A timer class that accumulates time.
|
||||
// Usefull for measuring elapsed times between code sections.
|
||||
class b3Time
|
||||
{
|
||||
public:
|
||||
b3Time()
|
||||
{
|
||||
m_c0 = mach_absolute_time();
|
||||
m_t0 = 0.0;
|
||||
m_t = 0.0;
|
||||
}
|
||||
|
||||
// Get the accumulated time in miliseconds from this timer.
|
||||
double GetCurrentMilis() const
|
||||
{
|
||||
return m_t;
|
||||
}
|
||||
|
||||
// Get the elapsed time since this timer was updated.
|
||||
double GetElapsedMilis() const
|
||||
{
|
||||
return m_t - m_t0;
|
||||
}
|
||||
|
||||
// Add the elapsed time since this function was called to this timer.
|
||||
void Update()
|
||||
{
|
||||
static double inv_frequency = 0.0;
|
||||
if (inv_frequency == 0.0)
|
||||
{
|
||||
mach_timebase_info_data_t info;
|
||||
mach_timebase_info(&info);
|
||||
inv_frequency = double(info.numer) / (double(info.denom) * 1.0e6);
|
||||
}
|
||||
|
||||
uint64_t c = mach_absolute_time();
|
||||
double dt = inv_frequency * (double)(c - m_c0);
|
||||
m_c0 = c;
|
||||
Add(dt);
|
||||
}
|
||||
|
||||
// Add time to this timer.
|
||||
void Add(double dt)
|
||||
{
|
||||
m_t0 = m_t;
|
||||
m_t += dt;
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t m_c0;
|
||||
double m_t0;
|
||||
double m_t;
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
#include <time.h>
|
||||
|
||||
// A timer class that accumulates time.
|
||||
// Usefull for measuring elapsed times between code sections.
|
||||
class b3Time
|
||||
{
|
||||
public:
|
||||
b3Time()
|
||||
{
|
||||
clock_gettime(CLOCK_MONOTONIC, &m_c0);
|
||||
m_t0 = 0.0;
|
||||
m_t = 0.0;
|
||||
}
|
||||
|
||||
// Get the accumulated time in miliseconds from this timer.
|
||||
double GetCurrentMilis() const
|
||||
{
|
||||
return m_t;
|
||||
}
|
||||
|
||||
// Get the elapsed time since this timer was updated.
|
||||
double GetElapsedMilis() const
|
||||
{
|
||||
return m_t - m_t0;
|
||||
}
|
||||
|
||||
// Add the elapsed time since this function was called to this timer.
|
||||
void Update()
|
||||
{
|
||||
struct timespec c;
|
||||
clock_gettime(CLOCK_MONOTONIC, &c);
|
||||
double dt = (double)(c.tv_nsec - m_c0.tv_nsec) * 1.0e-6;
|
||||
m_c0 = c;
|
||||
Add(dt);
|
||||
}
|
||||
|
||||
// Add time to this timer.
|
||||
void Add(double dt)
|
||||
{
|
||||
m_t0 = m_t;
|
||||
m_t += dt;
|
||||
}
|
||||
|
||||
private:
|
||||
struct timespec m_c0;
|
||||
double m_t0;
|
||||
double m_t;
|
||||
};
|
||||
|
||||
#endif // B3_PLATFORM
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user