Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Application::loop() {
- using HRC = std::chrono::high_resolution_clock;
- using TimePoint = std::chrono::time_point<HRC>;
- using Nano = std::chrono::nanoseconds;
- using std::chrono::duration_cast;
- Nano sum{ Nano::zero() };
- TimePoint then{ HRC::now() };
- constexpr Nano ONE_SECOND{ 1000000000L };
- while (looping) {
- Nano timestep{ ONE_SECOND / tickrate };
- sum += duration_cast<Nano>(HRC::now() - then);
- then = HRC::now();
- //check for GameState swaps.
- if (gameStateQueue != nullptr && currentGameState != gameStateQueue) {
- currentGameState = gameStateQueue;
- currentGameState->init();
- }
- SDL_Event e{};
- while (SDL_PollEvent(&e)) {
- currentGameState->event_SDL(e);
- }
- while (sum >= timestep) {
- /* Temp start */
- static TimePoint lastTick{ HRC::now() };
- std::cout << "Time elapsed: " << duration_cast<Nano>(HRC::now() - lastTick).count() / 1000000000.0L << std::endl;
- lastTick = HRC::now();
- /* Temp end */
- currentGameState->tick();
- sum -= timestep;
- }
- currentGameState->render(static_cast<double>(sum.count()) / timestep.count());
- }
- currentGameState->shutdown();
- cleanup();
- }
Advertisement
Add Comment
Please, Sign In to add comment