Renderer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "client/rendering/Renderer.h"
  2. #include "rendering/Attributes.h"
  3. #include "utils/Buffer.h"
  4. Renderer::Renderer()
  5. : buffer(16), font("resources/font8x8.png"), text(nullptr), index(0),
  6. maxIndex(0), x(0.0f), y(0.0f), vertices(0) {
  7. vertexBuffer.setAttributes(
  8. Attributes().addFloat(2).addFloat(2).addColor4());
  9. vertexBuffer.setStreamData(1024 * 1024);
  10. }
  11. void Renderer::setupStringRead(const char* s, int limit) {
  12. text = s;
  13. index = 0;
  14. maxIndex = limit;
  15. }
  16. void Renderer::setupString(const Vector2& pos, const char* s, int limit) {
  17. buffer.clear();
  18. setupStringRead(s, limit);
  19. x = pos[0];
  20. y = pos[1];
  21. vertices = 0;
  22. color = {0xFF, 0xFF, 0xFF, 0x00};
  23. }
  24. bool Renderer::hasStringData() const {
  25. return (index < maxIndex || maxIndex < 0) && text[index] != '\0';
  26. }
  27. char32_t Renderer::readUnicode() {
  28. char32_t c = text[index];
  29. index++;
  30. if(c > 128 && hasStringData()) {
  31. c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
  32. index++;
  33. }
  34. return c;
  35. }
  36. bool Renderer::readColor(char32_t c) {
  37. if(c != '&') {
  38. return false;
  39. }
  40. for(int i = 0; i < 3; i++) {
  41. if(!hasStringData()) {
  42. return true;
  43. }
  44. color[i] = ((text[index] - '0') * 255) / 9;
  45. index++;
  46. }
  47. return true;
  48. }
  49. void Renderer::addChar(char32_t c) {
  50. float minX = (c & 0xF) * (1.0f / 16.0f) + (1.0f / 128.0f);
  51. float minY = (c >> 4) * (1.0f / 16.0f);
  52. float maxX = minX + (1.0f / 16.0f) - (2.0f / 128.0f);
  53. float maxY = minY + (1.0f / 16.0f);
  54. buffer.add(x).add(y).add(minX).add(minY).add(color);
  55. buffer.add(x).add(y + 8.0f).add(minX).add(maxY).add(color);
  56. buffer.add(x + 6.0f).add(y).add(maxX).add(minY).add(color);
  57. buffer.add(x + 6.0f).add(y + 8.0f).add(maxX).add(maxY).add(color);
  58. x += 6;
  59. vertices += 4;
  60. }
  61. void Renderer::renderString(const Vector2& pos, const char* text, int limit) {
  62. setupString(pos, text, limit);
  63. while(hasStringData()) {
  64. char32_t c = readUnicode();
  65. if(readColor(c)) {
  66. continue;
  67. }
  68. addChar(c);
  69. }
  70. font.bindTo(0);
  71. update();
  72. vertexBuffer.drawStrip(vertices);
  73. }
  74. Vector2 Renderer::getStringSize(const char* text, int limit) {
  75. setupStringRead(text, limit);
  76. Vector2 size(0.0f, 8.0f);
  77. while(hasStringData()) {
  78. char32_t c = readUnicode();
  79. if(readColor(c)) {
  80. continue;
  81. }
  82. size[0] += 6.0f;
  83. }
  84. return size;
  85. }
  86. void Renderer::renderRectangle(const Vector2& pos, const Vector2& size,
  87. Color4 c) {
  88. buffer.clear();
  89. buffer.add(pos[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
  90. buffer.add(pos[0]).add(pos[1] + size[1]).add(0.0f).add(0.0f).add(c);
  91. buffer.add(pos[0] + size[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
  92. buffer.add(pos[0] + size[0])
  93. .add(pos[1] + size[1])
  94. .add(0.0f)
  95. .add(0.0f)
  96. .add(c);
  97. update();
  98. vertexBuffer.drawStrip(4);
  99. }
  100. void Renderer::update() {
  101. vertexBuffer.updateData(
  102. 0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
  103. }
  104. int Renderer::charsInSpace(float width) {
  105. return std::abs(width / 6.0f);
  106. }