From 015b67d0a1c8d4a24b868d3819effd0a1568692f Mon Sep 17 00:00:00 2001 From: Jeffery Myers Date: Sun, 6 Feb 2022 09:14:28 -0800 Subject: [PATCH] Add info on centering text --- ...ently-asked-Questions--Common-Questions.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Frequently-asked-Questions--Common-Questions.md b/Frequently-asked-Questions--Common-Questions.md index 3d5bab9..5d91407 100644 --- a/Frequently-asked-Questions--Common-Questions.md +++ b/Frequently-asked-Questions--Common-Questions.md @@ -68,4 +68,26 @@ This will convert a screen point into a world point for a camera. This will incl This function gets a screen point for a world point, using zoom and scale. It is useful for computing the location of HUD elements that should not be scaled or rotated with the world view, such as player names, health bars, or other labels. +# How do I center text on the screen? +Raylib does not offer any text formatting functions, so you need to compute the starting point for all text that you draw. The starting point for text is always the upper left corner. + +You can compute the center of the screen by dividing the screen width and hieght in half. +``` +int screenCenterX = GetScreenWidth() / 2; +int screenCenterY = GetScreenHeight() / 2; +``` +Next you need to compute how large the text is, and offset the center by half the text size. + +``` +const char* text = "Congrats! You created your first window!"; + +int fontSize = 20; +int textWidth = MeasureText(text, fontSize); + +int textStartX = screenCenterX - textWidth / 2; +int textStartY = screenCenterY - fontSize / 2; + +DrawText(text, textStartX, textStartY, fontSize, LIGHTGRAY); +``` +`MeasureText` only measures the width of text, but takes fewer arguments. It is often acceptable to just use the font size as the total text height, but for some fonts, this may not be accurate. `MeasureTextEx` will measure both height and width of text, but does take more arguments. For this reason it is used less often.