1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "client/rendering/Renderer.h"
- #include "gaming-core/rendering/Attributes.h"
- #include "gaming-core/utils/Buffer.h"
- Renderer::Renderer() : buffer(16), font("resources/font8x8.png") {
- vertexBuffer.setAttributes(
- Attributes().addFloat(2).addFloat(2).addColor4());
- vertexBuffer.setStreamData(1024 * 1024);
- }
- void Renderer::drawString(float x, float y, const char* text) {
- buffer.clear();
- int index = 0;
- int vertices = 0;
- Color4 color(0xFF, 0xFF, 0xFF, 0x00);
- while(text[index] != '\0') {
- char32_t c = text[index];
- if(c > 128 && text[index + 1] != '\0') {
- index++;
- c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
- }
- if(c == '&') {
- if(text[index + 1] == '\0' || text[index + 2] == '\0' ||
- text[index + 3] == '\0') {
- break;
- }
- color[0] = ((text[index + 1] - '0') * 255) / 9;
- color[1] = ((text[index + 2] - '0') * 255) / 9;
- color[2] = ((text[index + 3] - '0') * 255) / 9;
- index += 4;
- continue;
- }
- float minX = (c & 0xF) * (1.0f / 16.0f) + 1.0f / 128.0f;
- float minY = (c >> 4) * (1.0f / 16.0f);
- float maxX = minX + (1.0f / 16.0f) - 2.0f / 128.0f;
- float maxY = minY + (1.0f / 16.0f);
- buffer.add(x).add(y).add(minX).add(minY).add(color);
- buffer.add(x).add(y + 8).add(minX).add(maxY).add(color);
- buffer.add(x + 6).add(y).add(maxX).add(minY).add(color);
- buffer.add(x + 6).add(y + 8).add(maxX).add(maxY).add(color);
- x += 6;
- index++;
- vertices += 4;
- }
- font.bindTo(0);
- update();
- vertexBuffer.drawStrip(vertices);
- }
- void Renderer::drawRectangle(float x, float y, float width, float height,
- Color4 c) {
- buffer.clear();
- buffer.add(x).add(y).add(0.0f).add(0.0f).add(c);
- buffer.add(x).add(y + height).add(0.0f).add(0.0f).add(c);
- buffer.add(x + width).add(y).add(0.0f).add(0.0f).add(c);
- buffer.add(x + width).add(y + height).add(0.0f).add(0.0f).add(c);
- update();
- vertexBuffer.drawStrip(4);
- }
- void Renderer::update() {
- vertexBuffer.updateData(
- 0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
- }
|