Realise blink function and make speicail character with a speicial signal. and realise display the correct content when string content is bigger than window size.

This commit is contained in:
my 2019-12-18 12:51:46 +01:00
parent 9770d102e5
commit f6789ced97
3 changed files with 252 additions and 93 deletions

View File

@ -18,7 +18,7 @@ bool isQuit(std::string command);
#include "TextRenderer.h"
void outputWindow(int windowHeight, int windowWidth, vkvm::Color fontColor);
TextRenderer generateTextRender();
void renderText(TextRenderer textRenderer);
void renderText(TextRenderer *textRenderer);
void test();
void test(TextRenderer renderer);
@ -30,6 +30,8 @@ void test1();
int main(int argc, char *argv[]) {
vkvm::initialize(0);
// vkvm::setHeight(60);
TextRenderer textRenderer = generateTextRender();
textRenderer.clear();
@ -37,7 +39,7 @@ int main(int argc, char *argv[]) {
// test(textRenderer);
vkvm::registerEvent(vkvm::EventType::RenderText, [&textRenderer]() {
renderText(textRenderer);
renderText(&textRenderer);
});
test1();
@ -50,8 +52,8 @@ void redraw(TextRenderer textRenderer) {
textRenderer.update(vkvm::getText());
}
void renderText(TextRenderer textRenderer) {
textRenderer.update(vkvm::getText());
void renderText(TextRenderer *textRenderer) {
textRenderer->update(vkvm::getText());
vkvm::callEvent(vkvm::EventType::Redraw);
}
@ -64,16 +66,23 @@ TextRenderer generateTextRender() {
int windowHeight = vkvm::getHeight();
vkvm::Color fontColor = vkvm::getForegroundColor();
vkvm::Color backgroundColor = vkvm::getBackgroundColor();
vkvm::Color speicialFontColor = vkvm::Color(100, 100, 100);
vkvm::FontType fontType = vkvm::getFont();
// vkvm::FontType fontType = vkvm::getFont();
int fontSize = std::stoi(command);
int font_id = fontType.getId();
// int font_id = fontType.getId();
std::string fontResourcePath = "../res/font" + std::to_string(3) + ".bmp";
std::string fontConfigureFilePath = "../res/font" + std::to_string(3) + ".toml";
Font font = Font(fontResourcePath, fontConfigureFilePath);
return TextRenderer(windowWidth, windowHeight, backgroundColor, fontColor, font, fontSize);
// Blink blink(0, 0, 0, fontColor, backgroundColor);
// std::thread thread(Blink(0, 0, 0, fontColor, backgroundColor));
TextRenderer textRenderer(windowWidth, windowHeight, backgroundColor, fontColor, speicialFontColor, font, fontSize);
textRenderer.startBlinkThread();
return textRenderer;
}
void test1() {
@ -83,12 +92,17 @@ void test1() {
while(command.compare("quit") != 0) {
if(command.compare("RenderText") == 0) {
vkvm::callEvent(vkvm::EventType::RenderText);
outputWindow(vkvm::getHeight(), vkvm::getWidth(), vkvm::getForegroundColor());
}
else if(command.compare("Redraw") == 0) {
vkvm::callEvent(vkvm::EventType::Redraw);
}
else if(command.compare("Reput") == 0) {
outputWindow(vkvm::getHeight(), vkvm::getWidth(), vkvm::getForegroundColor());
}
else {
vkvm::setText(command);
}
@ -121,51 +135,52 @@ void test(TextRenderer renderer) {
std::cout << "TextRender finished." << std::endl;
}
void test() {
std::string currentText;
/*************************set back to shared memory(only for test)********************************************/
int windowWidth = 40;
int windowHeight = 15;
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********************************************/
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);
outputWindow(windowHeight, windowWidth, fontColor);
std::cout << "TextRender: ";
std::getline(std::cin, command);
} else {
textRenderer.clear();
}
}
std::cout << "TextRender finished." << std::endl;
}
//void test() {
// std::string currentText;
//
// /*************************set back to shared memory(only for test)********************************************/
// int windowWidth = 40;
// int windowHeight = 15;
// 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,
// Blink(0, 0, 0, vkvm::Color(), vkvm::Color()));
// textRenderer.setLeftMargin(1);
// textRenderer.setBottomMargin(1);
// /*************************get Text and update back to shared meomory********************************************/
//
// 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);
// outputWindow(windowHeight, windowWidth, fontColor);
// std::cout << "TextRender: ";
// std::getline(std::cin, command);
// } else {
// textRenderer.clear();
// }
// }
//
// std::cout << "TextRender finished." << std::endl;
//}
//
/***************************read pixel in shared memory and test output in console******************************************/
void outputWindow(int windowHeight, int windowWidth, vkvm::Color fontColor) {

View File

@ -3,8 +3,9 @@
//
#include "TextRenderer.h"
TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backGroundColor, vkvm::Color fontColor,
Font font, int fontSize): backgroundColor(backgroundColor), fontColor(fontColor), font(font) {
TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color _backGroundColor, vkvm::Color fontColor, vkvm::Color speicialFontColor,
Font font, int fontSize)
: backgroundColor(_backGroundColor), fontColor(fontColor), font(font), speicialFontColor(speicialFontColor) {
this-> windowWidth = windowWidth;
this-> windowHeight = windowHeight;
currentX = 0;
@ -13,50 +14,86 @@ TextRenderer::TextRenderer(int windowWidth, int windowHeight, vkvm::Color backGr
fontWidth = font.width() * fontSize;
this -> fontSize = fontSize;
// startBlinkThread();
// blink.setblinkHeight(font.height());
// blink.startThread();
// std::thread thread1(&TextRenderer::blink1, this);
// std::thread thread1(&Blink::blink, std::ref(blink));
// thread1.detach();
// thread1.join();
// blink.blink();
// blink.blink();
}
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;
currentX = 0;
currentY = 0;
fontHeight = font.height() * fontSize;
fontWidth = font.width() * fontSize;
}
//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;
// currentX = 0;
// currentY = 0;
//
// fontHeight = font.height() * fontSize;
// fontWidth = font.width() * fontSize;
//
// blink.setblinkHeight(font.height());
//// blink.blink();
//}
void TextRenderer::update(std::string newText) {
currentX = 0;
currentY = 0;
int i;
int space = 0;
int hidenLine = 0;
int currentLine;
int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
std::vector<std::vector<bool>> characterBitmap;
if(newText.size() < oldTextsize) {
clear(currentX, currentY, fontWidth, currentY + fontHeight);
}
newText = adjustText(newText);
std::cout << newText << "\n" << std::endl;
for(i = 0; i < newText.size(); i++) {
if(i > oldText.size() || oldText[i] != newText[i]) {
if(newText[i] == '\n') {
if(newText[i] == returnCharacter) {
space += (fontNumbersInOneLine - ((i + space) % fontNumbersInOneLine) - 1);
if(newText.size() < oldTextsize) {
clear(currentX, currentY, fontWidth, currentY + fontHeight);
}
} else {
currentX = ((i + space) % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = ((i + space) / fontNumbersInOneLine) * (fontHeight + bottom_margin);
if(newText[i] == -127) {
blinkX = currentX;
blinkY = currentY + fontWidth - 1;
if(newText[i] == specialCharacter) {
space -= 1;
} else {
currentX = ((i + space) % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = ((i + space) / fontNumbersInOneLine) * (fontHeight + bottom_margin);
// blink.setCurrentX(currentX + fontWidth);
// blink.setCurrentY(currentY);
blinkX = currentX + fontWidth;
blinkY = currentY;
characterBitmap = getCharacter(newText[i], font);
// fontProcessing(characterBitmap);
translateToSharedMemory(characterBitmap, currentX, currentY);
translateToSharedMemory(characterBitmap, currentX, currentY, speicialFontColor);
} else {
currentLine = ((i + space) / fontNumbersInOneLine);
// if(currentLine - hidenLine >= totalLine) {
// hidenLine++;
// }
currentX = ((i + space) % fontNumbersInOneLine) * (fontWidth + left_margin);
currentY = (currentLine) * (fontHeight + bottom_margin);
characterBitmap = getCharacter(newText[i], font);
translateToSharedMemory(characterBitmap, currentX, currentY, fontColor);
}
}
}
@ -70,11 +107,103 @@ void TextRenderer::update(std::string newText) {
setOldText(newText);
}
void TextRenderer::blink() {
std::string TextRenderer::adjustText(std::string newText) {
int fontNumbersInOneLine = windowWidth / (fontWidth + left_margin);
int totalLine = windowHeight / (fontHeight + bottom_margin) - 1;
int stringLine = 1;
int characterNumberOfLastLine = 0;
for(int i = 0; i < newText.size(); i++) {
if(newText[i] == returnCharacter) {
stringLine++;
characterNumberOfLastLine = 0;
} else if(newText[i] != 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, 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 / (fontWidth + 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;
}
} else if (newText[i] != specialCharacter) {
characterNumberOfCurrentLine++;
if(characterNumberOfCurrentLine == fontNumbersInOneLine) {
characterNumberOfCurrentLine = 0;
currentLine++;
if(currentLine == startLine) {
startIndex = i;
}
if(currentLine == endLine) {
endIndex = i;
}
}
}
}
return std::string();
}
void TextRenderer::blink1() {
bool signal;
int startY = fontHeight / 4;
int endX = fontHeight * 3 / 4 + 1;
while(true) {
for(int y = 0; y < fontHeight; y++) {
for(int y = startY; y < endX; y++) {
if(signal)
vkvm::setPixel(blinkX, blinkY + y, backgroundColor);
else
@ -82,12 +211,10 @@ void TextRenderer::blink() {
}
signal = !signal;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(800));
}
}
void TextRenderer::clear() {
int x, y;
for(y = 0; y < windowHeight; y++) {
@ -180,7 +307,7 @@ bool TextRenderer::isUnderline() {
return type & UNDERLINE != 0;
}
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, vkvm::Color fontColor) {
int x, y;
int _currentX = startX;
int _currentY = startY;
@ -255,6 +382,10 @@ int TextRenderer::getWindowHeight() {
return windowHeight;
}
void TextRenderer::startBlinkThread() {
blink_thread = std::thread(&TextRenderer::blink1, this);
}
//void TextRenderer::setPixelRange(int startX, int startY, int endX, int endY, int type) {
// if(type == 0) {
// for(int i = startX + 1; i < endX; i++) {

View File

@ -23,9 +23,10 @@
*/
class TextRenderer {
public:
TextRenderer(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font);
TextRenderer(int windowWidth, int windowHeight, vkvm::Color defaultBackgroundColor, vkvm::Color fontColor, Font font, int fontSize);
// TextRenderer(int windowWidth, int windowHeight, vkvm::Color backgroundColor, vkvm::Color fontColor,
// Font font);
TextRenderer(int windowWidth, int windowHeight, vkvm::Color backGroundColor, vkvm::Color fontColor, vkvm::Color speicialFontColor,
Font font, int fontSize);
void update(std::string text);
void setOldText(std::string text);
@ -40,34 +41,42 @@ public:
void setWindowHeight(int windowHeight);
int getWindowWidth();
int getWindowHeight();
void startBlinkThread();
std::thread blink_thread;
private:
std::mutex mutex;
// std::mutex mutex;
std::string oldText;
vkvm::Color backgroundColor;
vkvm::Color fontColor;
vkvm::Color speicialFontColor;
// Blink blink;
Font font;
char returnCharacter = '\n';
char specialCharacter = -127;
int fontSize = 1;
int left_margin = 1;
int bottom_margin = 1;
int type;
int type{};
int windowWidth;
int windowHeight;
int fontWidth;
int fontHeight;
int currentX;
int currentY;
int oldTextsize;
int oldTextsize{};
int blinkX = 0;
int blinkY = 0;
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);
void translateToSharedMemory(std::vector<std::vector<bool>> characterBitmap, int startX, int startY, vkvm::Color fontColor);
void checkWindowSize();
void blink();
void blink1();
void checkFontColor();
@ -75,7 +84,11 @@ private:
void checkText();
// void setPixelRange(int startX, int startY, int endX, int endY, int type);
std::string adjustText(std::string newText);
std::string adjustText(std::string newText, int startLine, int endLine);
};