Advertisement
spacechase0

Fixed Time-Step

Mar 17th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1.         // In the class
  2.         bool isRunning;
  3.         sf::Clock simulationTimer;
  4.         const sf::Uint8 simulationRate;
  5.        
  6.         // The Loop
  7.         sf::Uint32 rate = 0;
  8.         simulationTimer.Reset();
  9.         isRunning = true;
  10.  
  11.         while ( isRunning )
  12.         {
  13.             // Process to the next scene, if it's different
  14.             // ...
  15.             ScenePtr scene = scenes[ currentScene ];
  16.  
  17.             // Simulate only simulationRate times a second
  18.             while ( rate > simulationRate )
  19.             {
  20.                 // Events, update loop
  21.                 sf::Event event;
  22.                 while ( window.PollEvent( event ) )
  23.                 {
  24.                     scene->Update( event );
  25.                 }
  26.                 scene->Update();
  27.  
  28.                 rate -= ( 1000 / simulationRate );
  29.             }
  30.            
  31.             // Render as much as possible
  32.             // You could probably put this in the above loop, for battery
  33.             scene->Draw( window );
  34.            
  35.            
  36.            
  37.             // Update the simulation rate...
  38.             rate += simulationTimer.GetElapsedTime();
  39.             simulationTimer.Reset();
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement