Sleepi_

new loop

Aug 29th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. void Application::loop() {
  2.     using HRC = std::chrono::high_resolution_clock;
  3.     using Nano = std::chrono::nanoseconds;
  4.     using TimePoint = std::chrono::time_point<Nano>;
  5.  
  6.     Stopwatch sw{};
  7.     constexpr Nano ONE_SECOND{ 1000000000L };
  8.     long long sum{};
  9.  
  10.     while( looping ) {
  11.         Nano timestep{ ONE_SECOND / tickrate };
  12.         sum += sw.time();
  13.         sw.start();
  14.        
  15.         /* Ignore this part
  16.         //check for GameState swaps.
  17.         if (gameStateQueue != nullptr && currentGameState != gameStateQueue) {
  18.             currentGameState = gameStateQueue;
  19.             currentGameState->init();
  20.         }
  21.         */
  22.        
  23.         /* Ignore this part
  24.         SDL_Event e{};
  25.         while (SDL_PollEvent(&e)) {
  26.             currentGameState->event_SDL(e);
  27.         }
  28.         */
  29.  
  30.         while (sum >= timestep.count()) {
  31.             currentGameState->tick();
  32.  
  33.             static Stopwatch tempWatch{};
  34.             std::cout << "tick was called " << tempWatch.time() / 1000000000.0L << std::endl;
  35.             tempWatch.start();
  36.  
  37.             sum -= timestep.count();
  38.         }
  39.     }
  40. }
  41.  
  42.  
  43.  
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////
  45.     STOPWATCH.HPP
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////
  47. #pragma once
  48. #include <chrono>
  49.  
  50. /*
  51.  * Wrapper class for <chrono>
  52.  */
  53. class Stopwatch {
  54. private:
  55.     std::chrono::time_point<std::chrono::high_resolution_clock> beginning;
  56.     bool started = false;
  57.  
  58. public:
  59.     Stopwatch();
  60.     Stopwatch(const Stopwatch& other);
  61.  
  62.     // Starts the timer.
  63.     void start();
  64.  
  65.     /*
  66.      * returns the elapsed time since starting in microseconds.
  67.      * If the timer has not been started, the timer is started and the function returns 0LL.
  68.      */
  69.     long long time();
  70.  
  71.     virtual ~Stopwatch();
  72. };
  73.  
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////
  75.     STOPWATCH.CPP
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////
  77.  
  78. #include "Stopwatch.hpp"
  79. #include <chrono>
  80.  
  81. namespace {
  82.     using HRC = std::chrono::high_resolution_clock;
  83.     using Nano = std::chrono::nanoseconds;
  84. }
  85.  
  86. Stopwatch::Stopwatch()
  87.     : beginning{} {}
  88.  
  89. Stopwatch::Stopwatch(const Stopwatch& other)
  90.     : beginning{ other.beginning } {}
  91.  
  92. void Stopwatch::start() {
  93.     beginning = HRC::now();
  94.     started = true;
  95. }
  96.  
  97. long long Stopwatch::time() {
  98.     Nano interval = HRC::now() - beginning;
  99.     if (started) {
  100.         return interval.count();
  101.     } else {
  102.         start();
  103.         return 0LL;
  104.     }
  105. }
  106.  
  107. Stopwatch::~Stopwatch() = default;
Advertisement
Add Comment
Please, Sign In to add comment