2D sprites rotation...

Started by
12 comments, last by smr 18 years, 10 months ago
Well I don't know too much about SDL but I can suggest two obvious things: first - get those multiplications out of the inner loop. You should be able to get by with just additions and bit shifts. Second - it will be a LOT fater if you can get a pointer to the pixels on the surface and manipulate them directly instead of working through SDL_GetRGB and PutPixel. If you're working in more than one colour depth this will mean more than one version of the function but it won't exactly kill you - it only depends on the number of bits per pixel not the format.
Advertisement
Quote:Original post by haora
...but still, with like 20 sprites rotating at the same time, the game gets really slow...


Here's a suggestion: Don't rotate 20 sprites at the same time. Instead, make an array of surfaces for each sprite, each element being a sprite rotated X degrees. Something like:

SDL_surface* rotated_foo_sprite[64];void init_foo_sprite(){   int i;   /* Pretend I want to bother writing code to initialize rotated_foo_sprite to be    * full of surfaces of the appropriate size here... */   for( i = 0; i < 64; i++ )   {      draw_rotated_sprite( foo_sprite, rotated_foo_sprite, (int) (i * 5.625), 0, 0, true );   }}


Classic trade-off of speed for memory. Of course, I don't know what exactly you're trying to do with this, so this may not be appropriate.

Sorry the SDL library didn't work out; I'm not an expert on VC, but surely if it compiled it should link fine... what kind of errors were you having?
-----http://alopex.liLet's Program: http://youtube.com/user/icefox192
Quote:
Here's a suggestion: Don't rotate 20 sprites at the same time. Instead, make an array of surfaces for each sprite, each element being a sprite rotated X degrees.


Thanks, that's an idea..., I could do that....

About the linking errors.., well I don't remember them right now, but they were the "undefined reference to..." type of errors...


I'm actually having another problem now..., I don't know if anybody noticed, but the "draw_rotated_sprite" function only work with square sprites..., if the sprite is rectangular, the whole thing (the sprite) gets cut down....

Any ideas on how could I fix that???

Thanks again!
xminusCenter = x-haldWidth;yminusCenter = y-halfHeight;cosin = oTrig->_Cos(angle); //this only access an array with the precalculated valuesin = oTrig->_Sin(angle);xrot = (int)(((cosin * (xminusCenter)) - (sin*(yminusCenter))));yrot = (int)(((sin * (xminusCenter)) + (cosin*(yminusCenter))));


You can remove all of this code from the inner loop, or at least simplify it. Since your angle never changes, there's no need to get it for every pixel. Just do it once outside both of your loops. Also I'm not sure there's a need for using lookup tables for your sine and cosines. Modern processors are pretty darn fast at floating point nowadays. You can remove the multiplications by changing them to simple floating point additions. Just calculate your x increment and your y increment, then add them to the current x and current y for each pixel. You're done with the row when x >= the width of the sprite, and you're done with the sprite when y >= the hieght.

The calls to getpixel and putpixel are also showstoppers. You'll never get good performance with these. They don't inline well because they both do a great deal of setup each time you call them that could be done just once if you implemented them yourself... which is no trivial task to boot.

This topic is closed to new replies.

Advertisement