Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a basic (very high level pseudo-code) game loop:</p> <pre><code>void RunGameLoop() { // record the frame time and calculate the time since the last frame float timeTemp = time(); float deltaTime = timeTemp - lastFrameTime; lastFrameTime = timeTemp; // iterate through all units and let them update unitManager-&gt;update(deltaTime); // iterate through all units and let them draw drawManager-&gt;draw(); } </code></pre> <p>The purpose of passing deltaTime (the time since the last frame in seconds) to unitManager->update() is so that when the units are updating, they can multiply their movement by deltaTime so their values can be in units per second.</p> <pre><code>abstract class Unit { public: abstract void update(float deltaTime); } FallingBlockUnit::update(float deltaTime) { moveDown(fallSpeed * deltaTime); } </code></pre> <p>The draw manager is going to be responsible for managing the draw buffers (I suggest double buffering to prevent screen flicker)</p> <pre><code>DrawManager::draw() { // set the back buffer to a blank color backBuffer-&gt;clear(); // draw all units here // limit the frame rate by sleeping until the next frame should be drawn // const float frameDuration = 1.0f / framesPerSecond; float sleepTime = lastDrawTime + frameDuration - time(); sleep(sleepTime); lastDrawTime = time(); // swap the back buffer to the front frontBuffer-&gt;draw(backBuffer); } </code></pre> <p>For further research, here's a book that my Game Programming professor wrote about 2d game programming. <a href="http://rads.stackoverflow.com/amzn/click/1466501898" rel="nofollow">http://www.amazon.com/Graphics-Programming-Games-John-Pile/dp/1466501898</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload