cause this problem was a bug in my LD game and will be a bug in other games I want to present it and possible solutions for it. I would like to know what you think about these or have another solution.
Not all computers are as fast as other computers. That’s why a game without any timing limiter would execute on other computers faster or slower.
There are some possible solutions I already know:
- Limit the frame rate to a constant value (all procedural movement can be done with a constant value)
- Compute the elapsed time between two frames (the elapsed time must be multiplied with all speed values)
I want to show you my opinion about these.
Limit the frame rate to a constant value
It looks very easy you calculate the time span of the current frame and subtract it from the time span a full frame for the desired frame rate and you have the time you must wait before a new frame may start. But what is when you notice that your game is suddenly slow? The problem in the code can be thousand code lines before the code you were actually working on, but you don’t saw it because you can’t see when the frame takes more time. The frame rate, if you don’t have a very bad computer, stays at his value.
Compute the elapsed time between two frames
That is what I did all the time and it worked. But at that time I used the graphic functions of the SDL, but I switched to OpenGL because SDL was too slow (especially for large images). As I used SDL the max frame rate was about 500 and this was good enough for me. As I started Mr.No’s Laboratory I got 10.000 FPS and the timer function was broken. The reason is that I use milliseconds for computing the elapsed time. But at 10.000 FPS a frame only take 0.1 seconds but the function for getting the milliseconds returns an unsigned int (so there is no comma). The timing function works only with frame rates up to 1.000 frames per second (elapsed time=1)
For ludum dare I already knew this problem but I thought I solved it with computing the elapsed time every 50 frames (max frame rate=50.000). But I doesn’t work. Multiple people reported that the player is moving very fast. For a postmortem version I activate VSync (so the frame rate is about 60-75), but I want to keep my high frame rates. But what to do? I already know two solutions:
- using nanoseconds instead of milliseconds
- determine a proportion between the frames to skip (for the elapsed time calculation) and a performance of the computer (like cycles in one millisecond)
There is a easy-to-use function for java but for c++ I only know the functions for windows and even if I don’t port my applications to Linux or Mac I want to keep the possibility for it.+
determine a proportion
Using a code like: while (SDL_GetTicks()-ticksAtStart<100) count++; you get a performance value of your computer. Maybe there is a proportion between this value and the number of frames I must skip for a elapsed time value which I can use to do procedural movements.
But whats your opinion about all these problems?