You Like My Tutorials? Click The Banner To Keep 'Em Coming!

glGameDeveloper(): The Game Developer's Source for OpenGL Coding
Main Menu
Latest News
Archived News
Tutorials
Links
E-Mail Me

Other
Message Board
My Guestbook

Tutorials -- Updating Games Based on Time
 


Updating Your Game Based on Time Page 2 of 3
< Previous Jump to Page: 1 | 2 | 3 Next >

The Code

Make sure you have #include <time.h> in your project somewhere. If you followed my Basic OpenGL tutorial, I already added that in "main.h".

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.

Updating Your Game Based on Time Page 2 of 3
< Previous Jump to Page: 1 | 2 | 3 Next >

This site best viewed with MS Internet Explorer 5.0 at 16-bit color and at least 800x600.

All Contents of this site are Copyright 1999, 2000 Joe Zimmerman. No part of this site (code, HTML, etc.) may be reproduced without my permission.

Click Here!