Game Development Reference
In-Depth Information
3.
Get the current time:
struct timeval Time;
gettimeofday( &Time, NULL );
4.
Calculate the number of microseconds:
int64_t T1 = Time.tv_usec + Time.tv_sec * usec_per_sec;
5.
Return the current time in seconds. The double precision is necessary here, since
the timer counts time since the moment the system starts and the 32-bit float
precision is not enough:
return (double)( T1 ) / (double)usec_per_sec;
}
6.
We use three variables with current, previous, and total time. First, we initialize the
g_OldTime and g_NewTime time counters:
g_OldTime = GetSeconds();
g_NewTime = g_OldTime;
7.
Before we start, the total time counter should be set to zero:
g_ExecutionTime = 0;
8.
Each frame we call the GenerateTicks() method to set up the animation:
void GenerateTicks()
{
g_NewTime = GetSeconds();
9.
Calculate how much time has passed since the previous update:
float DeltaSeconds = static_cast<float>(g_NewTime-
g_OldTime);
g_OldTime = g_NewTime;
10. Call the OnTimer() routine with the non-zero number of seconds:
if (DeltaSeconds > 0) { OnTimer(DeltaSeconds); }
}
11. For the Windows version, time stepping is done using the SetTimer() function,
which enables a system timer event every 10 milliseconds:
SetTimer( hWnd, 1, 10, NULL);
12. Each time these milliseconds pass, the WM_TIMER event is sent to our window
function. We add another case in the switch construction, where we just call the
OnTimer() method:
LRESULT CALLBACK MyFunc( HWND h, UINT msg, WPARAM w, LPARAM
p )
...
case WM_TIMER:
 
Search WWH ::




Custom Search