Renderer.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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).add(minX).add(maxY).add(color);
  37. buffer.add(x + 6).add(y).add(maxX).add(minY).add(color);
  38. buffer.add(x + 6).add(y + 8).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. void Renderer::drawRectangle(float x, float y, float width, float height,
  48. Color4 c) {
  49. buffer.clear();
  50. buffer.add(x).add(y).add(0.0f).add(0.0f).add(c);
  51. buffer.add(x).add(y + height).add(0.0f).add(0.0f).add(c);
  52. buffer.add(x + width).add(y).add(0.0f).add(0.0f).add(c);
  53. buffer.add(x + width).add(y + height).add(0.0f).add(0.0f).add(c);
  54. update();
  55. vertexBuffer.drawStrip(4);
  56. }
  57. void Renderer::update() {
  58. vertexBuffer.updateData(
  59. 0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
  60. }