From 227183bfe5fa4de5757d7f384988f7e7bd720365 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Mon, 9 Dec 2019 14:32:30 +0100 Subject: [PATCH] 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