aboutsummaryrefslogtreecommitdiff
path: root/src/sdl
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-16 08:36:29 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-16 08:36:29 -0500
commit11fe11568e27e5fc7d5d58ee64cb83b185240daf (patch)
treedf7e8e029c00632daf0f2fbd36d3e0cb9270d7d6 /src/sdl
parent63215211350215c2971090cc0e3807e8941a0e37 (diff)
downloadsok-11fe11568e27e5fc7d5d58ee64cb83b185240daf.tar.bz2
sok-11fe11568e27e5fc7d5d58ee64cb83b185240daf.zip
limit step events to 100ms
Diffstat (limited to 'src/sdl')
-rw-r--r--src/sdl/solve.c16
1 files changed, 14 insertions, 2 deletions
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;