From c3f6862e1678ed41346b54cca2584891308c7acb Mon Sep 17 00:00:00 2001 From: Shaohua Tong Date: Wed, 20 Nov 2019 15:51:01 +0100 Subject: [PATCH] add test method and change datatype of character_bitmap from bool** to vector> --- main/main.cpp | 40 +++++++++++++++---- src/TextRenderer.cpp | 92 +++++++++++++++++++++++++++++--------------- src/TextRenderer.h | 15 +++++--- 3 files changed, 104 insertions(+), 43 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index e698eb3..49a775d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -15,14 +15,19 @@ bool isQuit(std::string command); */ #include "TextRenderer.h" +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor); int main() { vkvm::initialize(0); std::string currentText; - /*************************set back to shared memory(only for test)********************************************/ + int windowWidth = 40; + int windowHeight = 15; + vkvm::setWidth(windowWidth); + vkvm::setHeight(windowHeight); + vkvm::Color fontColor1(0, 0, 0); vkvm::Color backgroudColor1(255, 255, 255); Font font1("../res/font3.bmp", "../res/font3.toml"); @@ -33,22 +38,27 @@ int main() { std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".bmp"; std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".toml"; Font font = Font(fontResourcePath, fontConfigureFilePath); - TextRenderer textRenderer(vkvm::Color(255, 255, 255), vkvm::Color(0, 0, 0). font); vkvm::Color fontColor = vkvm::getForegroundColor(); vkvm::Color backgroundColor = vkvm::getBackgroundColor(); - /*********************************************************************/ + TextRenderer textRenderer(windowWidth, windowHeight, backgroundColor, fontColor, font); + textRenderer.setLeftMargin(1); + textRenderer.setBottomMargin(1); + /*************************get Text and update back to shared meomory********************************************/ std::string command; - std::cout << "TextRender: " << std::endl; + std::cout << "TextRender: "; std::getline(std::cin, command); while(!isQuit(command)) { - if(command.compare("update")) { - vkvm::setText("Hello World."); + if(command.compare("clear")) { + vkvm::setText(command); currentText = vkvm::getText(); textRenderer.update(currentText); + outPutPixel(windowHeight, windowWidth, fontColor); + std::cout << "TextRender: "; + std::getline(std::cin, command); + } else { + textRenderer.clear(); } - std::cout << "TextRender: " << std::endl; - std::getline(std::cin, command); } std::cout << "TextRender finished." << std::endl; @@ -56,6 +66,20 @@ int main() { return 0; } +/***************************read pixel in shared memory and test output in console******************************************/ +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor) { + for(int y = 0; y < windowHeight; y++) { + for(int x = 0; x < windowWidth; x++) { + if(vkvm::getPixel(x, y).getRed() == fontColor.getRed()) { + std::cout << "*"; + } else { + std::cout << " "; + } + } + std::cout << "\n"; + } +} + // std::string str; // std::cout << "string to draw: "; diff --git a/src/TextRenderer.cpp b/src/TextRenderer.cpp index ca0c4e0..c827131 100644 --- a/src/TextRenderer.cpp +++ b/src/TextRenderer.cpp @@ -3,47 +3,57 @@ // #include "TextRenderer.h" -TextRenderer::TextRenderer(vkvm::Color backgroundColor, vkvm::Color fontColor, Font font) +TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color fontColor, Font font) : backgroundColor(backgroundColor), fontColor(fontColor), font(font) { - windowWidth = vkvm::getWidth(); + this-> windowWidth = windowWidth; + this-> windowHeight = windowHeight; fontWidth = font.width(); fontHeight = font.height(); } void TextRenderer::update(std::string newText) { - int startX; - int startY; + int startX = 0; + int startY = 0; int i; - int fontNumbersInOneLine = windowWidth / fontWidth; - bool** characterBitmap; - - std::cout << "get window's width from shared memery: (" << windowWidth << ")" << std::endl; + int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin); + std::vector> characterBitmap; for(i = 0; i < newText.size(); i++) { if(i > oldText.size() || oldText[i] != newText[i]) { - startX = i % fontNumbersInOneLine * fontWidth; - startY = i / fontNumbersInOneLine * fontHeight; + startX = (i % fontNumbersInOneLine) * (fontWidth + left_margin); + startY = (i / fontNumbersInOneLine) * (fontHeight + bottom_margin); characterBitmap = getCharacter(newText[i], font); - fontProcessing(characterBitmap); +// fontProcessing(characterBitmap); translateToSharedMemory(characterBitmap, startX, startY); } } - oldText = newText; + setOldText(newText); +} + +void TextRenderer::clear() { + int x, y; + for(y = 0; y < windowHeight; y++) { + for(x = 0; x < windowWidth; x++) { + vkvm::setPixel(x, y, backgroundColor); + } + } } void TextRenderer::setOldText(std::string text) { - + oldText = text; } -bool** TextRenderer::getCharacter(unsigned char character, Font font) { +std::vector> TextRenderer::getCharacter(unsigned char character, Font font) { int fontHeight = font.height(); int fontWidth = font.width(); - bool **bitmap_character; + std::vector> bitmap_character; - bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*)); +// bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*)); + bitmap_character.resize(fontHeight); for (int i = 0; i < fontHeight; i++) { - bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool)); +// bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool)); + bitmap_character[i].resize(fontWidth); for (int j = 0; j < fontWidth; j++) { bitmap_character[i][j] = font.getPixel(character, j, i); } @@ -52,8 +62,8 @@ bool** TextRenderer::getCharacter(unsigned char character, Font font) { return bitmap_character; } -void TextRenderer::fontProcessing(bool **characterBitmap) { - if(isBold()) { +void TextRenderer::fontProcessing(std::vector> characterBitmap) { +// if(isBold()) { for (int i = fontHeight - 1; i >= 0; i--) { for (int j = fontWidth - 1; j >= 0; j--) { if (i != fontHeight - 1 && j != fontWidth - 1 && characterBitmap[i][j]) { @@ -62,11 +72,11 @@ void TextRenderer::fontProcessing(bool **characterBitmap) { } } } - } - if(isUnderline()) { - for (int j = 0; j < fontWidth; j++) - characterBitmap[fontHeight - 1][j] = true; - } +// } +// if(isUnderline()) { +// for (int j = 0; j < fontWidth; j++) +// characterBitmap[fontHeight - 1][j] = true; +// } } bool TextRenderer::isBold() { @@ -77,20 +87,42 @@ bool TextRenderer::isUnderline() { return type & UNDERLINE != 0; } -void TextRenderer::translateToSharedMemory(bool **characterBitmap, int startX, int startY) { +void TextRenderer::translateToSharedMemory(std::vector> characterBitmap, int startX, int startY) { int x, y; int currentX = startX; int currentY = startY; for(y = 0; y < fontHeight; y++) { for(x = 0; x < fontWidth; x++) { - if(characterBitmap[y + startY][x + startX]) - setPixel(currentX, currentY, fontColor); - else - setPixel(currentX, currentY, backgroundColor); + if(characterBitmap[y][x]) { + vkvm::setPixel(currentX, currentY, fontColor); + } + else { + vkvm::setPixel(currentX, currentY, backgroundColor); + } currentX++; } currentX = startX; currentY++; } -} \ No newline at end of file + + for(x = 0; x < left_margin; x++) { + for(y = 0; y < fontHeight; y++) { + vkvm::setPixel(startX + fontWidth + x, startY + y, backgroundColor); + } + } + + for(y = 0; y < bottom_margin; y++) { + for(x = 0; x < fontWidth + left_margin; x++) { + vkvm::setPixel(startX + x, startY + fontHeight + y, backgroundColor); + } + } +} + +void TextRenderer::setLeftMargin(int margin) { + left_margin = margin; +} + +void TextRenderer::setBottomMargin(int margin) { + bottom_margin = margin; +} diff --git a/src/TextRenderer.h b/src/TextRenderer.h index 65ced25..1195563 100644 --- a/src/TextRenderer.h +++ b/src/TextRenderer.h @@ -22,27 +22,32 @@ */ class TextRenderer { public: - TextRenderer(vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font); + TextRenderer(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font); void update(std::string text); void setOldText(std::string text); - bool** getCharacter(unsigned char character, Font font); + std::vector> getCharacter(unsigned char character, Font font); + void setLeftMargin(int margin); + void setBottomMargin(int margin); + void clear(); private: std::string oldText; vkvm::Color backgroundColor; vkvm::Color fontColor; - vkvm::FontType fontType = vkvm::getFont(); Font font; + int left_margin = 1; + int bottom_margin = 1; int type; int windowWidth; + int windowHeight; int fontWidth; int fontHeight; bool isBold(); bool isItalics(); bool isUnderline(); - void fontProcessing(bool **characterBitmap); - void translateToSharedMemory(bool **characterBitmap, int startX, int startY); + void fontProcessing(std::vector> characterBitmap); + void translateToSharedMemory(std::vector> characterBitmap, int startX, int startY); };