merge dev1 into dev

This commit is contained in:
Johannes Theiner 2019-12-19 10:52:26 +01:00
commit 445a11158d
3 changed files with 262 additions and 85 deletions

View File

@ -1,60 +1,35 @@
#include "../src/Bitmap.h" #include "../src/Bitmap.h"
#include "../src/Font.h"
#include "../src/TextRenderer.h" #include "../src/TextRenderer.h"
#include <internal.hpp> #include <internal.hpp>
#include <iostream>
bool isQuit(const std::string& command);
/** /**
* @author: Julian Hinxlage * @author: Julian Hinxlage
* @since: v0.0.0 * @since: v0.0.0
* @brief: An image is loaded and used as a font. * @brief: An image is loaded and used as a font.
* A string is converted and displayed in the console.
* Currently only to test.
*/ */
void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor);
int main() { int main() {
vkvm::initialize(0); vkvm::initialize(0);
vkvm::setLogLevel(vkvm::DEBUG);
std::string currentText;
/**************************get text and font from shared memory*******************************************/
TextRenderer textRenderer = TextRenderer(); TextRenderer textRenderer = TextRenderer();
textRenderer.setLeftMargin(1); textRenderer.setLeftMargin(1);
textRenderer.setBottomMargin(1); textRenderer.setBottomMargin(1);
/*************************get Text and update back to shared meomory********************************************/
vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer]() { vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer]() {
std::string currentText = vkvm::getText(); std::string currentText = vkvm::getText();
vkvm::log(vkvm::DEBUG, currentText);
textRenderer.update(currentText); textRenderer.update(currentText);
vkvm::callEvent(vkvm::EventType::Redraw); vkvm::callEvent(vkvm::EventType::Redraw);
}); });
vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [&textRenderer]() {
std::string command; textRenderer.updateParameters();
std::cout << "TextRender: "; });
std::getline(std::cin, command); while (true) {
while (command != "quit") {
if (command == "clear") {
textRenderer.clear();
} if (command == "redblue") {
vkvm::setBackgroundColor(vkvm::red);
vkvm::setForegroundColor(vkvm::blue);
} else {
vkvm::setText(command);
currentText = vkvm::getText();
textRenderer.update(currentText);
}
std::cout << "TextRender: ";
std::getline(std::cin, command);
} }
std::cout << "TextRender finished." << std::endl;
return 0; return 0;
} }

View File

@ -1,98 +1,269 @@
// //
// Created by my on 2019/11/16. // Created by my on 2019/11/16.
// //
#include <internal.hpp>
#include "TextRenderer.h" #include "TextRenderer.h"
#include <utility> TextRenderer::TextRenderer() {
updateParameters();
}
TextRenderer::TextRenderer() = default;
void TextRenderer::update(std::string newText) { void TextRenderer::update(std::string newText) {
int startX = 0; currentX = 0;
int startY = 0; currentY = 0;
int i; int i;
std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont()) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont()) + ".toml"; int fontNumbersInOneLine = windowWidth / (font.width() + left_margin);
font = Font(fontResourcePath, fontConfigureFilePath); int space = 0;
int fontNumbersInOneLine = vkvm::getWidth() / (font.width() + left_margin); int currentLine;
std::vector<std::vector<bool>> characterBitmap; std::vector<std::vector<bool>> characterBitmap;
clear();
newText = adjustText(newText);
vkvm::log(vkvm::LogLevel::DEBUG, newText, "\n");
for(i = 0; i < newText.size(); i++) { for(i = 0; i < newText.size(); i++) {
if(i > oldText.size() || oldText[i] != newText[i]) {
startX = (i % fontNumbersInOneLine) * (font.width() + left_margin); if(newText[i] == returnCharacter) {
startY = (i / fontNumbersInOneLine) * (font.height() + bottom_margin); space += (fontNumbersInOneLine - ((i + space) % fontNumbersInOneLine) - 1);
characterBitmap = getCharacter(newText[i]); } else {
// fontProcessing(characterBitmap); if(newText[i] == specialCharacter) {
translateToSharedMemory(characterBitmap, startX, startY); 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;
}
} }
} }
setOldText(newText); 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() { void TextRenderer::clear() {
int x; clear(0, 0, windowWidth, windowHeight);
int y; }
for(y = 0; y < vkvm::getHeight(); y++) {
for(x = 0; x < vkvm::getWidth(); x++) { void TextRenderer::clear(int startX, int startY, int endX, int endY) {
vkvm::setPixel(x, y, vkvm::getBackgroundColor()); 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 = std::move(text);
}
std::vector<std::vector<bool>> TextRenderer::getCharacter(unsigned char character) { 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*)); std::vector<std::vector<bool>> bitmap_character;
bitmap_character.resize(fontHeight); bitmap_character.resize(font.height());
for (int i = 0; i < fontHeight; i++) {
// bitmap_character[i] = (bool*)malloc(fontWidth * sizeof(bool)); vkvm::log(vkvm::DEBUG, bitmap_character.size());
bitmap_character[i].resize(fontWidth);
for (int j = 0; j < fontWidth; j++) { for (int y = 0; y < font.height(); y += fontSize) {
bitmap_character[i][j] = font.getPixel(character, j, i); 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; return bitmap_character;
} }
void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) { void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY, bool flipColors) {
int x; int x, y;
int y; int _currentX = startX;
int currentX = startX; int _currentY = startY;
int currentY = startY;
for(y = 0; y < font.height(); y++) { for(y = 0; y < font.height(); y++) {
for(x = 0; x < font.width(); x++) { for(x = 0; x < font.width(); x++) {
if(characterBitmap[y][x]) { if(characterBitmap[y][x]) {
vkvm::setPixel(currentX, currentY, vkvm::getForegroundColor()); vkvm::log(vkvm::DEBUG, x, y);
if(flipColors) {
vkvm::setPixel(currentX, currentY, backgroundColor);
}else {
vkvm::setPixel(currentX, currentY, foregroundColor);
}
} }
else { else {
vkvm::setPixel(currentX, currentY, vkvm::getBackgroundColor()); if(flipColors)
vkvm::setPixel(currentX, currentY, foregroundColor);
else
vkvm::setPixel(currentX, currentY, backgroundColor);
} }
currentX++; _currentX++;
} }
currentX = startX; _currentX = startX;
currentY++; _currentY++;
} }
for(x = 0; x < left_margin; x++) { for(x = 0; x < left_margin; x++) {
for(y = 0; y < font.height(); y++) { for(y = 0; y < font.height(); y++) {
vkvm::setPixel(startX + font.width() + x, startY + y, vkvm::getBackgroundColor()); vkvm::setPixel(startX + font.width() + x, startY + y, backgroundColor);
} }
} }
for(y = 0; y < bottom_margin; y++) { for(y = 0; y < bottom_margin; y++) {
for(x = 0; x < font.width() + left_margin; x++) { for(x = 0; x < font.width() + left_margin; x++) {
vkvm::setPixel(startX + x, startY + font.height() + y, vkvm::getBackgroundColor()); vkvm::setPixel(startX + x, startY + font.height() + y, backgroundColor);
} }
} }
} }
@ -104,3 +275,16 @@ void TextRenderer::setLeftMargin(int margin) {
void TextRenderer::setBottomMargin(int margin) { void TextRenderer::setBottomMargin(int margin) {
bottom_margin = 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();
}

View File

@ -9,6 +9,7 @@
#include <Color.hpp> #include <Color.hpp>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <thread>
#include <vkvm.hpp> #include <vkvm.hpp>
/** /**
@ -19,21 +20,38 @@
class TextRenderer { class TextRenderer {
public: public:
TextRenderer(); TextRenderer();
void update(std::string text); void update(std::string text);
void setOldText(std::string text); void updateParameters();
std::vector<std::vector<bool>> getCharacter(unsigned char character); std::vector<std::vector<bool>> getCharacter(unsigned char character);
void setLeftMargin(int margin); void setLeftMargin(int margin);
void setBottomMargin(int margin); void setBottomMargin(int margin);
void clear(); void clear();
void clear(int startX, int startY, int endX, int endY);
private: private:
std::string oldText;
int left_margin = 1; int left_margin = 1;
int bottom_margin = 1; int bottom_margin = 1;
int type;
Font font; Font font;
void translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY); 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);
}; };