painting circle,square and rectangle with mouse!

This commit is contained in:
Shaohua Tong 2019-11-29 17:54:09 +01:00
parent 53444ab215
commit 36113024fb
3 changed files with 271 additions and 75 deletions

View File

@ -1,5 +1,4 @@
#include <iostream> #include <iostream>
#include "../src/demo.h"
#include "internal.hpp" #include "internal.hpp"
#include "vkvm.hpp" #include "vkvm.hpp"
#include "../src/DrawRender.hpp" #include "../src/DrawRender.hpp"
@ -8,28 +7,81 @@
/** /**
* @author: Shaohua Tong * @author: Shaohua Tong
* @since: v0.0.1 * @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); void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor);
int main() { int main() {
vkvm::initialize(0); vkvm::initialize(0);
vkvm::registerEvent(vkvm::EventType::MouseDrag, [](){ vkvm::Color penColor(250,102,0);
std::cout << "drag" << std::endl; int penWidth = 5;
vkvm::Color backgroundColor = vkvm::getBackgroundColor(); DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), vkvm::getBackgroundColor(), penColor, penWidth);
vkvm::Color penColor(160,180,123);
int penWidth = 10; vkvm::registerEvent(vkvm::EventType::MouseMove, [&drawRender]() {
DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); if(drawRender.getMouseDown() == true) {
drawRender.update(vkvm::getMousePosition(), BRUSH); 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::registerEvent(vkvm::EventType::MouseLeftDown, [&drawRender]() {
vkvm::Color backgroundColor = vkvm::getBackgroundColor(); if(drawRender.getMouseDown() == false) {
vkvm::Color penColor(160,180,123); drawRender.setMouseDown(true);
int penWidth = 10;
DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); vkvm::Coordinates mousePosition = vkvm::getMousePosition();
drawRender.update(vkvm::getMousePosition(), CIRCLE); 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){ while(1){
@ -52,5 +104,3 @@ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) {
std::cout << "\n"; std::cout << "\n";
} }
} }

View File

@ -1,27 +1,32 @@
// //
// Created by shaohuatong on 21.11.19. // Created by shaohuatong on 28.11.19.
// //
#include "DrawRender.hpp" #include "DrawRender.hpp"
#include <algorithm> #include <algorithm>
DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color penColor, int penWidth) DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth)
: backgroundColor(backgroundColor), penColor(penColor) { : backgroundColor(defaultBackgroundColor), penColor(penColor) {
this-> windowWidth = windowWidth; this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight; this-> windowHeight = windowHeight;
this-> penWidth = penWidth; this-> penWidth = penWidth;
} }
void DrawRender::update(vkvm::Coordinates mousePostion, int type) { void DrawRender::graphicsUpdate(int type) {
if(type == BRUSH) { if(type == CIRCLE || type == SQUARE) {
std::vector<std::vector<bool>> circleBrush = brushCreator(mousePostion); std::vector<std::vector<bool>> graphics = circleAndSquareCreator(type);
translateToSharedMemory(circleBrush, startX, startY, type); translateToSharedMemory(graphics, startX, startY);
}
else if(type == RECTANGLE) {
std::vector<std::vector<bool>> graphics = rectangleCreator();
translateToSharedMemory(graphics, startX, startY);
}
} }
else if(type == CIRCLE || type == SQUARE) {
std::vector<std::vector<bool>> graphics = circleAndSquareCreator(mousePostion, type); void DrawRender::brushUpdate(vkvm::Coordinates mousePosition) {
translateToSharedMemory(graphics, startX, startY, type); std::vector<std::vector<bool>> brush = brushCreator(mousePosition);
} translateToSharedMemory(brush, startX, startY);
} }
void DrawRender::clear() { void DrawRender::clear() {
@ -32,20 +37,65 @@ void DrawRender::clear() {
} }
} }
} }
std::vector<std::vector<bool>> DrawRender::rectangleCreator() {
if(painting == true)
clearToSharedMemory(oldGraphics, startX, startY);
std::vector<std::vector<bool>> DrawRender::circleAndSquareCreator(vkvm::Coordinates mousePosition, int type) { int x_draw = 0;
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; std::vector<std::vector<bool>> circle;
int x_draw = 0; int x_draw = 0;
int y_draw = 0; int y_draw = 0;
int distance = 0; int distance = 0;
radius = std::min(std::min(mousePosition.x, mousePosition.y), radius = getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2;
std::min(windowWidth - mousePosition.x, windowHeight - mousePosition.y)) * 0.8; vkvm::Coordinates middlePosition;
middlePosition.x = (mousePosition.x + mouseLeftDownPosition.x) / 2;
middlePosition.y = (mousePosition.y + mouseLeftDownPosition.y) / 2;
vkvm::Coordinates uperLeft; vkvm::Coordinates uperLeft;
vkvm::Coordinates bottomRight; vkvm::Coordinates bottomRight;
uperLeft.x = mousePosition.x - radius; uperLeft.x = middlePosition.x - radius;
uperLeft.y = mousePosition.y - radius; uperLeft.y = middlePosition.y - radius;
bottomRight.x = mousePosition.x + radius; bottomRight.x = middlePosition.x + radius;
bottomRight.y = mousePosition.y + radius; bottomRight.y = middlePosition.y + radius;
vkvm::Coordinates temp; vkvm::Coordinates temp;
circle.resize(2 * radius); circle.resize(2 * radius);
@ -55,7 +105,7 @@ std::vector<std::vector<bool>> DrawRender::circleAndSquareCreator(vkvm::Coordina
if(type == CIRCLE) { if(type == CIRCLE) {
temp.x = uperLeft.x + x_draw; temp.x = uperLeft.x + x_draw;
temp.y = uperLeft.y + y_draw; temp.y = uperLeft.y + y_draw;
distance = squareOfDistance(temp, mousePosition); distance = squareOfDistance(temp, middlePosition);
if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) { if( distance < (radius * radius) && distance > ((radius - penWidth) * (radius - penWidth))) {
circle[y_draw][x_draw] = true; circle[y_draw][x_draw] = true;
@ -71,6 +121,15 @@ std::vector<std::vector<bool>> DrawRender::circleAndSquareCreator(vkvm::Coordina
} }
} }
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; startX = uperLeft.x;
startY = uperLeft.y; startY = uperLeft.y;
return circle; return circle;
@ -109,13 +168,13 @@ std::vector<std::vector<bool>> DrawRender::brushCreator(vkvm::Coordinates mouseP
} }
void DrawRender::translateToSharedMemory(std::vector<std::vector<bool>> graphics, int startX, int startY, int type) { 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;
if(type == CIRCLE || type == SQUARE || type == BRUSH) {
for(y = 0; y < 2 * radius; y++) { for(y = 0; y < graphics.size(); y++) {
for(x = 0; x < 2 * radius; x++) { for(x = 0; x < graphics[y].size(); x++) {
if(graphics[y][x]) { if(graphics[y][x]) {
vkvm::setPixel(currentX, currentY, penColor); vkvm::setPixel(currentX, currentY, penColor);
} }
@ -124,6 +183,22 @@ void DrawRender::translateToSharedMemory(std::vector<std::vector<bool>> graphics
currentX = startX; currentX = startX;
currentY++; currentY++;
} }
}
void DrawRender::clearToSharedMemory(std::vector<std::vector<bool>> 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); 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;
}

View File

@ -10,39 +10,73 @@
#include <iostream> #include <iostream>
#include "Font.h" #include "Font.h"
#include "vector" #include "vector"
#include <cmath>
#define CIRCLE 0 #define CIRCLE 0
#define SQUARE 1 #define SQUARE 1
#define BRUSH 2 #define RECTANGLE 2
#define BRUSH 3
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 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(); void clear();
private: private:
std::vector<std::vector<bool>> oldGraphics;
vkvm::Coordinates mouseLeftDownPosition;
vkvm::Coordinates mousePosition; vkvm::Coordinates mousePosition;
vkvm::Color backgroundColor; vkvm::Color backgroundColor;
vkvm::Color penColor; vkvm::Color penColor;
int type; vkvm::KeyCode keyCode;
int penWidth; int penWidth;
int windowWidth; int windowWidth;
int windowHeight; int windowHeight;
int graphicsHeight;
int graphicsWidth;
int radius; int radius;
int length;
int width;
int startX; int startX;
int startY; int startY;
bool turnOnBrush; bool painting = true;
bool mouseDown = false;
bool turnOnBrush = false;
std::vector<std::vector<bool>> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); std::vector<std::vector<bool>> rectangleCreator();
std::vector<std::vector<bool>> circleAndSquareCreator(int type);
std::vector<std::vector<bool>> brushCreator(vkvm::Coordinates mousePosition); std::vector<std::vector<bool>> brushCreator(vkvm::Coordinates mousePosition);
int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y);
void translateToSharedMemory(std::vector<std::vector<bool>> 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<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