new architecture of Code for show cursor and Preview

This commit is contained in:
Shaohua Tong 2019-12-09 14:32:30 +01:00
parent cca36e6a32
commit 227183bfe5
12 changed files with 533 additions and 208 deletions

View File

@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
include_directories(${LIB_PATH}/include) 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) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a)

View File

@ -19,22 +19,20 @@ int main() {
DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), vkvm::getBackgroundColor(), penColor, penWidth); DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), vkvm::getBackgroundColor(), penColor, penWidth);
vkvm::registerEvent(vkvm::EventType::MouseMove, [&drawRender]() { vkvm::registerEvent(vkvm::EventType::MouseMove, [&drawRender]() {
if(drawRender.getMouseDown() == true) { vkvm::Coordinates mousePosition = vkvm::getMousePosition();
vkvm::Coordinates mousePosition = vkvm::getMousePosition(); mousePosition.y = mousePosition.y - 30;
mousePosition.y = mousePosition.y - 30; drawRender.setMousePostion(mousePosition);
drawRender.setMousePostion(mousePosition);
if (drawRender.getTurnOnBrush() == true ) { drawRender.graphicsUpdate(CURSOR);
drawRender.brushUpdate(mousePosition); vkvm::callEvent(vkvm::EventType::Redraw);
if(drawRender.getMouseDown()) {
if (drawRender.getTurnOnBrush()) {
drawRender.graphicsUpdate(BRUSH);
vkvm::callEvent(vkvm::EventType::Redraw); vkvm::callEvent(vkvm::EventType::Redraw);
} }
else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::S) { if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) {
drawRender.graphicsUpdate(SQUARE);
vkvm::callEvent(vkvm::EventType::Redraw);
}
else if((drawRender.getKeyCode() - 65536) == vkvm::KeyCode::C) {
drawRender.graphicsUpdate(CIRCLE); drawRender.graphicsUpdate(CIRCLE);
vkvm::callEvent(vkvm::EventType::Redraw); vkvm::callEvent(vkvm::EventType::Redraw);
} }
@ -47,7 +45,9 @@ int main() {
}); });
vkvm::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() { vkvm::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() {
if(drawRender.getMouseDown() == false) { drawRender.setFinish(false);
if(!drawRender.getMouseDown()) {
drawRender.setMouseDown(true); drawRender.setMouseDown(true);
vkvm::Coordinates mousePosition = vkvm::getMousePosition(); vkvm::Coordinates mousePosition = vkvm::getMousePosition();
@ -55,15 +55,33 @@ int main() {
drawRender.setMouseLeftDownPostion(mousePosition); 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); drawRender.setTurnOnBrush(true);
} }
}); });
vkvm::registerEvent(vkvm::EventType::MouseLeftUp, [&drawRender]() { 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.setTurnOnBrush(false);
drawRender.setMouseDown(false); drawRender.setMouseDown(false);
drawRender.setPainting(false); drawRender.setPainting(false);
}); });
vkvm::registerEvent(vkvm::EventType::KeyDown, [&drawRender]() { vkvm::registerEvent(vkvm::EventType::KeyDown, [&drawRender]() {
@ -73,6 +91,11 @@ int main() {
drawRender.clear(); drawRender.clear();
vkvm::callEvent(vkvm::EventType::Redraw); 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]() { vkvm::registerEvent(vkvm::EventType::KeyUp, [&drawRender]() {

60
src/Circle.cpp Normal file
View File

@ -0,0 +1,60 @@
//
// Created by shaohuatong on 06.12.19.
//
#include <iostream>
#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<std::vector<bool>> Circle::getCircle() {
return circle;
}
int Circle::getRadius() {
return radius;
}
vkvm::Coordinates Circle::getUperLeft() {
return uperLeft;
}
vkvm::Coordinates Circle::getBottomRight() {
return bottomRight;
}

34
src/Circle.hpp Normal file
View File

@ -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<std::vector<bool>> getCircle();
int getRadius();
vkvm::Coordinates getUperLeft();
vkvm::Coordinates getBottomRight();
private:
vkvm::Coordinates center;
int radius;
vkvm::Coordinates uperLeft;
vkvm::Coordinates bottomRight;
std::vector<std::vector<bool>> circle;
std::vector<std::vector<bool>> circleCreator(int penWidth, bool brush);
};
#endif //SIMPLE_DRAW_CIRCLE_H

47
src/Cursor.cpp Normal file
View File

@ -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<std::vector<bool>> Cursor::getCursor() {
return cursor;
}
vkvm::Coordinates Cursor::getUperLeft() {
return uperLeft;
}
vkvm::Coordinates Cursor::getBottomRight() {
return bottomRight;
}

31
src/Cursor.hpp Normal file
View File

@ -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<std::vector<bool>> getCursor();
vkvm::Coordinates getUperLeft();
vkvm::Coordinates getBottomRight();
private:
int radius;
vkvm::Coordinates uperLeft;
vkvm::Coordinates bottomRight;
vkvm::Coordinates mousePosition;
std::vector<std::vector<bool>> cursor;
};
#endif //SIMPLE_DRAW_CURSOR_HPP

View File

@ -4,6 +4,7 @@
#include "DrawRender.hpp" #include "DrawRender.hpp"
#include <algorithm> #include <algorithm>
#include <unistd.h>
DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth) DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth)
: backgroundColor(defaultBackgroundColor), penColor(penColor) { : backgroundColor(defaultBackgroundColor), penColor(penColor) {
@ -13,20 +14,79 @@ DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBac
} }
void DrawRender::graphicsUpdate(int type) { void DrawRender::graphicsUpdate(int type) {
if(type == CIRCLE || type == SQUARE) { std::vector<std::vector<bool>> shape;
std::vector<std::vector<bool>> graphics = circleAndSquareCreator(type);
translateToSharedMemory(graphics, startX, startY); if (type == CIRCLE) {
} if(painting)
else if(type == RECTANGLE) { clearToSharedMemory(oldCircle.getCircle(),
std::vector<std::vector<bool>> graphics = rectangleCreator(); oldCircle.getUperLeft().x, oldCircle.getUperLeft().y);
translateToSharedMemory(graphics, startX, startY);
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) { oldRectangle = Rectangle(mouseLeftDownPosition, mousePosition, penWidth);
std::vector<std::vector<bool>> brush = brushCreator(mousePosition);
translateToSharedMemory(brush, startX, startY); 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() { void DrawRender::clear() {
@ -37,163 +97,54 @@ void DrawRender::clear() {
} }
} }
} }
std::vector<std::vector<bool>> DrawRender::rectangleCreator() {
if(painting == true)
clearToSharedMemory(oldGraphics, startX, startY);
int x_draw = 0; void DrawRender::cursorCreator() {
int y_draw = 0;
std::vector<std::vector<bool>> 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<std::vector<bool>> DrawRender::circleAndSquareCreator(int type) {
if(painting == true)
clearToSharedMemory(oldGraphics, startX, startY);
std::vector<std::vector<bool>> 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<std::vector<bool>> DrawRender::brushCreator(vkvm::Coordinates mousePosition) { void DrawRender::translateToSharedMemory(std::vector<std::vector<bool>> shape, int startX, int startY, bool isCursor) {
std::vector<std::vector<bool>> 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<std::vector<bool>> graphics, int startX, int startY) {
int x, y; int x, y;
int currentX = startX; int currentX = startX;
int currentY = startY; int currentY = startY;
for(y = 0; y < graphics.size(); y++) { for(y = 0; y < shape.size(); y++) {
for(x = 0; x < graphics[y].size(); x++) { for(x = 0; x < shape[y].size(); x++) {
if(graphics[y][x]) { if(shape[y][x]) {
vkvm::setPixel(currentX, currentY, penColor); 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++;
} }
currentX = startX; currentX = startX;
currentY++; 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<std::vector<bool>> graphics, int startX, int startY) { void DrawRender::clearToSharedMemory(std::vector<std::vector<bool>> shape, int startX, int startY) {
int x, y; int x, y;
int currentX = startX; int currentX = startX;
int currentY = startY; int currentY = startY;
for(y = 0; y < graphics.size(); y++) { for(y = 0; y < shape.size(); y++) {
for(x = 0; x < graphics[y].size(); x++) { for(x = 0; x < shape[y].size(); x++) {
if(graphics[y][x]) { if((shape[y][x])) {
vkvm::setPixel(currentX, currentY, backgroundColor); if(!shapes.containsPixel(x + startX, y + startY)) {
vkvm::setPixel(currentX, currentY, backgroundColor);
}
else {
vkvm::setPixel(currentX, currentY, penColor);
}
} }
currentX++; currentX++;
} }
@ -202,26 +153,6 @@ void DrawRender::clearToSharedMemory(std::vector<std::vector<bool>> 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() { vkvm::Coordinates DrawRender::getMouseLeftDownPosition() {
return mouseLeftDownPosition; return mouseLeftDownPosition;
} }
@ -235,6 +166,16 @@ vkvm::Coordinates DrawRender::getMousePostion() {
} }
void DrawRender::setMousePostion(vkvm::Coordinates newMousePosition) { 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; mousePosition = newMousePosition;
} }
@ -254,9 +195,6 @@ bool DrawRender::getTurnOnBrush() {
return turnOnBrush; return turnOnBrush;
} }
int DrawRender::getRadius() {
return radius;
}
bool DrawRender::getMouseDown() { bool DrawRender::getMouseDown() {
return mouseDown; return mouseDown;
@ -273,3 +211,11 @@ bool DrawRender::getPainting() {
void DrawRender::setPainting(bool isPainting) { void DrawRender::setPainting(bool isPainting) {
painting = isPainting; painting = isPainting;
} }
void DrawRender::setFinish(bool isFinish) {
finish = isFinish;
}
bool DrawRender::getFinish() {
return finish;
}

View File

@ -10,64 +10,69 @@
#include <iostream> #include <iostream>
#include "Font.h" #include "Font.h"
#include "vector" #include "vector"
#include "utils.hpp"
#include <cmath> #include <cmath>
#include "Rectangle.hpp"
#include "Circle.hpp"
#include "Shapes.hpp"
#include "Cursor.hpp"
#define CIRCLE 0 #define CIRCLE 0
#define SQUARE 1 #define RECTANGLE 1
#define RECTANGLE 2 #define BRUSH 2
#define BRUSH 3 #define CURSOR 3
#define SHAPE 4
class DrawRender { class DrawRender {
public: public:
DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth);
void brushUpdate(vkvm::Coordinates mousePosition);
void graphicsUpdate(int type); void graphicsUpdate(int type);
void setMouseLeftDownPostion(vkvm::Coordinates newMousePosition); void setMouseLeftDownPostion(vkvm::Coordinates newMousePosition);
vkvm::Coordinates getMouseLeftDownPosition(); vkvm::Coordinates getMouseLeftDownPosition();
void setMousePostion(vkvm::Coordinates newMousePosition); void setMousePostion(vkvm::Coordinates newMousePosition);
vkvm::Coordinates getMousePostion(); vkvm::Coordinates getMousePostion();
void setKeyCode(vkvm::KeyCode newKeyCode); void setKeyCode(vkvm::KeyCode newKeyCode);
vkvm::KeyCode getKeyCode();
void setTurnOnBrush(bool turnOnBrush); void setTurnOnBrush(bool turnOnBrush);
bool getTurnOnBrush(); bool getTurnOnBrush();
vkvm::KeyCode getKeyCode();
void setKeyCode();
void setMouseDown(bool isMouseDown); void setMouseDown(bool isMouseDown);
bool getMouseDown(); bool getMouseDown();
void setPainting(bool isOneFinish); void setPainting(bool isOneFinish);
bool getPainting(); bool getPainting();
int getRadius();
void setFinish(bool isFinish);
bool getFinish();
void clear(); void clear();
private: private:
std::vector<std::vector<bool>> oldGraphics; Shapes shapes;
Circle oldCircle = Circle();
Rectangle oldRectangle = Rectangle();
Cursor oldCursor = Cursor();
vkvm::Coordinates mouseLeftDownPosition; vkvm::Coordinates mouseLeftDownPosition;
vkvm::Coordinates mousePosition; vkvm::Coordinates mousePosition;
vkvm::Color backgroundColor; vkvm::Color backgroundColor;
vkvm::Color penColor; vkvm::Color penColor;
vkvm::KeyCode keyCode; vkvm::KeyCode keyCode;
int penWidth; int penWidth;
int windowWidth; int windowWidth;
int windowHeight; int windowHeight;
int radius; int cursorStart;
int length;
int width;
int startX;
int startY;
bool painting = true; bool painting = true;
bool finish = false;
bool mouseDown = false; bool mouseDown = false;
bool turnOnBrush = false; bool turnOnBrush = false;
std::vector<std::vector<bool>> rectangleCreator(); void cursorCreator();
std::vector<std::vector<bool>> circleAndSquareCreator(int type); void translateToSharedMemory(std::vector<std::vector<bool>> graphic, int startX, int startY, bool isCursor);
std::vector<std::vector<bool>> brushCreator(vkvm::Coordinates mousePosition); void clearToSharedMemory(std::vector<std::vector<bool>> graphic, int startX, int startY);
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<std::vector<bool>> graphics, int startX, int startY);
void clearToSharedMemory(std::vector<std::vector<bool>> graphics, int startX, int startY);
}; };
#endif //SIMPLE_DRAW_DRAWRENDER_HPP #endif //SIMPLE_DRAW_DRAWRENDER_HPP

57
src/Rectangle.cpp Normal file
View File

@ -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<std::vector<bool>> 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;
}

34
src/Rectangle.hpp Normal file
View File

@ -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<std::vector<bool>> 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<std::vector<bool>> rectangle;
};
#endif //SIMPLE_DRAW_RECTANGLE_HPP

57
src/Shapes.cpp Normal file
View File

@ -0,0 +1,57 @@
//
// Created by shaohuatong on 06.12.19.
//
#include <iostream>
#include "Shapes.hpp"
void Shapes::addShape(std::vector<std::vector<bool>> 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<std::vector<bool>> Shapes::getShape(int index) {
return shapes[index];
}
int Shapes::getStartX(int index) {
return startXs[index];
}
int Shapes::getStartY(int index) {
return startYs[index];
}

31
src/Shapes.hpp Normal file
View File

@ -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<std::vector<bool>> shape, int startX,
int startY, int endX, int endY);
bool containsPixel(int x, int y);
int getCount();
std::vector<std::vector<bool>> getShape(int index);
int getStartX(int index);
int getStartY(int index);
private:
int count = 0;
std::vector<int> startXs;
std::vector<int> startYs;
std::vector<int> endXs;
std::vector<int> endYs;
std::vector<std::vector<std::vector<bool>>> shapes;
};
#endif //SIMPLE_DRAW_SHAPES_HPP