#include "DrawRender.hpp" #include "vkvm.hpp" #include #include #include DrawRender::DrawRender(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color penColor, int penWidth) : backgroundColor(defaultBackgroundColor), penColor(penColor) { this-> windowWidth = windowWidth; this-> windowHeight = windowHeight; this-> penWidth = penWidth; } void DrawRender::graphicsUpdate(int type) { std::vector> shape; if (type == CIRCLE) { if(painting) clearToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y); int radius = utils::getDistance(getMouseLeftDownPosition(), getMousePostion()) / 2; vkvm::Coordinates center; center.x = (mousePosition.x + mouseLeftDownPosition.x) / 2; center.y = (mousePosition.y + mouseLeftDownPosition.y) / 2; if(center.x + radius > windowWidth || center.x - radius < 0 || center.y + radius > windowHeight || center.y - radius < 0) { radius = std::min(std::min(center.x, windowWidth - center.x), std::min(center.y, windowHeight - center.y)); } oldCircle = Circle(center, radius, penWidth, false); painting = true; translateToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, false); if(finish) shapes.addShape(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, oldCircle.getBottomRight().x, oldCircle.getBottomRight().y); } else if (type == RECTANGLE) { if(painting) clearToSharedMemory(oldRectangle.getRectangle(), oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y); oldRectangle = Rectangle(mouseLeftDownPosition, mousePosition, penWidth); painting = true; translateToSharedMemory(oldRectangle.getRectangle(), oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y, false); if(finish) shapes.addShape(oldRectangle.getRectangle(), oldRectangle.getUperLeft().x, oldRectangle.getUperLeft().y, oldRectangle.getBottomRight().x, oldRectangle.getBottomRight().y); } else if (type == BRUSH) { int radius = penWidth; vkvm::Coordinates center = mousePosition; oldCircle = Circle(center, radius, penWidth, true); translateToSharedMemory(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, false); if(finish) shapes.addShape(oldCircle.getCircle(), oldCircle.getUperLeft().x, oldCircle.getUperLeft().y, oldCircle.getBottomRight().x, oldCircle.getBottomRight().y); } else if (type == CURSOR) { if(cursorStart != 0) clearToSharedMemory(oldCursor.getCursor(), oldCursor.getUperLeft().x, oldCursor.getUperLeft().y); int radius = 10; oldCursor = Cursor(mousePosition, 1, radius); cursorStart = 1; translateToSharedMemory(oldCursor.getCursor(), oldCursor.getUperLeft().x, oldCursor.getUperLeft().y, true); } else if (type == SHAPE) { for(int i = 0; i < shapes.getCount(); i++) { penColor = vkvm::Color(rand() % 255,rand() % 255,rand() % 255); translateToSharedMemory(shapes.getShape(i), shapes.getStartX(i), shapes.getStartY(i), false); } } } void DrawRender::clear() { int x, y; for(y = 0; y < windowHeight; y++) { for(x = 0; x < windowWidth; x++) { vkvm::setPixel(x, y, backgroundColor); } } shapes = Shapes(); } void DrawRender::cursorCreator() { } void DrawRender::translateToSharedMemory(std::vector> shape, int startX, int startY, bool isCursor) { int x, y; int currentX = startX; int currentY = startY; for(y = 0; y < shape.size(); y++) { for(x = 0; x < shape[y].size(); x++) { if(shape[y][x]) { if(isCursor) vkvm::setPixel(currentX, currentY, vkvm::Color(0,238,0)); else if(turnOnBrush || finish) vkvm::setPixel(currentX, currentY, penColor); else vkvm::setPixel(currentX, currentY, vkvm::Color(217,217,217)); } currentX++; } currentX = startX; currentY++; } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } void DrawRender::clearToSharedMemory(std::vector> shape, int startX, int startY) { int x, y; int currentX = startX; int currentY = startY; for(y = 0; y < shape.size(); y++) { for(x = 0; x < shape[y].size(); x++) { if((shape[y][x])) { if(!shapes.containsPixel(x + startX, y + startY)) { vkvm::setPixel(currentX, currentY, backgroundColor); } else { vkvm::setPixel(currentX, currentY, penColor); } } currentX++; } currentX = startX; currentY++; } } 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) { if(newMousePosition.x >= 0 && newMousePosition.x <= windowWidth && newMousePosition.y >= 30 && newMousePosition.y <= windowHeight) { } else { newMousePosition.x = newMousePosition.x < 0 ? 0: newMousePosition.x; newMousePosition.y = newMousePosition.y < 30 ? 0 : newMousePosition.y; newMousePosition.x = newMousePosition.x > windowWidth ? windowWidth : newMousePosition.x; newMousePosition.y = newMousePosition.y > windowHeight ? windowHeight : newMousePosition.y; } 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; } bool DrawRender::getMouseDown() { return mouseDown; } void DrawRender::setMouseDown(bool isMouseDown) { mouseDown = isMouseDown; } bool DrawRender::getPainting() { return painting; } void DrawRender::setPainting(bool isPainting) { painting = isPainting; } void DrawRender::setFinish(bool isFinish) { finish = isFinish; } bool DrawRender::getFinish() { return finish; }