From 29e82292a84d27e83b3acc5d4948f81cc934ca68 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 11 Dec 2019 13:03:50 +0100 Subject: [PATCH] + tests for keyboard buffer ~ fix wrong keycode bug ~ reduce footprint of FontType ~ modify default values --- src/FontType.cpp | 24 ------------------------ src/FontType.hpp | 28 +++++----------------------- src/internal.cpp | 6 +++--- src/internal.hpp | 4 ++-- src/vkvm.cpp | 9 ++++----- test/buffer_test.cpp | 19 +++++++++++++++++++ test/default_test.cpp | 16 ++++++++-------- 7 files changed, 41 insertions(+), 65 deletions(-) delete mode 100644 src/FontType.cpp create mode 100644 test/buffer_test.cpp diff --git a/src/FontType.cpp b/src/FontType.cpp deleted file mode 100644 index 5e69054..0000000 --- a/src/FontType.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "FontType.hpp" - -namespace vkvm { - - FontType::FontType(int id, const char * name, int height, int width) noexcept : id(id), name(name), height(height), width(width) { - } - - auto FontType::getId() const -> int { - return id; - } - - auto FontType::getName() const -> std::string { - return std::string(name); - } - - auto FontType::getHeight() const -> int { - return height; - } - - auto FontType::getWidth() const -> int { - return width; - } - -} \ No newline at end of file diff --git a/src/FontType.hpp b/src/FontType.hpp index 349abd1..9ff223f 100644 --- a/src/FontType.hpp +++ b/src/FontType.hpp @@ -2,32 +2,14 @@ #define LIBRARY_FONT_H -#include -#include - namespace vkvm { - class FontType { - private: - int id; - const char * name; - int height; - int width; - - public: - FontType(int id, const char * name, int height, int width) noexcept; - - auto getId() const -> int; - - auto getName() const -> std::string; - - auto getHeight() const -> int; - - auto getWidth() const -> int; - + enum FontType { + FuturisticBlack = 1, + //TODO(julian): find name of this font + NoNameFound = 2, + ProFontIIX = 3 }; - - static const FontType font_1 = FontType(1, "DummyFont", 10, 5); } #endif diff --git a/src/internal.cpp b/src/internal.cpp index f576563..36d381c 100644 --- a/src/internal.cpp +++ b/src/internal.cpp @@ -19,9 +19,9 @@ namespace vkvm { setMousePosition(42, 42); setBackgroundColor(black); setForegroundColor(white); - setRedrawInterval(20); - setTimerInterruptInterval(10); - setFont(FontType(3, "", 0, 0)); + setRedrawInterval(500); + setTimerInterruptInterval(1000); + setFont(FontType::FuturisticBlack); } impl.localMemoryWarn = true; } diff --git a/src/internal.hpp b/src/internal.hpp index ff57774..b4b65c3 100644 --- a/src/internal.hpp +++ b/src/internal.hpp @@ -38,7 +38,7 @@ constexpr int keyboardBufferSize = 16; int textMode_font_height; int mouse_pos_x; int mouse_pos_y; - std::array keyboardBuffer; + std::array keyboardBuffer; int keyboardBuffer_index_write; int keyboardBuffer_index_read; }; @@ -130,7 +130,7 @@ constexpr int keyboardBufferSize = 16; auto buttonPressed(KeyCode keyCode) -> void; /** - * get registered Prosesses for a given event type. + * get registered Processes for a given event type. * @param eventType. */ auto getRegisteredProcesses(EventType eventType) -> std::vector; diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 837500b..1c236bc 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -301,14 +301,12 @@ namespace vkvm { } auto getFont() -> FontType { - //TODO(julian): get font properly - return {getRegisters()->textMode_font, "", 0, 0}; + return FontType(getRegisters()->textMode_font); } auto setFont(const FontType &newValue) -> void { - //TODO(julian): setFont properly lockSharedMemory(); - getRegisters()->textMode_font = newValue.getId(); + getRegisters()->textMode_font = newValue; unlockSharedMemory(); callEvent(EventType::UpdateControlRegisters); } @@ -322,7 +320,8 @@ namespace vkvm { auto keyCode = KeyCode(0); auto reg = getRegisters(); if (reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) { - keyCode = static_cast(reg->keyboardBuffer[reg->keyboardBuffer_index_read++]); + auto code = reg->keyboardBuffer[reg->keyboardBuffer_index_read++]; + keyCode = KeyCode(code); if (reg->keyboardBuffer_index_read >= keyboardBufferSize) { reg->keyboardBuffer_index_read = 0; } diff --git a/test/buffer_test.cpp b/test/buffer_test.cpp new file mode 100644 index 0000000..8daacd8 --- /dev/null +++ b/test/buffer_test.cpp @@ -0,0 +1,19 @@ +#include "../src/internal.hpp" +#include "../src/vkvm.hpp" +#include + + +TEST_CASE("Keyboard Buffer") { + SECTION("write") { + vkvm::setLogLevel(vkvm::DEBUG); + vkvm::buttonPressed(vkvm::A); + vkvm::buttonPressed(vkvm::B); + vkvm::buttonPressed(vkvm::C); + } + + SECTION("read") { + CHECK(vkvm::getLastPressedKey() == vkvm::A); + CHECK(vkvm::getLastPressedKey() == vkvm::B); + CHECK(vkvm::getLastPressedKey() == vkvm::C); + } +} \ No newline at end of file diff --git a/test/default_test.cpp b/test/default_test.cpp index eac0105..b9d046d 100644 --- a/test/default_test.cpp +++ b/test/default_test.cpp @@ -15,9 +15,9 @@ TEST_CASE("Default values") { REQUIRE(vkvm::getMousePosition().y == 42); REQUIRE(vkvm::getBackgroundColor() == vkvm::black); REQUIRE(vkvm::getForegroundColor() == vkvm::white); - REQUIRE(vkvm::getRedrawInterval() == 20); - REQUIRE(vkvm::getTimerInterruptInterval() == 10); - REQUIRE(vkvm::getFont().getId() == 3); + REQUIRE(vkvm::getRedrawInterval() == 500); + REQUIRE(vkvm::getTimerInterruptInterval() == 1000); + REQUIRE(vkvm::getFont() == vkvm::FuturisticBlack); } SECTION("change") { @@ -31,7 +31,7 @@ TEST_CASE("Default values") { vkvm::setForegroundColor(vkvm::blue); vkvm::setRedrawInterval(1); vkvm::setTimerInterruptInterval(2); - vkvm::setFont(vkvm::FontType(5, "", 5, 5)); + vkvm::setFont(vkvm::ProFontIIX); @@ -46,7 +46,7 @@ TEST_CASE("Default values") { REQUIRE(vkvm::getForegroundColor() == vkvm::blue); REQUIRE(vkvm::getRedrawInterval() == 1); REQUIRE(vkvm::getTimerInterruptInterval() == 2); - REQUIRE(vkvm::getFont().getId() == 5); + REQUIRE(vkvm::getFont() == vkvm::ProFontIIX); } @@ -61,9 +61,9 @@ TEST_CASE("Default values") { REQUIRE(vkvm::getMousePosition().y == 42); REQUIRE(vkvm::getBackgroundColor() == vkvm::black); REQUIRE(vkvm::getForegroundColor() == vkvm::white); - REQUIRE(vkvm::getRedrawInterval() == 20); - REQUIRE(vkvm::getTimerInterruptInterval() == 10); - REQUIRE(vkvm::getFont().getId() == 3); + REQUIRE(vkvm::getRedrawInterval() == 500); + REQUIRE(vkvm::getTimerInterruptInterval() == 1000); + REQUIRE(vkvm::getFont() == vkvm::FuturisticBlack); }