NOGDUS

Articles, Tutorials, and other things. => SDL Game Programming => Topic started by: Richard Marks on September 12, 2008, 04:23:17 PM



Title: timing games with SDL_GetTicks
Post by: Richard Marks 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);
}


Title: Re: timing games with SDL_GetTicks
Post by: Captain Vimes 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?


Title: Re: timing games with SDL_GetTicks
Post by: RedSlash 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.