vectozavr-shooter/engine/utils/Time.cpp

79 lines
1.7 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 11.01.2021.
//
#include "Time.h"
#include "Log.h"
2021-10-31 11:39:08 +03:00
#include "../Consts.h"
2021-09-13 15:53:43 +03:00
using namespace std::chrono;
2021-10-31 11:39:08 +03:00
Time *Time::_instance = nullptr;
2021-10-29 23:44:37 +03:00
bool Time::_validInstance = false;
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
void Time::init() {
_instance = new Time();
2021-10-29 23:44:37 +03:00
_validInstance = true;
Log::log("Time::init(): time was initialized");
2021-10-17 19:38:16 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
double Time::time() {
2021-10-31 11:39:08 +03:00
if (!_validInstance) {
2021-10-17 19:38:16 +03:00
return 0;
2021-10-29 23:44:37 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
return _instance->_time;
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
double Time::deltaTime() {
2021-10-31 11:39:08 +03:00
if (!_validInstance) {
2021-10-17 19:38:16 +03:00
return 0;
2021-10-29 23:44:37 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
return _instance->_deltaTime;
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
void Time::update() {
2021-10-31 11:39:08 +03:00
if (!_validInstance) {
2021-10-17 19:38:16 +03:00
return;
2021-10-29 23:44:37 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
high_resolution_clock::time_point t = high_resolution_clock::now();
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
_instance->_deltaTime = duration<double>(t - _instance->_last).count();
_instance->_time = duration<double>(t - _instance->_start).count();
// in case when fps < 10 it is useful to decrease _deltaTime (to avoid collision problems)
2021-10-31 11:39:08 +03:00
if (_instance->_deltaTime > Consts::LARGEST_TIME_STEP) {
2021-10-17 19:38:16 +03:00
_instance->_deltaTime = Consts::LARGEST_TIME_STEP;
2021-10-31 11:39:08 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
_instance->_last = t;
2021-10-31 11:39:08 +03:00
if (_instance->_deltaTime > 10000) {
2021-10-17 19:38:16 +03:00
return;
2021-10-31 11:39:08 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
_instance->_fpsCounter++;
if (t - _instance->_fpsStart > _instance->_fpsCountTime) {
_instance->_lastFps = _instance->_fpsCounter / duration<double>(t - _instance->_fpsStart).count();
_instance->_fpsCounter = 0;
_instance->_fpsStart = t;
2021-09-13 15:53:43 +03:00
}
}
2021-10-17 19:38:16 +03:00
int Time::fps() {
2021-10-31 11:39:08 +03:00
if (!_validInstance) {
2021-10-17 19:38:16 +03:00
return 0;
2021-10-29 23:44:37 +03:00
}
2021-10-17 19:38:16 +03:00
// Cast is faster than floor and has the same behavior for positive numbers
return static_cast<int>(_instance->_lastFps);
}
void Time::free() {
2021-10-29 23:44:37 +03:00
_validInstance = false;
2021-10-17 19:38:16 +03:00
delete _instance;
Log::log("Time::free(): pointer to 'Time' was freed");
2021-10-17 19:38:16 +03:00
}