SFML community forums

Help => Graphics => Topic started by: kol on November 14, 2013, 11:58:35 am

Title: Color changing 2 times
Post by: kol on November 14, 2013, 11:58:35 am
Is there a way to change the color of the sprite twice, taking into account the previous?
Example:

sprite.setColor(255,255,255);
sprite.setColor(0,240,100);

(http://i.imgur.com/Gp3ULjt.png)

Thanks :)
Title: Re: Color changing 2 times
Post by: Laurent on November 14, 2013, 12:12:36 pm
Nop. I think the easiest solution would be to use a shader.
Title: Re: Color changing 2 times
Post by: Rhimlock on November 14, 2013, 12:20:42 pm
I don't know if it is usefull to you, but what if you change your original texture of the sprite to grey-scale an then set the color with sprite.setColor?
Title: Re: Color changing 2 times
Post by: kol on November 14, 2013, 12:38:27 pm
Thanks Laurent.

I can not do this, they are very sprites. I was going to make a border climbing sprite (-1.0) (+1.0) (0, -1) (0, +1), to make a border. Would do it in shader? Not find any tutorial about = /
Title: Re: Color changing 2 times
Post by: G. on November 14, 2013, 01:18:10 pm
Take a look at the shader tutorial (http://www.sfml-dev.org/tutorials/2.1/graphics-shader.php). The sprite coloring effect can easily be achieved with the following shader.
//main.cpp
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "ยค");

    sf::Shader shader;
    if (!shader.loadFromFile("fragment.frag", sf::Shader::Fragment)) {/*error*/}

    sf::Texture texture;
    texture.loadFromFile("sprite.png");
    sf::Sprite sprite;
    sprite.setTexture(texture);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            if (event.type == sf::Event::KeyReleased) {
                if (event.key.code == sf::Keyboard::Escape) {
                    window.close ();
                }
            }
        }

        shader.setParameter("texture", sf::Shader::CurrentTexture);
        shader.setParameter("color", sf::Color::Green);

        window.clear();
        window.draw(sprite, &shader);
        window.display();
    }

    return 0;
}
//fragment.frag
uniform sampler2D texture;
uniform vec4 color;

void main()
{
    vec4 pixel = vec4(color.rgb, texture2D(texture, gl_TexCoord[0].xy).a);

    gl_FragColor = gl_Color * pixel;
}
 

If you want to draw a border around your sprite (I'm not sure that's what your asking), there was a thread a few weeks ago about how to draw a text with an outline (http://en.sfml-dev.org/forums/index.php?topic=13158) (border).

Right part is what the above code looks like, left part is what it looks like if you draw 8 additional sprites moved with (0, 1) (1, 1) etc. offsets and a black color passed to the shader.
(http://i.imgur.com/0lJLhU3.png)
Title: Re: Color changing 2 times
Post by: kol on November 14, 2013, 02:17:27 pm
Thanks, now I can make the outline :)

But, Is to make the outline, using only shader?
Title: Re: Color changing 2 times
Post by: Rhimlock on November 14, 2013, 03:58:13 pm
That Shader posted by G. draw the Sprite in a single Color, defined by:
shader.setParameter("color", sf::Color::Green);

For the outline you draw the Sprite 8 times in Black with an offset in every direction, so it will be larger than the original Sprite.

After that in green at the original location.
Title: Re: Color changing 2 times
Post by: kol on November 14, 2013, 06:08:08 pm
Thanks very much.