From abc05c68b836ba7c1d1465130a20eee146b570e4 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Tue, 22 Dec 2020 15:50:12 +0000 Subject: [PATCH 1/2] Update buttons pressed/released before calling update Instead of up to 1000s of times a second. --- 32blit/engine/engine.cpp | 9 +++++++-- 32blit/engine/input.hpp | 6 +----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/32blit/engine/engine.cpp b/32blit/engine/engine.cpp index d4173767e..8883f7359 100644 --- a/32blit/engine/engine.cpp +++ b/32blit/engine/engine.cpp @@ -77,10 +77,15 @@ namespace blit { // catch up on updates if any pending pending_update_time += (time - last_tick_time); while (pending_update_time >= update_rate_ms) { + // button state changes + uint32_t changed = api.buttons.state ^ api.buttons.last_state; + + api.buttons.pressed = changed & api.buttons.state; + api.buttons.released = changed & api.buttons.last_state; + api.buttons.last_state = api.buttons.state; + update(time - pending_update_time); // create fake timestamp that would have been accurate for the update event pending_update_time -= update_rate_ms; - - api.buttons.pressed = api.buttons.released = 0; } last_tick_time = time; diff --git a/32blit/engine/input.hpp b/32blit/engine/input.hpp index b82dae5d7..e4d18e70a 100644 --- a/32blit/engine/input.hpp +++ b/32blit/engine/input.hpp @@ -21,11 +21,6 @@ namespace blit { struct ButtonState { ButtonState &operator=(uint32_t v) { - uint32_t changed = state ^ v; - - pressed |= changed & v; - released |= changed & state; - state = v; return *this; @@ -37,6 +32,7 @@ namespace blit { uint32_t state; uint32_t pressed, released; // state change since last update + uint32_t last_state = 0; }; extern bool pressed(uint32_t button); From f0dd0637076404176af7252fc34c7bbc7e20ac9e Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Sat, 2 Jan 2021 15:24:41 +0000 Subject: [PATCH 2/2] Avoid API break with last_state --- 32blit/engine/engine.cpp | 7 ++++--- 32blit/engine/input.hpp | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/32blit/engine/engine.cpp b/32blit/engine/engine.cpp index 8883f7359..519721057 100644 --- a/32blit/engine/engine.cpp +++ b/32blit/engine/engine.cpp @@ -64,6 +64,7 @@ namespace blit { uint32_t pending_render_time = 0; uint32_t last_tick_time = 0; + uint32_t last_state = 0; bool tick(uint32_t time) { if (last_tick_time == 0) { @@ -78,11 +79,11 @@ namespace blit { pending_update_time += (time - last_tick_time); while (pending_update_time >= update_rate_ms) { // button state changes - uint32_t changed = api.buttons.state ^ api.buttons.last_state; + uint32_t changed = api.buttons.state ^ last_state; api.buttons.pressed = changed & api.buttons.state; - api.buttons.released = changed & api.buttons.last_state; - api.buttons.last_state = api.buttons.state; + api.buttons.released = changed & last_state; + last_state = api.buttons.state; update(time - pending_update_time); // create fake timestamp that would have been accurate for the update event pending_update_time -= update_rate_ms; diff --git a/32blit/engine/input.hpp b/32blit/engine/input.hpp index e4d18e70a..6e5c423ed 100644 --- a/32blit/engine/input.hpp +++ b/32blit/engine/input.hpp @@ -32,7 +32,6 @@ namespace blit { uint32_t state; uint32_t pressed, released; // state change since last update - uint32_t last_state = 0; }; extern bool pressed(uint32_t button);