| |||||||||||
|
Main Menu Other |
Tutorials -- Updating Games Based on Time | ||||||||||
|
The CodeMake sure you have Now add these lines whereever you declare your global variables. If you used the Basic OGL tutorial, put them near the top of "main.cpp" after the #include line(s) at the top. // time information float timeoffset; DWORD currenttime, lasttime; // GetTimePassed() function declaration float GetTimePassed(); That's all we'll need for declarations. Now put the following function somewhere in your program. It can be at the very end if you want, whatever works for you. Just make sure it's in a .cpp file, and not a .h file. Put it in "main.cpp" if you used my Basic OGL tutorial.
float GetTimePassed() {
// if the game just started, set lasttime to the
// current time this prevents the game from
// thinking that several hours have passed from
// the last frame.
if (lasttime == 0) lasttime = timeGetTime();
// get the current time
currenttime = timeGetTime();
// calculate the offset
timeoffset = currenttime - lasttime;
// put the current time in the lasttime variable
lasttime = currenttime;
// return the time offset as a decimal
return timeoffset / 1000;
}
There we go. Now move on and I'll show you how to use it.
|
|||||||||||