# [SDL_gfx] Memory leaking issues with rotozoomSurface()

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I'm attempting to rotate my player sprite on the fly based on the mouse position around the sprite. After reading several threads in this forum I came up with this:
double angleRotation = (atan2(static_cast<double>(input.getMouseY()-sprite->getDstY()),static_cast<double>(input.getMouseX()-sprite->getDstX())/PI))*180;

SDL_Surface* oldSprite( sprite->getTexture() );
SDL_Surface* rotatedSprite( rotozoomSurface(sprite->getTexture(),angleRotation,1,1) );

sprite->setTex( rotatedSprite );

SDL_FreeSurface(oldSprite);

First things first: My game leaps in memory consumption when it runs with this code, and also the surface that gets drawn is black. I don't know if the black surface is a side-effect of the massive memory leak(which lags the game like no tomorrow). I thought I was taking care of the memory leak by storing the original surface, and then deleting freeing it afterward, so where am I wrong in this? Also my math skills aren't top notch so I was wondering if the equation for getting the angle of rotation looked alright? Thanks in advance, if I was vague in anyway please tell me so I can clear things up.

##### Share on other sites
Just out of interest have you considered pre-rendering the rotation of the sprite, just to cut the neccecity out of having to call rotozoomSurface(), which im sure i read is rather slow.

The way i tackled the same problem was to save my player image with 36 frames, each rotated 10 degrees, then find the angle and use that to pick the correct frame.

It looks like you have the angleRotation line a bit muddled up aswell, but i could just be reading all the brackets wrong [its late... or very early], atan2 returns the angle in radians

so you need

[source language="cpp"]double angleRotation = atan2(static_cast<double>(input.getMouseY()-sprite->getDstY()),static_cast<double>(input.getMouseX()-sprite->getDstX())) * (180 / 3.141);

As for the memory leak, i cant spot that one^^ havnt used rotozoom all that much myself, and it looks like your method of freeing the old surface should be stopping it.

##### Share on other sites
Alright I will try to prerender the sprite then, but if anyone could spot that memory leak it would be bring me much satisfaction.

And thank you for the corrected calculation for the angle.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
15

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009195
• ### Who's Online (See full list)

There are no registered users currently online

×