views:

97

answers:

3

My game uses a d = vt calculation for movement of objects where t is the time since the last frame (one frame per loop).

I'm using SDL and the gist of the timing calculation is that I create an instance of a Timer class and start it. I call GetSeconds() when it's needed which returns the difference between when the timer was started and the current time (divided by 1000 because everything is in milliseconds).

Ex:

return (SDL_GetTicks() - m_StartingTicks) / MILLISECONDS_PER_SECOND;

After each loop the timer is reset. I.e. m_StartingTicks = SDL_GetTicks() However, I recently changed this so that it's only reset if m_StartingTicks is < SDL_GetTicks, but it didn't fix the problem.

This was all hunky dory until I recently wrote a game engine to handle different game states and various other things which are used in my main game loop. This seriously improved performance, but unfortunately each iteration of the game loop is now occuring in less than 1 millisecond so when I pass GetSeconds(), 0 is returned and things on the screen don't move.

The easiest way to handle this is a simple kludge where if SDL_GetTicks() - m_StartingTicks) == 0 I change it to a 1 (as in 1 millisecond instead of 0). I don't really like this though, and I'd like to hear any suggestions, fixes, improvements, etc.

If you need more info I'd be happy to offer it.

A: 

If you're running at 60Hz, then each frame should be roughly 16ms. Sounds like you have disabled vertical sync?

tsalter
I don't think SDL allows you to disable/enable vertical sync for a game. I could try using SDL_HWSURFACE...
Anonymous
Nope, tried flagging both SDL_HWSURFACE and SDL_ASYNCBLIT
Anonymous
Was able to get it working if I called reset immediately after the passing of GetSeconds()
Anonymous
I think you should make sure to take the time only once per logic update. You shouldn't have a problem with a 0 ms time difference. If you manage to update that often, then things just don't move (perhaps ignore updating anything). Depending on the kind of game, may-be give the CPU some rest?
UncleBens
+2  A: 

You should be accumulating time and updating your game loop at some fixed rate. Check out this article for details.

Brian
A: 

It sounds like your timer code is wrong. If a frame truly did happen in less than 1 millisecond and it rounds down to zero, that's fine, because eventually as time increases that will reach 1 millisecond and you'll see movement, averaging at the proper rate. It sounds like you're trying to measure a frame's duration instead of measuring the time since the same point in the last frame, which will cause you problems if the majority of the time is actually spent outside of your update function (eg. in the vsync mechanism).

It also sounds like you might be having rounding issues - show the data types you're using. Dividing a small integer by 1000 will tend to make it into zero and you'll lose any data within it, even if it was 999 before. Often it's best to cast it to a float first before doing the division.

Kylotan