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

Author Topic: Team coloring in SFML  (Read 2830 times)

0 Members and 1 Guest are viewing this topic.

stefanhendriks

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Team coloring in SFML
« on: September 26, 2013, 08:50:24 am »
Hello,

This is my first post, regarding SFML so please bear with me :)

At this moment I am working on my project http://dune2themaker.fundynamic.com/ (Dune II - The Maker) using SDL 1.2, and I am strongly considering using SFML instead while we haven't gone too far yet. The only thing I can't seem to figure out how to do with SFML is to do team coloring.

We manipulate the palette of surfaces to draw the correct team. But in SFML there does not seem to be such a thing as a palette. So the question is: How do you apply team coloring then? Is there any other way that is adviced?

Regards,

Stefan

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: Team coloring in SFML
« Reply #1 on: September 26, 2013, 09:10:13 am »
sf::Color can be constructed from rgb values, therefore it's possible to manipulate the colors via arithmetic.

Alternatively, a simple function like this would do:
sf::Color getColor(const Team& team){
    if (team==Team1) return sf::Color::Red;
    if (team==Team2) return sf::Color::Blue;
    etc
}
 

stefanhendriks

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Team coloring in SFML
« Reply #2 on: September 26, 2013, 09:13:38 am »
what color are you changing then when drawing? The default color of a unit is red. How will returning an sf::Color::Blue make the unit have a blue shade?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Team coloring in SFML
« Reply #3 on: September 26, 2013, 09:18:52 am »
Have you looked at Sprite::setColor?

Quote
void sf::Sprite::setColor   (   const Color &    color   )   
Set the global color of the sprite.

This color is modulated (multiplied) with the sprite's texture. It can be used to colorize the sprite, or change its global opacity. By default, the sprite's color is opaque white.

Polyfructol

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Team coloring in SFML
« Reply #4 on: September 26, 2013, 12:33:06 pm »
Sprite::setColor will not achieve the same effect as a palette would. But fortunately, SFML supports shaders, and the palette "effect" could be achieved with shader.

On the OpenGL website : http://www.opengl.org/wiki/Common_Mistakes#Paletted_textures

//Fragment shader
#version 110
uniform sampler2D ColorTable;     //256 x 1 pixels
uniform sampler2D MyIndexTexture;
varying vec2 TexCoord0;
 
void main()
{
  //What color do we want to index?
  vec4 myindex = texture2D(MyIndexTexture, TexCoord0);
  //Do a dependency texture read
  vec4 texel = texture2D(ColorTable, myindex.xy);
  gl_FragColor = texel;   //Output the color
}

So you need to create an indexed texture and a palette texture.

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: Team coloring in SFML
« Reply #5 on: September 26, 2013, 07:00:10 pm »
Though I 'm not certain what exactly is it that SDL palette does, I could see manipulating the pixels of a sf::Image instead:

sf::Image trikeImage;
trikeImage.loadFromFile("graphics/Unit_Trike.bmp");
sf::Vector2u trikeSize = trikeImage.getSize();

sf::Image trikeGreen;
trikeGreen.create(trikeSize.x, trikeSize.y);
sf::Image trikeBlue;
trikeBlue.create(trikeSize.x, trikeSize.y);

for (int i=0; i<trikeSize.x; ++i){
    for (int j=0; j<trikeSize.y; ++j){
        sf::Color pixelColor = trikeImage.getPixel(i,j);
        sf::Color bluePixel(pixelColor);
        sf::Color greenPixel(pixelColor);
        if ((pixelColor == sf::Color(125,0,0))
            ||(pixelColor == sf::Color(214,0,0))
            ||(pixelColor == sf::Color(60,0,0))
            ||(pixelColor == sf::Color(89,0,0))
            ||(pixelColor == sf::Color(153,0,0))
            ||(pixelColor == sf::Color(182,0,0))
            )
        {
            greenPixel = sf::Color(0,pixelColor.g+pixelColor.r,pixelColor.b, pixelColor.a);
            bluePixel = sf::Color(0,pixelColor.g,pixelColor.b+pixelColor.r, pixelColor.a);
        }
        trikeGreen.setPixel(i,j,greenPixel);
        trikeBlue.setPixel(i,j,bluePixel);
    }
}
 


stefanhendriks

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Team coloring in SFML
« Reply #6 on: September 26, 2013, 10:11:36 pm »
thats exactly what i meant, nice!