Skip to main content
GameDev.net gamedev.net
🔒 Locked

Sprite rotation in SDL?

Started by Gameteen Mar 16, 2006 at 2:51 PM 6 replies 11.2k views
Original Post
Gameteen
Gameteen
More questions about my favorite graphics API! In order to make an Asteroids clone in SDL, I nedd to figure out how to rotate sprites. Seeing as SDL is very low level (unlike Allegro with functions for everything like rotation, flipping, and walking your dog), I believe I need to implement this myself. A quick search on google revealed an OpenGL solution, but for 2d games I want to stick with SDL only. I want to keep things simple. thx again! "No matter what the job is, you can always find someone dumber than you to do it!" - Daniel Croce
Disclaimer: The contents of this post do not necessarily reflect the opinion of any sane person, and may in fact, contradict such views. Deal with it. ;)
Lazy Foo
Lazy Foo
Just rotate them ahead of time using Photoshop/GIMP/whatever.

Or use SDL_gfx
Learn to make games with my SDL 2 Tutorials
andrew_480
andrew_480
Definitely use the SDL_gfx library. You can use the function
rotozoomSurface(SDL_Surface* surface, double zoom, double angle, int smooth)

to easily rotate and zoom in on your sprites.
Gameteen
Gameteen
Thanks guys, this is exactly what I'm looking for. I want in-game rotation. Just a couple more question:

How do I get the spaceship to go in the direction it's facing when I press the up arrow?

How do I get the bullets to move in that direction when I press the fire button?

thx again!
Disclaimer: The contents of this post do not necessarily reflect the opinion of any sane person, and may in fact, contradict such views. Deal with it. ;)
Lazy Foo
Lazy Foo
Quote:
Original post by Gameteen
Thanks guys, this is exactly what I'm looking for. I want in-game rotation. Just a couple more question:

How do I get the spaceship to go in the direction it's facing when I press the up arrow?

How do I get the bullets to move in that direction when I press the fire button?

thx again!


Think back to your trigonomtery.

on up increase angle, on down decrease angle.

on fire shoot bullet at cosine of angle * velocity and sine of angle * velocity.
Learn to make games with my SDL 2 Tutorials
Rob Loach
Rob Loach
Don't use rotozoomSurface in realtime. It's extremely slow and if you call it a bunch of times in your main loop, your framerate will drop quite fast. It would be a better idea to cache the rotation and then use it when needed. This will dramatically speed up the framerate an ensure you don't loose any frames.

This was a huge issue I had with Blastoids and I ended up implementing a low detail and high detail graphic settings for the user so that they can choose to allow or disallow realtime rotations on objects that don't need them.
Rob Loach [Website] [Projects] [
nullsquared
nullsquared
Quote:
Original post by Rob Loach
Don't use rotozoomSurface in realtime. It's extremely slow and if you call it a bunch of times in your main loop, your framerate will drop quite fast. It would be a better idea to cache the rotation and then use it when needed. This will dramatically speed up the framerate an ensure you don't loose any frames.

This was a huge issue I had with Blastoids and I ended up implementing a low detail and high detail graphic settings for the user so that they can choose to allow or disallow realtime rotations on objects that don't need them.


Yes, do precache the surfaces... But just so you know, that would use a lot of memory because you might end up having 359 surfaces for 360 degrees (considering you can use the 0 degrees one for a 360 degrees one)... Which means creating 359 surfaces and then using pixel-access (slow-also) to rotate... What you can do is, to have approximate degreess... For example, rotate in 4 directions and estimate the angle... Or maybe 8 to make it a little more realistic... Just a few tips..

@Rob - So you are using the rotations in real-time? That seems incredibly fast.... (on my pc, atleast; which isn't exactly "fast")
Gameteen
Gameteen

Awesome! Alright, now I have to stop messing around and actually do it! sigh....... Thx!
Disclaimer: The contents of this post do not necessarily reflect the opinion of any sane person, and may in fact, contradict such views. Deal with it. ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.