circle,square and MouseBrush can already show in GUI

This commit is contained in:
Shaohua Tong 2019-11-27 16:03:56 +01:00
parent 7de540d01a
commit 06bca3b2d2
4 changed files with 78 additions and 57 deletions

View File

@ -16,13 +16,13 @@ set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*")
set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;) set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;)
file(GLOB_RECURSE SOURCES src/*.cpp) file(GLOB_RECURSE SOURCES src/*.cpp)
file(GLOB_RECURSE HEADERS src/*.h) file(GLOB_RECURSE HEADERS src/*.hpp)
file(GLOB_RECURSE TESTS test/*.cpp) 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.h) add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.hpp)
target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a)

View File

@ -2,59 +2,38 @@
#include "../src/demo.h" #include "../src/demo.h"
#include "internal.hpp" #include "internal.hpp"
#include "vkvm.hpp" #include "vkvm.hpp"
#include "../src/DrawRender.h" #include "../src/DrawRender.hpp"
#include <unistd.h>
/** /**
* @author: Shaohua Tong * @author: Shaohua Tong
* @since: v0.0.0 * @since: v0.0.1
* A circle and squre is converted and displayed in the console. * Circle and Square and Mouse brush can show in GUI
* Currently only to test.
*/ */
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);
/*************************set back to shared memory(only for test)********************************************/ vkvm::registerEvent(vkvm::EventType::MouseDrag, [](){
int windowWidth = 30; std::cout << "drag" << std::endl;
int windowHeight = 30; vkvm::Color backgroundColor = vkvm::getBackgroundColor();
int penWidth = 2; vkvm::Color penColor(160,180,123);
vkvm::setWidth(windowWidth); int penWidth = 10;
vkvm::setHeight(windowHeight); DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth);
//vkvm::setPenWIdth(penWidth); maybe drawRender.update(vkvm::getMousePosition(), BRUSH);
});
vkvm::Color penColor1(1, 1, 1); vkvm::registerEvent(vkvm::EventType::MouseButton, [](){
vkvm::Color backgroudColor1(255, 255, 255); vkvm::Color backgroundColor = vkvm::getBackgroundColor();
vkvm::setForegroundColor(penColor1); vkvm::Color penColor(160,180,123);
vkvm::setBackgroundColor(backgroudColor1); int penWidth = 10;
/**************************get color, widnowsWidth, windowsHeightm, penColor, penWidth from shared memory****************************/ DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth);
vkvm::Color penColor = vkvm::getForegroundColor(); drawRender.update(vkvm::getMousePosition(), CIRCLE);
vkvm::Color backgroundColor = vkvm::getBackgroundColor(); });
DrawRender drawRender(windowWidth, windowHeight, backgroundColor, penColor, penWidth);
/*************************get Mouseposition and update back to shared meomory********************************************/
std::string command;
std::cout << "DrawRender: ";
std::getline(std::cin, command);
//vkvm::getMousePosition(); while(1){
vkvm::Coordinates mousePostiion; sleep(5);
mousePostiion.x = 15;
mousePostiion.y = 15;
while(command.compare("quit") != 0) {
if(command.compare("circle") == 0) {
drawRender.update(mousePostiion, CIRCLE);
outPutPixel(windowHeight, windowWidth, penColor);
}
if(command.compare("square") == 0) {
drawRender.update(mousePostiion, SQUARE);
outPutPixel(windowHeight, windowWidth, penColor);
}
if(command.compare("clear")) {
drawRender.clear();
}
std::cout << "DrawRender: ";
std::getline(std::cin, command);
} }
return 0; return 0;
@ -72,4 +51,6 @@ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) {
} }
std::cout << "\n"; std::cout << "\n";
} }
} }

View File

@ -2,7 +2,7 @@
// Created by shaohuatong on 21.11.19. // Created by shaohuatong on 21.11.19.
// //
#include "DrawRender.h" #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 backgroundColor, vkvm::Color penColor, int penWidth)
@ -13,8 +13,15 @@ DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color background
} }
void DrawRender::update(vkvm::Coordinates mousePostion, int type) { void DrawRender::update(vkvm::Coordinates mousePostion, int type) {
std::vector<std::vector<bool>> graphics = circleAndSquareCreator(mousePostion, type); if(type == BRUSH) {
translateToSharedMemory(graphics, startX, startY, type); std::vector<std::vector<bool>> circleBrush = brushCreator(mousePostion);
translateToSharedMemory(circleBrush, startX, startY, type);
}
else if(type == CIRCLE || type == SQUARE) {
std::vector<std::vector<bool>> graphics = circleAndSquareCreator(mousePostion, type);
translateToSharedMemory(graphics, startX, startY, type);
}
} }
void DrawRender::clear() { void DrawRender::clear() {
@ -69,20 +76,49 @@ std::vector<std::vector<bool>> DrawRender::circleAndSquareCreator(vkvm::Coordina
return circle; return circle;
} }
std::vector<std::vector<bool>> DrawRender::brushCreator(vkvm::Coordinates mousePosition) {
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 type) { void DrawRender::translateToSharedMemory(std::vector<std::vector<bool>> graphics, int startX, int startY, int type) {
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) {
if(type == CIRCLE || type == SQUARE) {
for(y = 0; y < 2 * radius; y++) { for(y = 0; y < 2 * radius; y++) {
for(x = 0; x < 2 * radius; x++) { for(x = 0; x < 2 * radius; x++) {
if(graphics[y][x]) { if(graphics[y][x]) {
vkvm::setPixel(currentX, currentY, penColor); vkvm::setPixel(currentX, currentY, penColor);
} }
else {
vkvm::setPixel(currentX, currentY, backgroundColor);
}
currentX++; currentX++;
} }
currentX = startX; currentX = startX;

View File

@ -1,8 +1,8 @@
// //
// Created by shaohuatong on 21.11.19. // Created by shaohuatong on 21.11.19.
// //
#ifndef SIMPLE_DRAW_DRAWRENDER_H #ifndef SIMPLE_DRAW_DRAWRENDER_HPP
#define SIMPLE_DRAW_DRAWRENDER_H #define SIMPLE_DRAW_DRAWRENDER_HPP
#include <string> #include <string>
#include <Color.hpp> #include <Color.hpp>
@ -20,7 +20,7 @@ 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 setType(); void setType();
void update(vkvm::Coordinates mousePostion, int type); void update(vkvm::Coordinates mousePosition, int type);
void clear(); void clear();
private: private:
@ -36,9 +36,13 @@ private:
int radius; int radius;
int startX; int startX;
int startY; int startY;
bool turnOnBrush;
std::vector<std::vector<bool>> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); std::vector<std::vector<bool>> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type);
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); void translateToSharedMemory(std::vector<std::vector<bool>> graphics, int startX, int startY, int Type);
}; };
#endif //SIMPLE_DRAW_DRAWRENDER_H #endif //SIMPLE_DRAW_DRAWRENDER_HPP