-
Notifications
You must be signed in to change notification settings - Fork 23
Open
Description
To center the score as text inside the rectanble we have to obtain its width. However, when I use the function MeasureTextEx the returned value (x) is always 0. However, when I use MeasureText, with the default font, the values are returned.
I have tried with another font and same result. (raylib 5.5, in Ubuntu 24.04)
//main.h
// const char *MONOGRAM_FONT_FILE = "assets/fonts/monogram.ttf";
// const char *MARKLMONO_FONT_FILE = "assets/fonts/MarklMono-Regular.otf";
Font font1 = LoadFontEx(MONOGRAM_FONT_FILE, 64, nullptr, 0);
Font font2 = LoadFontEx(MARKLMONO_FONT_FILE, 64, nullptr, 0);
[...]
// Score
DrawTextEx(font1, "Score", {365, 15}, 38, 2, WHITE); // title
DrawRectangleRounded({320, 55, 170, 60}, 0.3, 6, lightBlue); //Rectangle lightBlue
// convert to text and center it
char scoreText[10] = {0};
sprintf(scoreText, "%d", game.score);
const auto textWidthDefault = static_cast<float>(MeasureText(scoreText, 38.0)); // measures default font. It works
const float textWidth = MeasureTextEx(font1, scoreText, 38.0, 2.0).x; // Measures used font, always returns 0 (either font1 or font2)
std::cout << scoreText << " --- " << textWidth << " --- " << textWidthDefault << std::endl; // debug
DrawTextEx(font1, scoreText, {(320 + (170 - textWidth)/2), 65}, 38, 2, WHITE);
Terminal: 128 --- 0 --- 51
Metadata
Metadata
Assignees
Labels
No labels