Game Manager
Up

The Game Manager performs the rendering loop and the objects management for the entire game.

It is a worker thread invoked by the User Interface thread at game start.

Data structures

List of game objects (sorted by z-order).
Off-screen buffer for double-buffering.
Update rectangle - the smallest rectangle containing the changes from the last frame.
Update array - array of object image and position.
Image change flag - set if frame has changed.

Algorithm

While game is on do:

  1. Get current time
  2. See if it's time to send a game state check-sum to the server, if it is - do so.
  3. Empty the incoming messages queue, handling each message (e.g. adding bullet, removing tanks, etc.). The message only affect the list of objects. Message in the incoming queue can originate from local game objects or arrive from the network.
  4. Clear the update rectangle.
  5. Clear the image change flag.
  6. First pass - Loop over all game objects. For each object:
    6.1. Calculate state at current time
    6.2. Get current object's updated rectangle.
    6.3. If the object reports its image has changed, set the image change flag.
    6.4. Union object's updated rectangle with the global update rectangle.
    6.5. If the object dies, remove it from the objects list.
    6.6. Copy the object's updated rectangle from the background image to the off-screen buffer (restores the background, removing the object's image).
    6.7. If the object is alive, store its image index and position in the update array.
  7. Second pass - Skip this pass if the image change flag is clear (frame is the same as the previous one). Otherwise, loop over the update array. For each entry:
    7.1. Copy the current object's image to the off-screen buffer at the entry's location.
  8. If the image change flag is set, dump the off-screen buffer to the screen.
  9. Keep the constant frame rate - calculate the time passed from the beginning of the loop and if there's still time left, go to sleep.
 

Home Up Next