20

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?

Justen
  • 4,859
  • 9
  • 44
  • 68

3 Answers3

24

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.

Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43
Zack The Human
  • 8,373
  • 7
  • 39
  • 60
  • I think I'd like to explore the OpenGL route, however, I've never worked with it. Do you have any recommendations on a website to start on a tutorial for rotation? – Justen Jul 26 '09 at 07:13
  • I've looked at some code from NeHe, and this seems incredibly tedious to set up a rotation on an image... I can't understand it at all, any help on this topic? – Justen Jul 26 '09 at 09:59
  • I don't/haven't worked with OpenGL in a long time. Your OpenGL-specific rotation question may be a good topic for a new question. You're bound to get a much better answer than I can give. – Zack The Human Jul 26 '09 at 17:38
  • 1
    Just wanted to add that for your option 1 (which is the most common approach), RotSprite is a great solution -- google it, its a free tool. Also to note there is an option 4: Write it yourself using matrices; see Mark Ransom's answer [here](http://stackoverflow.com/questions/299267/image-scaling-and-rotating-in-c-c). – Engineer Jul 13 '11 at 14:21
  • @Nick Wiggill Thanks for the update. I added a link to some information about RotSprite (the RotSprite's author's site isn't working for me right now, but I may update it later). I didn't include the fourth option because it doesn't pertain to SDL (I suppose my answer #1 doesn't either, but oh well) and you already provided a link. Thanks again! – Zack The Human Jul 13 '11 at 15:48
  • 3
    The best way to thank someone is through an upvote on their comment :) – Engineer Jul 17 '11 at 11:11
  • @ZackTheHuman None of the links above in the answer seems to be useful. I have trouble understanding 3. option. I get what you mean, but how do I do it? Do you have any example code? – Silidrone Jun 18 '17 at 10:12
  • @MuhamedCicak Unfortunately it looks like some of the links are dead. The advice here was to use OpenGL's API, which supports rotation, and you can do this with SDL quite easily. I recommend taking a look at Lazyfoo's tutorials: http://lazyfoo.net/tutorials/SDL/15_rotation_and_flipping/index.php – Zack The Human Jun 29 '17 at 06:00
  • This solution is outdated. Julian Goddard's solution is the simplest way to do this now: use the SDL_RenderCopyEx() function. – cmc Jul 27 '20 at 00:03
  • How much og this applies to SDL 1.2? – vesperto Jun 06 '22 at 10:11
14
SDL_RenderCopyEx()

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

Julian Goddard
  • 431
  • 1
  • 4
  • 10
3

You can use a library like SDL_gfx

Artur Soler
  • 2,974
  • 2
  • 23
  • 24
  • Yes, it is right. Use `rotozoomSurface(...)` from SDL1 or SDL2: https://www.ferzkopp.net/Software/SDL_rotozoom/ or `rotateSurface90Degrees(...)` – Alexander Lubyagin Jun 22 '21 at 08:10