Renderer.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "client/rendering/Renderer.h"
  2. #include "gaming-core/rendering/Attributes.h"
  3. #include "gaming-core/utils/Buffer.h"
  4. Renderer::Renderer() : buffer(16), font("resources/font8x8.png") {
  5. vertexBuffer.setAttributes(
  6. Attributes().addFloat(2).addFloat(2).addColor4());
  7. vertexBuffer.setStreamData(1024 * 1024);
  8. }
  9. void Renderer::drawString(float x, float y, const char* text) {
  10. buffer.clear();
  11. int index = 0;
  12. int vertices = 0;
  13. Color4 color(0xFF, 0xFF, 0xFF, 0x00);
  14. while(text[index] != '\0') {
  15. char32_t c = text[index];
  16. if(c > 128 && text[index + 1] != '\0') {
  17. index++;
  18. c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
  19. }
  20. if(c == '&') {
  21. if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
  22. text[index + 3] == '\0') {
  23. break;
  24. }
  25. color[0] = ((text[index + 1] - '0') * 255) / 9;
  26. color[1] = ((text[index + 2] - '0') * 255) / 9;
  27. color[2] = ((text[index + 3] - '0') * 255) / 9;
  28. index += 4;
  29. continue;
  30. }
  31. float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f;
  32. float minY = (c >> 4) * (1.0f / 16.0f);
  33. float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
  34. float maxY = minY + (1.0f / 16.0f);
  35. buffer.add(x).add(y).add(minX).add(minY).add(color);
  36. buffer.add(x).add(y + 8.0f).add(minX).add(maxY).add(color);
  37. buffer.add(x + 6.0f).add(y).add(maxX).add(minY).add(color);
  38. buffer.add(x + 6.0f).add(y + 8.0f).add(maxX).add(maxY).add(color);
  39. x += 6;
  40. index++;
  41. vertices += 4;
  42. }
  43. font.bindTo(0);
  44. update();
  45. vertexBuffer.drawStrip(vertices);
  46. }
  47. float Renderer::drawStringWidth(const char* text) {
  48. float width = 0.0f;
  49. int index = 0;
  50. while(text[index] != '\0') {
  51. char32_t c = text[index];
  52. if(c > 128 && text[index + 1] != '\0') {
  53. index++;
  54. c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
  55. }
  56. if(c == '&') {
  57. if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
  58. text[index + 3] == '\0') {
  59. break;
  60. }
  61. index += 4;
  62. continue;
  63. }
  64. width += 6.0f;
  65. index++;
  66. }
  67. return width;
  68. }
  69. void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
  70. Color4 c) {
  71. buffer.clear();
  72. buffer.add(pos[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
  73. buffer.add(pos[0]).add(pos[1] + size[1]).add(0.0f).add(0.0f).add(c);
  74. buffer.add(pos[0] + size[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
  75. buffer.add(pos[0] + size[0])
  76. .add(pos[1] + size[1])
  77. .add(0.0f)
  78. .add(0.0f)
  79. .add(c);
  80. update();
  81. vertexBuffer.drawStrip(4);
  82. }
  83. void Renderer::update() {
  84. vertexBuffer.updateData(
  85. 0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
  86. }