diff --git a/CMakeLists.txt b/CMakeLists.txt index 4636246..bea93b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,13 +16,13 @@ set(CMAKE_CXX_CLANG_TIDY "clang-tidy;-checks=*") set(CMAKE_CXX_CLANG_TIDY clang-tidy;-header-filter=.;-checks=*;) file(GLOB_RECURSE SOURCES src/*.cpp) -file(GLOB_RECURSE HEADERS src/*.h) +file(GLOB_RECURSE HEADERS src/*.hpp) 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.h) +add_executable(SimpleDraw ${SOURCES} ${HEADERS} main/main.cpp src/DrawRender.cpp src/DrawRender.hpp) target_link_libraries(SimpleDraw ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index 025e5f0..fa07588 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -2,59 +2,38 @@ #include "../src/demo.h" #include "internal.hpp" #include "vkvm.hpp" -#include "../src/DrawRender.h" +#include "../src/DrawRender.hpp" +#include /** * @author: Shaohua Tong - * @since: v0.0.0 - * A circle and squre is converted and displayed in the console. - * Currently only to test. + * @since: v0.0.1 + * Circle and Square and Mouse brush can show in GUI */ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color brushColor); int main() { 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::registerEvent(vkvm::EventType::MouseDrag, [](){ + std::cout << "drag" << std::endl; + vkvm::Color backgroundColor = vkvm::getBackgroundColor(); + vkvm::Color penColor(160,180,123); + int penWidth = 10; + DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); + drawRender.update(vkvm::getMousePosition(), BRUSH); + }); - 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::registerEvent(vkvm::EventType::MouseButton, [](){ + vkvm::Color backgroundColor = vkvm::getBackgroundColor(); + vkvm::Color penColor(160,180,123); + int penWidth = 10; + DrawRender drawRender(vkvm::getWidth(), vkvm::getHeight(), backgroundColor, penColor, penWidth); + drawRender.update(vkvm::getMousePosition(), CIRCLE); + }); - //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); + while(1){ + sleep(5); } return 0; @@ -72,4 +51,6 @@ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color penColor) { } std::cout << "\n"; } -} \ No newline at end of file +} + + diff --git a/src/DrawRender.cpp b/src/DrawRender.cpp index 8735225..93fa6a4 100644 --- a/src/DrawRender.cpp +++ b/src/DrawRender.cpp @@ -2,7 +2,7 @@ // Created by shaohuatong on 21.11.19. // -#include "DrawRender.h" +#include "DrawRender.hpp" #include 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) { - std::vector> graphics = circleAndSquareCreator(mousePostion, type); - translateToSharedMemory(graphics, startX, startY, type); + if(type == BRUSH) { + std::vector> circleBrush = brushCreator(mousePostion); + translateToSharedMemory(circleBrush, startX, startY, type); + + } + else if(type == CIRCLE || type == SQUARE) { + std::vector> graphics = circleAndSquareCreator(mousePostion, type); + translateToSharedMemory(graphics, startX, startY, type); + } } void DrawRender::clear() { @@ -69,20 +76,49 @@ std::vector> DrawRender::circleAndSquareCreator(vkvm::Coordina 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, int type) { int x, y; int currentX = startX; int currentY = startY; - - if(type == CIRCLE || type == SQUARE) { + if(type == CIRCLE || type == SQUARE || type == BRUSH) { 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; diff --git a/src/DrawRender.h b/src/DrawRender.hpp similarity index 77% rename from src/DrawRender.h rename to src/DrawRender.hpp index 9eaa8e1..543d238 100644 --- a/src/DrawRender.h +++ b/src/DrawRender.hpp @@ -1,8 +1,8 @@ // // Created by shaohuatong on 21.11.19. // -#ifndef SIMPLE_DRAW_DRAWRENDER_H -#define SIMPLE_DRAW_DRAWRENDER_H +#ifndef SIMPLE_DRAW_DRAWRENDER_HPP +#define SIMPLE_DRAW_DRAWRENDER_HPP #include #include @@ -20,7 +20,7 @@ public: DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth); void setType(); - void update(vkvm::Coordinates mousePostion, int type); + void update(vkvm::Coordinates mousePosition, int type); void clear(); private: @@ -36,9 +36,13 @@ private: int radius; int startX; int startY; + bool turnOnBrush; + std::vector> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type); + std::vector> brushCreator(vkvm::Coordinates mousePosition); + int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y); void translateToSharedMemory(std::vector> graphics, int startX, int startY, int Type); }; -#endif //SIMPLE_DRAW_DRAWRENDER_H +#endif //SIMPLE_DRAW_DRAWRENDER_HPP