NOGDUS

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



: timing games with SDL_GetTicks
: Richard Marks 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.

:
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);
}


: Re: timing games with SDL_GetTicks
: Captain Vimes September 22, 2009, 10:43:15 AM
: Richard Marks
:
deltaTime = (float)(currentTime - lastTime) / 1000;

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


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


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.