text_length(3alleg4)

✨ text_length ✨

okay bestie, welcome to text_length from the Allegro manual β€” yes, that Allegro, the game programming library πŸ’–


🌸 NAME 🌸

text_length β€” it hands you the length of a string in pixels. that's it, that's the whole job. Allegro game programming library, as always πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <allegro.h>

int text_length(const FONT *f, const char *str);

πŸ’… DESCRIPTION πŸ’…

so like, you give it a string and a font, and it tells you how wide that string is gonna be when it's actually drawn β€” in pixels, not in characters. which is lowkey the thing you actually need, because a string of 40 "i"s and a string of 40 "W"s are absolutely not the same width, and the computer knows it even if your brain doesn't 😭

the font is the FONT *f, the string is the const char *str, and the return value is that pixel length as an int. that's it. iconic behaviour, no cap ✨

and here's why you'd care β€” the classic move is measuring first, then making a bitmap exactly that big. like this:

int width = text_length(font, "I love spam");
...
bmp = create_bitmap(width, height);

see what she did there? measured the vibe, THEN built the canvas. main character energy fr fr πŸ‘‘


🫢 SEE ALSO 🫢


not me writing an entire love letter to a function that returns an int πŸ’€ but she's useful, okay?? go read the friends above, they're all part of the same squad.

Allegro, version 4.4.3, text_length(3alleg4) β€” mwah, that's a wrap, go measure some strings, you absolute legend πŸŒˆβ­πŸ’œ