123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #include "rendering/FontRenderer.h"
- #include "rendering/Attributes.h"
- #include "utils/Buffer.h"
- #include "utils/Color.h"
- FontRenderer::FontRenderer() : activeTex(0), scale(1) {
- tex[0].load("resources/font8x8.png", 0);
- tex[1].load("resources/font16x16.png", 0);
- tex[2].load("resources/font24x24.png", 0);
- vertexBuffer.init(Attributes().addFloat(2).addFloat(2).addColor4());
- vertexBuffer.setStreamData(256 * 4 * sizeof(float) * 8);
- }
- float FontRenderer::drawString(float x, float y, const char* text) {
- const int maxIndex = 256;
- Buffer buffer(sizeof(float) * maxIndex * 4 * 8);
- int index = 0;
- Color4 color(0xFF, 0xFF, 0xFF, 0xFF);
- float addX = 6.0f * scale;
- float addY = 8.0f * scale;
- 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;
- }
- color[0] = ((text[index + 1] - '0') * 255) / 9;
- color[1] = ((text[index + 2] - '0') * 255) / 9;
- color[2] = ((text[index + 3] - '0') * 255) / 9;
- 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(color);
- buffer.add(x).add(y + addY).add(minX).add(maxY).add(color);
- buffer.add(x + addX).add(y).add(maxX).add(minY).add(color);
- buffer.add(x + addX).add(y + addY).add(maxX).add(maxY).add(color);
- x += addX;
- index++;
- }
- tex[activeTex].bindTo(0);
- vertexBuffer.updateData(0, buffer.getLength(), buffer);
- vertexBuffer.drawStrip(buffer.getLength() /
- (4 * sizeof(float) + sizeof(Color4)));
- return y + addY;
- }
- void FontRenderer::setSize(int size) {
- if(size == 1 || size == 2) {
- activeTex = size;
- scale = size + 1;
- } else {
- activeTex = 0;
- scale = 1;
- }
- }
|