FontRenderer.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "rendering/FontRenderer.h"
  2. FontRenderer::FontRenderer() : buffer(8 * 1024 * 1024, 8 * sizeof(float)), tex("resources/font.png") {
  3. vertexBuffer.bind();
  4. uint step = 8 * sizeof(float);
  5. vertexBuffer.setFloatAttribute(0, 2, 0, step);
  6. vertexBuffer.setFloatAttribute(1, 2, 2 * sizeof(float), step);
  7. vertexBuffer.setFloatAttribute(2, 4, 4 * sizeof(float), step);
  8. }
  9. void FontRenderer::drawString(float x, float y, const char* text) {
  10. vertexBuffer.bind();
  11. const u64 maxIndex = 256;
  12. buffer.reset(maxIndex * 4 * sizeof (float) * 8);
  13. u64 index = 0;
  14. float r = 1.0f;
  15. float g = 1.0f;
  16. float b = 1.0f;
  17. while(text[index] != '\0' && index < maxIndex) {
  18. char32_t c = text[index];
  19. if(c > 128 && index + 1 < maxIndex && text[index + 1] != '\0') {
  20. index++;
  21. c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
  22. }
  23. if(c == '&') {
  24. if(text[index + 1] == '\0' || text[index + 2] == '\0' || text[index + 3] == '\0') {
  25. break;
  26. }
  27. r = (text[index + 1] - '0') * (1.0f / 9.0f);
  28. g = (text[index + 2] - '0') * (1.0f / 9.0f);
  29. b = (text[index + 3] - '0') * (1.0f / 9.0f);
  30. index += 4;
  31. continue;
  32. }
  33. float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f;
  34. float minY = (c >> 4) * (1.0f / 16.0f);
  35. float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
  36. float maxY = minY + (1.0f / 16.0f);
  37. buffer.add(x).add(y).add(minX).add(minY).add(r).add(g).add(b).add(1.0f);
  38. buffer.add(x).add(y + 8).add(minX).add(maxY).add(r).add(g).add(b).add(1.0f);
  39. buffer.add(x + 6).add(y).add(maxX).add(minY).add(r).add(g).add(b).add(1.0f);
  40. buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).add(r).add(g).add(b).add(1.0f);
  41. x += 6;
  42. index++;
  43. }
  44. tex.bind();
  45. buffer.draw();
  46. }