I am wondering how others handle this situation:
You have an application (e.g. a game) that calculates a state (e.g. the game logic) and also displays this one. From what I read it seems that the game logic is most of the time running in a different thread than the display. Which brings me to the question:
How is the display synchronized with the game logic? If there is no synchronization, we can have following situations:
- game is stepped forward, displayed, stepped forward twice, displayed, etc. --> the display will appear shacking!
- while the game is stepped forward a frame is displayed --> the display can appear "strange" (e.g. the bullet can appear as hitting the game character, but since its state was not yet updated, it will actually not hit it)
- A game character can be removed from the scene during the game logic calculation. If the display happens at that time, there might be a crash.
We can synchronize to some extent the 2 threads by locking resources (the last example above can be handled by deferring object destruction). But in order to avoid all above mentionned problems, one should run the 2 threads in alternance (or similar, e.g. step the game twice, render, step the game twice, render, etc.). Doing so makes the use of 2 threads not interesting anymore, since a single thread would be running at the same speed (more or less) and all the resource locking synchronization would not have to be taken care of: a single thread would be easier and have the same result. No?
I guess that the game state must be demultiplied in some way (e.g. all positions would be stored as "current" and "forDisplay", and at the end of a game step, "current" would be copied to "forDisplay", so that the rendering thread would be able to run concurrently).
And what happens if the game logic needs to use some OpenGL commands? e.g. to render to FBO and do some simple image processing on that? Then there will be again the need to synchronize the 2 threads in order to be able to correctly switch the OpenGL contexts!
Just curious how things are done usually ;)