Jump to content
  • Advertisement
Sign in to follow this  
GunGrave12

[SDL_gfx] Memory leaking issues with rotozoomSurface()

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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

deg = rad x (180/pi)

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!