[SDL] Best way to rotate a sprite?

Glenn Maynard g_sdl at zewt.org
Tue Jan 28 17:49:00 PST 2003


> > angle = (angle + 1) % 32;
> > angle = (angle + 31) % 32;

To generalize this trick a bit, (a + b) % c can be written as 
(a + b + c) % c, to get positive results from a negative "b", as long as
abs(b) < c (that is, as long as the +c is enough to push it back >= 0).  If
that's not the case, there's "t = (a + b) % c" (gives a number between
-c and c, exclusive) and then "(t + c) % c".

On Wed, Jan 29, 2003 at 03:16:51AM +0200, Sami Näätänen wrote:
> This is good if the divisor is not an exact power of two. How ever if it 
> is the original example, which uses and is faster, so no reason to use 
> this.

(Oh.  I had to read this for a moment to figure out what you meant.
Tip: it helps to capitalize boolean operations--"which uses AND"--for
readability.)

This is incorrect.  The compiler will use a bitwise AND for integer MOD
when possible; using AND has no effect except to make the code harder to
understand.

If you'd like an example of this, let me know in private (too off-topic)
and I'll show you how this can be demonstrated.


(Apologies for the off-topic post; I have difficulty letting misdirected
optimization advise go without being corrected, as it leads to ugly code
that I may some day have to deal with.  :)  

-- 
Glenn Maynard




More information about the SDL mailing list