Sleepi_

thing

Aug 28th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. void Application::loop() {
  2.     using HRC = std::chrono::high_resolution_clock;
  3.     using TimePoint = std::chrono::time_point<HRC>;
  4.     using Nano = std::chrono::nanoseconds;
  5.     using std::chrono::duration_cast;
  6.  
  7.     Nano sum{ Nano::zero() };
  8.     TimePoint then{ HRC::now() };
  9.     constexpr Nano ONE_SECOND{ 1000000000L };
  10.  
  11.     while (looping) {
  12.         Nano timestep{ ONE_SECOND / tickrate };
  13.         sum += duration_cast<Nano>(HRC::now() - then);
  14.         then = HRC::now();
  15.  
  16.         //check for GameState swaps.
  17.         if (gameStateQueue != nullptr && currentGameState != gameStateQueue) {
  18.             currentGameState = gameStateQueue;
  19.             currentGameState->init();
  20.         }
  21.  
  22.         SDL_Event e{};
  23.         while (SDL_PollEvent(&e)) {
  24.             currentGameState->event_SDL(e);
  25.         }
  26.  
  27.         while (sum >= timestep) {
  28.             /* Temp start */
  29.             static TimePoint lastTick{ HRC::now() };
  30.             std::cout << "Time elapsed: " << duration_cast<Nano>(HRC::now() - lastTick).count() / 1000000000.0L << std::endl;
  31.             lastTick = HRC::now();
  32.             /* Temp end */
  33.             currentGameState->tick();
  34.             sum -= timestep;
  35.         }
  36.  
  37.         currentGameState->render(static_cast<double>(sum.count()) / timestep.count());
  38.     }
  39.     currentGameState->shutdown();
  40.     cleanup();
  41. }
Advertisement
Add Comment
Please, Sign In to add comment