From e4dce850cc6114c20742a0a46e042669a98d99d0 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 21 Nov 2019 11:35:54 +0100 Subject: [PATCH] + pixelwerte getter/setter --- .ci/clang-tidy.sh | 2 +- .clang-tidy | 3 ++- CMakeLists.txt | 5 ++--- src/Color.cpp | 8 ++++++++ src/Color.hpp | 7 +++++++ src/internal.hpp | 2 +- src/vkvm.cpp | 24 ++++++++++++++++++------ test/color_test.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 test/color_test.cpp diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index e5861a2..f567cec 100755 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -9,7 +9,7 @@ filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' for file in $filelist; do if echo "$file" | grep -q -E ".*(\.cpp|\.h|\.hpp)$" ; then #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. - clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' --color -header-filter='.*,-cpptoml.hpp' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' --color "$file" -- -I. -std=c++14 2>&1)" for tidy_line in $clang_tidy_lib_check; do echo "$tidy_line" | grep -q -v -E "^Error while processing*" if [ $? -eq 1 ]; then diff --git a/.clang-tidy b/.clang-tidy index de644d1..f404c2e 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +1,3 @@ Checks: '*,-llvm-header-guard,-fuchsia*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-pro-bounds-constant-array-index,-bugprone-narrowing-conversions,-cppcoreguidelines-narrowing-conversions,-cppcoreguidelines-pro-type-const-cast,-cppcoreguidelines-pro-type-reinterpret-cast,-hicpp-signed-bitwise,-bugprone-exception-escape,-cppcoreguidelines-pro-type-member-init,-cppcoreguidelines-pro-type-cstyle-cast,-hicpp-member-init,-google-readability-namespace-comments,-llvm-namespace-comment,-cppcoreguidelines-pro-type-vararg,-hicpp-vararg,-modernize-use-trailing-return-type' -WarningsAsErrors: 'true' \ No newline at end of file +WarningsAsErrors: 'true' +HeaderFilterRegex: '.*,-catch.hpp' \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a23c62..828a9ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,7 @@ project(library) set(CMAKE_CXX_STANDARD 14) # enable clang_tidy -set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*") -set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;) +set(CMAKE_CXX_CLANG_TIDY clang-tidy;) file(GLOB_RECURSE SOURCES src/*.cpp) @@ -22,7 +21,7 @@ file(GLOB_RECURSE TESTS test/*.cpp) include_directories(src) include_directories(test) -add_library(library ${SOURCES} ${HEADERS} src/FontType.cpp) +add_library(library ${SOURCES} ${HEADERS}) file(COPY "${CMAKE_SOURCE_DIR}/src/" DESTINATION "${CMAKE_SOURCE_DIR}/include" diff --git a/src/Color.cpp b/src/Color.cpp index 85ac7df..3acc402 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -30,4 +30,12 @@ namespace vkvm { auto Color::setBlue(unsigned char value) -> void { blue = value; } + + bool Color::operator==(const Color &other) const { + return this->blue == other.blue && this->green == other.green && this->red == other.red; + } + + bool Color::operator!=(const Color &other) const { + return this->blue != other.blue || this->green != other.green || this->red != other.red; + } } \ No newline at end of file diff --git a/src/Color.hpp b/src/Color.hpp index a8159b1..909eb9e 100644 --- a/src/Color.hpp +++ b/src/Color.hpp @@ -30,10 +30,17 @@ namespace vkvm { auto setBlue(unsigned char value) -> void; + bool operator==(const Color &other) const ; + + bool operator!=(const Color &other) const ; + }; const static Color black = Color(0, 0, 0); const static Color white = Color(255, 255, 255); + const static Color green = Color(0, 255, 0); + const static Color red = Color(255, 0, 0); + const static Color blue = Color(0, 0, 255); } diff --git a/src/internal.hpp b/src/internal.hpp index a2fe49f..c29e932 100644 --- a/src/internal.hpp +++ b/src/internal.hpp @@ -55,7 +55,7 @@ constexpr int keyboardBufferSize = 16; key_t sharedMemoryKey; int sharedMemorySize; int interruptEntyCount = 256; //NOLINT - int interruptEntrysPerEventType = 8; + int interruptEntrysPerEventType = 8; //NOLINT int reservedSize = 1024; //NOLINT std::vector> eventTable; }; diff --git a/src/vkvm.cpp b/src/vkvm.cpp index 57cf76e..46e5709 100644 --- a/src/vkvm.cpp +++ b/src/vkvm.cpp @@ -23,9 +23,9 @@ namespace vkvm { setHeight(600);// NOLINT setWidth(800);// NOLINT setMousePosition(42, 42);// NOLINT - setBackgroundColor(Color(200, 50, 20));// NOLINT - setForegroundColor(Color(20, 200, 50));// NOLINT - setMode(GraphicMode::RGB);// NOLINT + setBackgroundColor(black); + setForegroundColor(white); + setMode(GraphicMode::RGB); setRedrawInterval(20);// NOLINT setTimerInterruptInterval(10);// NOLINT } @@ -69,10 +69,22 @@ namespace vkvm { } auto getPixel(int x, int y) -> Color { - //TODO(julian): other than RGB colores - //only RGB colores for now unsigned char *ptr = reinterpret_cast(getPixelArea()) + (y * getWidth() + x) * 3; - return {ptr[0], ptr[1], ptr[2]}; + + Color color = {ptr[0], ptr[1], ptr[2]}; + + if(getMode() == GraphicMode::TwoColors || getMode() == GraphicMode::Text) { + if(color != getForegroundColor()) { + color = getBackgroundColor(); + } + } + + else if(getMode() == GraphicMode::Gray_256) { + unsigned char average = (color.getBlue() + color.getGreen() + color.getRed()) / 3; + color = {average, average, average}; + } + + return color; } auto setText(std::string text) -> bool { diff --git a/test/color_test.cpp b/test/color_test.cpp new file mode 100644 index 0000000..9037a40 --- /dev/null +++ b/test/color_test.cpp @@ -0,0 +1,45 @@ +#include "../src/vkvm.hpp" +#include +#include + +TEST_CASE("Colors") { + vkvm::initialize(0); + vkvm::setPixel(10, 10, vkvm::black);//NOLINT + vkvm::setPixel(11, 11, vkvm::white);//NOLINT + vkvm::setPixel(12, 12, vkvm::green);//NOLINT + vkvm::setPixel(13, 13, vkvm::red);//NOLINT + vkvm::setPixel(14, 14, vkvm::blue);//NOLINT + vkvm::setPixel(15, 15, vkvm::Color(66, 77, 88)); //NOLINT + + SECTION("RGB") { + REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT + REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT + REQUIRE(vkvm::getPixel(12, 12) == vkvm::green);//NOLINT + REQUIRE(vkvm::getPixel(13, 13) == vkvm::red);//NOLINT + REQUIRE(vkvm::getPixel(14, 14) == vkvm::blue);//NOLINT + REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(66, 77, 88));//NOLINT + } + + SECTION("TwoColors") { + vkvm::setMode(vkvm::TwoColors); + REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT + REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT + REQUIRE(vkvm::getPixel(12, 12) == vkvm::black);//NOLINT + REQUIRE(vkvm::getPixel(13, 13) == vkvm::black);//NOLINT + REQUIRE(vkvm::getPixel(14, 14) == vkvm::black);//NOLINT + REQUIRE(vkvm::getPixel(15, 15) == vkvm::black);//NOLINT + + } + + SECTION("Gray256") { + vkvm::setMode(vkvm::Gray_256); + REQUIRE(vkvm::getPixel(10, 10) == vkvm::black);//NOLINT + REQUIRE(vkvm::getPixel(11, 11) == vkvm::white);//NOLINT + REQUIRE(vkvm::getPixel(12, 12) == vkvm::Color(85, 85, 85));//NOLINT + REQUIRE(vkvm::getPixel(13, 13) == vkvm::Color(85, 85, 85));//NOLINT + REQUIRE(vkvm::getPixel(14, 14) == vkvm::Color(85, 85, 85));//NOLINT + REQUIRE(vkvm::getPixel(15, 15) == vkvm::Color(77, 77, 77));//NOLINT + } + +} +