#include "rendering/FontRenderer.h" FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof(float)), tex("resources/font.png") { vertexBuffer.bind(); uint step = 8 * sizeof(float); vertexBuffer.setFloatAttribute(0, 2, 0, step); vertexBuffer.setFloatAttribute(1, 2, 2 * sizeof(float), step); vertexBuffer.setFloatAttribute(2, 4, 4 * sizeof(float), step); } void FontRenderer::drawString(float x, float y, const char* text) { vertexBuffer.bind(); const u64 maxIndex = 256; buffer.reset(maxIndex * 4 * sizeof (float) * 8); u64 index = 0; float r = 1.0f; float g = 1.0f; float b = 1.0f; while(text[index] != '\0' && index < maxIndex) { char32_t c = text[index]; if(c > 128 && index + 1 < maxIndex && text[index + 1] != '\0') { index++; c = (text[index] & 0x3F) | ((c & 0x1F) << 6); } if(c == '&') { if(text[index + 1] == '\0' || text[index + 2] == '\0' || text[index + 3] == '\0') { break; } r = (text[index + 1] - '0') * (1.0f / 9.0f); g = (text[index + 2] - '0') * (1.0f / 9.0f); b = (text[index + 3] - '0') * (1.0f / 9.0f); index += 4; continue; } float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f; float minY = (c >> 4) * (1.0f / 16.0f); float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f; float maxY = minY + (1.0f / 16.0f); buffer.add(x).add(y).add(minX).add(minY).add(r).add(g).add(b).add(1.0f); buffer.add(x).add(y + 8).add(minX).add(maxY).add(r).add(g).add(b).add(1.0f); buffer.add(x + 6).add(y).add(maxX).add(minY).add(r).add(g).add(b).add(1.0f); buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).add(r).add(g).add(b).add(1.0f); x += 6; index++; } tex.bind(); buffer.draw(); }