Sign in to follow this  
Gameteen

Sprite rotation in SDL?

Recommended Posts

Gameteen    100
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

Share this post


Link to post
Share on other sites
andrew_480    163
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.

Share this post


Link to post
Share on other sites
Gameteen    100
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!

Share this post


Link to post
Share on other sites
Lazy Foo    1113
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.

Share this post


Link to post
Share on other sites
Rob Loach    1504
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.

Share this post


Link to post
Share on other sites
nullsquared    126
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")

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this