diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh new file mode 100644 index 0000000..85e0e6f --- /dev/null +++ b/.ci/clang-tidy.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +echo "Doing clang-tidy..." +bool=false +# explicitly set IFS to contain only a line feed +IFS=' +' +filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' -prune \) -type f ! -name "$(printf "*\n*")")" +for file in $filelist; do + if echo "$file" | grep -q -E ".*(\.cpp|\.hpp)$" ; then + #Extra check missing dependencies due to clang-tidy doesn't toggle exit code. + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' "$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 + bool=true + fi + echo "$tidy_line" | grep -q -v -E ".* error: .*" + if [ $? -eq 1 ]; then + bool=true + fi + echo "$tidy_line" + done + fi +done +if $bool; then + exit 1 +else + echo "No clang-tidy errors found." +fi + +exit 0 diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..d7cc14e --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,2 @@ +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,-clang-diagnostic-error' +WarningsAsErrors: 'true' diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..27763b0 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,76 @@ +--- + +image: samueldebruyn/debian-git:latest + +stages: + - style + - test + - build + + +clang_tidy: + image: joethei/clang_tidy + stage: style + tags: + - docker-ci + script: + - mkdir current + - ls -d .[!.]* | grep -v current | xargs mv -t current + - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git + - mkdir library/build + - cd library/build + - cmake .. + - make + - cd ../../current/.ci + - sh clang-tidy.sh + +make_test: + stage: test + tags: + - docker-ci + script: + - apt-get update + - apt-get install -y clang make cmake clang-tidy + - mkdir current + - ls | grep -v current | xargs mv -t current + - git clone https://github.com/catchorg/Catch2.git + - cd Catch2 + - cmake -Bbuild -H. -DBUILD_TESTING=OFF + - cmake --build build/ --target install + - cd .. + - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git + - mkdir library/build + - cd library/build + - cmake .. + - make + - cd ../../current + - mkdir build + - cd build + - cmake .. + - make + - make test + +cmake_build: + stage: build + tags: + - docker-ci + script: + - apt-get update + - apt-get install -y clang make cmake clang-tidy + - mkdir current + - ls | grep -v current | xargs mv -t current + - git clone https://github.com/catchorg/Catch2.git + - cd Catch2 + - cmake -Bbuild -H. -DBUILD_TESTING=OFF + - cmake --build build/ --target install + - cd .. + - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.repo.digitech.hs-emden-leer.de/link/projekte/ws19/vkvm-new/library.git + - mkdir library/build + - cd library/build + - cmake .. + - make + - cd ../../current + - mkdir build + - cd build + - cmake .. + - make diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b53bd3..a0848e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,15 +8,19 @@ if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") endif() set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "bin" "doc" "CMakeFiles" "lib" "include") + +set(CMAKE_C_COMPILER "clang") +set(CMAKE_CXX_COMPILER "clang++") + project(Demo) 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=.;-checks=*;) +set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;) file(GLOB_RECURSE SOURCES src/*.cpp) -file(GLOB_RECURSE HEADERS src/*.h) +file(GLOB_RECURSE HEADERS src/*.hpp) file(GLOB_RECURSE TESTS test/*.cpp) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") @@ -30,6 +34,7 @@ target_link_libraries(Demo ${LIB_PATH}/lib/liblibrary.a) enable_testing() find_package(Catch2 REQUIRED) add_executable(UnitTests ${SOURCES} ${HEADERS} ${TESTS}) +target_link_libraries(UnitTests ${LIB_PATH}/lib/liblibrary.a) target_link_libraries(UnitTests Catch2::Catch2) include(CTest) diff --git a/main/main.cpp b/main/main.cpp index 53edc26..445a9a6 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1 @@ -#include "../src/demo.h" - -int main() { - return test(); -} \ No newline at end of file +#include "../src/Bitmap.hpp" #include "../src/MathFunctions.hpp" #include "vkvm.hpp" #include "internal.hpp" #include //time to sleep for in seconds constexpr int sleepTime = 3; void displayImage(const std::string &file, vkvm::GraphicMode graphicMode) { vkvm::setMode(graphicMode); Bitmap image(file); vkvm::setWidth(image.getWidth()); vkvm::setHeight(image.getHeight()); for (int x = 0; x < vkvm::getWidth(); x++) { for (int y = 0; y < vkvm::getHeight(); y++) { unsigned int hex = image.getPixel(x, y); vkvm::Color color = vkvm::Color(hex); vkvm::setPixel(x, y, color); } } } void setColor(vkvm::Color color) { for (int x = 0; x < vkvm::getWidth(); ++x) { for (int y = 0; y < vkvm::getHeight(); ++y) { vkvm::setPixel(x, y, color); } } } void displayImage(const std::string &file) { displayImage(file, vkvm::GraphicMode::RGB); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::Gray_256); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::TwoColors); sleep(sleepTime); } int main() { bool running = true; vkvm::initialize(0); while(running) { displayImage("../res/P6.bmp"); sleep(sleepTime); vkvm::setBackgroundColor(vkvm::red); vkvm::setForegroundColor(vkvm::blue); sleep(sleepTime); vkvm::setBackgroundColor(vkvm::black); vkvm::setForegroundColor(vkvm::white); displayImage("../res/P8.bmp"); displayImage("../res/P9.bmp"); displayImage("../res/P10.bmp"); vkvm::setText("Hello World"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::FuturisticBlack); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); vkvm::setText("Hello World, lorem ipsum dolor sit amet"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::ProFontIIX); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); vkvm::setFont(vkvm::FontType::Unifont); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); vkvm::setText("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::RGB); mandelbrot(); sleep(sleepTime); setColor(vkvm::black); sleep(sleepTime); } } \ No newline at end of file diff --git a/res/P10.bmp b/res/P10.bmp new file mode 100644 index 0000000..bca427e Binary files /dev/null and b/res/P10.bmp differ diff --git a/res/P6.bmp b/res/P6.bmp new file mode 100644 index 0000000..dbaa2ac Binary files /dev/null and b/res/P6.bmp differ diff --git a/res/P8.bmp b/res/P8.bmp new file mode 100644 index 0000000..9c8e492 Binary files /dev/null and b/res/P8.bmp differ diff --git a/res/P9.bmp b/res/P9.bmp new file mode 100644 index 0000000..2407534 Binary files /dev/null and b/res/P9.bmp differ diff --git a/res/font1.bmp b/res/font1.bmp new file mode 100644 index 0000000..56e6810 Binary files /dev/null and b/res/font1.bmp differ diff --git a/res/test.bmp b/res/test.bmp new file mode 100644 index 0000000..a9a6d72 Binary files /dev/null and b/res/test.bmp differ diff --git a/res/test1.bmp b/res/test1.bmp new file mode 100644 index 0000000..40a274d Binary files /dev/null and b/res/test1.bmp differ diff --git a/res/test2.bmp b/res/test2.bmp new file mode 100644 index 0000000..24c7409 Binary files /dev/null and b/res/test2.bmp differ diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp new file mode 100644 index 0000000..ca78fa0 --- /dev/null +++ b/src/Bitmap.cpp @@ -0,0 +1,63 @@ +#include "Bitmap.hpp" +#include +#include + +Bitmap::Bitmap() { + offset = 0; + width = 0; + height = 0; + bitsPerPixel = 0; +} + +Bitmap::Bitmap(const std::string &file) { + offset = 0; + width = 0; + height = 0; + bitsPerPixel = 0; + load(file); +} + +void Bitmap::load(const std::string &file) { + std::ifstream stream; + stream.open(file); + if(stream.is_open()){ + std::string str((std::istreambuf_iterator(stream)),std::istreambuf_iterator()); + data.resize(str.size()); + for(int i = 0; i < str.size();i++){ + data[i] = str[i]; + } + offset = static_cast(*reinterpret_cast(&data[10])); + width = static_cast(*reinterpret_cast(&data[18])); + height = static_cast(*reinterpret_cast(&data[22])); + bitsPerPixel = static_cast(*reinterpret_cast(&data[28])); + } +} + +int Bitmap::getWidth() { + return width; +} + +int Bitmap::getHeight() { + return height; +} + +char *Bitmap::getData() { + return &data[offset]; +} + +int Bitmap::getBitsPerPixel() { + return bitsPerPixel; +} + +unsigned int Bitmap::getPixel(int x, int y) { + unsigned int pixel = 0; + char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8); + for(int i = 0; i < getBitsPerPixel() / 8;i++){ + *((char*)&pixel+i) = ptr[i];//NOLINT + } + if(pixel != 0){ + return pixel; + } + + return pixel; +} diff --git a/src/Bitmap.hpp b/src/Bitmap.hpp new file mode 100644 index 0000000..83a22ca --- /dev/null +++ b/src/Bitmap.hpp @@ -0,0 +1,24 @@ +#ifndef DEMO_BITMAP_HPP +#define DEMO_BITMAP_HPP + +#include +#include + +class Bitmap { +public: + Bitmap(); + explicit Bitmap(const std::string &file); + void load(const std::string &file); + int getWidth(); + int getHeight(); + char *getData(); + int getBitsPerPixel(); + unsigned int getPixel(int x, int y); +private: + int width; + int height; + int offset; + int bitsPerPixel; + std::vector data; +}; +#endif //DEMO_BITMAP_HPP diff --git a/src/MathFunctions.cpp b/src/MathFunctions.cpp new file mode 100644 index 0000000..9900ef4 --- /dev/null +++ b/src/MathFunctions.cpp @@ -0,0 +1,78 @@ +#include "vkvm.hpp" +#include +#include + +long double map(long double value, long double inputMin, long double inputMax, long double outputMin, long double outputMax) { + return (value - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin; +} + +void mandelbrot() { + + int width = 800; + int height = 600; + + vkvm::setWidth(width); + vkvm::setHeight(height); + vkvm::setRedrawInterval(-1); + + long double min = -2.84; + long double max = 1; + int maxIterations = 200; + double factor = 1; + + for (int count = 0; count < 50; count++) { + max -= 0.1 * factor; + min += 0.15 * factor; + factor *= 0.9349; + maxIterations += 5; + + if(count > 30) { + maxIterations *= 1.02; + } + + for (int x = 0; x < width; ++x) { + for (int y = 0; y < height; ++y) { + + long double xMap = map(x, 0, width, min, max); + long double yMap = map(y, 0, height, min, max); + + long double xInitial = xMap; + long double yInitial = yMap; + + int n = 0; + + for (int i = 0; i < maxIterations; ++i) { + + long double x1 = xMap * xMap - yMap * yMap; + long double y1 = 2 * xMap * yMap; + + xMap = x1 + xInitial; + yMap = y1 + yInitial; + + if ((xMap + yMap) > 2) { + break; + } + + + n++; + } + + int brightness = map(n, 0, maxIterations, 0, 255); + if (n == maxIterations || (brightness < 20)) { + brightness = 0; + } + + int red = map(brightness * brightness, 0, 255 * 255, 0, 255); + int green = brightness; + int blue = map(sqrt(brightness), 0, sqrt(255), 0, 255); + + + vkvm::setPixel(x, y, vkvm::Color(red, green, blue)); + + } + } + vkvm::callEvent(vkvm::EventType::Redraw); + } + + vkvm::setRedrawInterval(500); +} diff --git a/src/MathFunctions.hpp b/src/MathFunctions.hpp new file mode 100644 index 0000000..bbfe32d --- /dev/null +++ b/src/MathFunctions.hpp @@ -0,0 +1,5 @@ +#ifndef DEMO_MATHFUNCTIONS_H +#define DEMO_MATHFUNCTIONS_H + +void mandelbrot(); +#endif diff --git a/src/demo.cpp b/src/demo.cpp deleted file mode 100644 index 1cc01fc..0000000 --- a/src/demo.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "demo.h" - -int test() { - return 42; -} diff --git a/src/demo.h b/src/demo.h deleted file mode 100644 index 9b77cb9..0000000 --- a/src/demo.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SHARED_MEMORY_DEMO_H -#define SHARED_MEMORY_DEMO_H - - -int test(); - - -#endif //SHARED_MEMORY_DEMO_H diff --git a/test/test_demo.cpp b/test/test_demo.cpp index 7cb240e..5db7f1d 100644 --- a/test/test_demo.cpp +++ b/test/test_demo.cpp @@ -1,6 +1,5 @@ #include -#include "../src/demo.h" TEST_CASE("Demo test") { - REQUIRE(test() == 42); + REQUIRE(42 == 42); } \ No newline at end of file