diff --git a/.ci/clang-tidy.sh b/.ci/clang-tidy.sh new file mode 100644 index 0000000..13b2ac7 --- /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 \) -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. + 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..6e8db98 --- /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,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers' +WarningsAsErrors: 'true' diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..8ef29d7 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,77 @@ +--- + +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 d98dc7e..f3cfff7 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) @@ -16,7 +19,7 @@ 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") @@ -31,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/main/main.cpp b/main/main.cpp index 53edc26..b2e1af8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1,114 @@ -#include "../src/demo.h" +#include +#include "internal.hpp" +#include "vkvm.hpp" +#include "../src/DrawRender.hpp" +#include + +/** + * @author: Shaohua Tong + * @since: v0.0.1 + * draw circle, square, rectangle and mouse drawing + */ int main() { - return test(); + vkvm::initialize(0); + + 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]() { + vkvm::Coordinates mousePosition = vkvm::getMousePosition(); + mousePosition.y = mousePosition.y - 30; + drawRender.setMousePostion(mousePosition); + + drawRender.graphicsUpdate(CURSOR); + vkvm::callEvent(vkvm::EventType::Redraw); + + if(drawRender.getMouseDown()) { + if (drawRender.getTurnOnBrush()) { + drawRender.graphicsUpdate(BRUSH); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + if((drawRender.getKeyCode()) == vkvm::KeyCode::C) { + drawRender.graphicsUpdate(CIRCLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if((drawRender.getKeyCode()) == vkvm::KeyCode::R) { + drawRender.graphicsUpdate(RECTANGLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + } + }); + + vkvm::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() { + drawRender.setFinish(false); + + if(!drawRender.getMouseDown()) { + drawRender.setMouseDown(true); + + vkvm::Coordinates mousePosition = vkvm::getMousePosition(); + mousePosition.y = mousePosition.y - 30; + drawRender.setMouseLeftDownPostion(mousePosition); + } + + 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()) == vkvm::KeyCode::C) && drawRender.getPainting()) { + drawRender.graphicsUpdate(CIRCLE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if(((drawRender.getKeyCode()) == 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]() { + drawRender.setKeyCode(vkvm::getLastPressedKey()); + + if((drawRender.getKeyCode()) == vkvm::KeyCode::D) { + drawRender.clear(); + vkvm::callEvent(vkvm::EventType::Redraw); + } + + else if((drawRender.getKeyCode()) == vkvm::KeyCode::Q) { + drawRender.graphicsUpdate(SHAPE); + vkvm::callEvent(vkvm::EventType::Redraw); + } + }); + + vkvm::registerEvent(vkvm::EventType::KeyUp, [&drawRender]() { + drawRender.setKeyCode(vkvm::getLastPressedKey()); + + if((drawRender.getKeyCode()) == vkvm::KeyCode::D) { + drawRender.clear(); + vkvm::callEvent(vkvm::EventType::Redraw); + } + }); + + while(1){ + sleep(5); + } + + return 0; } \ No newline at end of file diff --git a/src/Circle.cpp b/src/Circle.cpp new file mode 100644 index 0000000..b3ba8c0 --- /dev/null +++ b/src/Circle.cpp @@ -0,0 +1,55 @@ +#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..9177747 --- /dev/null +++ b/src/Circle.hpp @@ -0,0 +1,30 @@ +#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 new file mode 100644 index 0000000..1d71731 --- /dev/null +++ b/src/DrawRender.cpp @@ -0,0 +1,222 @@ +#include "DrawRender.hpp" +#include "vkvm.hpp" +#include +#include +#include + +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::graphicsUpdate(int type) { + 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; + + if(center.x + radius > windowWidth || center.x - radius < 0 + || center.y + radius > windowHeight || center.y - radius < 0) { + radius = std::min(std::min(center.x, windowWidth - center.x), std::min(center.y, windowHeight - center.y)); + } + + 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); + + 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() { + int x, y; + for(y = 0; y < windowHeight; y++) { + for(x = 0; x < windowWidth; x++) { + vkvm::setPixel(x, y, backgroundColor); + } + } + + shapes = Shapes(); +} + +void DrawRender::cursorCreator() { + +} + +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 < 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++; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); +} + +void DrawRender::clearToSharedMemory(std::vector> shape, int startX, int startY) { + int x, y; + int currentX = startX; + int currentY = startY; + + 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++; + } + currentX = startX; + currentY++; + } +} + +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) { + 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; +} + +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; +} + + +bool DrawRender::getMouseDown() { + return mouseDown; +} + +void DrawRender::setMouseDown(bool isMouseDown) { + mouseDown = isMouseDown; +} + +bool DrawRender::getPainting() { + return painting; +} + +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 new file mode 100644 index 0000000..083bf24 --- /dev/null +++ b/src/DrawRender.hpp @@ -0,0 +1,85 @@ +// +// Created by shaohuatong on 21.11.19. +// +#ifndef SIMPLE_DRAW_DRAWRENDER_HPP +#define SIMPLE_DRAW_DRAWRENDER_HPP + +#include "Cursor.hpp" +#include "Rectangle.hpp" +#include "Circle.hpp" +#include "Shapes.hpp" +#include "utils.hpp" +#include "vkvm.hpp" +#include +#include +#include +#include + +#define CIRCLE 0 +#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 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(); + + void setMouseDown(bool isMouseDown); + bool getMouseDown(); + + void setPainting(bool isOneFinish); + bool getPainting(); + + void setFinish(bool isFinish); + bool getFinish(); + + void clear(); + +private: + 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 cursorStart; + bool painting = true; + bool finish = false; + bool mouseDown = false; + bool turnOnBrush = false; + + 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..b136560 --- /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 = 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++) { + 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..816bf67 --- /dev/null +++ b/src/Shapes.cpp @@ -0,0 +1,58 @@ +// +// 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]; +// } +// } + 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(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 diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..0c63ccb --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,9 @@ +#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))); +} \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..fd9c082 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,18 @@ +// +// Created by shaohuatong on 06.12.19. +// + +#ifndef SIMPLE_DRAW_UTILS_HPP +#define SIMPLE_DRAW_UTILS_HPP + +#include "vkvm.hpp" +#include + +class utils { +public: + static int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); + static int getDistance(vkvm::Coordinates x, vkvm::Coordinates y); +}; + + +#endif //SIMPLE_DRAW_UTILS_H