[SDL] Best way to rotate a sprite?

David Olofson david at olofson.net
Sun Jan 26 11:44:01 PST 2003

On Sunday 26 January 2003 19.34, Tane Piper wrote:
> Hey everyone,
> At the moment, I am using a rather dodgy control method in my game
> where the direction button changes the sprite on screen to face
> that direction,  but each direction has its own bitmap:
> Up button loads bitmap 0,
> Right button load bitmap 1, etc.
> What I want to do is change this so there is only one bitmap needed
> for the sprite and the buttons rotate the sprite, or make it go
> backwards or forwards along the angle it's facing - the same
> control system used in the original Grand Theft Auto.  I don't see
> anything in the SDL docs that directly allow you to control the
> rotation of a SDL_surface (and from this I can do the forward and
> backward motion along the vector of the sprite direction).  Is
> there any libraries that allow you to do this directly to the
> surface, or can anybody help with code for this problem?

There are a few solutions for this, but there are some things I think 
you should consider first:

	* Rotating surfaces is *expensive*, unless you do it in
	  hardware - and SDL doesn't support that. (Nor does glSDL,
	  BTW, as it's not part of the SDL API.)

	* Rotating in software is doable, but gets *very* expensive
	  if you want to do it with reasonable image quality.
	  (Linear interpolation is a minimum requirement, IMHO.)

	* Just rotating a sprite won't change the lighting of the
	  image (obviously), so unless the sprite is created with
	  the ligh right from top (rather unusual), it will look
	  plain wrong.

If you don't cane about "realistic" ligthing, I'd recommend you grab 
SGE or some other lib (I'm too lazy to find the URLs for you right 
now ;-) and do the rotation load time, creating an array of surfaces 
indexed by the direction of the object. This is fast and simple, and 
lets you test different numbers of images very easilly.

Later on, you can either ditch the rotation code and load a set of 
prerendered images, or combine the methods; have 8 or 16 prerendered 
images with correct lighting, and rotate at load time to generate 
intermediate images if you want more.

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`---------------------------> http://olofson.net/audiality -'
   --- http://olofson.net --- http://www.reologica.se ---

More information about the SDL mailing list