[SDL] Best way to rotate a sprite?

Kentaro Fukuchi fukuchi at is.titech.ac.jp
Tue Jan 28 14:56:01 PST 2003


From: Sami Näätänen <sami.naatanen at kolumbus.fi>
Subject: Re: [SDL] Best way to rotate a sprite?
Date: Wed, 29 Jan 2003 00:30:14 +0200

> > angle = (angle + 1) % 32;
> > angle = (angle - 1) % 32;
> >
> 
> In fact the above thing doesn't work correctly, because the C standard 
> defines the %-operator as mathematically correct reminder operation.
> 
> So the  -1 % 32 = -1, but we want it to be 31.

Use following code instead.

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

// "angle + 31" misses the chance to be optimized because IA-32 has "dec" instruction
// to decrement a value of the register. But it is not so important.
// Maybe this line will be exectuted less than 1000 times / frame for the purpose.

Kentaro




More information about the SDL mailing list