realize the interface of TextRender and now can make the font size bigger

This commit is contained in:
my 2019-11-27 15:16:35 +01:00
parent c3f6862e16
commit 557f14cc5e
3 changed files with 255 additions and 47 deletions

View File

@ -3,6 +3,7 @@
#include "Font.h"
#include <iostream>
#include <internal.hpp>
bool isQuit(std::string command);
@ -15,11 +16,109 @@ bool isQuit(std::string command);
*/
#include "TextRenderer.h"
void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor);
void outputWindow(int windowHeight, int windowWidth, vkvm::Color fontColor);
TextRenderer generateTextRender();
void renderText(TextRenderer textRenderer);
void test();
int main() {
void test(TextRenderer renderer);
void redraw(TextRenderer renderer);
void test1();
int main(int argc, char *argv[]) {
vkvm::initialize(0);
TextRenderer textRenderer = generateTextRender();
textRenderer.clear();
// test(textRenderer);
vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer]() {
renderText(textRenderer);
});
// test1();
return 0;
}
void redraw(TextRenderer textRenderer) {
textRenderer.clear();
textRenderer.update(vkvm::getText());
}
void renderText(TextRenderer textRenderer) {
textRenderer.update(vkvm::getText());
}
TextRenderer generateTextRender() {
std::string command;
std::cout << "fontSize: ";
std::getline(std::cin, command);
int windowWidth = vkvm::getWidth();
int windowHeight = vkvm::getHeight();
vkvm::Color fontColor = vkvm::getForegroundColor();
vkvm::Color backgroundColor = vkvm::getBackgroundColor();
vkvm::FontType fontType = vkvm::getFont();
int fontSize = std::stoi(command);
int font_id = fontType.getId();
std::string fontResourcePath = "../res/font" + std::to_string(3) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(3) + ".toml";
Font font = Font(fontResourcePath, fontConfigureFilePath);
return TextRenderer(windowWidth, windowHeight, backgroundColor, fontColor, font, fontSize);
}
void test1() {
std::string command;
std::cout << "TextRender: ";
std::getline(std::cin, command);
while(command.compare("quit") != 0) {
if(command.compare("RenderText") == 0) {
vkvm::callEvent(vkvm::EventType::RenderText);
}
else if(command.compare("Redraw") == 0) {
vkvm::callEvent(vkvm::EventType::Redraw);
}
else {
vkvm::setText(command);
}
}
std::cout << "TextRender finished." << std::endl;}
void test(TextRenderer renderer) {
std::string currentText;
std::string command;
std::cout << "TextRender: ";
std::getline(std::cin, command);
while(!isQuit(command)) {
if(command.compare("clear")) {
vkvm::setText(command);
currentText = vkvm::getText();
renderer.update(currentText);
} else {
renderer.clear();
}
outputWindow(renderer.getWindowHeight(), renderer.getWindowWidth(), vkvm::getForegroundColor());
std::cout << "TextRender: ";
std::getline(std::cin, command);
}
std::cout << "TextRender finished." << std::endl;
}
void test() {
std::string currentText;
/*************************set back to shared memory(only for test)********************************************/
@ -53,7 +152,7 @@ int main() {
vkvm::setText(command);
currentText = vkvm::getText();
textRenderer.update(currentText);
outPutPixel(windowHeight, windowWidth, fontColor);
outputWindow(windowHeight, windowWidth, fontColor);
std::cout << "TextRender: ";
std::getline(std::cin, command);
} else {
@ -62,12 +161,11 @@ int main() {
}
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) {
void outputWindow(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()) {
@ -80,27 +178,6 @@ void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor) {
}
}
// 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;
}

View File

@ -3,28 +3,47 @@
//
#include "TextRenderer.h"
TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backGroundColor, vkvm::Color fontColor,
Font font, int fontSize): backgroundColor(backgroundColor), fontColor(fontColor), font(font) {
this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
currentX = 0;
currentY = 0;
fontHeight = font.height() * fontSize;
fontWidth = font.width() * fontSize;
this -> fontSize = fontSize;
}
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();
currentX = 0;
currentY = 0;
fontHeight = font.height() * fontSize;
fontWidth = font.width() * fontSize;
}
void TextRenderer::update(std::string newText) {
int startX = 0;
int startY = 0;
currentX = 0;
currentY = 0;
int i;
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 + left_margin);
startY = (i / fontNumbersInOneLine) * (fontHeight + bottom_margin);
currentX = (i % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = (i / fontNumbersInOneLine) * (fontHeight + bottom_margin);
characterBitmap = getCharacter(newText[i], font);
// fontProcessing(characterBitmap);
translateToSharedMemory(characterBitmap, startX, startY);
translateToSharedMemory(characterBitmap, currentX, currentY);
}
}
@ -40,22 +59,58 @@ void TextRenderer::clear() {
}
}
void TextRenderer::clear(int startX, int startY, int endX, int endY) {
int x, y;
for(y = startY; y < endY; y++) {
for(x = startX; x < endX; x++) {
vkvm::setPixel(x, y, backgroundColor);
}
}
}
void TextRenderer::setOldText(std::string text) {
oldText = text;
}
std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character, Font font) {
int fontHeight = font.height();
int fontWidth = font.width();
std::vector<std::vector<bool>> bitmap_character;
// bitmap_character.resize(fontHeight);
// bitmap_character.resize(fontWidth);
// return bitmap_character;
// bitmap_character = (bool**)malloc(fontHeight * sizeof(bool*));
bitmap_character.resize(fontHeight);
for (int i = 0; i < fontHeight; i++) {
for (int y = 0; y < fontHeight; y += fontSize) {
// 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);
for(int i = 0; i < fontSize; i++) {
bitmap_character[y + i].resize(fontWidth);
}
for (int x = 0; x < fontWidth; x += fontSize) {
bitmap_character[y][x] = font.getPixel(character, x / fontSize, y / fontSize);
if(bitmap_character[y][x] && fontSize != 1) {
if(y != 0) {
for (int i = y - fontSize + 1; i < y; i++) {
bitmap_character[i][x] = bitmap_character[y - fontSize][x];
}
}
if(x != 0) {
for (int i = x - fontSize + 1; i < x; i++) {
bitmap_character[y][i] = bitmap_character[y][x - fontSize];
}
}
if(x != 0 && y != 0) {
for (int i = 1; i < fontSize; i++) {
bitmap_character[y - i][x - i] = bitmap_character[y - fontSize][x - fontSize];
}
}
}
}
}
@ -89,21 +144,21 @@ bool TextRenderer::isUnderline() {
void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) {
int x, y;
int currentX = startX;
int currentY = startY;
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);
vkvm::setPixel(_currentX, _currentY, fontColor);
}
else {
vkvm::setPixel(currentX, currentY, backgroundColor);
vkvm::setPixel(_currentX, _currentY, backgroundColor);
}
currentX++;
_currentX++;
}
currentX = startX;
currentY++;
_currentX = startX;
_currentY++;
}
for(x = 0; x < left_margin; x++) {
@ -126,3 +181,58 @@ void TextRenderer::setLeftMargin(int margin) {
void TextRenderer::setBottomMargin(int margin) {
bottom_margin = margin;
}
void TextRenderer::check() {
checkWindowSize();
checkFontColor();
checkBackgroundColor();
}
void TextRenderer::setWindowWidth(int windowWidth) {
this -> windowWidth = windowWidth;
}
void TextRenderer::setWindowHeight(int windowHeight) {
this -> windowHeight = windowHeight;
}
void TextRenderer::checkWindowSize() {
}
void TextRenderer::checkFontColor() {
}
void TextRenderer::checkBackgroundColor() {
}
int TextRenderer::getWindowWidth() {
return windowWidth;
}
int TextRenderer::getWindowHeight() {
return windowHeight;
}
//void TextRenderer::setPixelRange(int startX, int startY, int endX, int endY, int type) {
// if(type == 0) {
// for(int i = startX + 1; i < endX; i++) {
// vkvm::setPixel(i, startY, fontColor);
// }
// }
//
// if(type == 1) {
// for(int i = 1; i < endX; i++) {
// vkvm::setPixel(startX + i, startY - i, fontColor);
// }
// }
//
// if(type == 2) {
// for(int i = startY + 1; i < endY; i++) {
// vkvm::setPixel(i, startY, fontColor);
// }
// }
//}

View File

@ -23,6 +23,8 @@
class TextRenderer {
public:
TextRenderer(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font);
TextRenderer(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font, int fontSize);
void update(std::string text);
void setOldText(std::string text);
@ -30,12 +32,20 @@ public:
void setLeftMargin(int margin);
void setBottomMargin(int margin);
void clear();
void clear(int startX, int startY, int endX, int endY);
void check();
void setWindowWidth(int windowWidth);
void setWindowHeight(int windowHeight);
int getWindowWidth();
int getWindowHeight();
private:
std::string oldText;
vkvm::Color backgroundColor;
vkvm::Color fontColor;
Font font;
int fontSize = 1;
int left_margin = 1;
int bottom_margin = 1;
int type;
@ -43,11 +53,22 @@ private:
int windowHeight;
int fontWidth;
int fontHeight;
int currentX;
int currentY;
bool isBold();
bool isItalics();
bool isUnderline();
void fontProcessing(std::vector<std::vector<bool>> characterBitmap);
void translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY);
void checkWindowSize();
void checkFontColor();
void checkBackgroundColor();
void checkText();
// void setPixelRange(int startX, int startY, int endX, int endY, int type);
};