fix up the viewer to progress time even when we miss a frame or two basically sapp_frame_duration(); is not the amount of time since the last time frame(void) got called. and once we start missing the odd frame (easy to see on a 240hz monitor) we start losing track of time. this is doing much better than without. i'm sure its not perfect, also my c++ is copy pasta and then a bit of 🔨 . in any case it doesnt look like a big deal at this point Diffs= 6e95e7e1f fix up the viewer to progress time even when we miss a frame or two (#4912)
diff --git a/.rive_head b/.rive_head index 6c2564f..36401a8 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -b90ac530ad934458cfa49acc93622ee9a541b635 +6e95e7e1fcc76cc05d06b8454da4d1558a9c1d82
diff --git a/viewer/src/viewer.cpp b/viewer/src/viewer.cpp index fd62f51..dc343fa 100644 --- a/viewer/src/viewer.cpp +++ b/viewer/src/viewer.cpp
@@ -14,6 +14,7 @@ // Std lib #include <stdio.h> #include <memory> +#include <chrono> std::unique_ptr<ViewerHost> g_Host = ViewerHost::Make(); std::unique_ptr<ViewerContent> g_Content = ViewerContent::TrimPath(""); @@ -25,6 +26,8 @@ void displayStats(); static const int backgroundColor = rive::colorARGB(255, 22, 22, 22); +static std::chrono::time_point<std::chrono::high_resolution_clock> lastTime; +static const float billion = 1000000000.0; static void init(void) { @@ -59,6 +62,8 @@ }, }; + lastTime = std::chrono::high_resolution_clock::now(); + if (!g_Host->init(&state.pass_action, sapp_width(), sapp_height())) { fprintf(stderr, "failed to initialize host\n"); @@ -68,7 +73,11 @@ static void frame(void) { - auto dur = sapp_frame_duration(); + + auto newTime = std::chrono::high_resolution_clock::now(); + auto dur = + std::chrono::duration_cast<std::chrono::nanoseconds>(newTime - lastTime).count() / billion; + lastTime = newTime; g_Host->beforeDefaultPass(g_Content.get(), dur);