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

Author Topic: Question about Textures when passing resource holders  (Read 3357 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Question about Textures when passing resource holders
« on: January 01, 2018, 11:01:54 pm »
So I stopped using that singleton resource manager from SFML essentials and made a simple resource holder class:

std::map<std::string, sf::Texture> Textures;

sf::Texture& Resources::get_texture(std::string const& filename)
{
        // See if the texture is already loaded
        auto pairFound = Textures.find(filename);

        // If yes, return the texture
        if (pairFound != Textures.end()) { return pairFound->second; }

        // Else, load the texture and return it
        else
        {
                // Create an element in the texture map
                auto& texture = Textures[filename];
                texture.loadFromFile(filename);
                return texture;
        }
}
 

It has the same design as the one from SFML essentials, but it's meant to work with raii.

In this example I pass a resource holder to a class through the constructor:

class A
{
        Resources Resource;

        B objB{ Resource };

        A::A() { Resource.get_texture("x.png"); }
}

class B : public sf::Drawable, public sf::Transformable
{
        B::B(Resources& Resource) { tex = Resource.get_texture("x.png"); }
       
        sf::Texture tex;     //  <- MY QUESTION IS ABOUT THIS
        sf::VertexArray va;    
}
 

My question is: Should class B have an sf::Texture or an sf::Texture* ?

I took a look at sf::Texture Class Reference to see what the = overload does, but I couldn't understand if it performs some sort of copy or what.

I remember reading somewhere here not to pass SFML resources around using pointers. Something to do with const references? This is a bit over my head c++ wise.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about Textures when passing resource holders
« Reply #1 on: January 02, 2018, 06:36:09 am »
If you acquire the texture in the constructor, then use a reference; if it is acquired elsewhere or needs to be changed, use a pointer. Otherwise you'll duplicate the texture and ruin the benefit of using a manager.

By the way, you should avoid (or be extremelly careful with) members (objB) constructed from other members (Resource) of the same class; if you ever switch their declaration order in the header, then your code will become undefined behaviour.
Laurent Gomila - SFML developer

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Question about Textures when passing resource holders
« Reply #2 on: January 02, 2018, 02:13:07 pm »
If you acquire the texture in the constructor, then use a reference; if it is acquired elsewhere or needs to be changed, use a pointer. Otherwise you'll duplicate the texture and ruin the benefit of using a manager.

By the way, you should avoid (or be extremelly careful with) members (objB) constructed from other members (Resource) of the same class; if you ever switch their declaration order in the header, then your code will become undefined behaviour.

Just to be EXTREMELY clear: in class B, it should be
sf::Texture& tex
if I won't change it,

and
sf::Texture* tex
if I will?

sorry for being a noob who needs idiot-proof answers and thanks as always for your help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Question about Textures when passing resource holders
« Reply #3 on: January 02, 2018, 02:26:07 pm »
You can also change a reference, you just have to always reference a valid instance, from the construction of the object with the texture reference (i.e. needs to be set in the initialization list) to the destruction of said object. Where as with a pointer you can also have it set to a nullptr if there's no texture available at some point.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about Textures when passing resource holders
« Reply #4 on: January 02, 2018, 02:42:06 pm »
Quote
You can also change a reference
To be clear, you can modify the referenced object, but you can't rebind the reference to a different object. That's what I meant in my previous message.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Question about Textures when passing resource holders
« Reply #5 on: January 02, 2018, 03:30:19 pm »
To be clear, you can modify the referenced object, but you can't rebind the reference to a different object. That's what I meant in my previous message.
Not sure what you mean. Of course you can rebind a reference to a different object. Or what did you mean?
« Last Edit: January 02, 2018, 03:32:33 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about Textures when passing resource holders
« Reply #6 on: January 02, 2018, 05:12:31 pm »
No you can't. What you do in your example is assigning the value of B to A through the foo reference. You're not making of foo a reference to B.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Question about Textures when passing resource holders
« Reply #7 on: January 02, 2018, 06:34:05 pm »
You're right, I got confused. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/