5 * Created by damian on 21/03/07.
6 * Copyright 2007 __MyCompanyName__. All rights reserved.
15 implements a high resolution timer
17 can store either a high-precision relative time or a high-precision absolute time
26 #include <mach/mach_time.h>
41 time
.tv_sec
= 0; time
.tv_nsec
= 0;
43 last_update_time
= 0.1f
; };
45 FTime( const FTime
& other
) { Copy(other
); }
47 // set us to the current time
51 time
= mach_absolute_time();
53 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
54 clock_gettime(CLOCK_REALTIME
, &time
); // Works on Linux
58 // set us to the given time in seconds
59 void SetSeconds( double seconds
);
61 // update our time to the current time, returning
62 // delta time in seconds as a float
65 // get the last delta time returned by Update()
66 double GetLastUpdateTime() const { return last_update_time
; }
68 // return milliseconds
69 double ToMillis() const;
71 double ToSeconds() const { return ToMillis() / 1000.0; }
74 // calculate difference
75 FTime
operator-( const FTime
& other
) const
83 FTime
& operator -= (const FTime
& other
)
88 //assert( false && "broken code, please fix ");
89 if ( time
.tv_nsec
< other
.time
.tv_nsec
)
93 //printf("underflow: %10li:%10li - %10li:%10li = ", time.tv_sec, time.tv_nsec, other.time.tv_sec, other.time.tv_nsec );
94 time
.tv_sec
-= other
.time
.tv_sec
+ 1;
95 time
.tv_nsec
= 1e9
- (other
.time
.tv_nsec
-time
.tv_nsec
);
96 //printf(" %10li:%10li\n", time.tv_sec, time.tv_nsec );
100 time
.tv_sec
-= other
.time
.tv_sec
;
101 time
.tv_nsec
-= other
.time
.tv_nsec
;
108 FTime
& operator= (const FTime
& other
) { Copy(other
); return *this; }
111 bool operator== (const FTime
& other
) const {
113 assert( false && "implement me" );
115 return time
.tv_sec
== other
.time
.tv_sec
&& time
.tv_nsec
== other
.time
.tv_nsec
;
120 bool operator< (const FTime
& other
) const {
122 assert(false&&"implement me" );
125 /*bool res =*/return (time
.tv_sec
< other
.time
.tv_sec
) ||
126 (time
.tv_sec
== other
.time
.tv_sec
&& time
.tv_nsec
< other
.time
.tv_nsec
);
128 printf("%f < %f: %s\n", ToSeconds(), other.ToSeconds(), res?"yes":"no");
135 void Copy( const FTime
& other
)
137 if ( this != &other
)
149 double last_update_time
;
153 //inline bool operator< (const FTime& a, const FTime& b) { return a.operator<(b); }