Skip to content

Commit

Permalink
Update buttons pressed/released before calling update
Browse files Browse the repository at this point in the history
Instead of up to 1000s of times a second.
  • Loading branch information
Daft-Freak authored and Gadgetoid committed Jan 2, 2021
1 parent 8bf872f commit abc05c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
9 changes: 7 additions & 2 deletions 32blit/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions 32blit/engine/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit abc05c6

Please sign in to comment.