Text-Renderer/main/main.cpp

117 lines
3.3 KiB
C++

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