NOGDUS $1670.00 has been donated to NOGDUS!
April 19, 2014, 02:39:43 PM *
Welcome, Guest. Please login or register.

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

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

hmm

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.

Code:
float deltaTime = 0.0f;
int currentTime = 0;
int lastTime = 0;
// main game loop
while(1)
{
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
Draw_Sprite(backBuffer,
playerImage,
(int)player.x,
(int)player.y);

SDL_Delay(1);
}
Logged

Captain Vimes
Offline Offline

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

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

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

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
RedSlash
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.
Logged
Tags: SDL code  tutorial 
Pages: [1]   Go Up
  Print  
 
Jump to:  

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