Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to rotate an image using SDL?

I am building a game and the main character's arm will be following the mouse cursor, so it will be rotating quite frequently. What would be the best way to rotate it?

like image 851
Justen Avatar asked Jul 26 '09 06:07

Justen


People also ask

Which tool is used for rotating the image?

Answer. Answer: Rotate tool is used to rotate the position of a image.

What is rotation matrix in image processing?

A rotation maps every point of a preimage to an image rotated about a center point, usually the origin, using a rotation matrix. Use the following rules to rotate the figure for a specified rotation. To rotate counterclockwise about the origin, multiply the vertex matrix by the given matrix.


2 Answers

With SDL you have a few choices.


  1. Rotate all your sprites in advance (pre-render all possible rotations) and render them like you would any other sprite. This approach is fast but uses more memory and more sprites. As @Nick Wiggle pointed out, RotSprite is a great tool for generating sprite transformations.

  2. Use something like SDL_gfx to do real-time rotation/zooming. (Not recommended, very slow)

  3. Use SDL in OpenGL mode and render your sprites to primitives, applying a rotation to the primitives.


Option 3 is probably your best bet because you gain all of the advantages of using OpenGL. It's really up to you how to want to do it. It's also a possibility that you can load your sprites, perform all rotation calculations with SDL_gfx and then save the rotated versions to an SDL_Surface in memory.

EDIT: In response to your comment I would recommend checking out Lazyfoo's SDL tutorials. Specifically, this one about rotation. There is also an OpenGl function, glRotatef, which can be useful in your case. A quick search brought back this little tidbit which could also be helpful.

like image 60
Zack The Human Avatar answered Sep 24 '22 02:09

Zack The Human


SDL_RenderCopyEx()

has extra arguments for rotation, flipping, and the rotation center.

like image 21
Julian Goddard Avatar answered Sep 23 '22 02:09

Julian Goddard