Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::chrono for FPS setting #7866

Closed
dimitre opened this issue Jan 18, 2024 · 2 comments
Closed

std::chrono for FPS setting #7866

dimitre opened this issue Jan 18, 2024 · 2 comments

Comments

@dimitre
Copy link
Member

dimitre commented Jan 18, 2024

I'm working on some experiments on using internally std::chrono for FPS timing inside OF Core.
I think we have some advantages as using the same code for all platforms, and having better precision.
here is an experiment with a fps counter

#include <chrono>
using namespace std::chrono;
using namespace std::chrono_literals;

struct fpsCounter {
public:
	int nAverages = 20;
	time_point<steady_clock> lastTick;
	using space = std::chrono::duration<long double, std::nano>;
	steady_clock::duration onesec = 1s;
	vector <space> intervals;
	space interval;
	space average;
	bool firstTick = true;
	int cursor = 0;

	void tick() {
		if (firstTick) {
			firstTick = false;
			lastTick = steady_clock::now();
			return;
		}

		interval = steady_clock::now() - lastTick;
		lastTick = steady_clock::now();
		if (intervals.size() < nAverages) {
			intervals.emplace_back(interval);
		} else {
			intervals[cursor] = interval;
			cursor = (cursor+1)%nAverages;
		}
	}
	
	float get() {
//		average = std::reduce(intervals.begin(), intervals.end())/intervals.size();
//		return onesec / average;
		average = std::reduce(intervals.begin(), intervals.end());
		return intervals.size() * onesec / average;
	}
} count;

it can be used like this

void ofApp::update() {
	count.tick();
	cout << "fps " << ofGetFrameRate() << endl;
	cout << "get " << count.get() << endl;
	cout << "---" << endl;
}
@dimitre
Copy link
Member Author

dimitre commented Jan 23, 2024

related:

@dimitre
Copy link
Member Author

dimitre commented May 14, 2024

closed by

@dimitre dimitre closed this as completed May 14, 2024
@dimitre dimitre changed the title std::chrono for FPS setting and counting std::chrono for FPS setting May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant