Compare commits

...

5 Commits

Author SHA1 Message Date
FakeShemp 981dc3467e Baggu fixed again
+Bonus document formatted
2016-03-09 22:11:17 +01:00
FakeShemp 42af029a26 fix baggu 2016-03-09 21:25:51 +01:00
FakeShemp 2e5dfc609a support headshot icons 2016-03-09 19:51:57 +01:00
FakeShemp ba6d2b758d Add killstreak icon 2016-03-09 12:34:09 +01:00
FakeShemp 12dd1d21de Text now handles color, alpha and icon parsing 2016-03-09 12:11:26 +01:00
2 changed files with 174 additions and 74 deletions
+1
View File
@@ -23,6 +23,7 @@ public:
private: private:
void renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix); void renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix);
std::string parseColors(std::string text, std::map<int, glm::vec4>& colorChanges, glm::vec4 originalColor);
Font* font; Font* font;
GLuint VAO, VBO; GLuint VAO, VBO;
+106 -7
View File
@@ -46,24 +46,117 @@ void TextPass::Draw(RenderScene& scene, FrameBuffer& frameBuffer)
delete state; delete state;
} }
std::string TextPass::parseColors(std::string text, std::map<int, glm::vec4>& colorChanges, glm::vec4 originalColor)
{
std::string parsedString = text;
glm::vec4 newColor = originalColor;
bool colorChange = false;
for (std::string::const_iterator c = parsedString.begin(); c != parsedString.end(); c++) {
if (*c == char(92)) { // Backlash
if ((c + 1) != parsedString.end()) {
if (*(c + 1) == char('C')) { // C for Color
if ((c + 7) != parsedString.end()) {
bool hasCorrectFormat = true;
for (std::string::const_iterator colorC = c + 2; colorC != c + 8; colorC++) {
if (*colorC < '0' || *colorC > 'F') {
hasCorrectFormat = false;
break;
}
}
if (hasCorrectFormat == true) {
colorChange = true;
std::array<int, 3> hexToInt = {
std::stoi(std::string(c + 2, c + 4), 0, 16),
std::stoi(std::string(c + 4, c + 6), 0, 16),
std::stoi(std::string(c + 6, c + 8), 0, 16)
};
newColor = glm::vec4(
float(hexToInt[0]) / 255.f,
float(hexToInt[1]) / 255.f,
float(hexToInt[2]) / 255.f,
newColor.a);
parsedString.erase(c, (c + 8));
}
}
}
if (*(c + 1) == char('A')) { // A for Alpha
if ((c + 3) != parsedString.end()) {
bool hasCorrectFormat = true;
for (std::string::const_iterator colorC = c + 2; colorC != c + 4; colorC++) {
if (*colorC < '0' || *colorC > 'F') {
hasCorrectFormat = false;
break;
}
}
if (hasCorrectFormat == true) {
colorChange = true;
int hexToInt = std::stoi(std::string(c + 2, c + 4), 0, 16);
newColor = glm::vec4(
newColor.r,
newColor.g,
newColor.b,
float(hexToInt) / 255.f);
parsedString.erase(c, (c + 4));
}
}
}
if ((*(c + 1) >= '1' && *(c + 1) <= '9') || *(c + 1) == 'B' || *(c + 1) == 'E' || *(c + 1) == 'F') { // Icons
if (*(c + 1) >= '1' && *(c + 1) <= '9') {
parsedString.replace(c, (c + 2), 1, (*(c + 1) - 48));
}
else {
parsedString.replace(c, (c + 2), 1, (*(c + 1) - 55));
}
}
if (colorChange) {
colorChanges[c - parsedString.begin()] = newColor;
}
}
}
}
return parsedString;
}
void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix) void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum alignment, glm::vec4 color, glm::mat4 modelMatrix, glm::mat4 projectionMatrix, glm::mat4 viewMatrix)
{ {
GLfloat penX = 0; GLfloat penX = 0;
GLfloat penY = 0; GLfloat penY = 0;
GLfloat scale = 1.0/font->FontSize; GLfloat scale = 1.0 / font->FontSize;
GLfloat stringWidth = 0.f; GLfloat stringWidth = 0.f;
for (std::string::const_iterator c = text.begin(); c != text.end(); c++) { std::map<int, glm::vec4> colorChanges;
std::string parsedText = parseColors(text, colorChanges, color);
for (std::string::const_iterator c = parsedText.begin(); c != parsedText.end(); c++) {
Font::Character ch = font->m_Characters[*c]; Font::Character ch = font->m_Characters[*c];
stringWidth += (ch.Advance >> 6) * scale; stringWidth += (ch.Advance >> 6) * scale;
} }
if(alignment == TextJob::AlignmentEnum::Center) { if (alignment == TextJob::AlignmentEnum::Center) {
penX = -stringWidth/2.f; penX = -stringWidth / 2.f;
} else if (alignment == TextJob::AlignmentEnum::Right) { }
else if (alignment == TextJob::AlignmentEnum::Right) {
penX = -stringWidth; penX = -stringWidth;
} else { }
else {
penX = 0; penX = 0;
} }
@@ -78,7 +171,13 @@ void TextPass::renderText(std::string text, Font* font, TextJob::AlignmentEnum a
glBindVertexArray(VAO); glBindVertexArray(VAO);
for (std::string::const_iterator c = text.begin(); c != text.end(); c++) { for (std::string::const_iterator c = parsedText.begin(); c != parsedText.end(); c++) {
auto it = colorChanges.find(c - parsedText.begin());
if (it != colorChanges.end()) {
glUniform4fv(glGetUniformLocation(m_TextProgram->GetHandle(), "textColor"), 1, glm::value_ptr(it->second));
}
Font::Character ch = font->m_Characters[*c]; Font::Character ch = font->m_Characters[*c];
GLfloat xpos = penX + ch.Bearing.x * scale; GLfloat xpos = penX + ch.Bearing.x * scale;