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

Author Topic: Texture equality operator  (Read 2606 times)

0 Members and 1 Guest are viewing this topic.

coolnonis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Texture equality operator
« on: February 10, 2014, 02:54:14 pm »
Hey guys, I was just wondering if there's a specific reason that an equality operator is not implemented for the sf::Texture class. I'm currently writing an OpenGL sprite batcher using sfml textures, but i'd like to implement automatic texture switching.

Is there a reason I shouldn't re-implement the texture class to support the comparison of 2 objects using the internal m_texture ID?
« Last Edit: February 10, 2014, 02:59:16 pm by coolnonis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Texture equality operator
« Reply #1 on: February 10, 2014, 04:19:10 pm »
Not fully sure why, but an equality operator for sf::Texture could have multiple meanings. For example whether it's the same OpenGL resource, or whether it's the same pixels, or ...
Thus having an equality operator would make things a bit ambiguous, which is what we like to prevent (hence the Simple in SFML). Next to that, it's really rather easy to keep track of of textures on your own (even though it might be harder for your layout).

Then again, I'm just some guy here, let the big people speak for themselves. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Texture equality operator
« Reply #2 on: February 10, 2014, 04:22:38 pm »
So... how would you define this operator to take into account that textures with the same GL name (ID) can exist in different contexts? Just wondering...  ::)
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

coolnonis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Texture equality operator
« Reply #3 on: February 10, 2014, 04:27:06 pm »
Quote
Not fully sure why, but an equality operator for sf::Texture could have multiple meanings. For example whether it's the same OpenGL resource, or whether it's the same pixels, or ...

I see. Thank you for the swift reply.

In my case, I want to check the equality between two textures in regards to their OpenGL texture ID.

Maybe this will do:

namespace sf
{

bool Texture::operator==(const Texture& other)
{
return (this->m_texture == other.m_texture);
}

}
 

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Texture equality operator
« Reply #4 on: February 10, 2014, 04:29:11 pm »
About the same issue: I wish SFML would let us get the m_cacheID(not the GL ID, the internal SFML 64 bit ID), now it's private and RenderTexture/Window are friends. Why not let user get the cache ID too? :-\
Back to C++ gamedev with SFML in May 2023

coolnonis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Texture equality operator
« Reply #5 on: February 10, 2014, 04:38:03 pm »
Quote
About the same issue: I wish SFML would let us get the m_cacheID(not the GL ID, the internal SFML 64 bit ID), now it's private and RenderTexture/Window are friends. Why not let user get the cache ID too?

Sort of related, do you think I should use the SFML cache ID rather than the GL ID?

Looking at the source code, It looks like the use of the assignment operator causes the object to generate a new cacheID for itself.

Texture& Texture::operator =(const Texture& right)
{
    Texture temp(right);

    std::swap(m_size, temp.m_size);
    std::swap(m_actualSize, temp.m_actualSize);
    std::swap(m_texture, temp.m_texture);
    std::swap(m_isSmooth, temp.m_isSmooth);
    std::swap(m_isRepeated, temp.m_isRepeated);
    std::swap(m_pixelsFlipped, temp.m_pixelsFlipped);
    // New cache ID
    m_cacheId = getUniqueId();

    return *this;
}

I assume that would make it difficult to compare texture equality.

Also, i'm not quite sure if you were being sarcastic there...  :P
« Last Edit: February 10, 2014, 05:04:42 pm by coolnonis »

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Texture equality operator
« Reply #6 on: February 10, 2014, 05:16:15 pm »
No, I really dislike how airtight SFML is about some things and how it might get in the way, SFML uses that ID for it's own cache, on each copy, load and construction it gets changed so it's always unique, there is really no reason why SFML wouldn't give it to us but it just doesn't...
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Texture equality operator
« Reply #7 on: February 10, 2014, 09:19:01 pm »
This ID is purely an implementation detail. I have no idea what it would mean if it appeared in a public API... Moreover, since it was added to implement an internal optimization, I may perfectly drop it one day, if the optimization strategy changes, or if I find bugs related to it.

Regarding Texture::operator=, yes it could have different meanings, so it's not trivial and should be left to the user. Don't forget that you can get (almost) everything that you want about (almost) any OpenGL object with glGetXxx calls. Including the ID of the currently bound texture. If you're afraid to call glGet at runtime, call it once after the creation of the texture and store it for later use.
Laurent Gomila - SFML developer

coolnonis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Texture equality operator
« Reply #8 on: February 11, 2014, 03:52:26 pm »
Thanks for the advice everyone.

I've decided to re-implement my sprite batcher to use shared_ptr address comparisons to check the equality of textures. It works well with my current implementation.

I feel that this is a better solution than modifying the SFML source, which would cause issues if the internal structure of the Texture class was changed.

 

anything