shybovycha
shybovycha

Reputation: 12265

SDL FPS input delay

I am using SDL in my playground project and i am a bit worried about my PC performance - it is too high or I am just wandering.

The thing is, when I change the coordinates of some sprite when the key on keyboard is down, for example, by 2 px the sprite moves too fast. And the same is valid for 1 px velocity.

Usually (in SFML) I do the next velocity changes: 1.f / App->GetTimeSinceLastFrame() and it works just perfectly! But now I wanna use SDL. And I can not choose delays because they will not be identical on different PCs (and that is a very ugly way for sure) or float-value conversions because of the next reason.

Doing lastTicks = SDL_GetTicks() and then using 1.f / (float) SDL_GetTicks() - lastTicks() does the bad job - that difference is always zero. So either I am mistaken or the time between two frames is so small that it is rounded to 0.

Can anyone give me an advice what should I do?

NOTE: change SDL to xxx is not a good advice ;)

Upvotes: 3

Views: 1528

Answers (1)

Piotr Praszmo
Piotr Praszmo

Reputation: 18330

It doesn't make sense to do more than 60 fps (monitor refresh rate). You are only wasting CPU time. Call SDL_Delay if your game is running too fast.

    int delay=1000/maxFPS-SDL_GetTicks()+lastTicks;

    if(delay>0)
        SDL_Delay(delay);

Upvotes: 1

Related Questions