NOGDUS $1670.00 has been donated to NOGDUS!
September 23, 2017, 12:38:03 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
   Home   Help Search Login Register  
Pages: [1]   Go Down
Author Topic: timing games with SDL_GetTicks  (Read 7818 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Offline Offline

Respect: 3425
« on: September 12, 2008, 04:23:17 PM »


you want to move your stuff by a certain amount of pixels over a certain amount of time, the same on old computers as on faster computers.

well try something like this.

float deltaTime = 0.0f;
int currentTime = 0;
int lastTime = 0;
// main game loop
currentTime = SDL_GetTicks();
deltaTime = (float)(currentTime - lastTime) / 1000;
lastTime = currentTime;

// assume we have this sprite to move
// both position and speed vars are floats
player.x += player.speedX * deltaTime;
player.y += player.speedY * deltaTime;

// call it to be drawn using a fictional
// routine


Captain Vimes
Offline Offline

Respect: 1
« Reply #1 on: September 22, 2009, 10:43:15 AM »

Quote from: Richard Marks
deltaTime = (float)(currentTime - lastTime) / 1000;

Why do you divide it by 1000? Isn't it already in milliseconds?

The crackling fire struck the end of Saturday's cane and flashed down his body, earthing itself in the floor underneat his feet. The zombie winked.
"No, ma'am," he said. "There ain't no way to kill a dead man."
 - "Witches Abroad", Terry Pratchett
Offline Offline

Respect: 10
« Reply #2 on: September 22, 2009, 11:46:18 PM »

I would assume the units (currentTime and lastTime) are in ms, but deltaTime is a value in seconds, thus divide by 1000.
Tags: SDL code  tutorial 
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!