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

Author Topic: SOLVED: Shape::SetColor doesn't work as expected  (Read 5659 times)

0 Members and 1 Guest are viewing this topic.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
SOLVED: Shape::SetColor doesn't work as expected
« on: April 18, 2010, 11:46:11 pm »
So I tried the below code and was in for a nasty surprise:

Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{
    // A rectangle
    sf::Shape rect = sf::Shape::Rectangle(100,100,200,200,sf::Color(255,255,255));

    sf::RenderWindow wnd(sf::VideoMode(640,480),"Test");

    while(wnd.IsOpened())
    {
        sf::Event evt;
        while(wnd.GetEvent(evt))
        {
            if(evt.Type == sf::Event::Closed)
                wnd.Close();
        }

        wnd.Clear( sf::Color(96,96,96) );

        rect.SetColor( sf::Color(180,0,0) );  // this should make the rectangle red, right?

        wnd.Draw( rect );  // but here, it's drawing as black!

        wnd.Display();
    }
    return 0;
}


After digging through the documentation and tearing apart my code, I finally decided to check SFML source.

It looks like Drawable::SetColor has no effect (or some kind of ill effect?) on the actual color of Shapes.

In order to get the actual desired effect, I would need to step through all the points in my Shape and change each of those colors.  EDIT:  Actually... can this even be done?  I don't see how to do it from the docs.  /Edit

Shouldn't SetColor have the expected behavior here?

I'm also curious as to why it's drawing as black on my machine... I mean if SetColor has no effect it should be drawing white since that was the constructed color.


EDIT2:  I should mention... this is SFML 1.6 on Ubuntu

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
SOLVED: Shape::SetColor doesn't work as expected
« Reply #1 on: April 19, 2010, 12:10:12 am »
It's drawing a red square for me.

I'm using SFML 2.x so I don't know if that's it.


Okay, I setup my project to use SFML 1.6 and ran it again and it worked. Maybe you are running an old build and need to rebuild?

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
SOLVED: Shape::SetColor doesn't work as expected
« Reply #2 on: April 19, 2010, 01:29:10 am »
On your example it is red, but only because you assigned the rect vertexes colour to white.

sf::Drawable color works more as a filter color. The default is white, meaning that the object colours are fully drawn as they are.

For instance, if you have a sprite with a nice image, and do
Code: [Select]
sprite.SetColor(sf::Color(255,0,0));
The sprite will only be drawn with it's red components, all greens and blues are discarded.
On your code try this:
Code: [Select]
sf::Shape rect = sf::Shape::Rectangle(100,100,200,200,sf::Color(0,255,255));
(...)
rect.SetColor( sf::Color(180,0,0) );

The result is black, because the vertexes colour (0,255,255) filtered by (180,0,0) results in (0,0,0).
Maybe the sf::Drawable::SetColor should have a different name, because on shapes it may be confusing.


I think the unique way to change the colour of your shape is to change all vertexes colors. :roll:
Pluma - Plug-in Management Framework

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
SOLVED: Shape::SetColor doesn't work as expected
« Reply #3 on: April 19, 2010, 02:04:12 am »
!!!   wtf?  I tried it now and it's working.  I guess I didn't actually test that code I posted.

What gsaurus said seems like it's right on the money.  I tried it again with black as an intial color and got the black box instead of the red box.  But with white as the initial color it's fine.


Okay that makes a lot of sense now.  Thanks everyone.

 

anything