add test method and change datatype of character_bitmap from bool** to vector<vector<bool>>

This commit is contained in:
Shaohua Tong 2019-11-20 15:51:01 +01:00
parent ab2d13f6bb
commit c3f6862e16
3 changed files with 104 additions and 43 deletions

View File

@ -15,14 +15,19 @@ bool isQuit(std::string command);
*/ */
#include "TextRenderer.h" #include "TextRenderer.h"
void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor);
int main() { int main() {
vkvm::initialize(0); vkvm::initialize(0);
std::string currentText; std::string currentText;
/*************************set back to shared memory(only for test)********************************************/ /*************************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 fontColor1(0, 0, 0);
vkvm::Color backgroudColor1(255, 255, 255); vkvm::Color backgroudColor1(255, 255, 255);
Font font1("../res/font3.bmp", "../res/font3.toml"); 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 fontResourcePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".toml"; std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".toml";
Font font = Font(fontResourcePath, fontConfigureFilePath); 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 fontColor = vkvm::getForegroundColor();
vkvm::Color backgroundColor = vkvm::getBackgroundColor(); 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::string command;
std::cout << "TextRender: " << std::endl; std::cout << "TextRender: ";
std::getline(std::cin, command); std::getline(std::cin, command);
while(!isQuit(command)) { while(!isQuit(command)) {
if(command.compare("update")) { if(command.compare("clear")) {
vkvm::setText("Hello World."); vkvm::setText(command);
currentText = vkvm::getText(); currentText = vkvm::getText();
textRenderer.update(currentText); 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; std::cout << "TextRender finished." << std::endl;
@ -56,6 +66,20 @@ int main() {
return 0; 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::string str;
// std::cout << "string to draw: "; // std::cout << "string to draw: ";

View File

@ -3,47 +3,57 @@
// //
#include "TextRenderer.h" #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) { : backgroundColor(backgroundColor), fontColor(fontColor), font(font) {
windowWidth = vkvm::getWidth(); this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
fontWidth = font.width(); fontWidth = font.width();
fontHeight = font.height(); fontHeight = font.height();
} }
void TextRenderer::update(std::string newText) { void TextRenderer::update(std::string newText) {
int startX; int startX = 0;
int startY; int startY = 0;
int i; int i;
int fontNumbersInOneLine = windowWidth / fontWidth; int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
bool** characterBitmap; std::vector<std::vector<bool>> characterBitmap;
std::cout << "get window's width from shared memery: (" << windowWidth << ")" << std::endl;
for(i = 0; i < newText.size(); i++) { for(i = 0; i < newText.size(); i++) {
if(i > oldText.size() || oldText[i] != newText[i]) { if(i > oldText.size() || oldText[i] != newText[i]) {
startX = i % fontNumbersInOneLine * fontWidth; startX = (i % fontNumbersInOneLine) * (fontWidth + left_margin);
startY = i / fontNumbersInOneLine * fontHeight; startY = (i / fontNumbersInOneLine) * (fontHeight + bottom_margin);
characterBitmap = getCharacter(newText[i], font); characterBitmap = getCharacter(newText[i], font);
fontProcessing(characterBitmap); // fontProcessing(characterBitmap);
translateToSharedMemory(characterBitmap, startX, startY); 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) { void TextRenderer::setOldText(std::string text) {
oldText = text;
} }
bool** TextRenderer::getCharacter(unsigned char character, Font font) { std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character, Font font) {
int fontHeight = font.height(); int fontHeight = font.height();
int fontWidth = font.width(); int fontWidth = font.width();
bool **bitmap_character; std::vector<std::vector<bool>> 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++) { 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++) { for (int j = 0; j < fontWidth; j++) {
bitmap_character[i][j] = font.getPixel(character, j, i); bitmap_character[i][j] = font.getPixel(character, j, i);
} }
@ -52,8 +62,8 @@ bool** TextRenderer::getCharacter(unsigned char character, Font font) {
return bitmap_character; return bitmap_character;
} }
void TextRenderer::fontProcessing(bool **characterBitmap) { void TextRenderer::fontProcessing(std::vector<std::vector<bool>> characterBitmap) {
if(isBold()) { // if(isBold()) {
for (int i = fontHeight - 1; i >= 0; i--) { for (int i = fontHeight - 1; i >= 0; i--) {
for (int j = fontWidth - 1; j >= 0; j--) { for (int j = fontWidth - 1; j >= 0; j--) {
if (i != fontHeight - 1 && j != fontWidth - 1 && characterBitmap[i][j]) { if (i != fontHeight - 1 && j != fontWidth - 1 && characterBitmap[i][j]) {
@ -62,11 +72,11 @@ void TextRenderer::fontProcessing(bool **characterBitmap) {
} }
} }
} }
} // }
if(isUnderline()) { // if(isUnderline()) {
for (int j = 0; j < fontWidth; j++) // for (int j = 0; j < fontWidth; j++)
characterBitmap[fontHeight - 1][j] = true; // characterBitmap[fontHeight - 1][j] = true;
} // }
} }
bool TextRenderer::isBold() { bool TextRenderer::isBold() {
@ -77,20 +87,42 @@ bool TextRenderer::isUnderline() {
return type & UNDERLINE != 0; return type & UNDERLINE != 0;
} }
void TextRenderer::translateToSharedMemory(bool **characterBitmap, int startX, int startY) { void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) {
int x, y; int x, y;
int currentX = startX; int currentX = startX;
int currentY = startY; int currentY = startY;
for(y = 0; y < fontHeight; y++) { for(y = 0; y < fontHeight; y++) {
for(x = 0; x < fontWidth; x++) { for(x = 0; x < fontWidth; x++) {
if(characterBitmap[y + startY][x + startX]) if(characterBitmap[y][x]) {
setPixel(currentX, currentY, fontColor); vkvm::setPixel(currentX, currentY, fontColor);
else }
setPixel(currentX, currentY, backgroundColor); else {
vkvm::setPixel(currentX, currentY, backgroundColor);
}
currentX++; currentX++;
} }
currentX = startX; currentX = startX;
currentY++; 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;
}

View File

@ -22,27 +22,32 @@
*/ */
class TextRenderer { class TextRenderer {
public: 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 update(std::string text);
void setOldText(std::string text); void setOldText(std::string text);
bool** getCharacter(unsigned char character, Font font); std::vector<std::vector<bool>> getCharacter(unsigned char character, Font font);
void setLeftMargin(int margin);
void setBottomMargin(int margin);
void clear();
private: private:
std::string oldText; std::string oldText;
vkvm::Color backgroundColor; vkvm::Color backgroundColor;
vkvm::Color fontColor; vkvm::Color fontColor;
vkvm::FontType fontType = vkvm::getFont();
Font font; Font font;
int left_margin = 1;
int bottom_margin = 1;
int type; int type;
int windowWidth; int windowWidth;
int windowHeight;
int fontWidth; int fontWidth;
int fontHeight; int fontHeight;
bool isBold(); bool isBold();
bool isItalics(); bool isItalics();
bool isUnderline(); bool isUnderline();
void fontProcessing(bool **characterBitmap); void fontProcessing(std::vector<std::vector<bool>> characterBitmap);
void translateToSharedMemory(bool **characterBitmap, int startX, int startY); void translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY);
}; };