1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #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.0f).add(minX).add(maxY).add(color);
- buffer.add(x + 6.0f).add(y).add(maxX).add(minY).add(color);
- buffer.add(x + 6.0f).add(y + 8.0f).add(maxX).add(maxY).add(color);
- x += 6;
- index++;
- vertices += 4;
- }
- font.bindTo(0);
- update();
- vertexBuffer.drawStrip(vertices);
- }
- float Renderer::drawStringWidth(const char* text) {
- float width = 0.0f;
- int index = 0;
- 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;
- }
- index += 4;
- continue;
- }
- width += 6.0f;
- index++;
- }
- return width;
- }
- void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
- Color4 c) {
- buffer.clear();
- buffer.add(pos[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
- buffer.add(pos[0]).add(pos[1] + size[1]).add(0.0f).add(0.0f).add(c);
- buffer.add(pos[0] + size[0]).add(pos[1]).add(0.0f).add(0.0f).add(c);
- buffer.add(pos[0] + size[0])
- .add(pos[1] + size[1])
- .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);
- }
|