Witam! Mam metodę, która wyświetla tekst i chciałem dodać do niej możliwość przesunięcia tekstu do następnej linijki w momencie kiedy wystąpi '\n'
typedef unsigned int uint;
inline void RemoveCharFromString(std::string& string, char remove)
{
if (string.find(remove) != std::string::npos) {
string.erase(std::remove(string.begin(), string.end(), remove), string.end());
}
}
void DrawString(const std::string& text, const Vector2& position, uint color, float scale, const Font& font)
{
using namespace ftgl;
texture_font_t* ftFont = font.getFTFont();
Vector2 finalPosition = position;
std::string finalText = text;
float lineHeight = font.getFontSize() * scale;
for (uint i = 0; i < finalText.length(); ++i) {
texture_glyph_t* glyph = texture_font_get_glyph(ftFont, finalText.c_str() + i);
if (glyph) {
float kerning = 0.0f;
if (i > 0) {
kerning = texture_glyph_get_kerning(glyph, finalText.c_str() + i - 1);
}
finalPosition.x += kerning * scale;
//Usuwam '\n', bo na ekranie pokazywał się kwadrat, a tego nie chce
RemoveCharFromString(finalText, '\n');
//sprawdzam czy w tym nie usuniętym tekście jest gdzieś '\n', jeśli jest to tekst przesuwamy w dół i pozycję X ustawiamy na początek tekstu
if(text[i] == '\n') {
finalPosition.y -= lineHeight;
finalPosition.x = position.x;
}
float x0 = finalPosition.x + static_cast<float>(glyph->offset_x) * scale,
y0 = finalPosition.y + static_cast<float>(glyph->offset_y) * scale,
x1 = x0 + static_cast<float>(glyph->width) * scale,
y1 = y0 - static_cast<float>(glyph->height) * scale,
u0 = glyph->s0,
v0 = glyph->t0,
u1 = glyph->s1,
v1 = glyph->t1;
//Dalej są x0, x1 itd.. przekazywane do buffera, nie ma znaczenia dla sprawy
finalPosition.x += glyph->advance_x * scale;
}
}
}
Problem pojawia się gdy w tekście jest więcej niż jeden '\n'
np.
Font jakisFont(...);
DrawString("String\nString", {300.0f, 300.0f}, 0xffffffff, 1.0f, jakisFont);
wyświetla
String
String
ale już
DrawString("String\nString\nString", {300.0f, 300.0f}, 0xffffffff, 1.0f, jakisFont);
wyświetla
String
Strings
tring
a powinien
String
String
String
Dlaczego tak się dzieje? W jaki sposób to naprawić?