From 90948c9edb9aba2f711d76a4e1036ce40677fcbd Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Tue, 7 Jan 2020 11:40:47 +0100 Subject: [PATCH] fixed keyboard buffer error --- src/internal.cpp | 7 ++----- src/vkvm.cpp | 18 ++++++------------ test/buffer_test.cpp | 4 ++-- test/default_test.cpp | 4 ++-- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/internal.cpp b/src/internal.cpp index 36d381c..3ec3b87 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -21,7 +21,7 @@ namespace vkvm { setForegroundColor(white); setRedrawInterval(500); setTimerInterruptInterval(1000); - setFont(FontType::FuturisticBlack); + setFont(FontType::ProFontIIX); } impl.localMemoryWarn = true; } @@ -96,10 +96,7 @@ namespace vkvm { auto buttonPressed(KeyCode keyCode) -> void { lockSharedMemory(); auto reg = getRegisters(); - reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode; - if (reg->keyboardBuffer_index_write >= keyboardBufferSize) { - reg->keyboardBuffer_index_write = 0; - } + reg->keyboardBuffer[(reg->keyboardBuffer_index_write) % keyboardBufferSize] = keyCode; unlockSharedMemory(); } diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 8f25971..4050251 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -319,21 +319,15 @@ namespace vkvm { lockSharedMemory(); auto keyCode = KeyCode(0); auto reg = getRegisters(); - if (reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) { - auto code = reg->keyboardBuffer[reg->keyboardBuffer_index_read++]; + + if(reg->keyboardBuffer_index_read >= reg->keyboardBuffer_index_write) { + auto code = reg->keyboardBuffer[(reg->keyboardBuffer_index_read) % keyboardBufferSize]; keyCode = KeyCode(code); - if (reg->keyboardBuffer_index_read >= keyboardBufferSize) { - reg->keyboardBuffer_index_read = 0; - } }else{ - if(reg->keyboardBuffer_index_read == 0){ - auto code = reg->keyboardBuffer[keyboardBufferSize - 1]; - keyCode = KeyCode(code); - }else{ - auto code = reg->keyboardBuffer[reg->keyboardBuffer_index_read - 1]; - keyCode = KeyCode(code); - } + auto code = reg->keyboardBuffer[(reg->keyboardBuffer_index_read) % keyboardBufferSize]; + keyCode = KeyCode(code); } + unlockSharedMemory(); return keyCode; } diff --git a/test/buffer_test.cpp b/test/buffer_test.cpp index 8daacd8..dd7ea53 100644 --- a/test/buffer_test.cpp +++ b/test/buffer_test.cpp @@ -12,8 +12,8 @@ TEST_CASE("Keyboard Buffer") { } SECTION("read") { - CHECK(vkvm::getLastPressedKey() == vkvm::A); - CHECK(vkvm::getLastPressedKey() == vkvm::B); + CHECK(vkvm::getLastPressedKey() == vkvm::C); + CHECK(vkvm::getLastPressedKey() == vkvm::C); CHECK(vkvm::getLastPressedKey() == vkvm::C); } } \ No newline at end of file diff --git a/test/default_test.cpp b/test/default_test.cpp index b9d046d..a5ca01e 100644 --- a/test/default_test.cpp +++ b/test/default_test.cpp @@ -17,7 +17,7 @@ TEST_CASE("Default values") { REQUIRE(vkvm::getForegroundColor() == vkvm::white); REQUIRE(vkvm::getRedrawInterval() == 500); REQUIRE(vkvm::getTimerInterruptInterval() == 1000); - REQUIRE(vkvm::getFont() == vkvm::FuturisticBlack); + REQUIRE(vkvm::getFont() == vkvm::ProFontIIX); } SECTION("change") { @@ -63,7 +63,7 @@ TEST_CASE("Default values") { REQUIRE(vkvm::getForegroundColor() == vkvm::white); REQUIRE(vkvm::getRedrawInterval() == 500); REQUIRE(vkvm::getTimerInterruptInterval() == 1000); - REQUIRE(vkvm::getFont() == vkvm::FuturisticBlack); + REQUIRE(vkvm::getFont() == vkvm::ProFontIIX); }