home *** CD-ROM | disk | FTP | other *** search
- #ifndef __TIMEUTILS__
- #define __TIMEUTILS__
- #pragma once
-
- //----------------------------------------------------------------------------------------
- // This routine checks to see if the timer has run out; it handles the case when
- // the stopTime calculation (startTime + timeout) wraps around (overflows a long int).
- // This only happens every 771 days or so, but Mike Gough said that I wasn't a stud
- // unless I handled this case.
-
- inline bool TimeExpired(unsigned long currentTime, unsigned long startTime, unsigned long stopTime)
- {
- // First adjust time so that the origin is at 'startTime'
- stopTime -= startTime;
- currentTime -= startTime;
-
- // Then, it is a simple matter to compare the current
- // time value with the time that we want to stop at.
- return currentTime >= stopTime;
- }
-
- //----------------------------------------------------------------------------------------
- // It is also important to know which of two ticks will happen first, starting from
- // a given tick count. If one of the ticks == currentTime, we consider that event
- // to have already gone by (hence the extra +1's), and won't be encountered again until
- // the timer wraps all the way around ~771 days later.
-
- inline unsigned long CloserTick(unsigned long currentTime, unsigned long tick1, unsigned long tick2)
- {
- currentTime += 1;
-
- //
- // Once again, move the beginning of time, this time to 'currentTime + 1'
- //
- tick1 -= currentTime;
- tick2 -= currentTime;
-
- //
- // Once we've converted to a zero-based system, it's easy to compare
- // the two times. Convert back to the appropriate time base by adding
- // currentTime + 1 back onto the tick after we've determined which will
- // happen sooner.
- //
- if(tick1 < tick2)
- return tick1 + currentTime;
- else
- return tick2 + currentTime;
- }
-
-
- #endif __TIMEUTILS__
-