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

View File

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

View File

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