SDL / C -> Loop timing

SDL / C -> Loop timing

Postby Sauer2 » 24 May 2010, 23:12

I have got a question:
If you have a main loop..

{l Code}: {l Select All Code}
while (gamerunning == 1)
{

}


..,integer variables LastTicks and ElapsedTicks, a drawing_stuff() function, a moving_stuff(deltatime) function, the SDL_FlipScreen function, SDL_Delay and SDL_GetTicks, how must a really good order of those things look to make the game performance independent?

Thank you and best regards

Sauer2
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: SDL / C -> Loop timing

Postby alapshin » 25 May 2010, 04:00

I use this order:

{l Code}: {l Select All Code}
LastTicks = SDL_GetTicks();
while (gamerunning == 1)
{
    deltatime = SDL_GetTicks() - LastTime; // Compute time delta between
    LastTicks = SDL_GetTicks();            // successive frames
    moving_stuff(deltatime);               // animate sprites
    drawing_stuff();                       // render them at new positions
    SDL_FlipScreen();                      // swap buffers
}


But, actually I do not kwon SDL and it's functions, so may be I am wrong. I set this order relaying on their name.
alapshin
 
Posts: 39
Joined: 28 Mar 2010, 12:09

Re: SDL / C -> Loop timing

Postby Sauer2 » 25 May 2010, 09:10

Thank you.
Just one question left: If you don't use something like SDL_Delay to make the game wait for ca. 10 ms, how do you prevent your game from taking all the cpu cycles in this context?

EDIT: Also I got speed problems using this way, the objects move far to fast. Is there a standard way of solving this?

Thanks and best regards

Sauer2
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Re: SDL / C -> Loop timing

Postby alapshin » 25 May 2010, 12:17

To resolve 100% CPU usage I turn vertical synchronization on for the game and get implicit "wait" during buffers swap process (in SDL context: During SDL_FlipScreen()) . I do not know if it possible for SDL, but I think there should be a way to do this. After turning VSync on I get application that is running with a constant frame rate (for example 60 fps) or at least it does not exceed 60 fps.

Do you use "deltatime" while computing shift for sprite movements and while computing frame animation? To make animation performance independent it should be shown with a constant frame rate. It means that you should use "deltatime" to compute what frame should be shown for the sprite..

For example: You use contant speed for your sprite - 100 pixels per second. It means that it should be moved on deltatime * 100 (deltatime should be in seconds in this case). I think that this approach may help you to avoid very fast or very slow object movements. The same is for animation.
alapshin
 
Posts: 39
Joined: 28 Mar 2010, 12:09

Re: SDL / C -> Loop timing

Postby Sauer2 » 25 May 2010, 12:44

OK, I think this should work. Just have to fix some smaller issues, but overall this works. Thank you. :)

best regard

Sauer2
User avatar
Sauer2
 
Posts: 430
Joined: 19 Jan 2010, 14:02

Who is online

Users browsing this forum: No registered users and 1 guest