FontRenderer.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "client/rendering/FontRenderer.h"
  2. #include "gaming-core/rendering/Attributes.h"
  3. #include "gaming-core/utils/Buffer.h"
  4. FontRenderer::FontRenderer() : tex("resources/font8x8.png") {
  5. vertexBuffer.setAttributes(
  6. Attributes().addFloat(2).addFloat(2).addColor4());
  7. vertexBuffer.setStreamData(MAX_CHARS * VERTEX_SIZE * 4);
  8. }
  9. void FontRenderer::drawString(float x, float y, const char* text) {
  10. Buffer buffer(MAX_CHARS * VERTEX_SIZE * 4);
  11. int index = 0;
  12. Color4 color(0xFF, 0xFF, 0xFF, 0xFF);
  13. while(text[index] != '\0' && index < MAX_CHARS) {
  14. char32_t c = text[index];
  15. if(c > 128 && index + 1 < MAX_CHARS && text[index + 1] != '\0') {
  16. index++;
  17. c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
  18. }
  19. if(c == '&') {
  20. if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
  21. text[index + 3] == '\0') {
  22. break;
  23. }
  24. color[0] = ((text[index + 1] - '0') * 255) / 9;
  25. color[1] = ((text[index + 2] - '0') * 255) / 9;
  26. color[2] = ((text[index + 3] - '0') * 255) / 9;
  27. index += 4;
  28. continue;
  29. }
  30. float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f;
  31. float minY = (c >> 4) * (1.0f / 16.0f);
  32. float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
  33. float maxY = minY + (1.0f / 16.0f);
  34. buffer.add(x).add(y).add(minX).add(minY).add(color);
  35. buffer.add(x).add(y + 8).add(minX).add(maxY).add(color);
  36. buffer.add(x + 6).add(y).add(maxX).add(minY).add(color);
  37. buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).add(color);
  38. x += 6;
  39. index++;
  40. }
  41. tex.bindTo(0);
  42. vertexBuffer.updateData(0, buffer.getLength(), buffer);
  43. vertexBuffer.drawStrip(buffer.getLength() / VERTEX_SIZE);
  44. }