Writing a Better Game Loop

I am coding a guitar hero clone game. It works well, but on fast computers (at high frame rates) the animation is not always smooth. I mean sometimes the notes scroll with bumps. I found out that the problem is probably due to my game loop not closing the fps (since I am updating the frame time, I thought it was not necessary). When there is a drop from, say, 140fps to 60, these bumps are noticeable.

I have looked at this site: http://www.koonsolo.com/news/dewitters-gameloop/ and I am trying to implement the "FPS depends on constant game speed" approach just to make sure the problem is fixed.

The problem is my update and rendering of the game are stable at 60fps and the soundtrack seems to be called a lot more. I am using Audiere for Sound and SDL for timer / input.

I thought SDL_Delay would pause the game thread until finished, but it doesn't seem like it. Is there something I am missing? Is SDL not a real lib for time?

+3


source to share


2 answers


Some examples or pseudo code will be very helpful to know what you are doing. From the documentation for SDL_Delay:

This function waits a specified number of milliseconds before returning. It waits for at least the specified time, but possibly longer due to OS scheduling. The latency granularity is at least 10 ms. Some platforms have shorter clock signals, but this is the most common.



It may linger too much. Assuming you are using a while loop, have you tried just "continue" to slow down the draws? If you do SDL_Delay (1), it can take longer and thus mess up your calculations.

+4


source


SDL is actually great for gaming! This has always been my favorite because it is surprisingly low.



Your SDL callback will always be called very quickly! Usually, you see visual animation rendered at 60fps because it looks good to our eyes. PC sound typically runs at 44,100 samples per second. This requires a lot more support to make sure the audio device has a sample to play! SDL will run the callback as quickly as necessary to prevent audio crashes.

+1


source







All Articles