From 7de540d01a1024fa3ed873b8d884658be2beea06 Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Fri, 22 Nov 2019 19:45:52 +0100 Subject: [PATCH] simple draw about circle and square --- CMakeLists.txt | 2 +- main/main.cpp | 72 ++++++++++++++++++++++++- src/DrawRender.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++ src/DrawRender.h | 44 ++++++++++++++++ 4 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 src/DrawRender.cpp create mode 100644 src/DrawRender.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d98dc7e..4636246 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) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.h) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index 53edc26..025e5f0 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1,75 @@ +#include #include "../src/demo.h" +#include "internal.hpp" +#include "vkvm.hpp" +#include "../src/DrawRender.h" + +/** + * @author: Shaohua Tong + * @since: v0.0.0 + * A circle and squre is converted and displayed in the console. + * Currently only to test. + */ +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor); int main() { - return test(); + vkvm::initialize(0); + + /*************************set back to shared memory(only for test)********************************************/ + int windowWidth = 30; + int windowHeight = 30; + int penWidth = 2; + vkvm::setWidth(windowWidth); + vkvm::setHeight(windowHeight); + //vkvm::setPenWIdth(penWidth); maybe + + vkvm::Color penColor1(1, 1, 1); + vkvm::Color backgroudColor1(255, 255, 255); + vkvm::setForegroundColor(penColor1); + vkvm::setBackgroundColor(backgroudColor1); + /**************************get color, widnowsWidth, windowsHeightm, penColor, penWidth from shared memory****************************/ + vkvm::Color penColor = vkvm::getForegroundColor(); + 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(); + vkvm::Coordinates mousePostiion; + 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; +} + +/***************************read pixel in shared memory and test output in console******************************************/ +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) { + for(int y = 0; y < windowHeight; y++) { + for(int x = 0; x < windowWidth; x++) { + if(vkvm::getPixel(x, y).getRed() == penColor.getRed()) { + std::cout << "*"; + } else { + std::cout << " "; + } + } + std::cout << "\n"; + } } \ No newline at end of file diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp new file mode 100644 index 0000000..8735225 --- /dev/null +++ b/src/DrawRender.cpp @@ -0,0 +1,127 @@ +// +// Created by shaohuatong on 21.11.19. +// + +#include "DrawRender.h" +#include + +DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color penColor, int penWidth) + : backgroundColor(backgroundColor), penColor(penColor) { + this-> windowWidth = windowWidth; + this-> windowHeight = windowHeight; + this-> penWidth = penWidth; +} + +void DrawRender::update(vkvm::Coordinates mousePostion, int type) { + std::vector> graphics = circleAndSquareCreator(mousePostion, type); + translateToSharedMemory(graphics, startX, startY, type); +} + +void DrawRender::clear() { + int x, y; + for(y = 0; y < windowHeight; y++) { + for(x = 0; x < windowWidth; x++) { + vkvm::setPixel(x, y, backgroundColor); + } + } +} + +std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordinates mousePosition, int type) { + std::vector> circle; + int x_draw = 0; + int y_draw = 0; + int distance = 0; + radius = std::min(std::min(mousePosition.x, mousePosition.y), + std::min(windowWidth - mousePosition.x, windowHeight - mousePosition.y)) * 0.8; + 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; + 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, mousePosition); + + 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; + } + } + } + } + + startX = uperLeft.x; + startY = uperLeft.y; + return circle; +} + +void DrawRender::translateToSharedMemory(std::vector> graphics, int startX, int startY, int type) { + int x, y; + int currentX = startX; + int currentY = startY; + + if(type == CIRCLE || type == SQUARE) { + for(y = 0; y < 2 * radius; y++) { + for(x = 0; x < 2 * radius; x++) { + if(graphics[y][x]) { + vkvm::setPixel(currentX, currentY, penColor); + } + else { + vkvm::setPixel(currentX, currentY, backgroundColor); + } + currentX++; + } + currentX = startX; + currentY++; + } + } +} + +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); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/DrawRender.h b/src/DrawRender.h new file mode 100644 index 0000000..9eaa8e1 --- /dev/null +++ b/src/DrawRender.h @@ -0,0 +1,44 @@ +// +// Created by shaohuatong on 21.11.19. +// +#ifndef SIMPLE_DRAW_DRAWRENDER_H +#define SIMPLE_DRAW_DRAWRENDER_H + +#include +#include +#include +#include +#include "Font.h" +#include "vector" + +#define CIRCLE 0 +#define SQUARE 1 +#define BRUSH 2 + +class DrawRender { +public: + DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); + + void setType(); + void update(vkvm::Coordinates mousePostion, int type); + void clear(); + +private: + vkvm::Coordinates mousePosition; + vkvm::Color backgroundColor; + vkvm::Color penColor; + int type; + int penWidth; + int windowWidth; + int windowHeight; + int graphicsHeight; + int graphicsWidth; + int radius; + int startX; + int startY; + std::vector> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); + int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); + void translateToSharedMemory(std::vector> graphics, int startX, int startY, int Type); +}; + +#endif //SIMPLE_DRAW_DRAWRENDER_H