Merge branch 'dev1' into dev

# Conflicts:
#	main/main.cpp
This commit is contained in:
Julian Hinxlage 2019-11-28 11:46:43 +01:00
commit 250e594f24
7 changed files with 280 additions and 20 deletions

View File

@ -27,7 +27,7 @@ include_directories(lib/toml)
set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library")
include_directories(${LIB_PATH}/include)
add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp)
add_executable(TextRenderer ${SOURCES} ${HEADERS} main/main.cpp src/TextRenderer.cpp src/TextRenderer.h)
target_link_libraries(TextRenderer ${LIB_PATH}/lib/liblibrary.a)

View File

@ -1,8 +1,11 @@
#include "Bitmap.h"
#include "Font.h"
#include "TextRenderer.h"
#include "internal.hpp"
#include <iostream>
bool isQuit(std::string command);
/**
* @author: Julian Hinxlage
* @since: v0.0.0
@ -10,27 +13,102 @@
* A string is converted and displayed in the console.
* Currently only to test.
*/
void outPutPixel(int windowHeight, int windowWidth, vkvm::Color fontColor);
int main() {
Font font("../res/profont-IIx.bmp","../res/profont-IIx.toml");
//Font font("../res/font2.bmp","../res/font2.toml");
vkvm::initialize(0);
std::string str;
std::cout << "string to draw: ";
std::getline(std::cin, str);
std::string currentText;
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 << " ";
/*************************set back to shared memory(only for test)********************************************/
int windowWidth = vkvm::getWidth();
int windowHeight = vkvm::getHeight();
vkvm::setWidth(windowWidth);
vkvm::setHeight(windowHeight);
vkvm::Color fontColor1(0, 0, 0);
vkvm::Color backgroudColor1(255, 255, 255);
Font font1("../res/font3.bmp", "../res/font3.toml");
vkvm::setFont(vkvm::FontType(3, "font", font1.height(), font1.width()));
vkvm::setForegroundColor(fontColor1);
vkvm::setBackgroundColor(backgroudColor1);
/**************************get text and font from shared memory*******************************************/
std::string fontResourcePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(vkvm::getFont().getId()) + ".toml";
Font font = Font(fontResourcePath, fontConfigureFilePath);
vkvm::Color fontColor = vkvm::getForegroundColor();
vkvm::Color backgroundColor = vkvm::getBackgroundColor();
TextRenderer textRenderer(windowWidth, windowHeight, backgroundColor, fontColor, font);
textRenderer.setLeftMargin(1);
textRenderer.setBottomMargin(1);
/*************************get Text and update back to shared meomory********************************************/
vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer](){
std::string currentText = vkvm::getText();
textRenderer.update(currentText);
vkvm::callEvent(vkvm::EventType::Redraw);
});
std::string command;
std::cout << "TextRender: ";
std::getline(std::cin, command);
while(!isQuit(command)) {
if(command.compare("clear")) {
vkvm::setText(command);
currentText = vkvm::getText();
textRenderer.update(currentText);
outPutPixel(windowHeight, windowWidth, fontColor);
std::cout << "TextRender: ";
std::getline(std::cin, command);
} else {
textRenderer.clear();
}
std::cout << std::endl;
}
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) {
for(int y = 0; y < windowHeight; y++) {
for(int x = 0; x < windowWidth; x++) {
if(vkvm::getPixel(x, y).getRed() == fontColor.getRed()) {
std::cout << "*";
} else {
std::cout << " ";
}
}
std::cout << "\n";
}
}
// 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

Before

Width:  |  Height:  |  Size: 933 KiB

After

Width:  |  Height:  |  Size: 933 KiB

View File

@ -9,4 +9,4 @@ yStart = 9
fillValue = 4294967295
firstChar = 25
pixelSize = 1
invertedColors = 1
invertedColors = 1

View File

@ -19,7 +19,7 @@ public:
* @since: v0.0.0
* @brief: Used to load a Bitmap from a file.
*/
void load(const std::string &file);
void load(const std::string &file);
/**
* @author: Julian Hinxlage

128
src/TextRenderer.cpp Normal file
View File

@ -0,0 +1,128 @@
//
// Created by my on 2019/11/16.
//
#include "TextRenderer.h"
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();
}
void TextRenderer::update(std::string newText) {
int startX = 0;
int startY = 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);
characterBitmap = getCharacter(newText[i], font);
// fontProcessing(characterBitmap);
translateToSharedMemory(characterBitmap, startX, startY);
}
}
setOldText(newText);
}
void TextRenderer::clear() {
int x, y;
for(y = 0; y < windowHeight; y++) {
for(x = 0; x < windowWidth; 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 = (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::fontProcessing(std::vector<std::vector<bool>> characterBitmap) {
// if(isBold()) {
for (int i = fontHeight - 1; i >= 0; i--) {
for (int j = fontWidth - 1; j >= 0; j--) {
if (i != fontHeight - 1 && j != fontWidth - 1 && characterBitmap[i][j]) {
characterBitmap[i + 1][j] = true;
characterBitmap[i][j + 1] = true;
}
}
}
// }
// if(isUnderline()) {
// for (int j = 0; j < fontWidth; j++)
// characterBitmap[fontHeight - 1][j] = true;
// }
}
bool TextRenderer::isBold() {
return type & BOLD != 0;
}
bool TextRenderer::isUnderline() {
return type & UNDERLINE != 0;
}
void TextRenderer::translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY) {
int x, y;
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);
}
else {
vkvm::setPixel(currentX, currentY, backgroundColor);
}
currentX++;
}
currentX = startX;
currentY++;
}
for(x = 0; x < left_margin; x++) {
for(y = 0; y < fontHeight; y++) {
vkvm::setPixel(startX + fontWidth + x, startY + y, backgroundColor);
}
}
for(y = 0; y < bottom_margin; y++) {
for(x = 0; x < fontWidth + left_margin; x++) {
vkvm::setPixel(startX + x, startY + fontHeight + y, backgroundColor);
}
}
}
void TextRenderer::setLeftMargin(int margin) {
left_margin = margin;
}
void TextRenderer::setBottomMargin(int margin) {
bottom_margin = margin;
}

54
src/TextRenderer.h Normal file
View File

@ -0,0 +1,54 @@
//
// Created by my on 2019/11/16.
//
#ifndef TEXTRENDERER_TEXTRENDERER_H
#define TEXTRENDERER_TEXTRENDERER_H
#define BOLD 0b001
#define ITALICS 0b010
#define UNDERLINE 0b100
#include <string>
#include <Color.hpp>
#include <vkvm.hpp>
#include <iostream>
#include "Font.h"
/**
* @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(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font);
void update(std::string text);
void setOldText(std::string text);
std::vector<std::vector<bool>> getCharacter(unsigned char character, Font font);
void setLeftMargin(int margin);
void setBottomMargin(int margin);
void clear();
private:
std::string oldText;
vkvm::Color backgroundColor;
vkvm::Color fontColor;
Font font;
int left_margin = 1;
int bottom_margin = 1;
int type;
int windowWidth;
int windowHeight;
int fontWidth;
int fontHeight;
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);
};
#endif //TEXTRENDERER_TEXTRENDERER_H