| 92 | } |
| 93 | |
| 94 | TextRenderer* TextRenderer::RenderText(const char* str, float centerX, |
| 95 | float centerY) { |
| 96 | float aspect = SceneManager::GetInstance()->GetScreenAspect(); |
| 97 | glm::mat4 orthoMat = glm::ortho(0.0f, aspect, 0.0f, 1.0f); |
| 98 | glm::mat4 modelMat, mat, scaleMat; |
| 99 | int cols, rows; |
| 100 | bool hadDepthTest; |
| 101 | |
| 102 | centerY += CORRECTION_Y * mFontScale; |
| 103 | |
| 104 | glLineWidth(TEXT_LINE_WIDTH); |
| 105 | |
| 106 | hadDepthTest = glIsEnabled(GL_DEPTH_TEST); |
| 107 | glDisable(GL_DEPTH_TEST); |
| 108 | |
| 109 | mTrivialShader->SetTintColor(mColor[0], mColor[1], mColor[2]); |
| 110 | |
| 111 | _count_rows_cols(str, &cols, &rows); |
| 112 | scaleMat = |
| 113 | glm::scale(glm::mat4(1.0f), glm::vec3(mFontScale, mFontScale, 1.0f)); |
| 114 | float charWidth = ALPHABET_GLYPH_COLS * ALPHABET_SCALE * mFontScale; |
| 115 | float charHeight = ALPHABET_GLYPH_ROWS * ALPHABET_SCALE * mFontScale; |
| 116 | float charSpacing = CHAR_SPACING_F * charWidth; |
| 117 | float lineSpacing = LINE_SPACING_F * charHeight; |
| 118 | float width = cols * charWidth + (cols - 1) * charSpacing; |
| 119 | float height = rows * charHeight + (rows - 1) * lineSpacing; |
| 120 | float startX = centerX - width * 0.5f + 0.5f * charWidth; |
| 121 | float startY = centerY + height * 0.5f - 0.5f * charHeight; |
| 122 | float y = startY; |
| 123 | |
| 124 | modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(startX, startY, 0.0f)); |
| 125 | for (; *str; ++str) { |
| 126 | if (*str == '\n') { |
| 127 | y -= charHeight + lineSpacing; |
| 128 | modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(startX, y, 0.0f)); |
| 129 | } else { |
| 130 | int code = (int)*str; |
| 131 | if (code >= 0 && code < CHAR_CODES && mCharGeom[code]) { |
| 132 | mat = orthoMat * modelMat * scaleMat * mMatrix; |
| 133 | mTrivialShader->RenderSimpleGeom(&mat, mCharGeom[code]); |
| 134 | } |
| 135 | modelMat = glm::translate(modelMat, |
| 136 | glm::vec3(charWidth + charSpacing, 0.0f, 0.0f)); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | glLineWidth(1); |
| 141 | if (hadDepthTest) { |
| 142 | glEnable(GL_DEPTH_TEST); |
| 143 | } |
| 144 | return this; |
| 145 | } |
no test coverage detected