Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Application::loop() {
- using HRC = std::chrono::high_resolution_clock;
- using Nano = std::chrono::nanoseconds;
- using TimePoint = std::chrono::time_point<Nano>;
- Stopwatch sw{};
- constexpr Nano ONE_SECOND{ 1000000000L };
- long long sum{};
- while( looping ) {
- Nano timestep{ ONE_SECOND / tickrate };
- sum += sw.time();
- sw.start();
- /* Ignore this part
- //check for GameState swaps.
- if (gameStateQueue != nullptr && currentGameState != gameStateQueue) {
- currentGameState = gameStateQueue;
- currentGameState->init();
- }
- */
- /* Ignore this part
- SDL_Event e{};
- while (SDL_PollEvent(&e)) {
- currentGameState->event_SDL(e);
- }
- */
- while (sum >= timestep.count()) {
- currentGameState->tick();
- static Stopwatch tempWatch{};
- std::cout << "tick was called " << tempWatch.time() / 1000000000.0L << std::endl;
- tempWatch.start();
- sum -= timestep.count();
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- STOPWATCH.HPP
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma once
- #include <chrono>
- /*
- * Wrapper class for <chrono>
- */
- class Stopwatch {
- private:
- std::chrono::time_point<std::chrono::high_resolution_clock> beginning;
- bool started = false;
- public:
- Stopwatch();
- Stopwatch(const Stopwatch& other);
- // Starts the timer.
- void start();
- /*
- * returns the elapsed time since starting in microseconds.
- * If the timer has not been started, the timer is started and the function returns 0LL.
- */
- long long time();
- virtual ~Stopwatch();
- };
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- STOPWATCH.CPP
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- #include "Stopwatch.hpp"
- #include <chrono>
- namespace {
- using HRC = std::chrono::high_resolution_clock;
- using Nano = std::chrono::nanoseconds;
- }
- Stopwatch::Stopwatch()
- : beginning{} {}
- Stopwatch::Stopwatch(const Stopwatch& other)
- : beginning{ other.beginning } {}
- void Stopwatch::start() {
- beginning = HRC::now();
- started = true;
- }
- long long Stopwatch::time() {
- Nano interval = HRC::now() - beginning;
- if (started) {
- return interval.count();
- } else {
- start();
- return 0LL;
- }
- }
- Stopwatch::~Stopwatch() = default;
Advertisement
Add Comment
Please, Sign In to add comment