0

so i was starting with fna and found this little piece of code. and i can't seem to wonder how the PremultiplyTexture function is working? I mean the parameter is passed by value right? So how is the Texture2D in the Load function getting updated?

public static class TextureLoader
{
    const bool usingPipeline = false;

    public static Texture2D Load(string filePath, ContentManager content)
    {
        Texture2D image = content.Load<Texture2D>(filePath);

        if (usingPipeline == false)
            PremultiplyTexture(image);

        return image;
    }

    private static void PremultiplyTexture(Texture2D texture)
    {
        Color[] buffer = new Color[texture.Width * texture.Height];
        texture.GetData(buffer);
        for (int i = 0; i < buffer.Length; i++)
        {
            buffer[i] = Color.FromNonPremultiplied(buffer[i].R, buffer[i].G, buffer[i].B, buffer[i].A);
        }
        texture.SetData(buffer);
    }
}
  • I think I need to clarify a bit more, because the terminology is easily misunderstood. In C#, a `class` is a reference type, so passing `image` into the function is not passing a copy of image, but a reference to the same instance. In contrast, a `struct` is a value type, so passing a struct as a parameter would cause a copy of the original to be made. I would recommend reading some articles on the differences between classes and structs, there are some great articles out there that describe it much more clearly than I can – Andrew Williamson Jul 23 '20 at 20:50
  • @AndrewWilliamson Your own link explains why this is not passed by reference. Saying it's passed by reference is not true, for the reason your own link explains. – Servy Jul 23 '20 at 21:08
  • @Servy that's why I added the second comment to clarify. It is a _reference type_, which is passed by value. You're welcome to give a better explanation – Andrew Williamson Jul 23 '20 at 21:33
  • @AndrewWilliamson That's like saying that a dog is a fungus, and oh by the way, it's also a mammal. Saying something that's completely wrong, and then following it up by saying something that's true, doesn't change the fact that you said something that's the opposite of the truth. The parameter is objectively not passed by reference. That's simply a fact. – Servy Jul 24 '20 at 00:24
  • @Servy you're right, my first explanation was technically wrong, and I could not edit it so I posted a second comment hoping to clarify. I don't think it's as bad as you make it out to be - passing a reference type by value is conceptually *similar* to passing a reference. It explains why the OP can modify `texture` in the `PremultiplyTexture` method and see the result in `Load`. Regardless, I have deleted the original comment since you believe it to be contradictory – Andrew Williamson Jul 24 '20 at 01:09
  • @AndrewWilliamson That it has a few similarities, but numerous notable differences, is precisely why it's problematic to use the terms improperly. It results in people believing it, because it sounds right at a glance, but then when they get into situations where the two behave differently they suddenly don't understand what's going on, and are put in the position of needing to unlearn incorrect information, which is much harder than just learning the correct information from nothing. – Servy Jul 24 '20 at 02:28

0 Answers0