SFML community forums

Help => Graphics => Topic started by: e_barroga on July 10, 2010, 03:13:51 am

Title: Unload Image
Post by: e_barroga on July 10, 2010, 03:13:51 am
How do you unload an image from memory? Right now I am assuming the image is cleared from memory in the destructor of sf::Image.

Although I was wondering if there was a way to put your sf::Image object back to the state before the invocation of: sf::Image::Load()
Title: Unload Image
Post by: Laurent on July 10, 2010, 03:17:17 pm
Quote
How do you unload an image from memory? Right now I am assuming the image is cleared from memory in the destructor of sf::Image.

You're right. So, to clear an image, simply destroy the corresponding instance.

Quote
Although I was wondering if there was a way to put your sf::Image object back to the state before the invocation of: sf::Image::Load()

You can copy it to a temporary image, an assign it back later.
Title: Unload Image
Post by: e_barroga on July 11, 2010, 03:36:19 am
Why isn't there an Unload() method when there is a Load() method?

If it is cleared in the destructor, why not load the image in the constructor?
Title: Unload Image
Post by: Hiura on July 11, 2010, 10:33:16 am
Quote from: "e_barroga"
Why isn't there an Unload() method when there is a Load() method?
You'll have an image in an invalid state. That's not a good idea.

If you really want to remove from memory an unused image, use a resource manager : it will be easy to add/remove anything.
Title: Unload Image
Post by: Laurent on July 11, 2010, 11:44:35 am
You can do this
Code: [Select]
image = sf::Image();

But the question is: why do you want to unload the image but keep the corresponding instance?

Quote
If it is cleared in the destructor, why not load the image in the constructor?

SFML doesn't use exceptions, therefore it never does anything that may fail in a constructor.
Moreover, images can't always be loaded at the moment they are constructed, so a default constructor + a Load function is always necessary.
Title: Unload Image
Post by: e_barroga on July 12, 2010, 01:31:13 am
Based on a flag, I wanted to preload an image or load it the first time it is used and unload it when it is not used (after the rendering step, the image was not drawn).
Title: Unload Image
Post by: Laurent on July 12, 2010, 08:19:01 am
Quote
and unload it when it is not used

If it's not used, what difference does it make if it contains the previous pixels or nothing?
Title: Unload Image
Post by: Mindiell on July 12, 2010, 10:17:04 am
I used something like
Code: [Select]
image.Load(""); without checking the error (since there is an error). But it was just for a small sample one year ago. The state of the image was not important, all I wanted was to have a white rectangle onto the sprite.

But I don't know how the SFML handle this.