diff --git a/main/main.cpp b/main/main.cpp index 1e087b8..4d324e0 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,6 +1,8 @@ -#include "../src/Bitmap.h" -#include "../src/TextRenderer.h" -#include +#include "Font.hpp" +#include "internal.hpp" +#include "vkvm.hpp" +#include + /** * @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; } \ No newline at end of file diff --git a/src/Bitmap.cpp b/src/Bitmap.cpp index aae17ce..4efc188 100644 --- a/src/Bitmap.cpp +++ b/src/Bitmap.cpp @@ -1,4 +1,4 @@ -#include "Bitmap.h" +#include "Bitmap.hpp" #include #include @@ -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(&pixel+i); + auto value = reinterpret_cast(&pixel)+i; *value = ptr[i]; } if(pixel != 0){ diff --git a/src/Bitmap.h b/src/Bitmap.hpp similarity index 92% rename from src/Bitmap.h rename to src/Bitmap.hpp index fd73947..6314810 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.hpp @@ -1,5 +1,5 @@ -#ifndef TEXTRENDERER_BITMAP_H -#define TEXTRENDERER_BITMAP_H +#ifndef TEXTRENDERER_BITMAP_HPP +#define TEXTRENDERER_BITMAP_HPP #include #include @@ -65,4 +65,4 @@ private: }; -#endif //TEXTRENDERER_BITMAP_H +#endif //TEXTRENDERER_BITMAP_HPP diff --git a/src/Font.cpp b/src/Font.cpp index a219ebd..8bcb062 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -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("firstChar").value_or(0); pixelSize = config->get_as("pixelSize").value_or(0); gap = config->get_as("gap").value_or(-1); - invertedColors = config->get_as("invertedColors").value_or(0); + invertedColors = static_cast(config->get_as("invertedColors").value_or(0)); } int Font::width() { diff --git a/src/Font.h b/src/Font.hpp similarity index 88% rename from src/Font.h rename to src/Font.hpp index a33cdfc..e97d4ac 100644 --- a/src/Font.h +++ b/src/Font.hpp @@ -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 diff --git a/src/TextRenderer.cpp b/src/TextRenderer.cpp deleted file mode 100644 index 7b6ab89..0000000 --- a/src/TextRenderer.cpp +++ /dev/null @@ -1,290 +0,0 @@ -// -// Created by my on 2019/11/16. -// -#include -#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> 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> TextRenderer::getCharacter(unsigned char character) { - - std::vector> 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> 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(); -} \ No newline at end of file diff --git a/src/TextRenderer.h b/src/TextRenderer.h deleted file mode 100644 index 9354103..0000000 --- a/src/TextRenderer.h +++ /dev/null @@ -1,58 +0,0 @@ -// -// Created by my on 2019/11/16. -// - -#ifndef TEXTRENDERER_TEXTRENDERER_H -#define TEXTRENDERER_TEXTRENDERER_H - -#include "Font.h" -#include -#include -#include -#include -#include - -/** - * @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> 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> 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 diff --git a/test/test_bitmap.cpp b/test/test_bitmap.cpp index 5847d49..40951b8 100644 --- a/test/test_bitmap.cpp +++ b/test/test_bitmap.cpp @@ -1,5 +1,5 @@ -#include "../src/Bitmap.h" -#include "../src/Font.h" +#include "Bitmap.hpp" +#include "Font.hpp" #include TEST_CASE("default values") {