From 11fe11568e27e5fc7d5d58ee64cb83b185240daf Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Wed, 16 Jan 2019 08:36:29 -0500 Subject: limit step events to 100ms --- src/sdl/solve.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/sdl') diff --git a/src/sdl/solve.c b/src/sdl/solve.c index 0fee274..a3e27e8 100644 --- a/src/sdl/solve.c +++ b/src/sdl/solve.c @@ -11,6 +11,7 @@ struct solve_t_ { // thread-private copy of context sok_ctx_t ctx; + // event type for custom SDL SOLVE_* events Uint32 solve_event_type; // number of moves at call to solve() @@ -18,6 +19,9 @@ struct solve_t_ { // number of times the on_step cb has been invoked size_t num_steps; + + // last time the SOLVE_EVENT_STEP event was pushed + Uint32 last_step_event_ticks; // result of solve bool result; @@ -65,7 +69,6 @@ solve_on_step( data->num_steps++; // cache number of steps and cancel state - const size_t num_steps = data->num_steps; const bool cancel = data->cancel; // unlock mutex @@ -73,7 +76,15 @@ solve_on_step( die("SDL_UnlockMutex(): %s", SDL_GetError()); } - if ((num_steps % 100) == 0) { + // get timestamp and next update timestamp + const Uint32 ticks = SDL_GetTicks(), + next_step_ticks = data->last_step_event_ticks + 100; + + // limit step events to every 100ms + if (SDL_TICKS_PASSED(ticks, next_step_ticks)) { + // cache timestamp + data->last_step_event_ticks = ticks; + // push event push_event(data, SOLVE_EVENT_STEP, NULL); } @@ -140,6 +151,7 @@ solve( data->solve_event_type = solve_event_type; data->old_num_moves = ctx->num_moves; data->num_steps = 0; + data->last_step_event_ticks = SDL_GetTicks(); data->result = false; data->cancel = false; -- cgit v1.2.3