To implement multi-threading I used POSIX threads (-pthreads). The fixed frequency game logic would update the game state based on user input and the physics of the moving rectangle "characters". It would then sleep for 100 ms, basically updating the game state 10 times a second.
The render loop was in the main thread and would both update the screen with the updated game state as well as do some animation interpolation based on the trajectories of the rectangles from the last game state. This also had to trap any user input (keyboard keys) and then update the game state with these inputs for when the fixed-frequency thread would next check it. To avoid synchronization issues, I used mutexes when in critical sections that accessed the shared variables containing game state info. The render loop will run as fast as the system it runs on, on my box it was humming along at 140 fps.
This is a high level diagram of the separation of the game logic:
References:
- Sleeping a Posix Thread: pthread_cond_timedwait()--Timed Wait for a Condition
- Valente, L., Conci, A., and Feijó, B. 2005. Real time game loop models for single-player computer games. In Proceedings of the IV Brazilian Symposium on Computer Games and Digital Entertainment, 89--99. Downloaded from: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.118.8892
No comments:
Post a Comment