From 1ee4846fddabcd1443837d5029f3e00b8080c612 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 7 Nov 2019 13:47:36 +0000 Subject: [PATCH 01/19] + ci pipeline --- .ci/clang-tidy.sh | 32 ++++++++++++++++++++++ .gitlab-ci.yml | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 .ci/clang-tidy.sh create mode 100644 .gitlab-ci.yml diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh new file mode 100644 index 0000000..eae5b12 --- /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 './client/cpptoml/*' -prune \) -type f ! -name "$(printf "*\n*")")" +for file in $filelist; do + if echo "$file" | grep -q -E ".*\.cpp$" ; 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' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-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' "$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/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..eb1309a --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,68 @@ +--- + +image: samueldebruyn/debian-git:latest + +stages: + - style + - test + - build + + +clang_tidy: + image: jhasse/clang-tidy + stage: style + tags: + - docker-ci + script: + - sh .ci/clang-tidy.sh; + +make_test: + stage: test + tags: + - docker-ci + script: + - apt-get update + - apt-get install -y g++ 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 g++ 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 \ No newline at end of file From 894d5538f00bfd36cc9b592fd9725585ae88d531 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 Nov 2019 11:06:41 +0000 Subject: [PATCH 02/19] ~ CI: clang_tidy step now contains catch2 & fltk --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index eb1309a..522f05f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ stages: clang_tidy: - image: jhasse/clang-tidy + image: joethei/clang_tidy stage: style tags: - docker-ci From e3a017cc3e41c8d251272c264f6caefdc5006020 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 Nov 2019 11:40:48 +0000 Subject: [PATCH 03/19] ~ default argument calls are now allowed --- .ci/clang-tidy.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index eae5b12..c20c8fb 100644 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -9,7 +9,7 @@ filelist="$(find . -not \( -path './client/cpptoml/*' -prune \) -type f ! -name for file in $filelist; do if echo "$file" | grep -q -E ".*\.cpp$" ; 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' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-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' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-argument-calls,-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' "$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 From 408d94d538b45c35d63b8629625d27a65d631660 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 Nov 2019 15:24:26 +0100 Subject: [PATCH 04/19] ~clang tidy checks all fies now --- .ci/clang-tidy.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index c20c8fb..913c79a 100644 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -5,11 +5,11 @@ bool=false # explicitly set IFS to contain only a line feed IFS=' ' -filelist="$(find . -not \( -path './client/cpptoml/*' -prune \) -type f ! -name "$(printf "*\n*")")" +filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf "*\n*")")" for file in $filelist; do - if echo "$file" | grep -q -E ".*\.cpp$" ; then + 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='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-argument-calls,-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' "$file" -- -I. -std=c++14 2>&1)" + clang_tidy_lib_check="$(clang-tidy -warnings-as-errors='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-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' "$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 From 7de540d01a1024fa3ed873b8d884658be2beea06 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Fri, 22 Nov 2019 19:45:52 +0100 Subject: [PATCH 05/19] simple draw about circle and square --- CMakeLists.txt | 2 +- main/main.cpp | 72 ++++++++++++++++++++++++- src/DrawRender.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++ src/DrawRender.h | 44 ++++++++++++++++ 4 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 src/DrawRender.cpp create mode 100644 src/DrawRender.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d98dc7e..4636246 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") include_directories(${LIB_PATH}/include) -add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.h) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index 53edc26..025e5f0 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1,75 @@ +#include #include "../src/demo.h" +#include "internal.hpp" +#include "vkvm.hpp" +#include "../src/DrawRender.h" + +/** + * @author: Shaohua Tong + * @since: v0.0.0 + * A circle and squre is converted and displayed in the console. + * Currently only to test. + */ +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor); int main() { - return test(); + vkvm::initialize(0); + + /*************************set back to shared memory(only for test)********************************************/ + int windowWidth = 30; + int windowHeight = 30; + int penWidth = 2; + vkvm::setWidth(windowWidth); + vkvm::setHeight(windowHeight); + //vkvm::setPenWIdth(penWidth); maybe + + vkvm::Color penColor1(1, 1, 1); + vkvm::Color backgroudColor1(255, 255, 255); + vkvm::setForegroundColor(penColor1); + vkvm::setBackgroundColor(backgroudColor1); + /**************************get color, widnowsWidth, windowsHeightm, penColor, penWidth from shared memory****************************/ + vkvm::Color penColor = vkvm::getForegroundColor(); + vkvm::Color backgroundColor = vkvm::getBackgroundColor(); + DrawRender drawRender(windowWidth, windowHeight, backgroundColor, penColor, penWidth); + /*************************get Mouseposition and update back to shared meomory********************************************/ + std::string command; + std::cout << "DrawRender: "; + std::getline(std::cin, command); + + //vkvm::getMousePosition(); + vkvm::Coordinates mousePostiion; + mousePostiion.x = 15; + mousePostiion.y = 15; + + while(command.compare("quit") != 0) { + if(command.compare("circle") == 0) { + drawRender.update(mousePostiion, CIRCLE); + outPutPixel(windowHeight, windowWidth, penColor); + } + if(command.compare("square") == 0) { + drawRender.update(mousePostiion, SQUARE); + outPutPixel(windowHeight, windowWidth, penColor); + } + if(command.compare("clear")) { + drawRender.clear(); + } + std::cout << "DrawRender: "; + std::getline(std::cin, command); + } + + return 0; +} + +/***************************read pixel in shared memory and test output in console******************************************/ +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) { + for(int y = 0; y < windowHeight; y++) { + for(int x = 0; x < windowWidth; x++) { + if(vkvm::getPixel(x, y).getRed() == penColor.getRed()) { + std::cout << "*"; + } else { + std::cout << " "; + } + } + std::cout << "\n"; + } } \ No newline at end of file diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp new file mode 100644 index 0000000..8735225 --- /dev/null +++ b/src/DrawRender.cpp @@ -0,0 +1,127 @@ +// +// Created by shaohuatong on 21.11.19. +// + +#include "DrawRender.h" +#include + +DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color penColor, int penWidth) + : backgroundColor(backgroundColor), penColor(penColor) { + this-> windowWidth = windowWidth; + this-> windowHeight = windowHeight; + this-> penWidth = penWidth; +} + +void DrawRender::update(vkvm::Coordinates mousePostion, int type) { + std::vector> graphics = circleAndSquareCreator(mousePostion, type); + translateToSharedMemory(graphics, startX, startY, type); +} + +void DrawRender::clear() { + int x, y; + for(y = 0; y < windowHeight; y++) { + for(x = 0; x < windowWidth; x++) { + vkvm::setPixel(x, y, backgroundColor); + } + } +} + +std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordinates mousePosition, int type) { + std::vector> circle; + int x_draw = 0; + int y_draw = 0; + int distance = 0; + radius = std::min(std::min(mousePosition.x, mousePosition.y), + std::min(windowWidth - mousePosition.x, windowHeight - mousePosition.y)) * 0.8; + vkvm::Coordinates uperLeft; + vkvm::Coordinates bottomRight; + uperLeft.x = mousePosition.x - radius; + uperLeft.y = mousePosition.y - radius; + bottomRight.x = mousePosition.x + radius; + bottomRight.y = mousePosition.y + radius; + + vkvm::Coordinates temp; + circle.resize(2 * radius); + for(y_draw = 0; y_draw < 2 * radius; y_draw++) { + circle[y_draw].resize(2 * radius); + for(x_draw = 0; x_draw < 2 * radius; x_draw++) { + if(type == CIRCLE) { + temp.x = uperLeft.x + x_draw; + temp.y = uperLeft.y + y_draw; + distance = squareOfDistance(temp, mousePosition); + + if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) { + circle[y_draw][x_draw] = true; + } + } + if(type == SQUARE) { + if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth) + || (x_draw <= 2 * radius && x_draw >= 2 * radius - penWidth) + || (y_draw <= 2 * radius && y_draw >= 2 * radius - penWidth)) { + circle[y_draw][x_draw] = true; + } + } + } + } + + startX = uperLeft.x; + startY = uperLeft.y; + return circle; +} + +void DrawRender::translateToSharedMemory(std::vector> graphics, int startX, int startY, int type) { + int x, y; + int currentX = startX; + int currentY = startY; + + if(type == CIRCLE || type == SQUARE) { + for(y = 0; y < 2 * radius; y++) { + for(x = 0; x < 2 * radius; x++) { + if(graphics[y][x]) { + vkvm::setPixel(currentX, currentY, penColor); + } + else { + vkvm::setPixel(currentX, currentY, backgroundColor); + } + currentX++; + } + currentX = startX; + currentY++; + } + } +} + +int DrawRender::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) { + return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/DrawRender.h b/src/DrawRender.h new file mode 100644 index 0000000..9eaa8e1 --- /dev/null +++ b/src/DrawRender.h @@ -0,0 +1,44 @@ +// +// Created by shaohuatong on 21.11.19. +// +#ifndef SIMPLE_DRAW_DRAWRENDER_H +#define SIMPLE_DRAW_DRAWRENDER_H + +#include +#include +#include +#include +#include "Font.h" +#include "vector" + +#define CIRCLE 0 +#define SQUARE 1 +#define BRUSH 2 + +class DrawRender { +public: + DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); + + void setType(); + void update(vkvm::Coordinates mousePostion, int type); + void clear(); + +private: + vkvm::Coordinates mousePosition; + vkvm::Color backgroundColor; + vkvm::Color penColor; + int type; + int penWidth; + int windowWidth; + int windowHeight; + int graphicsHeight; + int graphicsWidth; + int radius; + int startX; + int startY; + std::vector> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); + int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); + void translateToSharedMemory(std::vector> graphics, int startX, int startY, int Type); +}; + +#endif //SIMPLE_DRAW_DRAWRENDER_H From ea905ea8c9238b406a6454baa19e164ecd12b75d Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 27 Nov 2019 08:50:52 +0000 Subject: [PATCH 06/19] ~ update clang-tidy checks --- .ci/clang-tidy.sh | 2 +- .clang-tidy | 2 ++ CMakeLists.txt | 7 +++---- 3 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 .clang-tidy diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index 913c79a..1c3d7b3 100644 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -9,7 +9,7 @@ filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf 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='*' -header-filter='.*,-cpptoml.hpp' -checks='*,-llvm-header-guard,-fuchsia-statically-constructed-objects,-fuchsia-default-arguments,-fuchsia-default-arguments-calls,-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' "$file" -- -I. -std=c++14 2>&1)" + 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 diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..b0965ca --- /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' +WarningsAsErrors: 'true' diff --git a/CMakeLists.txt b/CMakeLists.txt index 4636246..3aaca14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,17 +12,16 @@ project(Simple_Draw) 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") include_directories(${LIB_PATH}/include) -add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.h) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) From 06bca3b2d2875ecbe2234d590f5c195017d94e92 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Wed, 27 Nov 2019 16:03:56 +0100 Subject: [PATCH 07/19] circle,square and MouseBrush can already show in GUI --- CMakeLists.txt | 4 +- main/main.cpp | 67 ++++++++++------------------ src/DrawRender.cpp | 52 +++++++++++++++++---- src/{DrawRender.h => DrawRender.hpp} | 12 +++-- 4 files changed, 78 insertions(+), 57 deletions(-) rename src/{DrawRender.h => DrawRender.hpp} (77%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4636246..bea93b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,13 +16,13 @@ set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*") set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;) 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") include_directories(${LIB_PATH}/include) -add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.h) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.hpp) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index 025e5f0..fa07588 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2,59 +2,38 @@ #include "../src/demo.h" #include "internal.hpp" #include "vkvm.hpp" -#include "../src/DrawRender.h" +#include "../src/DrawRender.hpp" +#include /** * @author: Shaohua Tong - * @since: v0.0.0 - * A circle and squre is converted and displayed in the console. - * Currently only to test. + * @since: v0.0.1 + * Circle and Square and Mouse brush can show in GUI */ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor); int main() { vkvm::initialize(0); - /*************************set back to shared memory(only for test)********************************************/ - int windowWidth = 30; - int windowHeight = 30; - int penWidth = 2; - vkvm::setWidth(windowWidth); - vkvm::setHeight(windowHeight); - //vkvm::setPenWIdth(penWidth); maybe + vkvm::registerEvent(vkvm::EventType::MouseDrag, [](){ + std::cout << "drag" << std::endl; + vkvm::Color backgroundColor = vkvm::getBackgroundColor(); + vkvm::Color penColor(160,180,123); + int penWidth = 10; + DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); + drawRender.update(vkvm::getMousePosition(), BRUSH); + }); - vkvm::Color penColor1(1, 1, 1); - vkvm::Color backgroudColor1(255, 255, 255); - vkvm::setForegroundColor(penColor1); - vkvm::setBackgroundColor(backgroudColor1); - /**************************get color, widnowsWidth, windowsHeightm, penColor, penWidth from shared memory****************************/ - vkvm::Color penColor = vkvm::getForegroundColor(); - vkvm::Color backgroundColor = vkvm::getBackgroundColor(); - DrawRender drawRender(windowWidth, windowHeight, backgroundColor, penColor, penWidth); - /*************************get Mouseposition and update back to shared meomory********************************************/ - std::string command; - std::cout << "DrawRender: "; - std::getline(std::cin, command); + vkvm::registerEvent(vkvm::EventType::MouseButton, [](){ + vkvm::Color backgroundColor = vkvm::getBackgroundColor(); + vkvm::Color penColor(160,180,123); + int penWidth = 10; + DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); + drawRender.update(vkvm::getMousePosition(), CIRCLE); + }); - //vkvm::getMousePosition(); - vkvm::Coordinates mousePostiion; - mousePostiion.x = 15; - mousePostiion.y = 15; - - while(command.compare("quit") != 0) { - if(command.compare("circle") == 0) { - drawRender.update(mousePostiion, CIRCLE); - outPutPixel(windowHeight, windowWidth, penColor); - } - if(command.compare("square") == 0) { - drawRender.update(mousePostiion, SQUARE); - outPutPixel(windowHeight, windowWidth, penColor); - } - if(command.compare("clear")) { - drawRender.clear(); - } - std::cout << "DrawRender: "; - std::getline(std::cin, command); + while(1){ + sleep(5); } return 0; @@ -72,4 +51,6 @@ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) { } std::cout << "\n"; } -} \ No newline at end of file +} + + diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index 8735225..93fa6a4 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -2,7 +2,7 @@ // Created by shaohuatong on 21.11.19. // -#include "DrawRender.h" +#include "DrawRender.hpp" #include DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color penColor, int penWidth) @@ -13,8 +13,15 @@ DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color background } void DrawRender::update(vkvm::Coordinates mousePostion, int type) { - std::vector> graphics = circleAndSquareCreator(mousePostion, type); - translateToSharedMemory(graphics, startX, startY, type); + if(type == BRUSH) { + std::vector> circleBrush = brushCreator(mousePostion); + translateToSharedMemory(circleBrush, startX, startY, type); + + } + else if(type == CIRCLE || type == SQUARE) { + std::vector> graphics = circleAndSquareCreator(mousePostion, type); + translateToSharedMemory(graphics, startX, startY, type); + } } void DrawRender::clear() { @@ -69,20 +76,49 @@ std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordina return circle; } +std::vector> DrawRender::brushCreator(vkvm::Coordinates mousePosition) { + std::vector> circleBrush; + int x_draw = 0; + int y_draw = 0; + int distance = 0; + radius = penWidth; + vkvm::Coordinates uperLeft; + vkvm::Coordinates bottomRight; + uperLeft.x = mousePosition.x - radius; + uperLeft.y = mousePosition.y - radius; + bottomRight.x = mousePosition.x + radius; + bottomRight.y = mousePosition.y + radius; + + vkvm::Coordinates temp; + circleBrush.resize(2 * radius); + for(y_draw = 0; y_draw < 2 * radius; y_draw++) { + circleBrush[y_draw].resize(2 * radius); + for(x_draw = 0; x_draw < 2 * radius; x_draw++) { + temp.x = uperLeft.x + x_draw; + temp.y = uperLeft.y + y_draw; + distance = squareOfDistance(temp, mousePosition); + if( distance < (radius * radius)) { + circleBrush[y_draw][x_draw] = true; + } + } + } + + startX = uperLeft.x; + startY = uperLeft.y; + return circleBrush; +} + + void DrawRender::translateToSharedMemory(std::vector> graphics, int startX, int startY, int type) { int x, y; int currentX = startX; int currentY = startY; - - if(type == CIRCLE || type == SQUARE) { + if(type == CIRCLE || type == SQUARE || type == BRUSH) { for(y = 0; y < 2 * radius; y++) { for(x = 0; x < 2 * radius; x++) { if(graphics[y][x]) { vkvm::setPixel(currentX, currentY, penColor); } - else { - vkvm::setPixel(currentX, currentY, backgroundColor); - } currentX++; } currentX = startX; diff --git a/src/DrawRender.h b/src/DrawRender.hpp similarity index 77% rename from src/DrawRender.h rename to src/DrawRender.hpp index 9eaa8e1..543d238 100644 --- a/src/DrawRender.h +++ b/src/DrawRender.hpp @@ -1,8 +1,8 @@ // // Created by shaohuatong on 21.11.19. // -#ifndef SIMPLE_DRAW_DRAWRENDER_H -#define SIMPLE_DRAW_DRAWRENDER_H +#ifndef SIMPLE_DRAW_DRAWRENDER_HPP +#define SIMPLE_DRAW_DRAWRENDER_HPP #include #include @@ -20,7 +20,7 @@ public: DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); void setType(); - void update(vkvm::Coordinates mousePostion, int type); + void update(vkvm::Coordinates mousePosition, int type); void clear(); private: @@ -36,9 +36,13 @@ private: int radius; int startX; int startY; + bool turnOnBrush; + std::vector> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); + std::vector> brushCreator(vkvm::Coordinates mousePosition); + int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); void translateToSharedMemory(std::vector> graphics, int startX, int startY, int Type); }; -#endif //SIMPLE_DRAW_DRAWRENDER_H +#endif //SIMPLE_DRAW_DRAWRENDER_HPP From 36113024fb01bc0808d02bc23322f4769b269cb1 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Fri, 29 Nov 2019 17:54:09 +0100 Subject: [PATCH 08/19] painting circle,square and rectangle with mouse! --- main/main.cpp | 86 +++++++++++++++---- src/DrawRender.cpp | 206 ++++++++++++++++++++++++++++++++++----------- src/DrawRender.hpp | 54 +++++++++--- 3 files changed, 271 insertions(+), 75 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index fa07588..37f6532 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1,4 @@ #include -#include "../src/demo.h" #include "internal.hpp" #include "vkvm.hpp" #include "../src/DrawRender.hpp" @@ -8,28 +7,81 @@ /** * @author: Shaohua Tong * @since: v0.0.1 - * Circle and Square and Mouse brush can show in GUI + * draw circle, square, rectangle and mouse drawing */ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor); int main() { vkvm::initialize(0); - vkvm::registerEvent(vkvm::EventType::MouseDrag, [](){ - std::cout << "drag" << std::endl; - vkvm::Color backgroundColor = vkvm::getBackgroundColor(); - vkvm::Color penColor(160,180,123); - int penWidth = 10; - DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); - drawRender.update(vkvm::getMousePosition(), BRUSH); + vkvm::Color penColor(250,102,0); + int penWidth = 5; + DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), vkvm::getBackgroundColor(), penColor, penWidth); + + vkvm::registerEvent(vkvm::EventType::MouseMove, [&drawRender]() { + if(drawRender.getMouseDown() == true) { + vkvm::Coordinates mousePosition = vkvm::getMousePosition(); + mousePosition.y = mousePosition.y - 30; + drawRender.setMousePostion(mousePosition); + + if (drawRender.getTurnOnBrush() == true ) { + drawRender.brushUpdate(mousePosition); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::S) { + drawRender.graphicsUpdate(SQUARE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) { + drawRender.graphicsUpdate(CIRCLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::R) { + drawRender.graphicsUpdate(RECTANGLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + } }); - vkvm::registerEvent(vkvm::EventType::MouseButton, [](){ - vkvm::Color backgroundColor = vkvm::getBackgroundColor(); - vkvm::Color penColor(160,180,123); - int penWidth = 10; - DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); - drawRender.update(vkvm::getMousePosition(), CIRCLE); + vkvm::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() { + if(drawRender.getMouseDown() == false) { + drawRender.setMouseDown(true); + + vkvm::Coordinates mousePosition = vkvm::getMousePosition(); + mousePosition.y = mousePosition.y - 30; + drawRender.setMouseLeftDownPostion(mousePosition); + } + + if((drawRender.getTurnOnBrush() == false) && ((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::B)) { + drawRender.setTurnOnBrush(true); + } + }); + + vkvm::registerEvent(vkvm::EventType::MouseLeftUp, [&drawRender]() { + drawRender.setTurnOnBrush(false); + drawRender.setMouseDown(false); + drawRender.setPainting(false); + }); + + vkvm::registerEvent(vkvm::EventType::KeyDown, [&drawRender]() { + drawRender.setKeyCode(vkvm::getLastPressedKey()); + + if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::D) { + drawRender.clear(); + vkvm::callEvent(vkvm::EventType::Redraw); + } + }); + + vkvm::registerEvent(vkvm::EventType::KeyUp, [&drawRender]() { + drawRender.setKeyCode(vkvm::getLastPressedKey()); + + if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::D) { + drawRender.clear(); + vkvm::callEvent(vkvm::EventType::Redraw); + } }); while(1){ @@ -51,6 +103,4 @@ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) { } std::cout << "\n"; } -} - - +} \ No newline at end of file diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index 93fa6a4..fbb4c3c 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -1,27 +1,32 @@ // -// Created by shaohuatong on 21.11.19. +// Created by shaohuatong on 28.11.19. // #include "DrawRender.hpp" #include -DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color penColor, int penWidth) - : backgroundColor(backgroundColor), penColor(penColor) { +DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth) + : backgroundColor(defaultBackgroundColor), penColor(penColor) { this-> windowWidth = windowWidth; this-> windowHeight = windowHeight; this-> penWidth = penWidth; } -void DrawRender::update(vkvm::Coordinates mousePostion, int type) { - if(type == BRUSH) { - std::vector> circleBrush = brushCreator(mousePostion); - translateToSharedMemory(circleBrush, startX, startY, type); +void DrawRender::graphicsUpdate(int type) { + if(type == CIRCLE || type == SQUARE) { + std::vector> graphics = circleAndSquareCreator(type); + translateToSharedMemory(graphics, startX, startY); + } + else if(type == RECTANGLE) { + std::vector> graphics = rectangleCreator(); + translateToSharedMemory(graphics, startX, startY); + } - } - else if(type == CIRCLE || type == SQUARE) { - std::vector> graphics = circleAndSquareCreator(mousePostion, type); - translateToSharedMemory(graphics, startX, startY, type); - } +} + +void DrawRender::brushUpdate(vkvm::Coordinates mousePosition) { + std::vector> brush = brushCreator(mousePosition); + translateToSharedMemory(brush, startX, startY); } void DrawRender::clear() { @@ -32,20 +37,65 @@ void DrawRender::clear() { } } } +std::vector> DrawRender::rectangleCreator() { + if(painting == true) + clearToSharedMemory(oldGraphics, startX, startY); -std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordinates mousePosition, int type) { + int x_draw = 0; + int y_draw = 0; + std::vector> rectangle; + length = abs(mouseLeftDownPosition.x - mousePosition.x); + width = abs(mouseLeftDownPosition.y - mousePosition.y); + vkvm::Coordinates uperLeft; + vkvm::Coordinates bottomRight; + uperLeft.x = min(mouseLeftDownPosition.x, mousePosition.x); + uperLeft.y = min(mouseLeftDownPosition.y, mousePosition.y); + bottomRight.x = max(mouseLeftDownPosition.x, mousePosition.x); + bottomRight.y = max(mouseLeftDownPosition.y, mousePosition.y); + + rectangle.resize(width); + for(y_draw = 0; y_draw < width; y_draw++) { + rectangle[y_draw].resize(length); + for(x_draw = 0; x_draw < length; x_draw++) { + if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth) + || (x_draw <= length && x_draw >= length - penWidth) + || (y_draw <= width && y_draw >= width - penWidth)) { + rectangle[y_draw][x_draw] = true; + } + } + } + + oldGraphics.resize(width); + for(y_draw = 0; y_draw < width; y_draw++) { + oldGraphics[y_draw].resize(length); + for(x_draw = 0; x_draw < length; x_draw++) { + oldGraphics[y_draw][x_draw] = true; + } + } + + painting = true; + startX = uperLeft.x; + startY = uperLeft.y; + return rectangle; +} +std::vector> DrawRender::circleAndSquareCreator(int type) { + if(painting == true) + clearToSharedMemory(oldGraphics, startX, startY); std::vector> circle; int x_draw = 0; int y_draw = 0; int distance = 0; - radius = std::min(std::min(mousePosition.x, mousePosition.y), - std::min(windowWidth - mousePosition.x, windowHeight - mousePosition.y)) * 0.8; + radius = getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2; + vkvm::Coordinates middlePosition; + middlePosition.x = (mousePosition.x + mouseLeftDownPosition.x) / 2; + middlePosition.y = (mousePosition.y + mouseLeftDownPosition.y) / 2; + vkvm::Coordinates uperLeft; vkvm::Coordinates bottomRight; - uperLeft.x = mousePosition.x - radius; - uperLeft.y = mousePosition.y - radius; - bottomRight.x = mousePosition.x + radius; - bottomRight.y = mousePosition.y + radius; + uperLeft.x = middlePosition.x - radius; + uperLeft.y = middlePosition.y - radius; + bottomRight.x = middlePosition.x + radius; + bottomRight.y = middlePosition.y + radius; vkvm::Coordinates temp; circle.resize(2 * radius); @@ -55,7 +105,7 @@ std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordina if(type == CIRCLE) { temp.x = uperLeft.x + x_draw; temp.y = uperLeft.y + y_draw; - distance = squareOfDistance(temp, mousePosition); + distance = squareOfDistance(temp, middlePosition); if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) { circle[y_draw][x_draw] = true; @@ -63,14 +113,23 @@ std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordina } if(type == SQUARE) { if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth) - || (x_draw <= 2 * radius && x_draw >= 2 * radius - penWidth) - || (y_draw <= 2 * radius && y_draw >= 2 * radius - penWidth)) { + || (x_draw <= 2 * radius && x_draw >= 2 * radius - penWidth) + || (y_draw <= 2 * radius && y_draw >= 2 * radius - penWidth)) { circle[y_draw][x_draw] = true; } } } } + oldGraphics.resize(2 * radius); + for(y_draw = 0; y_draw < 2 * radius; y_draw++) { + oldGraphics[y_draw].resize(2 * radius); + for(x_draw = 0; x_draw < 2 * radius; x_draw++) { + oldGraphics[y_draw][x_draw] = true; + } + } + + painting = true; startX = uperLeft.x; startY = uperLeft.y; return circle; @@ -109,21 +168,37 @@ std::vector> DrawRender::brushCreator(vkvm::Coordinates mouseP } -void DrawRender::translateToSharedMemory(std::vector> graphics, int startX, int startY, int type) { +void DrawRender::translateToSharedMemory(std::vector> graphics, int startX, int startY) { int x, y; int currentX = startX; int currentY = startY; - if(type == CIRCLE || type == SQUARE || type == BRUSH) { - for(y = 0; y < 2 * radius; y++) { - for(x = 0; x < 2 * radius; x++) { - if(graphics[y][x]) { - vkvm::setPixel(currentX, currentY, penColor); - } - currentX++; + + for(y = 0; y < graphics.size(); y++) { + for(x = 0; x < graphics[y].size(); x++) { + if(graphics[y][x]) { + vkvm::setPixel(currentX, currentY, penColor); } - currentX = startX; - currentY++; + currentX++; } + currentX = startX; + currentY++; + } +} + +void DrawRender::clearToSharedMemory(std::vector> graphics, int startX, int startY) { + int x, y; + int currentX = startX; + int currentY = startY; + + for(y = 0; y < graphics.size(); y++) { + for(x = 0; x < graphics[y].size(); x++) { + if(graphics[y][x]) { + vkvm::setPixel(currentX, currentY, backgroundColor); + } + currentX++; + } + currentX = startX; + currentY++; } } @@ -131,33 +206,70 @@ int DrawRender::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) { return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y); } +int DrawRender::getDistance(vkvm::Coordinates x, vkvm::Coordinates y) { + return (int)floor(sqrt(squareOfDistance(x, y))); +} +int DrawRender::min(int x, int y) { + if(x <= y) + return x; + return y; +} +int DrawRender::max(int x, int y) { + if(x<=y) + return y; + return x; +} +vkvm::Coordinates DrawRender::getMouseLeftDownPosition() { + return mouseLeftDownPosition; +} +void DrawRender::setMouseLeftDownPostion(vkvm::Coordinates newMousePosition) { + mouseLeftDownPosition = newMousePosition; +} +vkvm::Coordinates DrawRender::getMousePostion() { + return mousePosition; +} +void DrawRender::setMousePostion(vkvm::Coordinates newMousePosition) { + mousePosition = newMousePosition; +} +void DrawRender::setKeyCode(vkvm::KeyCode newKeyCode) { + keyCode = newKeyCode; +} +vkvm::KeyCode DrawRender::getKeyCode() { + return keyCode; +} +void DrawRender::setTurnOnBrush(bool newTurnOnBrush) { + turnOnBrush = newTurnOnBrush; +} +bool DrawRender::getTurnOnBrush() { + return turnOnBrush; +} +int DrawRender::getRadius() { + return radius; +} +bool DrawRender::getMouseDown() { + return mouseDown; +} +void DrawRender::setMouseDown(bool isMouseDown) { + mouseDown = isMouseDown; +} +bool DrawRender::getPainting() { + return painting; +} - - - - - - - - - - - - - - - +void DrawRender::setPainting(bool isPainting) { + painting = isPainting; +} \ No newline at end of file diff --git a/src/DrawRender.hpp b/src/DrawRender.hpp index 543d238..4b84d95 100644 --- a/src/DrawRender.hpp +++ b/src/DrawRender.hpp @@ -10,39 +10,73 @@ #include #include "Font.h" #include "vector" +#include #define CIRCLE 0 #define SQUARE 1 -#define BRUSH 2 +#define RECTANGLE 2 +#define BRUSH 3 class DrawRender { public: DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); + void brushUpdate(vkvm::Coordinates mousePosition); + void graphicsUpdate(int type); + + void setMouseLeftDownPostion(vkvm::Coordinates newMousePosition); + vkvm::Coordinates getMouseLeftDownPosition(); + void setMousePostion(vkvm::Coordinates newMousePosition); + vkvm::Coordinates getMousePostion(); + void setKeyCode(vkvm::KeyCode newKeyCode); + void setTurnOnBrush(bool turnOnBrush); + bool getTurnOnBrush(); + vkvm::KeyCode getKeyCode(); + void setKeyCode(); + void setMouseDown(bool isMouseDown); + bool getMouseDown(); + void setPainting(bool isOneFinish); + bool getPainting(); + int getRadius(); - void setType(); - void update(vkvm::Coordinates mousePosition, int type); void clear(); private: + std::vector> oldGraphics; + vkvm::Coordinates mouseLeftDownPosition; vkvm::Coordinates mousePosition; vkvm::Color backgroundColor; vkvm::Color penColor; - int type; + vkvm::KeyCode keyCode; int penWidth; int windowWidth; int windowHeight; - int graphicsHeight; - int graphicsWidth; int radius; + int length; + int width; int startX; int startY; - bool turnOnBrush; + bool painting = true; + bool mouseDown = false; + bool turnOnBrush = false; - std::vector> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); + std::vector> rectangleCreator(); + std::vector> circleAndSquareCreator(int type); std::vector> brushCreator(vkvm::Coordinates mousePosition); - int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); - void translateToSharedMemory(std::vector> graphics, int startX, int startY, int Type); + int getDistance(vkvm::Coordinates x, vkvm::Coordinates y); + int min(int x, int y); + int max(int x, int y); + void translateToSharedMemory(std::vector> graphics, int startX, int startY); + void clearToSharedMemory(std::vector> graphics, int startX, int startY); }; #endif //SIMPLE_DRAW_DRAWRENDER_HPP + + + + + + + + + From cca36e6a323791604dbc548c39f66ad7f43cad02 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 5 Dec 2019 07:46:28 +0000 Subject: [PATCH 09/19] ~ update ci to latest version --- .ci/clang-tidy.sh | 2 +- .clang-tidy | 2 +- .gitlab-ci.yml | 13 +++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh index 1c3d7b3..13b2ac7 100644 --- a/.ci/clang-tidy.sh +++ b/.ci/clang-tidy.sh @@ -5,7 +5,7 @@ bool=false # explicitly set IFS to contain only a line feed IFS=' ' -filelist="$(find . -not \( -path './*build*' -prune \) -type f ! -name "$(printf "*\n*")")" +filelist="$(find . -not \( -path './*build*' -prune \) -not \( -path './include' -prune \) -not \( -path './lib' -prune \) -type f ! -name "$(printf "*\n*")")" 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. diff --git a/.clang-tidy b/.clang-tidy index b0965ca..6e8db98 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,2 +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' +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,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers' WarningsAsErrors: 'true' diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 522f05f..6492961 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,7 +14,16 @@ clang_tidy: tags: - docker-ci script: - - sh .ci/clang-tidy.sh; + - 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 @@ -65,4 +74,4 @@ cmake_build: - mkdir build - cd build - cmake .. - - make \ No newline at end of file + - make From 227183bfe5fa4de5757d7f384988f7e7bd720365 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Mon, 9 Dec 2019 14:32:30 +0100 Subject: [PATCH 10/19] new architecture of Code for show cursor and Preview --- CMakeLists.txt | 2 +- main/main.cpp | 51 +++++--- src/Circle.cpp | 60 ++++++++++ src/Circle.hpp | 34 ++++++ src/Cursor.cpp | 47 ++++++++ src/Cursor.hpp | 31 +++++ src/DrawRender.cpp | 288 ++++++++++++++++++--------------------------- src/DrawRender.hpp | 49 ++++---- src/Rectangle.cpp | 57 +++++++++ src/Rectangle.hpp | 34 ++++++ src/Shapes.cpp | 57 +++++++++ src/Shapes.hpp | 31 +++++ 12 files changed, 533 insertions(+), 208 deletions(-) create mode 100644 src/Circle.cpp create mode 100644 src/Circle.hpp create mode 100644 src/Cursor.cpp create mode 100644 src/Cursor.hpp create mode 100644 src/Rectangle.cpp create mode 100644 src/Rectangle.hpp create mode 100644 src/Shapes.cpp create mode 100644 src/Shapes.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bea93b2..3eff2e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") include_directories(${LIB_PATH}/include) -add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.hpp) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.hpp src/Circle.cpp src/Circle.hpp src/Rectangle.cpp src/Rectangle.hpp src/utils.cpp src/utils.hpp src/Shapes.cpp src/Shapes.hpp src/Cursor.cpp src/Cursor.hpp) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index 37f6532..fd6c1cc 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -19,22 +19,20 @@ int main() { DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), vkvm::getBackgroundColor(), penColor, penWidth); vkvm::registerEvent(vkvm::EventType::MouseMove, [&drawRender]() { - if(drawRender.getMouseDown() == true) { - vkvm::Coordinates mousePosition = vkvm::getMousePosition(); - mousePosition.y = mousePosition.y - 30; - drawRender.setMousePostion(mousePosition); + vkvm::Coordinates mousePosition = vkvm::getMousePosition(); + mousePosition.y = mousePosition.y - 30; + drawRender.setMousePostion(mousePosition); - if (drawRender.getTurnOnBrush() == true ) { - drawRender.brushUpdate(mousePosition); + drawRender.graphicsUpdate(CURSOR); + vkvm::callEvent(vkvm::EventType::Redraw); + + if(drawRender.getMouseDown()) { + if (drawRender.getTurnOnBrush()) { + drawRender.graphicsUpdate(BRUSH); vkvm::callEvent(vkvm::EventType::Redraw); } - else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::S) { - drawRender.graphicsUpdate(SQUARE); - vkvm::callEvent(vkvm::EventType::Redraw); - } - - else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) { + if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) { drawRender.graphicsUpdate(CIRCLE); vkvm::callEvent(vkvm::EventType::Redraw); } @@ -47,7 +45,9 @@ int main() { }); vkvm::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() { - if(drawRender.getMouseDown() == false) { + drawRender.setFinish(false); + + if(!drawRender.getMouseDown()) { drawRender.setMouseDown(true); vkvm::Coordinates mousePosition = vkvm::getMousePosition(); @@ -55,15 +55,33 @@ int main() { drawRender.setMouseLeftDownPostion(mousePosition); } - if((drawRender.getTurnOnBrush() == false) && ((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::B)) { + if((!drawRender.getTurnOnBrush()) && ((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::B)) { drawRender.setTurnOnBrush(true); } }); vkvm::registerEvent(vkvm::EventType::MouseLeftUp, [&drawRender]() { + drawRender.setFinish(true); + + vkvm::Coordinates mousePosition = vkvm::getMousePosition(); + mousePosition.y = mousePosition.y - 30; + drawRender.setMousePostion(mousePosition); + + if(((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) && drawRender.getPainting()) { + drawRender.graphicsUpdate(CIRCLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if(((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::R) && drawRender.getPainting()) { + drawRender.graphicsUpdate(RECTANGLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + drawRender.setTurnOnBrush(false); drawRender.setMouseDown(false); drawRender.setPainting(false); + + }); vkvm::registerEvent(vkvm::EventType::KeyDown, [&drawRender]() { @@ -73,6 +91,11 @@ int main() { drawRender.clear(); vkvm::callEvent(vkvm::EventType::Redraw); } + + else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::Q) { + drawRender.graphicsUpdate(SHAPE); + vkvm::callEvent(vkvm::EventType::Redraw); + } }); vkvm::registerEvent(vkvm::EventType::KeyUp, [&drawRender]() { diff --git a/src/Circle.cpp b/src/Circle.cpp new file mode 100644 index 0000000..56e044b --- /dev/null +++ b/src/Circle.cpp @@ -0,0 +1,60 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#include +#include "Circle.hpp" + +Circle::Circle() { + +} + +Circle::Circle(vkvm::Coordinates center, int radius, int penWidth, bool brush) { + this -> center = center; + this -> radius = radius; + + int x_draw = 0; + int y_draw = 0; + int distance = 0; + + uperLeft.x = center.x - radius; + uperLeft.y = center.y - radius; + bottomRight.x = center.x + radius; + bottomRight.y = center.y + radius; + + vkvm::Coordinates temp; + circle.resize(2 * radius); + for(y_draw = 0; y_draw < 2 * radius; y_draw++) { + circle[y_draw].resize(2 * radius); + for(x_draw = 0; x_draw < 2 * radius; x_draw++) { + temp.x = uperLeft.x + x_draw; + temp.y = uperLeft.y + y_draw; + distance = utils::squareOfDistance(temp, center); + + if(!brush) { + if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) + circle[y_draw][x_draw] = true; + } + else { + if( distance < (radius * radius)) + circle[y_draw][x_draw] = true; + } + } + } +} + +std::vector> Circle::getCircle() { + return circle; +} + +int Circle::getRadius() { + return radius; +} + +vkvm::Coordinates Circle::getUperLeft() { + return uperLeft; +} + +vkvm::Coordinates Circle::getBottomRight() { + return bottomRight; +} \ No newline at end of file diff --git a/src/Circle.hpp b/src/Circle.hpp new file mode 100644 index 0000000..e11d8b3 --- /dev/null +++ b/src/Circle.hpp @@ -0,0 +1,34 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#ifndef SIMPLE_DRAW_CIRCLE_HPP +#define SIMPLE_DRAW_CIRCLE_HPP + +#include "internal.hpp" +#include "vkvm.hpp" +#include "utils.hpp" + +class Circle { +public: + Circle(); + + Circle(vkvm::Coordinates center, int radius, int penWidth, bool brush); + std::vector> getCircle(); + int getRadius(); + vkvm::Coordinates getUperLeft(); + vkvm::Coordinates getBottomRight(); + +private: + vkvm::Coordinates center; + int radius; + vkvm::Coordinates uperLeft; + vkvm::Coordinates bottomRight; + + std::vector> circle; + + std::vector> circleCreator(int penWidth, bool brush); +}; + + +#endif //SIMPLE_DRAW_CIRCLE_H diff --git a/src/Cursor.cpp b/src/Cursor.cpp new file mode 100644 index 0000000..7b2a6da --- /dev/null +++ b/src/Cursor.cpp @@ -0,0 +1,47 @@ +// +// Created by shaohuatong on 08.12.19. +// + +#include "Cursor.hpp" + +Cursor::Cursor() { + +} + +Cursor::Cursor(vkvm::Coordinates mousePosition, int penWidth, int radius) { + this->mousePosition = mousePosition; + this->radius = radius; + + int x_draw = 0; + int y_draw = 0; + + uperLeft.x = mousePosition.x - radius; + uperLeft.y = mousePosition.y - radius; + bottomRight.x = mousePosition.x + radius; + bottomRight.y = mousePosition.y + radius; + + vkvm::Coordinates temp; + cursor.resize(2 * radius); + for(y_draw = 0; y_draw < 2 * radius; y_draw++) { + cursor[y_draw].resize(2 * radius); + for(x_draw = 0; x_draw < 2 * radius; x_draw++) { + if((x_draw >= radius - penWidth && x_draw <= radius + penWidth) + || (y_draw >= radius - penWidth && y_draw <= radius + penWidth)) { + cursor[y_draw][x_draw] = true; + } + + } + } +} + +std::vector> Cursor::getCursor() { + return cursor; +} + +vkvm::Coordinates Cursor::getUperLeft() { + return uperLeft; +} + +vkvm::Coordinates Cursor::getBottomRight() { + return bottomRight; +} \ No newline at end of file diff --git a/src/Cursor.hpp b/src/Cursor.hpp new file mode 100644 index 0000000..0e0bf43 --- /dev/null +++ b/src/Cursor.hpp @@ -0,0 +1,31 @@ +// +// Created by shaohuatong on 08.12.19. +// + +#ifndef SIMPLE_DRAW_CURSOR_HPP +#define SIMPLE_DRAW_CURSOR_HPP + +#include "vkvm.hpp" +#include "internal.hpp" +#include "utils.hpp" + +class Cursor { +public: + Cursor(); + + Cursor(vkvm::Coordinates mousePosition, int penWidth, int radius); + std::vector> getCursor(); + vkvm::Coordinates getUperLeft(); + vkvm::Coordinates getBottomRight(); + +private: + int radius; + vkvm::Coordinates uperLeft; + vkvm::Coordinates bottomRight; + vkvm::Coordinates mousePosition; + std::vector> cursor; + +}; + + +#endif //SIMPLE_DRAW_CURSOR_HPP diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index fbb4c3c..8fdd4a4 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -4,6 +4,7 @@ #include "DrawRender.hpp" #include +#include DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth) : backgroundColor(defaultBackgroundColor), penColor(penColor) { @@ -13,20 +14,79 @@ DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBac } void DrawRender::graphicsUpdate(int type) { - if(type == CIRCLE || type == SQUARE) { - std::vector> graphics = circleAndSquareCreator(type); - translateToSharedMemory(graphics, startX, startY); - } - else if(type == RECTANGLE) { - std::vector> graphics = rectangleCreator(); - translateToSharedMemory(graphics, startX, startY); + std::vector> shape; + + if (type == CIRCLE) { + if(painting) + clearToSharedMemory(oldCircle.getCircle(), + oldCircle.getUperLeft().x, oldCircle.getUperLeft().y); + + int radius = utils::getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2; + vkvm::Coordinates center; + center.x = (mousePosition.x + mouseLeftDownPosition.x) / 2; + center.y = (mousePosition.y + mouseLeftDownPosition.y) / 2; + + oldCircle = Circle(center, radius, penWidth, false); + + painting = true; + + translateToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, false); + + if(finish) + shapes.addShape(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, + oldCircle.getBottomRight().x, oldCircle.getBottomRight().y); } -} + else if (type == RECTANGLE) { + if(painting) + clearToSharedMemory(oldRectangle.getRectangle(), + oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y); -void DrawRender::brushUpdate(vkvm::Coordinates mousePosition) { - std::vector> brush = brushCreator(mousePosition); - translateToSharedMemory(brush, startX, startY); + oldRectangle = Rectangle(mouseLeftDownPosition, mousePosition, penWidth); + + painting = true; + + translateToSharedMemory(oldRectangle.getRectangle(), + oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y, false); + + if(finish) + shapes.addShape(oldRectangle.getRectangle(), oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y, + oldRectangle.getBottomRight().x, oldRectangle.getBottomRight().y); + } + + else if (type == BRUSH) { + int radius = penWidth; + vkvm::Coordinates center = mousePosition; + + oldCircle = Circle(center, radius, penWidth, true); + + translateToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, false); + + if(finish) + shapes.addShape(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, + oldCircle.getBottomRight().x, oldCircle.getBottomRight().y); + } + + else if (type == CURSOR) { + if(cursorStart != 0) + clearToSharedMemory(oldCursor.getCursor(), + oldCursor.getUperLeft().x, oldCursor.getUperLeft().y); + + int radius = 10; + + oldCursor = Cursor(mousePosition, 1, radius); + + cursorStart = 1; + + translateToSharedMemory(oldCursor.getCursor(), oldCursor.getUperLeft().x, oldCursor.getUperLeft().y, true); + } + else if (type == SHAPE) { + for(int i = 0; i < shapes.getCount(); i++) { + penColor = vkvm::Color(rand() % 255,rand() % 255,rand() % 255); + translateToSharedMemory(shapes.getShape(i), shapes.getStartX(i), shapes.getStartY(i), false); + } + + } } void DrawRender::clear() { @@ -37,163 +97,54 @@ void DrawRender::clear() { } } } -std::vector> DrawRender::rectangleCreator() { - if(painting == true) - clearToSharedMemory(oldGraphics, startX, startY); - int x_draw = 0; - int y_draw = 0; - std::vector> rectangle; - length = abs(mouseLeftDownPosition.x - mousePosition.x); - width = abs(mouseLeftDownPosition.y - mousePosition.y); - vkvm::Coordinates uperLeft; - vkvm::Coordinates bottomRight; - uperLeft.x = min(mouseLeftDownPosition.x, mousePosition.x); - uperLeft.y = min(mouseLeftDownPosition.y, mousePosition.y); - bottomRight.x = max(mouseLeftDownPosition.x, mousePosition.x); - bottomRight.y = max(mouseLeftDownPosition.y, mousePosition.y); +void DrawRender::cursorCreator() { - rectangle.resize(width); - for(y_draw = 0; y_draw < width; y_draw++) { - rectangle[y_draw].resize(length); - for(x_draw = 0; x_draw < length; x_draw++) { - if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth) - || (x_draw <= length && x_draw >= length - penWidth) - || (y_draw <= width && y_draw >= width - penWidth)) { - rectangle[y_draw][x_draw] = true; - } - } - } - - oldGraphics.resize(width); - for(y_draw = 0; y_draw < width; y_draw++) { - oldGraphics[y_draw].resize(length); - for(x_draw = 0; x_draw < length; x_draw++) { - oldGraphics[y_draw][x_draw] = true; - } - } - - painting = true; - startX = uperLeft.x; - startY = uperLeft.y; - return rectangle; -} -std::vector> DrawRender::circleAndSquareCreator(int type) { - if(painting == true) - clearToSharedMemory(oldGraphics, startX, startY); - std::vector> circle; - int x_draw = 0; - int y_draw = 0; - int distance = 0; - radius = getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2; - vkvm::Coordinates middlePosition; - middlePosition.x = (mousePosition.x + mouseLeftDownPosition.x) / 2; - middlePosition.y = (mousePosition.y + mouseLeftDownPosition.y) / 2; - - vkvm::Coordinates uperLeft; - vkvm::Coordinates bottomRight; - uperLeft.x = middlePosition.x - radius; - uperLeft.y = middlePosition.y - radius; - bottomRight.x = middlePosition.x + radius; - bottomRight.y = middlePosition.y + radius; - - vkvm::Coordinates temp; - circle.resize(2 * radius); - for(y_draw = 0; y_draw < 2 * radius; y_draw++) { - circle[y_draw].resize(2 * radius); - for(x_draw = 0; x_draw < 2 * radius; x_draw++) { - if(type == CIRCLE) { - temp.x = uperLeft.x + x_draw; - temp.y = uperLeft.y + y_draw; - distance = squareOfDistance(temp, middlePosition); - - if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) { - circle[y_draw][x_draw] = true; - } - } - if(type == SQUARE) { - if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth) - || (x_draw <= 2 * radius && x_draw >= 2 * radius - penWidth) - || (y_draw <= 2 * radius && y_draw >= 2 * radius - penWidth)) { - circle[y_draw][x_draw] = true; - } - } - } - } - - oldGraphics.resize(2 * radius); - for(y_draw = 0; y_draw < 2 * radius; y_draw++) { - oldGraphics[y_draw].resize(2 * radius); - for(x_draw = 0; x_draw < 2 * radius; x_draw++) { - oldGraphics[y_draw][x_draw] = true; - } - } - - painting = true; - startX = uperLeft.x; - startY = uperLeft.y; - return circle; } -std::vector> DrawRender::brushCreator(vkvm::Coordinates mousePosition) { - std::vector> circleBrush; - int x_draw = 0; - int y_draw = 0; - int distance = 0; - radius = penWidth; - vkvm::Coordinates uperLeft; - vkvm::Coordinates bottomRight; - uperLeft.x = mousePosition.x - radius; - uperLeft.y = mousePosition.y - radius; - bottomRight.x = mousePosition.x + radius; - bottomRight.y = mousePosition.y + radius; - - vkvm::Coordinates temp; - circleBrush.resize(2 * radius); - for(y_draw = 0; y_draw < 2 * radius; y_draw++) { - circleBrush[y_draw].resize(2 * radius); - for(x_draw = 0; x_draw < 2 * radius; x_draw++) { - temp.x = uperLeft.x + x_draw; - temp.y = uperLeft.y + y_draw; - distance = squareOfDistance(temp, mousePosition); - if( distance < (radius * radius)) { - circleBrush[y_draw][x_draw] = true; - } - } - } - - startX = uperLeft.x; - startY = uperLeft.y; - return circleBrush; -} - - -void DrawRender::translateToSharedMemory(std::vector> graphics, int startX, int startY) { +void DrawRender::translateToSharedMemory(std::vector> shape, int startX, int startY, bool isCursor) { int x, y; int currentX = startX; int currentY = startY; - for(y = 0; y < graphics.size(); y++) { - for(x = 0; x < graphics[y].size(); x++) { - if(graphics[y][x]) { - vkvm::setPixel(currentX, currentY, penColor); + for(y = 0; y < shape.size(); y++) { + for(x = 0; x < shape[y].size(); x++) { + if(shape[y][x]) { + if(isCursor) + vkvm::setPixel(currentX, currentY, vkvm::Color(0,238,0)); + else if(turnOnBrush || finish) + vkvm::setPixel(currentX, currentY, penColor); + else + vkvm::setPixel(currentX, currentY, vkvm::Color(217,217,217)); } currentX++; } currentX = startX; currentY++; } + + // a violent way to delay redraw + for(y = 900; y < 1000; y++) { + for(x = 900; x < 1000; x++) + vkvm::setPixel(y, x, vkvm::Color(217,217,217)); + } } -void DrawRender::clearToSharedMemory(std::vector> graphics, int startX, int startY) { +void DrawRender::clearToSharedMemory(std::vector> shape, int startX, int startY) { int x, y; int currentX = startX; int currentY = startY; - for(y = 0; y < graphics.size(); y++) { - for(x = 0; x < graphics[y].size(); x++) { - if(graphics[y][x]) { - vkvm::setPixel(currentX, currentY, backgroundColor); + for(y = 0; y < shape.size(); y++) { + for(x = 0; x < shape[y].size(); x++) { + if((shape[y][x])) { + if(!shapes.containsPixel(x + startX, y + startY)) { + vkvm::setPixel(currentX, currentY, backgroundColor); + } + else { + vkvm::setPixel(currentX, currentY, penColor); + } + } currentX++; } @@ -202,26 +153,6 @@ void DrawRender::clearToSharedMemory(std::vector> graphics, in } } -int DrawRender::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) { - return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y); -} - -int DrawRender::getDistance(vkvm::Coordinates x, vkvm::Coordinates y) { - return (int)floor(sqrt(squareOfDistance(x, y))); -} - -int DrawRender::min(int x, int y) { - if(x <= y) - return x; - return y; -} - -int DrawRender::max(int x, int y) { - if(x<=y) - return y; - return x; -} - vkvm::Coordinates DrawRender::getMouseLeftDownPosition() { return mouseLeftDownPosition; } @@ -235,6 +166,16 @@ vkvm::Coordinates DrawRender::getMousePostion() { } void DrawRender::setMousePostion(vkvm::Coordinates newMousePosition) { + if(newMousePosition.x >= 0 && newMousePosition.x <= windowWidth && newMousePosition.y >= 30 && newMousePosition.y <= windowHeight) { + + } + else { + newMousePosition.x = newMousePosition.x < 0 ? 0: newMousePosition.x; + newMousePosition.y = newMousePosition.y < 30 ? 0 : newMousePosition.y; + newMousePosition.x = newMousePosition.x > windowWidth ? windowWidth : newMousePosition.x; + newMousePosition.y = newMousePosition.y > windowHeight ? windowHeight : newMousePosition.y; + } + mousePosition = newMousePosition; } @@ -254,9 +195,6 @@ bool DrawRender::getTurnOnBrush() { return turnOnBrush; } -int DrawRender::getRadius() { - return radius; -} bool DrawRender::getMouseDown() { return mouseDown; @@ -272,4 +210,12 @@ bool DrawRender::getPainting() { void DrawRender::setPainting(bool isPainting) { painting = isPainting; +} + +void DrawRender::setFinish(bool isFinish) { + finish = isFinish; +} + +bool DrawRender::getFinish() { + return finish; } \ No newline at end of file diff --git a/src/DrawRender.hpp b/src/DrawRender.hpp index 4b84d95..4f1697a 100644 --- a/src/DrawRender.hpp +++ b/src/DrawRender.hpp @@ -10,64 +10,69 @@ #include #include "Font.h" #include "vector" +#include "utils.hpp" #include +#include "Rectangle.hpp" +#include "Circle.hpp" +#include "Shapes.hpp" +#include "Cursor.hpp" #define CIRCLE 0 -#define SQUARE 1 -#define RECTANGLE 2 -#define BRUSH 3 +#define RECTANGLE 1 +#define BRUSH 2 +#define CURSOR 3 +#define SHAPE 4 class DrawRender { public: DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); - void brushUpdate(vkvm::Coordinates mousePosition); void graphicsUpdate(int type); void setMouseLeftDownPostion(vkvm::Coordinates newMousePosition); vkvm::Coordinates getMouseLeftDownPosition(); void setMousePostion(vkvm::Coordinates newMousePosition); vkvm::Coordinates getMousePostion(); + void setKeyCode(vkvm::KeyCode newKeyCode); + vkvm::KeyCode getKeyCode(); + void setTurnOnBrush(bool turnOnBrush); bool getTurnOnBrush(); - vkvm::KeyCode getKeyCode(); - void setKeyCode(); + void setMouseDown(bool isMouseDown); bool getMouseDown(); + void setPainting(bool isOneFinish); bool getPainting(); - int getRadius(); + + void setFinish(bool isFinish); + bool getFinish(); void clear(); private: - std::vector> oldGraphics; + Shapes shapes; + Circle oldCircle = Circle(); + Rectangle oldRectangle = Rectangle(); + Cursor oldCursor = Cursor(); vkvm::Coordinates mouseLeftDownPosition; vkvm::Coordinates mousePosition; vkvm::Color backgroundColor; vkvm::Color penColor; vkvm::KeyCode keyCode; + int penWidth; int windowWidth; int windowHeight; - int radius; - int length; - int width; - int startX; - int startY; + int cursorStart; bool painting = true; + bool finish = false; bool mouseDown = false; bool turnOnBrush = false; - std::vector> rectangleCreator(); - std::vector> circleAndSquareCreator(int type); - std::vector> brushCreator(vkvm::Coordinates mousePosition); - int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); - int getDistance(vkvm::Coordinates x, vkvm::Coordinates y); - int min(int x, int y); - int max(int x, int y); - void translateToSharedMemory(std::vector> graphics, int startX, int startY); - void clearToSharedMemory(std::vector> graphics, int startX, int startY); + void cursorCreator(); + void translateToSharedMemory(std::vector> graphic, int startX, int startY, bool isCursor); + void clearToSharedMemory(std::vector> graphic, int startX, int startY); }; #endif //SIMPLE_DRAW_DRAWRENDER_HPP diff --git a/src/Rectangle.cpp b/src/Rectangle.cpp new file mode 100644 index 0000000..cf5f18b --- /dev/null +++ b/src/Rectangle.cpp @@ -0,0 +1,57 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#include "Rectangle.hpp" + +Rectangle::Rectangle() { + +} + +Rectangle::Rectangle(vkvm::Coordinates mouseLeftDownPostion, vkvm::Coordinates mousePostion, int penWidth) { + this -> mouseLeftDownPosition = mouseLeftDownPostion; + this -> mousePosition = mousePostion; + + int x_draw = 0; + int y_draw = 0; + + length = abs(mouseLeftDownPosition.x - mousePosition.x); + width = abs(mouseLeftDownPosition.y - mousePosition.y); + uperLeft.x = utils::min(mouseLeftDownPosition.x, mousePosition.x); + uperLeft.y = utils::min(mouseLeftDownPosition.y, mousePosition.y); + bottomRight.x = utils::max(mouseLeftDownPosition.x, mousePosition.x); + bottomRight.y = utils::max(mouseLeftDownPosition.y, mousePosition.y); + + rectangle.resize(width); + for(y_draw = 0; y_draw < width; y_draw++) { + rectangle[y_draw].resize(length); + for(x_draw = 0; x_draw < length; x_draw++) { + if((x_draw >= 0 && x_draw <= penWidth) || (y_draw >= 0 && y_draw <= penWidth) + || (x_draw <= length && x_draw >= length - penWidth) + || (y_draw <= width && y_draw >= width - penWidth)) { + rectangle[y_draw][x_draw] = true; + } + } + } +} + + +std::vector> Rectangle::getRectangle() { + return rectangle; +} + +int Rectangle::getWidth() { + return width; +} + +int Rectangle::getLength() { + return length; +} + +vkvm::Coordinates Rectangle::getUperLeft() { + return uperLeft; +} + +vkvm::Coordinates Rectangle::getBottomRight() { + return bottomRight; +} \ No newline at end of file diff --git a/src/Rectangle.hpp b/src/Rectangle.hpp new file mode 100644 index 0000000..da2550c --- /dev/null +++ b/src/Rectangle.hpp @@ -0,0 +1,34 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#ifndef SIMPLE_DRAW_RECTANGLE_HPP +#define SIMPLE_DRAW_RECTANGLE_HPP + +#include "internal.hpp" +#include "vkvm.hpp" +#include "utils.hpp" + +class Rectangle { +public: + Rectangle(); + + Rectangle(vkvm::Coordinates mouseLeftDownPosition, vkvm::Coordinates mousePosition, int penWidth); + std::vector> getRectangle(); + int getLength(); + int getWidth(); + vkvm::Coordinates getUperLeft(); + vkvm::Coordinates getBottomRight(); + +private: + int length; + int width; + vkvm::Coordinates uperLeft; + vkvm::Coordinates bottomRight; + vkvm::Coordinates mouseLeftDownPosition; + vkvm::Coordinates mousePosition; + std::vector> rectangle; + +}; + +#endif //SIMPLE_DRAW_RECTANGLE_HPP diff --git a/src/Shapes.cpp b/src/Shapes.cpp new file mode 100644 index 0000000..8810d3d --- /dev/null +++ b/src/Shapes.cpp @@ -0,0 +1,57 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#include +#include "Shapes.hpp" + +void Shapes::addShape(std::vector> shape, int startX, int startY, int endX, int endY) { + count++; + + shapes.resize(count); + startXs.resize(count); + startYs.resize(count); + endXs.resize(count); + endYs.resize(count); + + startXs[count-1] = startX; + startYs[count-1] = startY; + endXs[count-1] = endX; + endYs[count-1] = endY; + + shapes[count-1].resize(shape.size()); + for(int y = 0; y < shape.size(); y++) { + shapes[count-1][y].resize(shape[y].size()); + for(int x = 0; x < shape[y].size(); x++) { + shapes[count-1][y][x] = shape[y][x]; + } + } +} + +bool Shapes::containsPixel(int x, int y) { + for(int i = 0; i < count; i++) { + if(x > startXs[i] && x < endXs[i] && y > startYs[i] && y < endYs[i]) { + if(shapes[i][y-startYs[i]][x-startXs[i]]) { + return true; + } + } + } + + return false; +} + +int Shapes::getCount() { + return count; +} + +std::vector> Shapes::getShape(int index) { + return shapes[index]; +} + +int Shapes::getStartX(int index) { + return startXs[index]; +} + +int Shapes::getStartY(int index) { + return startYs[index]; +} \ No newline at end of file diff --git a/src/Shapes.hpp b/src/Shapes.hpp new file mode 100644 index 0000000..2317d2d --- /dev/null +++ b/src/Shapes.hpp @@ -0,0 +1,31 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#ifndef SIMPLE_DRAW_SHAPES_HPP +#define SIMPLE_DRAW_SHAPES_HPP + +#include "vkvm.hpp" +#include "internal.hpp" + +class Shapes { +public: + void addShape(std::vector> shape, int startX, + int startY, int endX, int endY); + bool containsPixel(int x, int y); + int getCount(); + std::vector> getShape(int index); + int getStartX(int index); + int getStartY(int index); + +private: + int count = 0; + std::vector startXs; + std::vector startYs; + std::vector endXs; + std::vector endYs; + std::vector>> shapes; +}; + + +#endif //SIMPLE_DRAW_SHAPES_HPP From d053170cf560c698f4b55390c1aa4b9b43099ec8 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Wed, 11 Dec 2019 11:49:58 +0100 Subject: [PATCH 11/19] new architecture of Code for show cursor and Preview --- CMakeLists.txt | 2 +- src/Circle.cpp | 4 ++-- src/DrawRender.cpp | 3 ++- src/Shapes.cpp | 17 +++++++++-------- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3eff2e5..d1c23b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") include_directories(${LIB_PATH}/include) -add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.hpp src/Circle.cpp src/Circle.hpp src/Rectangle.cpp src/Rectangle.hpp src/utils.cpp src/utils.hpp src/Shapes.cpp src/Shapes.hpp src/Cursor.cpp src/Cursor.hpp) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) diff --git a/src/Circle.cpp b/src/Circle.cpp index 56e044b..d086aa9 100644 --- a/src/Circle.cpp +++ b/src/Circle.cpp @@ -32,11 +32,11 @@ Circle::Circle(vkvm::Coordinates center, int radius, int penWidth, bool brush) { distance = utils::squareOfDistance(temp, center); if(!brush) { - if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) + if( distance <= (radius * radius) && distance >= ((radius - penWidth) * (radius - penWidth))) circle[y_draw][x_draw] = true; } else { - if( distance < (radius * radius)) + if( distance <= (radius * radius)) circle[y_draw][x_draw] = true; } } diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index 8fdd4a4..085a3f2 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -96,6 +96,8 @@ void DrawRender::clear() { vkvm::setPixel(x, y, backgroundColor); } } + + shapes = Shapes(); } void DrawRender::cursorCreator() { @@ -144,7 +146,6 @@ void DrawRender::clearToSharedMemory(std::vector> shape, int s else { vkvm::setPixel(currentX, currentY, penColor); } - } currentX++; } diff --git a/src/Shapes.cpp b/src/Shapes.cpp index 8810d3d..816bf67 100644 --- a/src/Shapes.cpp +++ b/src/Shapes.cpp @@ -19,18 +19,19 @@ void Shapes::addShape(std::vector> shape, int startX, int star endXs[count-1] = endX; endYs[count-1] = endY; - shapes[count-1].resize(shape.size()); - for(int y = 0; y < shape.size(); y++) { - shapes[count-1][y].resize(shape[y].size()); - for(int x = 0; x < shape[y].size(); x++) { - shapes[count-1][y][x] = shape[y][x]; - } - } +// shapes[count-1].resize(shape.size()); +// for(int y = 0; y < shape.size(); y++) { +// shapes[count-1][y].resize(shape[y].size()); +// for(int x = 0; x < shape[y].size(); x++) { +// shapes[count-1][y][x] = shape[y][x]; +// } +// } + shapes[count-1] = shape; } bool Shapes::containsPixel(int x, int y) { for(int i = 0; i < count; i++) { - if(x > startXs[i] && x < endXs[i] && y > startYs[i] && y < endYs[i]) { + if(x >= startXs[i] && x < endXs[i] && y >= startYs[i] && y < endYs[i]) { if(shapes[i][y-startYs[i]][x-startXs[i]]) { return true; } From 14c13358812c238a51e731d4935ac1c5bb2bece7 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Wed, 11 Dec 2019 11:55:00 +0100 Subject: [PATCH 12/19] utils add --- src/utils.cpp | 25 +++++++++++++++++++++++++ src/utils.hpp | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/utils.cpp create mode 100644 src/utils.hpp diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..9fb3201 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,25 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#include "utils.hpp" + +int utils::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) { + return (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y); +} + +int utils::getDistance(vkvm::Coordinates x, vkvm::Coordinates y) { + return (int)floor(sqrt(squareOfDistance(x, y))); +} + +int utils::min(int x, int y) { + if(x <= y) + return x; + return y; +} + +int utils::max(int x, int y) { + if(x<=y) + return y; + return x; +} \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..c208db5 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,21 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#ifndef SIMPLE_DRAW_UTILS_HPP +#define SIMPLE_DRAW_UTILS_HPP + +#include "vkvm.hpp" +#include "internal.hpp" +#include "math.h" + +class utils { +public: + static int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); + static int getDistance(vkvm::Coordinates x, vkvm::Coordinates y); + static int min(int x, int y); + static int max(int x, int y); +}; + + +#endif //SIMPLE_DRAW_UTILS_H From 5bc558c7b1e39270430fa5d719889557ae27686f Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 18 Dec 2019 12:18:35 +0100 Subject: [PATCH 13/19] ~ adjust keycodes --- main/main.cpp | 14 +++++++------- src/DrawRender.hpp | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index fd6c1cc..7fe5f25 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -32,12 +32,12 @@ int main() { vkvm::callEvent(vkvm::EventType::Redraw); } - if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) { + if((drawRender.getKeyCode()) == vkvm::KeyCode::C) { drawRender.graphicsUpdate(CIRCLE); vkvm::callEvent(vkvm::EventType::Redraw); } - else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::R) { + else if((drawRender.getKeyCode()) == vkvm::KeyCode::R) { drawRender.graphicsUpdate(RECTANGLE); vkvm::callEvent(vkvm::EventType::Redraw); } @@ -67,12 +67,12 @@ int main() { mousePosition.y = mousePosition.y - 30; drawRender.setMousePostion(mousePosition); - if(((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) && drawRender.getPainting()) { + if(((drawRender.getKeyCode()) == vkvm::KeyCode::C) && drawRender.getPainting()) { drawRender.graphicsUpdate(CIRCLE); vkvm::callEvent(vkvm::EventType::Redraw); } - else if(((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::R) && drawRender.getPainting()) { + else if(((drawRender.getKeyCode()) == vkvm::KeyCode::R) && drawRender.getPainting()) { drawRender.graphicsUpdate(RECTANGLE); vkvm::callEvent(vkvm::EventType::Redraw); } @@ -87,12 +87,12 @@ int main() { vkvm::registerEvent(vkvm::EventType::KeyDown, [&drawRender]() { drawRender.setKeyCode(vkvm::getLastPressedKey()); - if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::D) { + if((drawRender.getKeyCode()) == vkvm::KeyCode::D) { drawRender.clear(); vkvm::callEvent(vkvm::EventType::Redraw); } - else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::Q) { + else if((drawRender.getKeyCode()) == vkvm::KeyCode::Q) { drawRender.graphicsUpdate(SHAPE); vkvm::callEvent(vkvm::EventType::Redraw); } @@ -101,7 +101,7 @@ int main() { vkvm::registerEvent(vkvm::EventType::KeyUp, [&drawRender]() { drawRender.setKeyCode(vkvm::getLastPressedKey()); - if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::D) { + if((drawRender.getKeyCode()) == vkvm::KeyCode::D) { drawRender.clear(); vkvm::callEvent(vkvm::EventType::Redraw); } diff --git a/src/DrawRender.hpp b/src/DrawRender.hpp index 4f1697a..724b030 100644 --- a/src/DrawRender.hpp +++ b/src/DrawRender.hpp @@ -8,7 +8,6 @@ #include #include #include -#include "Font.h" #include "vector" #include "utils.hpp" #include From 8fab19dd26aeb31ba38add81ffb8a6453c215123 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Wed, 18 Dec 2019 12:28:07 +0100 Subject: [PATCH 14/19] delay --- src/DrawRender.cpp | 8 +++----- src/DrawRender.hpp | 1 - 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index 085a3f2..baf886b 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -5,6 +5,8 @@ #include "DrawRender.hpp" #include #include +#include +#include DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth) : backgroundColor(defaultBackgroundColor), penColor(penColor) { @@ -125,11 +127,7 @@ void DrawRender::translateToSharedMemory(std::vector> shape, i currentY++; } - // a violent way to delay redraw - for(y = 900; y < 1000; y++) { - for(x = 900; x < 1000; x++) - vkvm::setPixel(y, x, vkvm::Color(217,217,217)); - } + std::this_thread::sleep_for(std::chrono::milliseconds(100)); } void DrawRender::clearToSharedMemory(std::vector> shape, int startX, int startY) { diff --git a/src/DrawRender.hpp b/src/DrawRender.hpp index 4f1697a..724b030 100644 --- a/src/DrawRender.hpp +++ b/src/DrawRender.hpp @@ -8,7 +8,6 @@ #include #include #include -#include "Font.h" #include "vector" #include "utils.hpp" #include From 0c9caab59e25081181c422b48d92d488840c3a47 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Wed, 8 Jan 2020 17:02:11 +0100 Subject: [PATCH 15/19] limit cycle radius --- src/DrawRender.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index baf886b..c531f38 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -24,10 +24,16 @@ void DrawRender::graphicsUpdate(int type) { oldCircle.getUperLeft().x, oldCircle.getUperLeft().y); int radius = utils::getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2; + vkvm::Coordinates center; center.x = (mousePosition.x + mouseLeftDownPosition.x) / 2; center.y = (mousePosition.y + mouseLeftDownPosition.y) / 2; + if(center.x + radius > windowWidth || center.x - radius < 0 + || center.y + radius > windowHeight || center.y - radius < 0) { + radius = utils::min(utils::min(center.x, windowWidth - center.x), utils::min(center.y, windowHeight - center.y)); + } + oldCircle = Circle(center, radius, penWidth, false); painting = true; From 9c6b09c9f6e815f7913590b1e5689f8f6892fbe6 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 16:33:34 +0000 Subject: [PATCH 16/19] ~ use clang instead of g++ --- .gitlab-ci.yml | 4 ++-- CMakeLists.txt | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6492961..8ef29d7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -31,7 +31,7 @@ make_test: - docker-ci script: - apt-get update - - apt-get install -y g++ make cmake clang-tidy + - 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 @@ -57,7 +57,7 @@ cmake_build: - docker-ci script: - apt-get update - - apt-get install -y g++ make cmake clang-tidy + - 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 diff --git a/CMakeLists.txt b/CMakeLists.txt index d1c23b1..adc7ebb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,9 @@ 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(Simple_Draw) set(CMAKE_CXX_STANDARD 14) From f6f283dcfcff3025cacb66c8c0f8bdf3745f81f7 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 20:33:11 +0100 Subject: [PATCH 17/19] ~ change up include type --- src/DrawRender.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/DrawRender.hpp b/src/DrawRender.hpp index 724b030..af1361f 100644 --- a/src/DrawRender.hpp +++ b/src/DrawRender.hpp @@ -5,10 +5,10 @@ #define SIMPLE_DRAW_DRAWRENDER_HPP #include -#include -#include +#include "Color.hpp" +#include "vkvm.hpp" #include -#include "vector" +#include #include "utils.hpp" #include #include "Rectangle.hpp" From 463ec11332759a920adbe0b5161ef961bbdc0103 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 20:56:48 +0100 Subject: [PATCH 18/19] ~ change a few includes --- main/main.cpp | 15 --------------- src/Circle.cpp | 5 ----- src/Circle.hpp | 4 ---- src/DrawRender.cpp | 7 ++----- src/DrawRender.hpp | 15 +++++++-------- 5 files changed, 9 insertions(+), 37 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 7fe5f25..b2e1af8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -9,7 +9,6 @@ * @since: v0.0.1 * draw circle, square, rectangle and mouse drawing */ -void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor); int main() { vkvm::initialize(0); @@ -112,18 +111,4 @@ int main() { } return 0; -} - -/***************************read pixel in shared memory and test output in console******************************************/ -void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) { - for(int y = 0; y < windowHeight; y++) { - for(int x = 0; x < windowWidth; x++) { - if(vkvm::getPixel(x, y).getRed() == penColor.getRed()) { - std::cout << "*"; - } else { - std::cout << " "; - } - } - std::cout << "\n"; - } } \ No newline at end of file diff --git a/src/Circle.cpp b/src/Circle.cpp index d086aa9..b3ba8c0 100644 --- a/src/Circle.cpp +++ b/src/Circle.cpp @@ -1,8 +1,3 @@ -// -// Created by shaohuatong on 06.12.19. -// - -#include #include "Circle.hpp" Circle::Circle() { diff --git a/src/Circle.hpp b/src/Circle.hpp index e11d8b3..9177747 100644 --- a/src/Circle.hpp +++ b/src/Circle.hpp @@ -1,7 +1,3 @@ -// -// Created by shaohuatong on 06.12.19. -// - #ifndef SIMPLE_DRAW_CIRCLE_HPP #define SIMPLE_DRAW_CIRCLE_HPP diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index c531f38..435a0ec 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -1,10 +1,7 @@ -// -// Created by shaohuatong on 28.11.19. -// - +#include "Color.hpp" #include "DrawRender.hpp" +#include "vkvm.hpp" #include -#include #include #include diff --git a/src/DrawRender.hpp b/src/DrawRender.hpp index af1361f..083bf24 100644 --- a/src/DrawRender.hpp +++ b/src/DrawRender.hpp @@ -4,17 +4,16 @@ #ifndef SIMPLE_DRAW_DRAWRENDER_HPP #define SIMPLE_DRAW_DRAWRENDER_HPP -#include -#include "Color.hpp" -#include "vkvm.hpp" -#include -#include -#include "utils.hpp" -#include +#include "Cursor.hpp" #include "Rectangle.hpp" #include "Circle.hpp" #include "Shapes.hpp" -#include "Cursor.hpp" +#include "utils.hpp" +#include "vkvm.hpp" +#include +#include +#include +#include #define CIRCLE 0 #define RECTANGLE 1 From 255698cba21bbb559c7bca635ba98b52df19797c Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 21:57:41 +0100 Subject: [PATCH 19/19] ~ somehow forgot to link library to unitTests --- CMakeLists.txt | 1 + src/DrawRender.cpp | 3 +-- src/Rectangle.cpp | 8 ++++---- src/utils.cpp | 16 ---------------- src/utils.hpp | 5 +---- 5 files changed, 7 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index adc7ebb..f3cfff7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ enable_testing() find_package(Catch2 REQUIRED) add_executable(UnitTests ${SOURCES} ${HEADERS} ${TESTS}) target_link_libraries(UnitTests Catch2::Catch2) +target_link_libraries(UnitTests ${LIB_PATH}/lib/liblibrary.a) include(CTest) include(Catch) diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index 435a0ec..1d71731 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -1,4 +1,3 @@ -#include "Color.hpp" #include "DrawRender.hpp" #include "vkvm.hpp" #include @@ -28,7 +27,7 @@ void DrawRender::graphicsUpdate(int type) { if(center.x + radius > windowWidth || center.x - radius < 0 || center.y + radius > windowHeight || center.y - radius < 0) { - radius = utils::min(utils::min(center.x, windowWidth - center.x), utils::min(center.y, windowHeight - center.y)); + radius = std::min(std::min(center.x, windowWidth - center.x), std::min(center.y, windowHeight - center.y)); } oldCircle = Circle(center, radius, penWidth, false); diff --git a/src/Rectangle.cpp b/src/Rectangle.cpp index cf5f18b..b136560 100644 --- a/src/Rectangle.cpp +++ b/src/Rectangle.cpp @@ -17,10 +17,10 @@ Rectangle::Rectangle(vkvm::Coordinates mouseLeftDownPostion, vkvm::Coordinates m length = abs(mouseLeftDownPosition.x - mousePosition.x); width = abs(mouseLeftDownPosition.y - mousePosition.y); - uperLeft.x = utils::min(mouseLeftDownPosition.x, mousePosition.x); - uperLeft.y = utils::min(mouseLeftDownPosition.y, mousePosition.y); - bottomRight.x = utils::max(mouseLeftDownPosition.x, mousePosition.x); - bottomRight.y = utils::max(mouseLeftDownPosition.y, mousePosition.y); + uperLeft.x = std::min(mouseLeftDownPosition.x, mousePosition.x); + uperLeft.y = std::min(mouseLeftDownPosition.y, mousePosition.y); + bottomRight.x = std::max(mouseLeftDownPosition.x, mousePosition.x); + bottomRight.y = std::max(mouseLeftDownPosition.y, mousePosition.y); rectangle.resize(width); for(y_draw = 0; y_draw < width; y_draw++) { diff --git a/src/utils.cpp b/src/utils.cpp index 9fb3201..0c63ccb 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,7 +1,3 @@ -// -// Created by shaohuatong on 06.12.19. -// - #include "utils.hpp" int utils::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) { @@ -10,16 +6,4 @@ int utils::squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y) { int utils::getDistance(vkvm::Coordinates x, vkvm::Coordinates y) { return (int)floor(sqrt(squareOfDistance(x, y))); -} - -int utils::min(int x, int y) { - if(x <= y) - return x; - return y; -} - -int utils::max(int x, int y) { - if(x<=y) - return y; - return x; } \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp index c208db5..fd9c082 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -6,15 +6,12 @@ #define SIMPLE_DRAW_UTILS_HPP #include "vkvm.hpp" -#include "internal.hpp" -#include "math.h" +#include class utils { public: static int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); static int getDistance(vkvm::Coordinates x, vkvm::Coordinates y); - static int min(int x, int y); - static int max(int x, int y); };