simple draw about circle and square
This commit is contained in:
parent
408d94d538
commit
7de540d01a
@ -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)
|
||||
|
||||
|
@ -1,5 +1,75 @@
|
||||
#include <iostream>
|
||||
#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";
|
||||
}
|
||||
}
|
127
src/DrawRender.cpp
Normal file
127
src/DrawRender.cpp
Normal file
@ -0,0 +1,127 @@
|
||||
//
|
||||
// Created by shaohuatong on 21.11.19.
|
||||
//
|
||||
|
||||
#include "DrawRender.h"
|
||||
#include <algorithm>
|
||||
|
||||
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<std::vector<bool>> 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<std::vector<bool>> DrawRender::circleAndSquareCreator(vkvm::Coordinates mousePosition, int type) {
|
||||
std::vector<std::vector<bool>> 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<std::vector<bool>> 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
44
src/DrawRender.h
Normal file
44
src/DrawRender.h
Normal file
@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by shaohuatong on 21.11.19.
|
||||
//
|
||||
#ifndef SIMPLE_DRAW_DRAWRENDER_H
|
||||
#define SIMPLE_DRAW_DRAWRENDER_H
|
||||
|
||||
#include <string>
|
||||
#include <Color.hpp>
|
||||
#include <vkvm.hpp>
|
||||
#include <iostream>
|
||||
#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<std::vector<bool>> circleAndSquareCreator(vkvm::Coordinates mousePosition, int type);
|
||||
int squareOfDistance(vkvm::Coordinates x, vkvm::Coordinates y);
|
||||
void translateToSharedMemory(std::vector<std::vector<bool>> graphics, int startX, int startY, int Type);
|
||||
};
|
||||
|
||||
#endif //SIMPLE_DRAW_DRAWRENDER_H
|
Loading…
Reference in New Issue
Block a user