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
22 #include <mach/mach_time.h>
35 time
.tv_sec
= 0; time
.tv_nsec
= 0;
37 last_update_time
= 0.1f
; };
39 FTime( const FTime
& other
) { Copy(other
); }
41 // set us to the current time
45 time
= mach_absolute_time();
47 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
48 clock_gettime(CLOCK_REALTIME
, &time
); // Works on Linux
52 // set us to the given time in seconds
53 void SetSeconds( double seconds
);
55 // update our time to the current time, returning
56 // delta time in seconds as a float
59 // get the last delta time returned by Update()
60 double GetLastUpdateTime() const { return last_update_time
; }
62 // return milliseconds
63 double ToMillis() const;
65 double ToSeconds() const { return ToMillis() / 1000.0; }
68 // calculate difference
69 FTime
operator-( const FTime
& other
) const
77 FTime
& operator -= (const FTime
& other
)
82 //assert( false && "broken code, please fix ");
83 if ( time
.tv_nsec
< other
.time
.tv_nsec
)
87 //printf("underflow: %10li:%10li - %10li:%10li = ", time.tv_sec, time.tv_nsec, other.time.tv_sec, other.time.tv_nsec );
88 time
.tv_sec
-= other
.time
.tv_sec
+ 1;
89 time
.tv_nsec
= 1e9
- (other
.time
.tv_nsec
-time
.tv_nsec
);
90 //printf(" %10li:%10li\n", time.tv_sec, time.tv_nsec );
94 time
.tv_sec
-= other
.time
.tv_sec
;
95 time
.tv_nsec
-= other
.time
.tv_nsec
;
102 FTime
& operator= (const FTime
& other
) { Copy(other
); return *this; }
105 bool operator== (const FTime
& other
) const {
107 assert( false && "implement me" );
109 return time
.tv_sec
== other
.time
.tv_sec
&& time
.tv_nsec
== other
.time
.tv_nsec
;
114 bool operator< (const FTime
& other
) const {
116 assert(false&&"implement me" );
119 /*bool res =*/return (time
.tv_sec
< other
.time
.tv_sec
) ||
120 (time
.tv_sec
== other
.time
.tv_sec
&& time
.tv_nsec
< other
.time
.tv_nsec
);
122 printf("%f < %f: %s\n", ToSeconds(), other.ToSeconds(), res?"yes":"no");
129 void Copy( const FTime
& other
)
131 if ( this != &other
)
143 double last_update_time
;
147 //inline bool operator< (const FTime& a, const FTime& b) { return a.operator<(b); }