diff --git a/core/internal.h b/core/internal.h index f6a25f2..486c2e1 100644 --- a/core/internal.h +++ b/core/internal.h @@ -130,6 +130,8 @@ typedef struct reaktor_damage { int kw, kh; struct nk_color clear; int checking; + unsigned idle; + Uint64 drawn; float peak; unsigned long partial, full, mismatched; } reaktor_damage; diff --git a/core/render/damage.c b/core/render/damage.c index 26c1b10..39c00b9 100644 --- a/core/render/damage.c +++ b/core/render/damage.c @@ -2,6 +2,7 @@ #define LOOKAHEAD 32 #define SERIAL "reaktor.serial" +#define IDLE_MS 1000 typedef struct sink { reaktor_damage_list *l; @@ -587,11 +588,29 @@ drop(App *app) SDL_SetRenderScale(app->ren, app->render_scale, app->render_scale); } +/* K exists to draw the next frame over; an idle window does not need it. */ +static void +idle_due(App *app, void *user) +{ + reaktor_damage *d = &app->damage; + Uint64 idle = SDL_GetTicks() - d->drawn; + + (void)user; + d->idle = 0; + if (!d->k) return; + if (idle < IDLE_MS) + d->idle = reaktor_timer(app, (int)(IDLE_MS - idle), 0, idle_due, NULL); + else + drop(app); +} + void reaktor_damage_free(App *app) { int i; + if (app->damage.idle) reaktor_timer_stop(app->damage.idle); + app->damage.idle = 0; if (app->ren) drop(app); for (i = 0; i < 2; i++) { SDL_free(app->damage.list[i].item); @@ -736,6 +755,8 @@ reaktor_damage_render(App *app) SDL_RenderTexture(app->ren, d->k, NULL, NULL); d->now = !d->now; d->clear = app->clear; + d->drawn = SDL_GetTicks(); + if (!d->idle) d->idle = reaktor_timer(app, IDLE_MS, 0, idle_due, NULL); return 1; } diff --git a/tests/damagetest.c b/tests/damagetest.c index 9655f3e..13640dd 100644 --- a/tests/damagetest.c +++ b/tests/damagetest.c @@ -503,6 +503,17 @@ script(reaktor_test *t, void *user) for (i = 0; i < 30; i++) move(t, 4.0f + i * 6.0f, 14.0f); reaktor_test_check(t, d->partial > partial, "back at scale 1, partial frames again"); + SDL_Delay(1300); + reaktor_test_settle(t); + reaktor_test_check(t, !d->k, "an idle second frees the last frame"); + partial = d->partial; + full = d->full; + move(t, 600.0f, 400.0f); + move(t, 30.0f, 20.0f); + move(t, 110.0f, 20.0f); + reaktor_test_check(t, d->k && d->full == full + 1 && d->partial > partial, + "after an idle second, one full frame and then partial ones"); + SDL_snprintf(log, sizeof log, "%lu partial and %lu full frames compared", d->partial, d->full); reaktor_test_check(t, d->partial > 50, log);