fenster icon indicating copy to clipboard operation
fenster copied to clipboard

Only sleep for the rest of the frame budget

Open tekknolagi opened this issue 1 year ago • 1 comments

Previously, we were sleeping as long as the paint took. Now, we only sleep for the rest of the allotted budget (if any).

Thanks to @lgarbarini for helping debug.

tekknolagi avatar Aug 11 '24 19:08 tekknolagi

I think this is still slightly off; it doesn't take into account the amount of time slept for and so each alternate loop sleeps for almost no time.

With the following test code I'd expect the printed delta to be ~1000000us each iteration (1 fps).

#include "fenster.h"
#include <chrono>
#include <iostream>

int main()
{
  Fenster window { 100, 100, "Window" };

  using std::chrono::steady_clock;
  using std::chrono::microseconds;
  using std::chrono::duration_cast;

  auto start = steady_clock::now();

  while (window.loop(1))
  {
    auto end = steady_clock::now();
    auto delta = end - start;
    start = end;

    std::cout << "delta: "
              << duration_cast<microseconds>(delta).count()
              << "us"
              << std::endl;
  }
}

However I actually get this:

$ ./a.out
delta: 999714us
delta: 65us
delta: 1000747us
delta: 56us
delta: 1000090us
delta: 59us
delta: 1000257us
delta: 56us
delta: 1000382us
delta: 57us

Which I fixed by changing fenster.h:359 from this->now = t; to this->now = fenster_time();.

$ ./a.out
delta: 999186us
delta: 1000195us
delta: 1000718us
delta: 1000742us
delta: 1000230us
delta: 1001481us
delta: 999216us
delta: 1000225us
delta: 1000308us

daveth avatar Jan 14 '25 16:01 daveth