Files
itgmania212121/src/RageTimer.h
T
sukibabyandteejusb 7b45a5f599 RageTimer bug fix
Fast data types are good for audio processing, but not so much for timekeeping. This PR should resolve a number of smaller issues.
2024-12-04 09:41:26 -08:00

85 lines
3.4 KiB
C++

/* RageTimer - Timer services. */
#ifndef RAGE_TIMER_H
#define RAGE_TIMER_H
#include <cstdint>
class RageTimer
{
public:
/* Initialize the m_secs and m_us values to 0 and then fill them with the current time. */
RageTimer(): m_secs(0), m_us(0) { Touch(); }
RageTimer( int64_t secs, int64_t us ): m_secs(secs), m_us(us) { }
/* Time ago this RageTimer represents. */
float Ago() const;
void Touch();
inline bool IsZero() const { return m_secs == 0 && m_us == 0; }
inline void SetZero() { m_secs = m_us = 0; }
/* Time between last call to GetDeltaTime() (Ago() + Touch()): */
float GetDeltaTime();
/* (alias) */
float PeekDeltaTime() const { return Ago(); }
static double GetTimeSinceStart(); // seconds since the program was started
static double GetTimeSinceStartFast() { return GetTimeSinceStart(); }
static int GetTimeSinceStartSeconds(); // This is used where GetTimeSinceStart would be cast to an int without rounding.
static uint64_t GetTimeSinceStartMicroseconds();
/* Get a timer representing half of the time ago as this one. */
RageTimer Half() const;
/* Add (or subtract) a duration from a timestamp. The result is another timestamp. */
RageTimer operator+( float tm ) const;
RageTimer operator-( float tm ) const { return *this + -tm; }
void operator+=( float tm ) { *this = *this + tm; }
void operator-=( float tm ) { *this = *this + -tm; }
/* Find the amount of time between two timestamps. The result is a duration. */
float operator-( const RageTimer &rhs ) const;
bool operator<( const RageTimer &rhs ) const;
/* The following is a "time since start" RageTimer. Splitting the seconds and
* microseconds values into two integers and combining them later allows for
* better precision. Use caution when changing data types, since resolution
* mismatch errors are easy to cause when changing things in RageTimer. */
uint64_t m_secs, m_us;
private:
static RageTimer Sum( const RageTimer &lhs, float tm );
static double Difference( const RageTimer &lhs, const RageTimer &rhs );
};
extern const RageTimer RageZeroTimer;
#endif // RAGE_TIMER_H
/*
* Copyright (c) 2001-2003 Chris Danford, Glenn Maynard
* All rights reserved.
*
* 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, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* 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 OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/