diff --git a/CMakeLists.txt b/CMakeLists.txt index 808d4a5..4b7dad2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ include_directories(lib/toml) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") include_directories(${LIB_PATH}/include) -add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp) +add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp src/TextRenderer.cpp src/TextRenderer.h) target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index a9a229c..cd16cca 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,8 +1,11 @@ #include "Bitmap.h" #include "Font.h" - +#include "TextRenderer.h" +#include "internal.hpp" #include +bool isQuit(std::string command); + /** * @author: Julian Hinxlage * @since: v0.0.0 @@ -10,27 +13,102 @@ * A string is converted and displayed in the console. * Currently only to test. */ +void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor); + int main() { - Font font("../res/profont-IIx.bmp","../res/profont-IIx.toml"); - //Font font("../res/font2.bmp","../res/font2.toml"); + vkvm::initialize(0); - std::string str; - std::cout << "string to draw: "; - std::getline(std::cin, str); + std::string currentText; - for (int i = 0; i < font.height(); i++) { - for (char c : str) { - for (int j = 0; j < font.width(); j++) { - if (font.getPixel(c,j,i)) { - std::cout << "█"; - } else { - std::cout << " "; - } - } - std::cout << " "; + + /*************************set back to shared memory(only for test)********************************************/ + int windowWidth = vkvm::getWidth(); + int windowHeight = vkvm::getHeight(); + 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"); + vkvm::setFont(vkvm::FontType(3, "font", font1.height(), font1.width())); + vkvm::setForegroundColor(fontColor1); + vkvm::setBackgroundColor(backgroudColor1); + /**************************get text and font from shared memory*******************************************/ + 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); + 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********************************************/ + + vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer](){ + std::string currentText = vkvm::getText(); + textRenderer.update(currentText); + vkvm::callEvent(vkvm::EventType::Redraw); + }); + + + + std::string command; + std::cout << "TextRender: "; + std::getline(std::cin, command); + while(!isQuit(command)) { + 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 << std::endl; } + + std::cout << "TextRender finished." << std::endl; + 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: "; +// std::getline(std::cin, str); +// +// for (int i = 0; i < font.height(); i++) { +// for (char c : str) { +// for (int j = 0; j < font.width(); j++) { +// if (font.getPixel(c,j,i)) { +// std::cout << "█"; +// } else { +// std::cout << " "; +// } +// } +// std::cout << " "; +// } +// std::cout << std::endl; +// } +// +// return 0; + +bool isQuit(std::string command) { + return command.compare("quit") == 0; +} diff --git a/res/profont-IIx.bmp b/res/font3.bmp similarity index 100% rename from res/profont-IIx.bmp rename to res/font3.bmp diff --git a/res/profont-IIx.toml b/res/font3.toml similarity index 88% rename from res/profont-IIx.toml rename to res/font3.toml index 5bed19f..102a09f 100644 --- a/res/profont-IIx.toml +++ b/res/font3.toml @@ -9,4 +9,4 @@ yStart = 9 fillValue = 4294967295 firstChar = 25 pixelSize = 1 -invertedColors = 1 \ No newline at end of file +invertedColors = 1 diff --git a/src/Bitmap.h b/src/Bitmap.h index 7c82032..fd73947 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.h @@ -19,7 +19,7 @@ public: * @since: v0.0.0 * @brief: Used to load a Bitmap from a file. */ - void load(const std::string &file); + void load(const std::string &file); /** * @author: Julian Hinxlage diff --git a/src/TextRenderer.cpp b/src/TextRenderer.cpp new file mode 100644 index 0000000..c827131 --- /dev/null +++ b/src/TextRenderer.cpp @@ -0,0 +1,128 @@ +// +// Created by my on 2019/11/16. +// +#include "TextRenderer.h" + +TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color fontColor, Font font) + : backgroundColor(backgroundColor), fontColor(fontColor), font(font) { + this-> windowWidth = windowWidth; + this-> windowHeight = windowHeight; + fontWidth = font.width(); + fontHeight = font.height(); +} + +void TextRenderer::update(std::string newText) { + int startX = 0; + int startY = 0; + int i; + 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 + left_margin); + startY = (i / fontNumbersInOneLine) * (fontHeight + bottom_margin); + characterBitmap = getCharacter(newText[i], font); +// fontProcessing(characterBitmap); + translateToSharedMemory(characterBitmap, startX, startY); + } + } + + 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; +} + +std::vector> TextRenderer::getCharacter(unsigned char character, Font font) { + int fontHeight = font.height(); + int fontWidth = font.width(); + std::vector> bitmap_character; + +// 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].resize(fontWidth); + for (int j = 0; j < fontWidth; j++) { + bitmap_character[i][j] = font.getPixel(character, j, i); + } + } + + return bitmap_character; +} + +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]) { + characterBitmap[i + 1][j] = true; + characterBitmap[i][j + 1] = true; + } + } + } +// } +// if(isUnderline()) { +// for (int j = 0; j < fontWidth; j++) +// characterBitmap[fontHeight - 1][j] = true; +// } +} + +bool TextRenderer::isBold() { + return type & BOLD != 0; +} + +bool TextRenderer::isUnderline() { + return type & UNDERLINE != 0; +} + +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][x]) { + vkvm::setPixel(currentX, currentY, fontColor); + } + else { + vkvm::setPixel(currentX, currentY, backgroundColor); + } + currentX++; + } + currentX = startX; + currentY++; + } + + 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 new file mode 100644 index 0000000..1195563 --- /dev/null +++ b/src/TextRenderer.h @@ -0,0 +1,54 @@ +// +// Created by my on 2019/11/16. +// + +#ifndef TEXTRENDERER_TEXTRENDERER_H +#define TEXTRENDERER_TEXTRENDERER_H + +#define BOLD 0b001 +#define ITALICS 0b010 +#define UNDERLINE 0b100 + +#include +#include +#include +#include +#include "Font.h" + +/** + * @author: Yajie Qi, Shaohua Tong + * @since: v0.0.0 + * @brief: gets a string from the Shared Memory and converts the text into a bitmap-array. + */ +class TextRenderer { +public: + TextRenderer(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font); + + void update(std::string text); + void setOldText(std::string text); + 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; + 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(std::vector> characterBitmap); + void translateToSharedMemory(std::vector> characterBitmap, int startX, int startY); +}; + + +#endif //TEXTRENDERER_TEXTRENDERER_H