Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Deleting image and using one sprite for displaying all images in the program  (Read 4582 times)

0 Members and 2 Guests are viewing this topic.

UltrasonicAfroPower

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hi. I started using SFML (version is 1.6) and found that there is no Release-like function for SFML resources (like in SDL). I know, that resources in SFML are freed as soon as they go out of scope, but i need to be able to load and delete images when and where i want. So i decided to make this class
class texture
{
private:
        sf::Image image;
public:
        texture(char* destination)
        {
                  image.LoadFromFile(destination);
        };
        ~texture()
        {
                  //do nothing
        }
        sf::Image* get_pImage()
        {
                  return ℑ
        }
};
 
to be able to locate and free memory using new and delete on texture objects (yeah, i know, this is awful).
The sprite correctly draws on the screen, but when i try to draw it after deleting texture it shows only a white rectangle of image size. So the question is: is image actually deleting when i use delete, or it just screws up connection with sprite?
And the second question: can i use one sprite to display all images on the screen (like when using directx) or it will cause low performance?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Really? Why in the world would you need to handle your existences of the files on your own? This is really bad to do since if can get you in many memory leaks. Your thinking is kinda just wrong at its roots and you should really look into C++ and what the current directions for memory management is (e.g. RAII). Judging from your code and mentioning of SDL I guess you're still a bit stuck in the C world. ;)

Next I'd suggest you to use SFML 2.0, since it has a way better API and less bugs.

So now to your (self created) problem. The thing that objects that go out of scope are lost isn't special to SFML but it's a standard C++ thing. It's a good practice to do so.
You can already use new and delete directly on a sf::Image class, so your wrapper class is kind of useless.
sf::Image* img = new sf::Image;
img->LoadFromFile("hello.png");
delete img;
// Instead of
texture* img = new texture("hello.png");
delete img;
But I really advice you not to use such code!

What would you except happens if you delete a texture and then draw the sprite? I'm glad it just shows a white rectangle instead of crashing complelty since the reference to the texture just leads into nothing. If you pass a texture/image to the sprite it sets its size and if the texture isn't valid anymore it will keep the size but instead of drawing the image it will draw a white rectangle.

What do you mean by one sprite for all images? You don't draw images/textures directly you always need to reference a image to one sprite, so no this is not possible.
In SFML 2 there's a VertexArray with which you can draw multiple textures with one draw call.

My advice to you: Get a modern book about C++ and read some stuff about memory managment on the internet, then eventually look into some modern C++11 memory managment. ;)
Desperetly haning onto the old ways of C isn't good at all.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

UltrasonicAfroPower

  • Newbie
  • *
  • Posts: 2
    • View Profile
Wow, this code is really useless. Well, i guess using new and delete gives me illusion of more control on what program actually does  ???. Anyways, thanks for help, looks like it's time to move on.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
In the year 2012, it's almost always a mistake to use new and delete directly in end-user code. With the availability of smart pointers, containers and especially the RAII idiom, manual memory management is rarely required. Often, you can directly use automatic objects, like
sf::Image img;
img.LoadFromFile("hello.png");
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: