2 Copyright 2007, 2008, 2009, 2010 Damian Stewart <damian@frey.co.nz>.
3 Distributed under the terms of the GNU General Public License v3.
5 This file is part of The Artvertiser.
7 The Artvertiser is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 The Artvertiser is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with The Artvertiser. If not, see <http://www.gnu.org/licenses/>.
26 implements a high resolution timer
28 can store either a high-precision relative time or a high-precision absolute time
37 #include <mach/mach_time.h>
52 time
.tv_sec
= 0; time
.tv_nsec
= 0;
54 last_update_time
= 0.1f
; };
56 FTime( const FTime
& other
) { Copy(other
); }
58 // set us to the current time
62 time
= mach_absolute_time();
64 // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
65 clock_gettime(CLOCK_REALTIME
, &time
); // Works on Linux
69 // set us to the given time in seconds
70 void SetSeconds( double seconds
);
72 // update our time to the current time, returning
73 // delta time in seconds as a float
76 // get the last delta time returned by Update()
77 double GetLastUpdateTime() const { return last_update_time
; }
79 // return milliseconds
80 double ToMillis() const;
82 double ToSeconds() const { return ToMillis() / 1000.0; }
85 // calculate difference
86 FTime
operator-( const FTime
& other
) const
94 FTime
& operator -= (const FTime
& other
)
99 //assert( false && "broken code, please fix ");
100 if ( time
.tv_nsec
< other
.time
.tv_nsec
)
104 //printf("underflow: %10li:%10li - %10li:%10li = ", time.tv_sec, time.tv_nsec, other.time.tv_sec, other.time.tv_nsec );
105 time
.tv_sec
-= other
.time
.tv_sec
+ 1;
106 time
.tv_nsec
= 1e9
- (other
.time
.tv_nsec
-time
.tv_nsec
);
107 //printf(" %10li:%10li\n", time.tv_sec, time.tv_nsec );
111 time
.tv_sec
-= other
.time
.tv_sec
;
112 time
.tv_nsec
-= other
.time
.tv_nsec
;
119 FTime
& operator= (const FTime
& other
) { Copy(other
); return *this; }
122 bool operator== (const FTime
& other
) const {
124 return ( time
== other
.time
);
126 return time
.tv_sec
== other
.time
.tv_sec
&& time
.tv_nsec
== other
.time
.tv_nsec
;
131 bool operator< (const FTime
& other
) const {
133 return ( time
< other
.time
);
135 /*bool res =*/return (time
.tv_sec
< other
.time
.tv_sec
) ||
136 (time
.tv_sec
== other
.time
.tv_sec
&& time
.tv_nsec
< other
.time
.tv_nsec
);
138 printf("%f < %f: %s\n", ToSeconds(), other.ToSeconds(), res?"yes":"no");
145 void Copy( const FTime
& other
)
147 if ( this != &other
)
159 double last_update_time
;
163 //inline bool operator< (const FTime& a, const FTime& b) { return a.operator<(b); }