update framehistory to use nanosecond timestamps
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
|
||||
#ifndef LLMR_MAP_MAP
|
||||
#define LLMR_MAP_MAP
|
||||
|
||||
@@ -89,6 +90,7 @@ public:
|
||||
inline const Style &getStyle() const { return style; }
|
||||
inline GlyphAtlas &getGlyphAtlas() { return glyphAtlas; }
|
||||
inline uv_loop_t *getLoop() { return loop; }
|
||||
inline time getAnimationTime() const { return animationTime; }
|
||||
|
||||
private:
|
||||
// uv async callbacks
|
||||
|
||||
@@ -6,21 +6,22 @@
|
||||
#include <cmath>
|
||||
|
||||
#include <llmr/platform/platform.hpp>
|
||||
#include <llmr/util/time.hpp>
|
||||
|
||||
namespace llmr {
|
||||
|
||||
struct FrameSnapshot {
|
||||
explicit inline FrameSnapshot(float time, float z) : time(time), z(z) {}
|
||||
float time;
|
||||
explicit inline FrameSnapshot(time timestamp, float z) : timestamp(timestamp), z(z) {}
|
||||
float timestamp;
|
||||
float z;
|
||||
};
|
||||
|
||||
class FrameHistory {
|
||||
public:
|
||||
// Record frame history that will be used to calculate fading params
|
||||
void record(float zoom);
|
||||
void record(time now, float zoom);
|
||||
|
||||
bool needsAnimation(float duration) const;
|
||||
bool needsAnimation(time duration) const;
|
||||
|
||||
public:
|
||||
std::deque<FrameSnapshot> history;
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
#include <llmr/renderer/frame_history.hpp>
|
||||
#include <llmr/util/time.hpp>
|
||||
|
||||
using namespace llmr;
|
||||
|
||||
// Record frame history that will be used to calculate fading params
|
||||
void FrameHistory::record(float zoom) {
|
||||
void FrameHistory::record(time now, float zoom) {
|
||||
// first frame ever
|
||||
if (!history.size()) {
|
||||
history.emplace_back(FrameSnapshot{0.0f, zoom});
|
||||
history.emplace_back(FrameSnapshot{0.0f, zoom});
|
||||
history.emplace_back(FrameSnapshot{0, zoom});
|
||||
history.emplace_back(FrameSnapshot{0, zoom});
|
||||
}
|
||||
|
||||
if (history.size() > 0 || history.back().z != zoom) {
|
||||
history.emplace_back(FrameSnapshot{static_cast<float>((double) util::now() / 1_millisecond), zoom});
|
||||
history.emplace_back(FrameSnapshot{now, zoom});
|
||||
}
|
||||
}
|
||||
|
||||
bool FrameHistory::needsAnimation(const float duration) const {
|
||||
if (!history.size()) return false;
|
||||
bool FrameHistory::needsAnimation(const time duration) const {
|
||||
if (!history.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If we have a value that is older than duration and whose z value is the
|
||||
// same as the most current z value, and if all values inbetween have the
|
||||
@@ -25,8 +26,7 @@ bool FrameHistory::needsAnimation(const float duration) const {
|
||||
const FrameSnapshot &pivot = history.back();
|
||||
|
||||
int i = -1;
|
||||
while ((int)history.size() > i + 1 &&
|
||||
history[i + 1].time + duration < pivot.time) {
|
||||
while ((int)history.size() > i + 1 && history[i + 1].timestamp + duration < pivot.timestamp) {
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
@@ -181,7 +181,7 @@ void Painter::render(const Tile& tile) {
|
||||
return;
|
||||
}
|
||||
|
||||
frameHistory.record(map.getState().getNormalizedZoom());
|
||||
frameHistory.record(map.getAnimationTime(), map.getState().getNormalizedZoom());
|
||||
|
||||
matrix = tile.matrix;
|
||||
glStencilFunc(GL_EQUAL, tile.clip_id, 0xFF);
|
||||
@@ -599,17 +599,17 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons
|
||||
textShader->setZoom((map.getState().getNormalizedZoom() - zoomAdjust) * 10); // current zoom level
|
||||
|
||||
// Label fading
|
||||
const float duration = 300.0f;
|
||||
const float currentTime = util::now() / 1_millisecond;
|
||||
const time duration = 300_milliseconds;
|
||||
const time currentTime = util::now();
|
||||
|
||||
std::deque<FrameSnapshot> &history = frameHistory.history;
|
||||
|
||||
// Remove frames until only one is outside the duration, or until there are only three
|
||||
while (history.size() > 3 && history[1].time + duration < currentTime) {
|
||||
while (history.size() > 3 && history[1].timestamp + duration < currentTime) {
|
||||
history.pop_front();
|
||||
}
|
||||
|
||||
if (history[1].time + duration < currentTime) {
|
||||
if (history[1].timestamp + duration < currentTime) {
|
||||
history[0].z = history[1].z;
|
||||
}
|
||||
|
||||
@@ -625,7 +625,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons
|
||||
|
||||
// Calculate the speed of zooming, and how far it would zoom in terms of zoom levels in one duration
|
||||
float zoomDiff = endingZ - history[1].z,
|
||||
timeDiff = lastFrame.time - history[1].time;
|
||||
timeDiff = lastFrame.timestamp - history[1].timestamp;
|
||||
if (timeDiff > duration) timeDiff = 1;
|
||||
float fadedist = zoomDiff / (timeDiff / duration);
|
||||
|
||||
@@ -633,7 +633,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons
|
||||
|
||||
// At end of a zoom when the zoom stops changing continue pretending to zoom at that speed
|
||||
// bump is how much farther it would have been if it had continued zooming at the same rate
|
||||
float bump = (currentTime - lastFrame.time) / duration * fadedist;
|
||||
float bump = (currentTime - lastFrame.timestamp) / duration * fadedist;
|
||||
|
||||
textShader->setFadeDist(fadedist * 10);
|
||||
textShader->setMinFadeZoom(floor(lowZ * 10));
|
||||
|
||||
Reference in New Issue
Block a user