framework2

Форк
0
80 строк · 1.7 Кб
1
#include "ofFpsCounter.h"
2

3
ofFpsCounter::ofFpsCounter()
4
:nFrameCount(0)
5
,then(ofGetCurrentTime())
6
,fps(0)
7
,lastFrameTime(0)
8
,filteredTime(0)
9
,filterAlpha(0.9){}
10

11

12

13
ofFpsCounter::ofFpsCounter(double targetFPS)
14
:nFrameCount(0)
15
,then(ofGetCurrentTime())
16
,fps(targetFPS)
17
,lastFrameTime(0)
18
,filteredTime(0)
19
,filterAlpha(0.9){}
20

21
void ofFpsCounter::newFrame(){
22
	auto now = ofGetCurrentTime();
23
	update(now.getAsSeconds());
24
	timestamps.push(now.getAsSeconds());
25

26
	lastFrameTime = now - then;
27
	uint64_t filtered = filteredTime.count() * filterAlpha + lastFrameTime.count() * (1-filterAlpha);
28
	filteredTime = std::chrono::nanoseconds(filtered);
29
	then = now;
30
	nFrameCount++;
31
}
32

33
void ofFpsCounter::update(){
34
	auto now = ofGetCurrentTime();
35
	update(now.getAsSeconds());
36
}
37

38
void ofFpsCounter::update(double now){
39
	while(!timestamps.empty() && timestamps.front() + 2 < now){
40
		timestamps.pop();
41
	}
42

43
	auto diff = 0.0;
44
	if(!timestamps.empty() && timestamps.front() + 0.5 < now){
45
		diff = now - timestamps.front();
46
	}
47
	if(diff>0.0){
48
		fps = timestamps.size() / diff;
49
	}else{
50
		fps = timestamps.size();
51
	}
52
}
53

54
double ofFpsCounter::getFps() const{
55
	return fps;
56
}
57

58
uint64_t ofFpsCounter::getNumFrames() const{
59
	return nFrameCount;
60
}
61

62
uint64_t ofFpsCounter::getLastFrameNanos() const{
63
	return lastFrameTime.count();
64
}
65

66
double ofFpsCounter::getLastFrameSecs() const{
67
	return std::chrono::duration<double>(lastFrameTime).count();
68
}
69

70
uint64_t ofFpsCounter::getLastFrameFilteredNanos() const{
71
	return lastFrameTime.count();
72
}
73

74
double ofFpsCounter::getLastFrameFilteredSecs() const{
75
	return std::chrono::duration<double>(filteredTime).count();
76
}
77

78
void ofFpsCounter::setFilterAlpha(float alpha){
79
	filterAlpha = alpha;
80
}
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.