|
@@ -2,76 +2,95 @@
|
|
#include "gaming-core/rendering/Attributes.h"
|
|
#include "gaming-core/rendering/Attributes.h"
|
|
#include "gaming-core/utils/Buffer.h"
|
|
#include "gaming-core/utils/Buffer.h"
|
|
|
|
|
|
-Renderer::Renderer() : buffer(16), font("resources/font8x8.png") {
|
|
+Renderer::Renderer()
|
|
|
|
+ : buffer(16), font("resources/font8x8.png"), text(nullptr), index(0),
|
|
|
|
+ maxIndex(0), x(0.0f), y(0.0f), vertices(0) {
|
|
vertexBuffer.setAttributes(
|
|
vertexBuffer.setAttributes(
|
|
Attributes().addFloat(2).addFloat(2).addColor4());
|
|
Attributes().addFloat(2).addFloat(2).addColor4());
|
|
vertexBuffer.setStreamData(1024 * 1024);
|
|
vertexBuffer.setStreamData(1024 * 1024);
|
|
}
|
|
}
|
|
|
|
|
|
-void Renderer::drawString(float x, float y, const char* text) {
|
|
+void Renderer::setupStringRead(const char* s, int limit) {
|
|
|
|
+ text = s;
|
|
|
|
+ index = 0;
|
|
|
|
+ maxIndex = limit;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Renderer::setupString(const Vector2& pos, const char* s, int limit) {
|
|
buffer.clear();
|
|
buffer.clear();
|
|
- int index = 0;
|
|
+ setupStringRead(s, limit);
|
|
- int vertices = 0;
|
|
+ x = pos[0];
|
|
- Color4 color(0xFF, 0xFF, 0xFF, 0x00);
|
|
+ y = pos[1];
|
|
- while(text[index] != '\0') {
|
|
+ vertices = 0;
|
|
- char32_t c = text[index];
|
|
+ color = {0xFF, 0xFF, 0xFF, 0x00};
|
|
- 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;
|
|
+bool Renderer::hasStringData() const {
|
|
- float minY = (c >> 4) * (1.0f / 16.0f);
|
|
+ return (index < maxIndex || maxIndex < 0) && text[index] != '\0';
|
|
- 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);
|
|
+char32_t Renderer::readUnicode() {
|
|
- buffer.add(x).add(y + 8.0f).add(minX).add(maxY).add(color);
|
|
+ char32_t c = text[index];
|
|
- buffer.add(x + 6.0f).add(y).add(maxX).add(minY).add(color);
|
|
+ index++;
|
|
- buffer.add(x + 6.0f).add(y + 8.0f).add(maxX).add(maxY).add(color);
|
|
+ if(c > 128 && hasStringData()) {
|
|
|
|
+ c = (text[index] & 0x3F) | ((c & 0x1F) << 6);
|
|
|
|
+ index++;
|
|
|
|
+ }
|
|
|
|
+ return c;
|
|
|
|
+}
|
|
|
|
|
|
- x += 6;
|
|
+bool Renderer::readColor(char32_t c) {
|
|
|
|
+ if(c != '&') {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ for(int i = 0; i < 3; i++) {
|
|
|
|
+ if(!hasStringData()) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ color[i] = ((text[index] - '0') * 255) / 9;
|
|
index++;
|
|
index++;
|
|
- vertices += 4;
|
|
|
|
}
|
|
}
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Renderer::addChar(char32_t c) {
|
|
|
|
+ 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;
|
|
|
|
+ vertices += 4;
|
|
|
|
+}
|
|
|
|
|
|
|
|
+void Renderer::drawString(const Vector2& pos, const char* text, int limit) {
|
|
|
|
+ setupString(pos, text, limit);
|
|
|
|
+ while(hasStringData()) {
|
|
|
|
+ char32_t c = readUnicode();
|
|
|
|
+ if(readColor(c)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ addChar(c);
|
|
|
|
+ }
|
|
font.bindTo(0);
|
|
font.bindTo(0);
|
|
update();
|
|
update();
|
|
vertexBuffer.drawStrip(vertices);
|
|
vertexBuffer.drawStrip(vertices);
|
|
}
|
|
}
|
|
|
|
|
|
-float Renderer::drawStringWidth(const char* text) {
|
|
+Vector2 Renderer::getStringSize(const char* text, int limit) {
|
|
- float width = 0.0f;
|
|
+ setupStringRead(text, limit);
|
|
- int index = 0;
|
|
+ Vector2 size(0.0f, 8.0f);
|
|
- while(text[index] != '\0') {
|
|
+ while(hasStringData()) {
|
|
- char32_t c = text[index];
|
|
+ char32_t c = readUnicode();
|
|
- if(c > 128 && text[index + 1] != '\0') {
|
|
+ if(readColor(c)) {
|
|
- 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;
|
|
continue;
|
|
}
|
|
}
|
|
- width += 6.0f;
|
|
+ size[0] += 6.0f;
|
|
- index++;
|
|
|
|
}
|
|
}
|
|
- return width;
|
|
+ return size;
|
|
}
|
|
}
|
|
|
|
|
|
void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
|
|
void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
|
|
@@ -92,4 +111,8 @@ void Renderer::drawRectangle(const Vector2& pos, const Vector2& size,
|
|
void Renderer::update() {
|
|
void Renderer::update() {
|
|
vertexBuffer.updateData(
|
|
vertexBuffer.updateData(
|
|
0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
|
|
0, std::min(buffer.getLength(), vertexBuffer.getSize()), buffer);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int Renderer::charsInSpace(float width) {
|
|
|
|
+ return std::abs(width / 6.0f);
|
|
}
|
|
}
|