SDL_clamp(3)
π SDL_clamp β the manual page, but make it make sense π
okay bestie, welcome to the SDL3 FUNCTIONS corner of the docs, where today's main character is SDL_clamp(3). she's small, she's a macro, and she's lowkey carrying every "keep this number in range" moment in your codebase. let's get into it π«Ά
β¨ NAME β¨
SDL_clamp β Return a value clamped to a range.
like, she takes your number and squishes it into the box you gave her. that's the whole vibe. π
πΈ SYNOPSIS πΈ
#include <SDL3/SDL_stdinc.h>
#define SDL_clamp(x, a, b) (((x) < (a)) ? (a) : (((x) > (b)) ? (b) : (x)))
no cap, that's the entire macro right there. three params, two comparisons, one little nested ternary doing her absolute best. β¨
π¦ DESCRIPTION π¦
so here's the tea: if x is outside the range of values between a and b, the value you get back will be a or b, whichever one is appropriate for the situation. and if x is already chilling inside that range? then you just get x back. she didn't need to change a thing. queen behavior. π
β οΈ but listen β this macro will produce incorrect results ifbis less thana. like, that's not a vibe, that's a bug generator. don't do it. fr fr. π
this is a helper macro, and the reason she exists is that she might be more clear than writing out the comparisons directly by hand. she works with any type that can be compared with the < and > operators β so she's flexible, she's inclusive, she's for everybody. π
however β and this is the part you need to hear β she double-evaluates all her parameters. so do not use expressions with side-effects here. no i++, no function calls that change stuff, none of that. she'll run them twice and you'll be sitting there like π wondering why your counter jumped by two. side effects stay OUT of the macro. periodt.
π MACRO PARAMETERS π
xβ the value to compare. this is your girl, the one getting checked. πaβ the low end value. the floor. the "at minimum" energy.bβ the high end value. the ceiling. the "no further than this" boundary.
π RETURN VALUE π
Returns x, clamped between a and b. that's it, that's the whole return. she comes back with a number, and that number is inside your range, guaranteed (as long as b β₯ a, bestie, we covered this π).
π§ THREAD SAFETY π§
It is safe to call this macro from any thread. go wild, spawn those threads, she does not care, she is not here for drama. π«Ά
β AVAILABILITY β
This macro is available since SDL 3.2.0. if you're on something older, sorry babe, she hadn't been born yet. π
and that's the whole page!! short, sweet, iconic, ate and left no crumbs. go clamp something responsibly. πΈπβ¨
β with love from the Simple Directmedia Layer girlies, SDL 3.4.18, SDL_clamp(3) out ππ¦π
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms