Looking a bit over internal sf::Color and sf::Texture related code, I noticed that there are quite some assignments and comparisons done byte by byte.
How about storing the value of sf::Color in a union, while having references providing backwards compatibility and easier access? Especially considering the fact sf::Color uses unsigned byte instead of float.
Essentially something like this:
public:
Uint32 &rgba;
Uint8 &r;
Uint8 &g;
Uint8 &b;
Uint8 &a;
private:
union _data
{
Uint32 rgba;
struct _components {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
} components;
} data;
This could simplify quite some code (not sure how far this would be optimized by modern compilers).
Current code (color comparison):
bool operator ==(const Color& left, const Color& right)
{
return (left.r == right.r) &&
(left.g == right.g) &&
(left.b == right.b) &&
(left.a == right.a);
}
New code:
bool operator ==(const Color& left, const Color& right)
{
return left.rgba == right.rgba;
}
Current code (creating an image of a given color):
Uint8* ptr = &myPixels[0];
Uint8* end = ptr + myPixels.size();
while (ptr < end)
{
*ptr++ = color.r;
*ptr++ = color.g;
*ptr++ = color.b;
*ptr++ = color.a;
}
New code:
Uint32* ptr = (Uint32*)&myPixels[0];
Uint32* end = ptr + myPixels.size();
while (ptr < end)
*ptr++ = color.rgba;
There's still the issue of byte order, but this could be handled changing the order of the struct members (to ensure rgba has the right content).