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

Author Topic: [Solved] sf::Shader::setParameter and const correctness issues  (Read 2210 times)

0 Members and 1 Guest are viewing this topic.

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
[Solved] sf::Shader::setParameter and const correctness issues
« on: December 31, 2015, 01:09:39 pm »
In the following block of code:

    void gui::Button::draw(sf::RenderTarget& target, sf::RenderStates states)const
    {
       states.shader = &stateShader;
       if (!arePredicatesFulfilled()) states.shader->setParameter("buttonState", Unavailable);
            // draw statements....
    }

Where stateShader is a member of class Button, the compiler won't let me change the parameter of the shader, giving me an error reading: "error C2663: 'sf::Shader::setParameter' : 10 overloads have no legal conversion for 'this' pointer."
As far as I am aware, this is happening because of the fact that draw() is a const function and, as such, 'this' is a const pointer in it. Why does that, however, stop me from changing things in states, a non-const object?
I also tried instantiating a shader in the draw function instead of using the (const) one in class Button, still didn't allow me to change the parameter.
« Last Edit: December 31, 2015, 03:21:40 pm by hypnotix »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: sf::Shader::setParameter and const correctness issues
« Reply #1 on: December 31, 2015, 02:20:38 pm »
That error has nothing to do with the fact that your function is const. sf::RenderStates stores its Texture and Shader as const pointers. This is because you should never have to modify them through the sf::RenderStates object itself, this should be done before packing them into it. If you look at sf::RenderTarget::draw() you will see that it takes the sf::RenderStates as a const reference, you should as well. If you need to modify your shader in some way, do so directly through the shader object, not after you packed it into your states object. At that point it has already become const. Also, I don't think you will have an easy time modifying your shader object directly either, as long as your function is declared const. If it is supposed to be logically const, then you will have to make your shader object mutable, otherwise you will have to remove the const declaration since you don't intend to keep that promise.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: sf::Shader::setParameter and const correctness issues
« Reply #2 on: December 31, 2015, 02:41:39 pm »
I have made it const because I got the impression that, after inheriting from sf::Drawable, for things to work as intended I need to override the draw() function without changing it, and in sf::Drawable it is declared as follows: virtual void draw(RenderTarget& target, RenderStates states) const = 0;

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: sf::Shader::setParameter and const correctness issues
« Reply #3 on: December 31, 2015, 02:57:57 pm »
Hmm... I was under the impression that that would have also taken a const reference... This might have been left this way for historical reasons, no idea.

Taking this into consideration, if you really have to call setParameter() inside your draw() function, you will have to make the shader object (which I assume is stored inside gui::Button) mutable. That way you can call setParameter() directly on the object itself and then pack it into the sf::RenderStates.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader::setParameter and const correctness issues
« Reply #4 on: December 31, 2015, 03:16:10 pm »
Quote
Hmm... I was under the impression that that would have also taken a const reference... This might have been left this way for historical reasons, no idea.
This syntax makes it easier to do what you almost always do with the render-states.

void MyDrawable::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform();
    target.draw(child, states);
}

// vs...

void MyDrawable::draw(sf::RenderTarget& target, const sf::RenderStates& states) const
{
    sf::RenderStates newStates(states); // you have to find another name, where 'states' is so intuitive
    newStates.transform *= getTransform();
    target.draw(child, newStates); // beware, don't use 'states' :)
}

Quote
Taking this into consideration, if you really have to call setParameter() inside your draw() function, you will have to make the shader object (which I assume is stored inside gui::Button) mutable.
The draw function is const on purpose. Updating parameters should really be done in an update function.
« Last Edit: December 31, 2015, 03:17:48 pm by Laurent »
Laurent Gomila - SFML developer

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: sf::Shader::setParameter and const correctness issues
« Reply #5 on: December 31, 2015, 03:17:37 pm »
Yeah, but I can't make the sf::Shader instance in gui::Button mutable, because draw is declared const; nothing in gui::Button is mutable from draw(). Can I cast it or something so I can change its parameter?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader::setParameter and const correctness issues
« Reply #6 on: December 31, 2015, 03:18:40 pm »
Quote
Yeah, but I can't make the sf::Shader instance in gui::Button mutable, because draw is declared const; nothing in gui::Button is mutable from draw().
The point of the mutable keyword is to allow modification even in const member functions ;)
Laurent Gomila - SFML developer

hypnotix

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: sf::Shader::setParameter and const correctness issues
« Reply #7 on: December 31, 2015, 03:19:35 pm »
Oh. Well, you learn something every day I guess :p