new text rendering
This commit is contained in:
parent
445a11158d
commit
f6b0e7abe7
@ -1,6 +1,8 @@
|
||||
#include "../src/Bitmap.h"
|
||||
#include "../src/TextRenderer.h"
|
||||
#include <internal.hpp>
|
||||
#include "Font.hpp"
|
||||
#include "internal.hpp"
|
||||
#include "vkvm.hpp"
|
||||
#include <thread>
|
||||
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
@ -12,24 +14,58 @@ int main() {
|
||||
vkvm::initialize(0);
|
||||
vkvm::setLogLevel(vkvm::DEBUG);
|
||||
|
||||
TextRenderer textRenderer = TextRenderer();
|
||||
textRenderer.setLeftMargin(1);
|
||||
textRenderer.setBottomMargin(1);
|
||||
Font font(std::string() + "../res/font" + std::to_string(vkvm::getFont()) + ".bmp", std::string() + "../res/font" + std::to_string(vkvm::getFont()) + ".toml");
|
||||
|
||||
|
||||
vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer]() {
|
||||
vkvm::registerEvent(vkvm::EventType::RenderText, [&font]() {
|
||||
std::string currentText = vkvm::getText();
|
||||
vkvm::log(vkvm::DEBUG, currentText);
|
||||
textRenderer.update(currentText);
|
||||
int perRow = vkvm::getWidth() / font.width();
|
||||
auto bc = vkvm::getBackgroundColor();
|
||||
auto fc = vkvm::getForegroundColor();
|
||||
|
||||
//clear area
|
||||
for(int y = 0; y < vkvm::getHeight();y++){
|
||||
for(int x = 0; x < vkvm::getWidth();x++) {
|
||||
vkvm::setPixel(x,y,bc);
|
||||
}
|
||||
}
|
||||
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
for(char c : currentText){
|
||||
if(c == '\n'){
|
||||
y++;
|
||||
x = 0;
|
||||
}else if(c == -127){
|
||||
//TODO(julian): cursor
|
||||
}
|
||||
else{
|
||||
if(x == perRow){
|
||||
y++;
|
||||
x = 0;
|
||||
}
|
||||
|
||||
for(int i = 0; i < font.height();i++){
|
||||
for(int j = 0; j < font.width();j++){
|
||||
if(font.getPixel(c, j, i)){
|
||||
vkvm::setPixel(x * font.width() + j, y * font.height() + i, fc);
|
||||
}else{
|
||||
vkvm::setPixel(x * font.width() + j, y * font.height() + i, bc);
|
||||
}
|
||||
}
|
||||
}
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
vkvm::callEvent(vkvm::EventType::Redraw);
|
||||
});
|
||||
|
||||
vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [&textRenderer]() {
|
||||
textRenderer.updateParameters();
|
||||
vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [&font]() {
|
||||
font.load(std::string() + "../res/font" + std::to_string(vkvm::getFont()) + ".bmp", std::string() + "../res/font" + std::to_string(vkvm::getFont()) + ".toml");
|
||||
});
|
||||
|
||||
while (true) {
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
#include "Bitmap.h"
|
||||
#include "Bitmap.hpp"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
@ -53,7 +53,7 @@ unsigned int Bitmap::getPixel(int x, int y) {
|
||||
unsigned int pixel = 0;
|
||||
char *ptr = getData() + ((getHeight() - 1 - y) * getWidth() + x) * (getBitsPerPixel() / 8);
|
||||
for(int i = 0; i < getBitsPerPixel() / 8;i++){
|
||||
auto value = reinterpret_cast<char*>(&pixel+i);
|
||||
auto value = reinterpret_cast<char*>(&pixel)+i;
|
||||
*value = ptr[i];
|
||||
}
|
||||
if(pixel != 0){
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef TEXTRENDERER_BITMAP_H
|
||||
#define TEXTRENDERER_BITMAP_H
|
||||
#ifndef TEXTRENDERER_BITMAP_HPP
|
||||
#define TEXTRENDERER_BITMAP_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -65,4 +65,4 @@ private:
|
||||
};
|
||||
|
||||
|
||||
#endif //TEXTRENDERER_BITMAP_H
|
||||
#endif //TEXTRENDERER_BITMAP_HPP
|
@ -2,7 +2,7 @@
|
||||
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
|
||||
//
|
||||
|
||||
#include "Font.h"
|
||||
#include "Font.hpp"
|
||||
#include "../lib/toml/cpptoml.h"
|
||||
|
||||
Font::Font() {
|
||||
@ -16,6 +16,9 @@ Font::Font() {
|
||||
yStart = 0;
|
||||
fillValue = 0;
|
||||
firstChar = ' ';
|
||||
pixelSize = 1;
|
||||
gap = -1;
|
||||
invertedColors = false;
|
||||
}
|
||||
|
||||
Font::Font(const std::string &file, const std::string &configFile) : Font() {
|
||||
@ -37,7 +40,7 @@ void Font::load(const std::string &file, const std::string &configFile) {
|
||||
firstChar = config->get_as<char>("firstChar").value_or(0);
|
||||
pixelSize = config->get_as<int>("pixelSize").value_or(0);
|
||||
gap = config->get_as<int>("gap").value_or(-1);
|
||||
invertedColors = config->get_as<bool>("invertedColors").value_or(0);
|
||||
invertedColors = static_cast<bool>(config->get_as<int>("invertedColors").value_or(0));
|
||||
}
|
||||
|
||||
int Font::width() {
|
||||
|
@ -2,10 +2,10 @@
|
||||
// Copyright (c) 2019 Julian Hinxlage. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TEXTRENDERER_FONT_H
|
||||
#define TEXTRENDERER_FONT_H
|
||||
#ifndef TEXTRENDERER_FONT_HPP
|
||||
#define TEXTRENDERER_FONT_HPP
|
||||
|
||||
#include "Bitmap.h"
|
||||
#include "Bitmap.hpp"
|
||||
|
||||
/**
|
||||
* @author: Julian Hinxlage
|
||||
@ -52,4 +52,4 @@ public:
|
||||
};
|
||||
|
||||
|
||||
#endif //TEXTRENDERER_FONT_H
|
||||
#endif //TEXTRENDERER_FONT_HPP
|
@ -1,290 +0,0 @@
|
||||
//
|
||||
// Created by my on 2019/11/16.
|
||||
//
|
||||
#include <internal.hpp>
|
||||
#include "TextRenderer.h"
|
||||
|
||||
TextRenderer::TextRenderer() {
|
||||
updateParameters();
|
||||
}
|
||||
|
||||
|
||||
void TextRenderer::update(std::string newText) {
|
||||
currentX = 0;
|
||||
currentY = 0;
|
||||
int i;
|
||||
|
||||
int fontNumbersInOneLine = windowWidth / (font.width() + left_margin);
|
||||
int space = 0;
|
||||
int currentLine;
|
||||
std::vector<std::vector<bool>> characterBitmap;
|
||||
|
||||
clear();
|
||||
newText = adjustText(newText);
|
||||
|
||||
vkvm::log(vkvm::LogLevel::DEBUG, newText, "\n");
|
||||
|
||||
for(i = 0; i < newText.size(); i++) {
|
||||
|
||||
if(newText[i] == returnCharacter) {
|
||||
space += (fontNumbersInOneLine - ((i + space) % fontNumbersInOneLine) - 1);
|
||||
} else {
|
||||
if(newText[i] == specialCharacter) {
|
||||
space -= 1;
|
||||
|
||||
int tempBlinkX = blinkX;
|
||||
int tempBlinkY = blinkY;
|
||||
int tempSpecialCharacterCurrentX = specialCharacterCurrentX;
|
||||
int tempSpecialCharacterCurrentY = specialCharacterCurrentY;
|
||||
char tempSpecialChar = _specialCharacter;
|
||||
|
||||
specialCharacterCurrentX = ((i + space) % fontNumbersInOneLine) * (font.width() + left_margin);
|
||||
specialCharacterCurrentY = ((i + space) / fontNumbersInOneLine) * (font.height() + bottom_margin);
|
||||
|
||||
blinkX = specialCharacterCurrentX + font.width();
|
||||
blinkY = specialCharacterCurrentY;
|
||||
_specialCharacter = newText[i - 1];
|
||||
|
||||
if(_specialCharacter != '\n') {
|
||||
characterBitmap = getCharacter(_specialCharacter);
|
||||
translateToSharedMemory(characterBitmap, specialCharacterCurrentX, specialCharacterCurrentY, true);
|
||||
} else {
|
||||
blinkX = 0;
|
||||
blinkY += (font.height() + bottom_margin);
|
||||
}
|
||||
|
||||
if(specialCharacterCurrentX != tempSpecialCharacterCurrentX && specialCharacterCurrentY != tempSpecialCharacterCurrentY) {
|
||||
clear(tempBlinkX, tempBlinkY, tempBlinkX + 1, tempBlinkY + font.height() + 1);
|
||||
if(tempSpecialChar != '\n' && (tempSpecialCharacterCurrentY * 10 + tempSpecialCharacterCurrentX <= specialCharacterCurrentY * 10 + tempSpecialCharacterCurrentX)) {
|
||||
translateToSharedMemory(getCharacter(tempSpecialChar), tempSpecialCharacterCurrentX, tempSpecialCharacterCurrentY, true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
currentLine = ((i + space) / fontNumbersInOneLine);
|
||||
|
||||
currentX = ((i + space) % fontNumbersInOneLine) * (font.width() + left_margin);
|
||||
currentY = (currentLine) * (font.height() + bottom_margin);
|
||||
|
||||
characterBitmap = getCharacter(newText[i]);
|
||||
|
||||
translateToSharedMemory(characterBitmap, currentX, currentY, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(newText.size() < oldTextsize) {
|
||||
clear(currentX, currentY, font.width(), currentY + font.height());
|
||||
}
|
||||
}
|
||||
|
||||
std::string TextRenderer::adjustText(std::string newText) {
|
||||
int fontNumbersInOneLine = windowWidth / (font.width() + left_margin);
|
||||
int totalLine = windowHeight / (font.height() + bottom_margin);
|
||||
int stringLine = 1;
|
||||
int characterNumberOfLastLine = 0;
|
||||
|
||||
for(char c : newText) {
|
||||
if(c == returnCharacter) {
|
||||
stringLine++;
|
||||
characterNumberOfLastLine = 0;
|
||||
} else if(c != specialCharacter) {
|
||||
characterNumberOfLastLine++;
|
||||
if(characterNumberOfLastLine == fontNumbersInOneLine) {
|
||||
stringLine++;
|
||||
characterNumberOfLastLine = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(stringLine > totalLine) {
|
||||
int startLine = stringLine - totalLine;
|
||||
int currentLine = 0;
|
||||
int cursorLine;
|
||||
for(int i = 0; i < newText.size(); i++) {
|
||||
if(newText[i] == returnCharacter) {
|
||||
currentLine++;
|
||||
characterNumberOfLastLine = 0;
|
||||
} else if(newText[i] != specialCharacter) {
|
||||
characterNumberOfLastLine++;
|
||||
if(characterNumberOfLastLine == fontNumbersInOneLine) {
|
||||
currentLine++;
|
||||
characterNumberOfLastLine = 0;
|
||||
}
|
||||
} else {
|
||||
cursorLine = currentLine;
|
||||
|
||||
if(currentLine < startLine) {
|
||||
return adjustText(newText, cursorLine, cursorLine + totalLine);
|
||||
}
|
||||
}
|
||||
|
||||
if(currentLine == startLine) {
|
||||
return newText.substr(i + 1, newText.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newText;
|
||||
}
|
||||
|
||||
std::string TextRenderer::adjustText(std::string newText, int startLine, int endLine) {
|
||||
int currentLine = 0;
|
||||
int startIndex, endIndex;
|
||||
int characterNumberOfCurrentLine = 0;
|
||||
int fontNumbersInOneLine = windowWidth / (font.width() + left_margin);
|
||||
|
||||
for(int i = 0; i < newText.size(); i++) {
|
||||
if(newText[i] == returnCharacter) {
|
||||
characterNumberOfCurrentLine = 0;
|
||||
currentLine++;
|
||||
|
||||
if(currentLine == startLine) {
|
||||
startIndex = i;
|
||||
}
|
||||
|
||||
if(currentLine == endLine) {
|
||||
endIndex = i;
|
||||
|
||||
return newText.substr(startIndex + 1, endIndex);
|
||||
}
|
||||
} else if (newText[i] != specialCharacter) {
|
||||
characterNumberOfCurrentLine++;
|
||||
if(characterNumberOfCurrentLine == fontNumbersInOneLine) {
|
||||
characterNumberOfCurrentLine = 0;
|
||||
currentLine++;
|
||||
|
||||
if(currentLine == startLine) {
|
||||
startIndex = i;
|
||||
}
|
||||
|
||||
if(currentLine == endLine) {
|
||||
endIndex = i;
|
||||
|
||||
return newText.substr(startIndex + 1, endIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vkvm::log(vkvm::ERROR, "could not adjust string");
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void TextRenderer::clear() {
|
||||
clear(0, 0, windowWidth, windowHeight);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character) {
|
||||
|
||||
std::vector<std::vector<bool>> bitmap_character;
|
||||
bitmap_character.resize(font.height());
|
||||
|
||||
vkvm::log(vkvm::DEBUG, bitmap_character.size());
|
||||
|
||||
for (int y = 0; y < font.height(); y += fontSize) {
|
||||
vkvm::log(vkvm::DEBUG, y);
|
||||
for(int i = 0; i < fontSize; i++) {
|
||||
bitmap_character[y + i].resize(font.width());
|
||||
}
|
||||
for (int x = 0; x < font.width(); 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];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
vkvm::log(vkvm::DEBUG, bitmap_character.size());
|
||||
return bitmap_character;
|
||||
}
|
||||
|
||||
void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY, bool flipColors) {
|
||||
int x, 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::log(vkvm::DEBUG, x, y);
|
||||
if(flipColors) {
|
||||
vkvm::setPixel(currentX, currentY, backgroundColor);
|
||||
}else {
|
||||
vkvm::setPixel(currentX, currentY, foregroundColor);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(flipColors)
|
||||
vkvm::setPixel(currentX, currentY, foregroundColor);
|
||||
else
|
||||
vkvm::setPixel(currentX, currentY, backgroundColor);
|
||||
|
||||
}
|
||||
_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, backgroundColor);
|
||||
}
|
||||
}
|
||||
|
||||
for(y = 0; y < bottom_margin; y++) {
|
||||
for(x = 0; x < font.width() + left_margin; x++) {
|
||||
vkvm::setPixel(startX + x, startY + font.height() + y, backgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextRenderer::setLeftMargin(int margin) {
|
||||
left_margin = margin;
|
||||
}
|
||||
|
||||
void TextRenderer::setBottomMargin(int margin) {
|
||||
bottom_margin = margin;
|
||||
}
|
||||
|
||||
void TextRenderer::updateParameters() {
|
||||
windowHeight = vkvm::getHeight();
|
||||
windowWidth = vkvm::getWidth();
|
||||
|
||||
int fontId = vkvm::getFont();
|
||||
std::string fontResourcePath = "../res/font" + std::to_string(fontId) + ".bmp";
|
||||
std::string fontConfigureFilePath = "../res/font" + std::to_string(fontId) + ".toml";
|
||||
font = Font(fontResourcePath, fontConfigureFilePath);
|
||||
|
||||
backgroundColor = vkvm::getBackgroundColor();
|
||||
foregroundColor = vkvm::getForegroundColor();
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
//
|
||||
// Created by my on 2019/11/16.
|
||||
//
|
||||
|
||||
#ifndef TEXTRENDERER_TEXTRENDERER_H
|
||||
#define TEXTRENDERER_TEXTRENDERER_H
|
||||
|
||||
#include "Font.h"
|
||||
#include <Color.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vkvm.hpp>
|
||||
|
||||
/**
|
||||
* @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();
|
||||
void update(std::string text);
|
||||
void updateParameters();
|
||||
std::vector<std::vector<bool>> getCharacter(unsigned char character);
|
||||
void setLeftMargin(int margin);
|
||||
void setBottomMargin(int margin);
|
||||
void clear();
|
||||
void clear(int startX, int startY, int endX, int endY);
|
||||
|
||||
private:
|
||||
int left_margin = 1;
|
||||
int bottom_margin = 1;
|
||||
Font font;
|
||||
void translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY, bool flipColors);
|
||||
vkvm::Color backgroundColor;
|
||||
vkvm::Color foregroundColor;
|
||||
int windowWidth;
|
||||
int windowHeight;
|
||||
|
||||
char returnCharacter = '\n';
|
||||
char specialCharacter = -127;
|
||||
int specialCharacterCurrentX;
|
||||
int specialCharacterCurrentY;
|
||||
char _specialCharacter;
|
||||
int fontSize = 1;
|
||||
int currentX;
|
||||
int currentY;
|
||||
int oldTextsize{};
|
||||
int blinkX = 0;
|
||||
int blinkY = 0;
|
||||
|
||||
std::string adjustText(std::string newText);
|
||||
std::string adjustText(std::string newText, int startLine, int endLine);
|
||||
};
|
||||
|
||||
|
||||
#endif //TEXTRENDERER_TEXTRENDERER_H
|
@ -1,5 +1,5 @@
|
||||
#include "../src/Bitmap.h"
|
||||
#include "../src/Font.h"
|
||||
#include "Bitmap.hpp"
|
||||
#include "Font.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
TEST_CASE("default values") {
|
||||
|
Loading…
Reference in New Issue
Block a user