123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include "rendering/FontRenderer.h"
- #include "gaming-core/wrapper/Attributes.h"
- #include "gaming-core/utils/List.h"
- FontRenderer::FontRenderer() : tex1("resources/font8x8.png"), tex2("resources/font16x16.png"),
- tex3("resources/font24x24.png"), activeTex(&tex1), scale(1) {
- vertexBuffer.setAttributes(Attributes().addFloat(2).addFloat(2).addFloat(4));
- vertexBuffer.setStreamData(256 * 4 * sizeof (float) * 8);
- }
- float FontRenderer::drawString(float x, float y, const char* text) {
- const int maxIndex = 256;
- List<float, maxIndex * 4 * 8> buffer;
- int index = 0;
- float r = 1.0f;
- float g = 1.0f;
- float b = 1.0f;
- 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;
- }
- 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);
- buffer.add(y);
- buffer.add(minX);
- buffer.add(minY);
- buffer.add(r);
- buffer.add(g);
- buffer.add(b);
- buffer.add(1.0f);
- buffer.add(x);
- buffer.add(y + addY);
- buffer.add(minX);
- buffer.add(maxY);
- buffer.add(r);
- buffer.add(g);
- buffer.add(b);
- buffer.add(1.0f);
- buffer.add(x + addX);
- buffer.add(y);
- buffer.add(maxX);
- buffer.add(minY);
- buffer.add(r);
- buffer.add(g);
- buffer.add(b);
- buffer.add(1.0f);
- buffer.add(x + addX);
- buffer.add(y + addY);
- buffer.add(maxX);
- buffer.add(maxY);
- buffer.add(r);
- buffer.add(g);
- buffer.add(b);
- buffer.add(1.0f);
- x += addX;
- index++;
- }
- activeTex->bind();
- vertexBuffer.updateData(0, buffer.getLength() * sizeof(float), buffer.begin());
- vertexBuffer.drawStrip(buffer.getLength() / 8);
- return y + addY;
- }
- void FontRenderer::setSize(int size) {
- if(size == 1) {
- activeTex = &tex2;
- scale = 2;
- } else if(size == 2) {
- activeTex = &tex3;
- scale = 3;
- } else {
- activeTex = &tex1;
- scale = 1;
- }
- }
|