Text-Renderer/src/TextRenderer.cpp

107 lines
3.2 KiB
C++

//
// Created by my on 2019/11/16.
//
#include "TextRenderer.h"
#include <utility>
TextRenderer::TextRenderer() = default;
void TextRenderer::update(std::string newText) {
int startX = 0;
int startY = 0;
int i;
std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont()) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont()) + ".toml";
font = Font(fontResourcePath, fontConfigureFilePath);
int fontNumbersInOneLine = vkvm::getWidth() / (font.width() + 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) * (font.width() + left_margin);
startY = (i / fontNumbersInOneLine) * (font.height() + bottom_margin);
characterBitmap = getCharacter(newText[i]);
// fontProcessing(characterBitmap);
translateToSharedMemory(characterBitmap, startX, startY);
}
}
setOldText(newText);
}
void TextRenderer::clear() {
int x;
int y;
for(y = 0; y < vkvm::getHeight(); y++) {
for(x = 0; x < vkvm::getWidth(); x++) {
vkvm::setPixel(x, y, vkvm::getBackgroundColor());
}
}
}
void TextRenderer::setOldText(std::string text) {
oldText = std::move(text);
}
std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character) {
int fontHeight = font.height();
int fontWidth = font.width();
std::vector<std::vector<bool>> 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::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) {
int x;
int y;
int currentX = startX;
int currentY = startY;
for(y = 0; y < font.height(); y++) {
for(x = 0; x < font.width(); x++) {
if(characterBitmap[y][x]) {
vkvm::setPixel(currentX, currentY, vkvm::getForegroundColor());
}
else {
vkvm::setPixel(currentX, currentY, vkvm::getBackgroundColor());
}
currentX++;
}
currentX = startX;
currentY++;
}
for(x = 0; x < left_margin; x++) {
for(y = 0; y < font.height(); y++) {
vkvm::setPixel(startX + font.width() + x, startY + y, vkvm::getBackgroundColor());
}
}
for(y = 0; y < bottom_margin; y++) {
for(x = 0; x < font.width() + left_margin; x++) {
vkvm::setPixel(startX + x, startY + font.height() + y, vkvm::getBackgroundColor());
}
}
}
void TextRenderer::setLeftMargin(int margin) {
left_margin = margin;
}
void TextRenderer::setBottomMargin(int margin) {
bottom_margin = margin;
}