Text-Renderer/main/main.cpp

99 lines
2.9 KiB
C++

#include "Font.hpp"
#include "internal.hpp"
#include "vkvm.hpp"
#include <thread>
#include <iostream>
int cursorPosX = 0;
int cursorPosY = 0;
/**
* @author: Julian Hinxlage
* @since: v0.0.0
* @brief: An image is loaded and used as a font.
*/
void threadHandler(Font &font){
static bool showCurser{false};
int start = font.height() / 4;
int end = font.height() * 3 / 4 + 1;
auto bc = vkvm::getBackgroundColor();
auto fc = vkvm::getForegroundColor();
while(true){
for (int i = start; i < end; i++) {
if (showCurser) {
vkvm::setPixel(cursorPosX, cursorPosY + i, bc);
}
else {
vkvm::setPixel(cursorPosX, cursorPosY + i, fc);
}
}
vkvm::callEvent(vkvm::EventType::Redraw);
showCurser = !showCurser;
std::this_thread::sleep_for(std::chrono::milliseconds(800));
}
}
int main() {
vkvm::initialize(0);
vkvm::setLogLevel(vkvm::DEBUG);
Font font(std::string() + "../res/font" + std::to_string(vkvm::getFont()) + ".bmp", std::string() + "../res/font" + std::to_string(vkvm::getFont()) + ".toml");
std::thread thread(std::bind(threadHandler, font));
vkvm::registerEvent(vkvm::EventType::RenderText, [&font]() {
std::string currentText = vkvm::getText();
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){
cursorPosX = x * font.width();
cursorPosY = y * font.height();
}
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, [&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;
}