Rotation of rectangles in SDL.

Pages: 12
May 11, 2010 at 10:16am
Okay. I have this program in SDL, where there are a lot of objects (classes) that each contain a couple or rectangles grouped together. When I display these objects all of the rectangles are supposed to show. But my problem is, the objects are supposed to move and rotate, movement is easy, but I don't understand how to rotate one rectangle in SDL, much less a group of rectangles around a central point. How do I go about rotating a group of rectangles in a SDL program around a center point?
May 11, 2010 at 10:48am
If you are speaking of an SDL_Surface, then the bad news is: It's going to be slow. No hardware acceleration, except you use OpenGL instead the SDL_Surface struct. Look into SDLgfx library for rotation/zooming surfaces.

Ciao, Imi.
May 12, 2010 at 11:19am
Okay, I got SDL_GFX and added it to my program, searched for some basic tutoirals, but how do I rotate a shape?
May 12, 2010 at 11:41am
Google my friend. There is a file called "SDL_rotozoom.h", thats where the function you want is.


Notes on Rotozoomer

The rotozoom without interpolation code should be fast enough even for some realtime effects if the CPU is fast or bitmaps small. With interpolation the routines are typically used for pre-rendering stuff in higher quality (i.e. smoothing) - that's also a reason why the API differs from SDL_BlitRect() and creates a new target surface each time rotozoom is called. The final rendering speed is dependent on the target surface size as it is beeing xy-scanned when rendering the new surface.
Note also that the smoothing toggle is dependent on the input surface bit depth. 8bit surfaces will never be smoothed - only 32bit surfaces will.

Note that surfaces of other bit depth then 8 and 32 will be converted on the fly to a 32bit surface using a blit into a temporary surface. This impacts performance somewhat.



[[[ Interface ]]]

SDL_Surface * rotozoomSurface (SDL_Surface *src, double angle, double zoom, int smooth);
Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface.
'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1
then the destination 32bit surface is anti-aliased. If the surface is not 8bit
or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
1
2
SDL_Surface * rotozoomSurfaceXY (SDL_Surface *src, double angle, double zoomx,
double zoomy, int smooth);

Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface.
'angle' is the rotation in degrees. 'zoomx' and 'zoomy' are scaling factors that
can also be negative. In this case the corresponding axis is flipped. If 'smooth'
is 1 then the destination 32bit surface is anti-aliased. If the surface is not 8bit
or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
Note: Flipping currently only works with antialiasing turned off.


Reference: http://www.ferzkopp.net/joomla/content/view/19/14/
May 12, 2010 at 11:58am
Ah, that. I haven't read it all yet, but does it say anything about rotating/revolving a group of objects around a central point?
May 12, 2010 at 12:00pm
In SDL can't you setup something so the center of the quad / surface is the coordinates to place the surface on screen. Then when you rotate the object it wouldn't mater? I'm not sure exactly how to set that up in SDL, you'd be better off using openGL as well.
May 12, 2010 at 12:05pm
Yeah I've been thinking about that, I've used OpenGL before so I might just use it too, but I'd still need to know how do with this, weather it's with OpenGL or SDL.
May 12, 2010 at 12:08pm
Are you able to setup libraries and what not quite easily. If you like I can go dig up an old project that I've possibly done this in.
May 12, 2010 at 12:12pm
Sure
May 12, 2010 at 12:26pm
Ok, I've found a few projects with some of the stuff I've used. I have to fix one up and make it simple and able to build. So I'll be a little bit. I'll give you an upload link soon.
May 12, 2010 at 12:28pm
With rotations of objects/groups of objects?
May 12, 2010 at 12:30pm
What do you mean by a group of objects? As in to make an object rotate around another object?
May 12, 2010 at 12:31pm
As in to take a group of objects (rectangles/shapes) and rotate them around the same point as one larger object.

As if you had a stick figure person, with a rectangle for a body arms and legs and a rectangle/square for a head, and you wanted to rotate the whole person, the center rectangle would just rotate while the others would have to move and rotate to make it look as if the whole person was moved.

But any rotation examples would be helpful.
Last edited on May 12, 2010 at 12:33pm
May 12, 2010 at 12:36pm
Well doing what you want there would be all Matrix math. You'd have a root Node for an object that relates to all of the other objects you attach to it. I do Have that somewhere but it'd be fair bit of code. I'm willing to just give you a whole project I made but it does have a lot of files in it. Node, Animation, physics classes etc.
May 12, 2010 at 12:37pm
It's okay, anything is helpful I suppose.
May 12, 2010 at 12:42pm
Well this is strange, running it at the moment and no textures will display. Gah, give me a minute to fix it.
May 12, 2010 at 12:44pm
Is it a SDL program or an OpenGL program?
May 12, 2010 at 12:47pm
SDL Loads the window, OpenGL is the renderer. I tell you what I'll just upload it now. You can see all the math behind things and the Rotation functions. I did make this like a year ago though so I can't remember everything about it and it's late. So I'll let you have a look. It runs and compiles, just may have a few issues. Could possibly be because I made it on Vista and now I'm running 7 and it's sooking.
May 12, 2010 at 12:48pm
Okay, I'll look at it now, actually download it tonight when I get home.
May 12, 2010 at 12:52pm
It's only 10mb, will only take a few mins to upload it. I'll put the link here and you can have a play with it. It has some useless stuff in it, but at the least it has a small framework I whooped up quickly that does a few cool things. I made it a lot more advanced near the end of last year but. I'm not going to give you that one as it's huge. Check back 10 minutes from now and I should have a link up. Don't get me wrong though, this is nothing perfect. You should just be able to read it and hopefully understand how I did everything.
Pages: 12