openFrameworks
openFrameworks copied to clipboard
[feature request] better (deterministic) frame render timepoint
in need of this function:
/// \brief predicts the (future) timepoint at which the frame
/// being drawn is scheduled (or expected) to appear.
/// This is in alternative to ofGetElapsedTimef() usage for
/// animations (specifically when relying on isochronous steps).
///
/// \return in floating-point seconds the expected paint time
/// of the frame currently being prepared (timepoint
/// at which the frame should be painted on screen)
float ofGetExpectedPaintTimef() const
what is the best way to implement this? (knowing that calling ofGetElapsedTimef() at the beginning of update() is unreliable) https://github.com/openframeworks/openFrameworks/issues/7752#issuecomment-1817035425
not sure if it helps you but I'm using a function to animate proportionally to the last animation frame (FPS independent) using this code based in std::chrono::steady_clock. works well for my application
#include <chrono>
using namespace std::chrono;
using namespace std::chrono_literals;
// Genérico pra Uso no featureCam.
struct incrementador {
std::unordered_map <std::string, float> incrementadorTemporal;
using space = std::chrono::duration<long double, std::nano>;
space interval;
float mult;
std::unordered_map <std::string, std::chrono::time_point<steady_clock>> lastTick;
float inc(const std::string & uniqueId, float amount) {
if (incrementadorTemporal.find(uniqueId) == incrementadorTemporal.end() ) {
reset(uniqueId);
}
if (lastTick.find(uniqueId) != lastTick.end()) {
interval = steady_clock::now() - lastTick[uniqueId];
mult = duration_cast<milliseconds>(interval).count()/33.0f;
incrementadorTemporal[uniqueId] += mult * amount;
}
lastTick[uniqueId] = std::chrono::steady_clock::now();
return incrementadorTemporal[uniqueId];
}
void reset(const std::string & uniqueId) {
incrementadorTemporal[uniqueId] = 0;
}
};