Browse Subversion Repository
Contents of /common/CycleTimer.cpp
Parent Directory
| Revision Log
Revision 223 -
( show annotations)
( download)
( as text)
Fri Feb 1 15:14:25 2008 UTC
(16 years, 4 months ago)
by satofumi
File MIME type: text/x-c++src
File size: 1068 byte(s)
remove namespace GL, SDL;
| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief 周期タイマー |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "CycleTimer.h" |
| 11 |
#include "GetTicks.h" |
| 12 |
#include "Delay.h" |
| 13 |
|
| 14 |
using namespace beego; |
| 15 |
|
| 16 |
|
| 17 |
struct CycleTimer::pImpl { |
| 18 |
size_t cycle_msec; |
| 19 |
bool times_exactly; |
| 20 |
size_t next_ticks; |
| 21 |
|
| 22 |
pImpl(size_t cycle_msec_, bool times_exactly_) |
| 23 |
: cycle_msec(cycle_msec_), times_exactly(times_exactly_), |
| 24 |
next_ticks(GetTicks() + cycle_msec) { |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
CycleTimer::CycleTimer(size_t cycle_msec, bool times_exactly) |
| 30 |
: pimpl(new pImpl(cycle_msec, times_exactly)) { |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
CycleTimer::~CycleTimer(void) { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
void CycleTimer::wait(void) { |
| 39 |
|
| 40 |
size_t now_ticks = GetTicks(); |
| 41 |
if (pimpl->next_ticks > now_ticks) { |
| 42 |
size_t delay_msec = pimpl->next_ticks - now_ticks; |
| 43 |
delay(delay_msec); |
| 44 |
} |
| 45 |
|
| 46 |
pimpl->next_ticks += pimpl->cycle_msec; |
| 47 |
|
| 48 |
if (! pimpl->times_exactly) { |
| 49 |
// 間隔を保持しさえすればよい場合、待機目標が常に今よりも先になるようにする |
| 50 |
now_ticks = GetTicks(); |
| 51 |
while (now_ticks > (pimpl->next_ticks + pimpl->cycle_msec)) { |
| 52 |
pimpl->next_ticks += pimpl->cycle_msec; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
} |
|