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

Author Topic: Palette swapping  (Read 2839 times)

0 Members and 3 Guests are viewing this topic.

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Palette swapping
« on: March 30, 2012, 08:05:06 pm »
There doesn't seem to be such a thing in SFML...
A typical sprite trick i've tried to use before is designating several colours of a palette to specific areas of the sprite. Some for Flesh colour, Some for Hair, Eyes, Clothes etc. Then when you create the sprite, you take the template palette, and several derivative palettes, and swap colours around on sprite load, applying this new palette to the sprite, effectively recolouring several different parts of the same sprite, with different colours, some parts remaining unchanged.

Doing this in SDL is fairly easy, i've actually done it before, But I dislike SDL, since it uses software rendering only, the graphics bogging down the entire program, Hence my move over to SFML 1.6.

Problem is... what do I do to do this in SFML?


The ONLY thing that comes to mind is creating a seperate unique overlay sprite for each part, for each instance, but then that basicly means i'm drawing 5 sprites on top of each other for every sprite.


Does anyone else have any better sugestions?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Palette swapping
« Reply #1 on: March 30, 2012, 08:22:14 pm »
First, you should rather use SFML 2, it has much more features and bugfixes than version 1.6. The final release is expected soon, and you can already use the development version via GitHub.

And you shouldn't look for very complicated approaches unless you really need to. You are probably afraid of long render times if you have to draw 5 sprites instead of 1, but if you only have 10 characters on your screen, there won't be a significant performance impact. So I'd suggest to have a class that gathers the 5 sprites and positions/rotates/scales them correctly. Hereby, the classes sf::Transform, sf::Transformable and sf::Drawable can be of help. The color overlay can then be implemented with sf::Sprite::setColor().

Short example:
#include <array>

// Drawable provides draw() method
// Transformable provides getPosition(), rotate() etc.
class Character : public sf::Drawable, public sf::Transformable
{
    public:
        enum Part { Flesh, Hair, Shirt, Pants, Shoes };
       
        void setColor(Part p, sf::Color c)
        {
            mSprites[p].setColor(c);
        }

        virtual void draw(sf::RenderTarget& target, sf::RenderStates states)
        {
            // Combine transform of *this
            // (getTransform() is in the base class sf::Transformable)
            // with the argument by using multiplication operator
            states.transform *= getTransform();

            for (unsigned int i = 0; i < mSprites.size(); ++i)
                target.draw(mSprites[i], states);
        }

    private:
        std::array<sf::Sprite, 5> mSprites;
};

An alternative would be sf::RenderTexture and pre-rendered characters, but I'd use this only if the other approach doesn't suffice.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

bobingabout

  • Newbie
  • *
  • Posts: 47
    • View Profile
Re: Palette swapping
« Reply #2 on: March 30, 2012, 11:00:40 pm »
hmmmm, maybe i should look into SFML2, though i've only really just got the hang of SFML1.6