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

Author Topic: Color changing 2 times  (Read 2357 times)

0 Members and 1 Guest are viewing this topic.

kol

  • Newbie
  • *
  • Posts: 31
    • View Profile
Color changing 2 times
« 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);



Thanks :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Color changing 2 times
« Reply #1 on: November 14, 2013, 12:12:36 pm »
Nop. I think the easiest solution would be to use a shader.
Laurent Gomila - SFML developer

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Color changing 2 times
« Reply #2 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?

kol

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Color changing 2 times
« Reply #3 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 = /

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Color changing 2 times
« Reply #4 on: November 14, 2013, 01:18:10 pm »
Take a look at the shader tutorial. 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 (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.
« Last Edit: November 14, 2013, 01:38:30 pm by G. »

kol

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Color changing 2 times
« Reply #5 on: November 14, 2013, 02:17:27 pm »
Thanks, now I can make the outline :)

But, Is to make the outline, using only shader?

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Color changing 2 times
« Reply #6 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.
« Last Edit: November 14, 2013, 03:59:51 pm by Ghosa »

kol

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Color changing 2 times
« Reply #7 on: November 14, 2013, 06:08:08 pm »
Thanks very much.

 

anything