Sign in to follow this  
ursus

timing - last time hopefully

Recommended Posts

Hi all I'm sick and tired of my fruitless struggles to create time based game engine. I’ve read so many articles but still it appears my dumb brain has problems with implementing some code that would work properly. I therefore kindly ask anyone to point or present some simple c code of some professional timing engine. I don’t want any articles any more. Thank you in advance Peter

Share this post


Link to post
Share on other sites

int time_per_frame = 1000 / 60; //time in milliseconds: ~60 FPS
int last_update = SDL_GetTicks(); //insert any millisecond-returning function here
while (in_game)
{
int this_time = SDL_GetTicks();
if (this_time - last_update >= time_per_frame)
{
UpdateGameState();
last_update = this_time;
}
DrawGameGraphics();
}



This is likely the most simple method for time-based game control. It is accurate enough for simple purposes.

Hope it helps,
Twilight Dragon

Share this post


Link to post
Share on other sites
gosh, forgot to add i'd like QPC

I understand drawing should be done as fast as possible, however, I'm a bit confused on how often the phisics and the input should be calculated - the more often the better but this causes problems.

I'd be extremally gratful for some kind of tamplate used for proffesional looking games.

Share this post


Link to post
Share on other sites
Quote:
Original post by ursus
I therefore kindly ask anyone to point or present some simple c code of some professional timing engine.
So you want simple C code, but you want professional? Everything depends on what you mean by simple and what you mean by professional, but going by commonly accepted usage that's an oxymoron. I gave you simple. If you want simple with QPC:

float ticks_per_milli;

int GetTicks()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return li.LowPart / ticks_per_milli;
}

void InitTimer()
{
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
ticks_per_milli = (double)li.QuadPart / 1000.0;
}






For something a bit more along the lines of professional, allow me to present the Canonical Game Loop.

Enjoy.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this