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"
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);
}
std::cout << "TextRender: " << std::endl;
outPutPixel(windowHeight, windowWidth, fontColor);
std::cout << "TextRender: ";
std::getline(std::cin, command);
} else {
textRenderer.clear();
}
}
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: ";

View File

@ -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<std::vector<bool>> 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<std::vector<bool>> TextRenderer::getCharacter(unsigned char character, Font font) {
int fontHeight = font.height();
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++) {
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<std::vector<bool>> 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<std::vector<bool>> 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++;
}
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 {
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<std::vector<bool>> 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<std::vector<bool>> characterBitmap);
void translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY);
};