shooter/engine/utils/Time.cpp

116 lines
2.6 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-09-13 15:53:43 +03:00
2021-10-17 19:38:16 +03:00
void Time::init() {
2021-11-09 22:54:20 +03:00
delete _instance;
2021-10-17 19:38:16 +03:00
_instance = new Time();
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-11-09 22:54:20 +03:00
if (_instance == nullptr) {
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-11-09 22:54:20 +03:00
if (_instance == nullptr) {
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-11-09 22:54:20 +03:00
if (_instance == nullptr) {
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;
2022-02-23 07:51:53 +03:00
if (_instance->_deltaTime > 10) {
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-11-09 22:54:20 +03:00
if (_instance == nullptr) {
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::startTimer(const std::string &timerName) {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
return;
}
_instance->_timers.insert({timerName, Timer()});
_instance->_timers[timerName].start();
}
void Time::stopTimer(const std::string &timerName) {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
return;
}
if(_instance->_timers.count(timerName) > 0) {
_instance->_timers[timerName].stop();
}
}
double Time::elapsedTimerMilliseconds(const std::string &timerName) {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
return 0;
}
if(_instance->_timers.count(timerName) > 0) {
return _instance->_timers[timerName].elapsedMilliseconds();
}
return 0;
}
double Time::elapsedTimerSeconds(const std::string &timerName) {
2021-11-09 22:54:20 +03:00
if (_instance == nullptr) {
return 0;
}
if(_instance->_timers.count(timerName) > 0) {
return _instance->_timers[timerName].elapsedSeconds();
}
return 0;
}
2021-10-17 19:38:16 +03:00
void Time::free() {
delete _instance;
2021-11-09 22:54:20 +03:00
_instance = nullptr;
Log::log("Time::free(): pointer to 'Time' was freed");
2021-10-17 19:38:16 +03:00
}