From ef06c8eca4830699e412223e84fe33ae787b48ce Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Tue, 21 Mar 2017 10:47:32 +0100 Subject: [PATCH] Input: Hack around desync with mkxpRawKeyStates Make sure that when mkxpRawKeyStates was called, the Input module will work off of the same queried snapshot of pressed keys during the next update call. --- binding-mri/binding-mri.cpp | 15 ++++++++++++++- src/input.cpp | 16 +++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 4b60567..7d9865a 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -214,12 +214,25 @@ RB_METHOD(mkxpPuts) return Qnil; } +static uint8_t keyStatesMirrorAry[SDL_NUM_SCANCODES]; + +// points at either EventThread::keyStates (which is continuously updated) +// or keyStatesMirrorAry (which is a frame-local snapshot of the former) +uint8_t *keyStatesMirror = EventThread::keyStates; + RB_METHOD(mkxpRawKeyStates) { RB_UNUSED_PARAM; VALUE str = rb_str_new(0, sizeof(EventThread::keyStates)); - memcpy(RSTRING_PTR(str), EventThread::keyStates, sizeof(EventThread::keyStates)); + + // copy keystates to a shadow array so the Input module uses the same snapshot + memcpy(keyStatesMirrorAry, EventThread::keyStates, sizeof(keyStatesMirrorAry)); + memcpy(RSTRING_PTR(str), keyStatesMirrorAry, sizeof(keyStatesMirrorAry)); + + // let the Input module use our snapshot instead (Input will take care of resetting + // the pointer back to the EventThread array) + keyStatesMirror = keyStatesMirrorAry; return str; } diff --git a/src/input.cpp b/src/input.cpp index d7978bd..32bb8cb 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -66,6 +66,9 @@ struct Binding Input::ButtonCode target; }; +// binding-mri.cpp +extern uint8_t *keyStatesMirror; + /* Keyboard binding */ struct KbBinding : public Binding { @@ -80,14 +83,14 @@ struct KbBinding : public Binding { /* Special case aliases */ if (source == SDL_SCANCODE_LSHIFT) - return EventThread::keyStates[source] - || EventThread::keyStates[SDL_SCANCODE_RSHIFT]; + return keyStatesMirror[source] + || keyStatesMirror[SDL_SCANCODE_RSHIFT]; if (source == SDL_SCANCODE_RETURN) - return EventThread::keyStates[source] - || EventThread::keyStates[SDL_SCANCODE_KP_ENTER]; + return keyStatesMirror[source] + || keyStatesMirror[SDL_SCANCODE_KP_ENTER]; - return EventThread::keyStates[source]; + return keyStatesMirror[source]; } bool sourceRepeatable() const @@ -607,6 +610,9 @@ void Input::update() /* Poll all bindings */ p->pollBindings(repeatCand); + // reset keystate source from potential mirror to EventThread source array + keyStatesMirror = EventThread::keyStates; + /* Check for new repeating key */ if (repeatCand != None && repeatCand != p->repeating) {