Is there a better / more efficient way to cycle through all colors than this?!
// setup
sf::Color color;
//...
// update loop ( fTime is the elapsed frame time)
fRed = color.r;
fGreen = color.g;
fBlue = color.b;
if (fRed == 255 && fGreen < 255 && fBlue == 0)
color = sf::Color(255, fGreen + 25 * fTime, 0);
else if (fRed > 0 && fGreen == 255 && fBlue == 0)
color = sf::Color(fRed - 25 * fTime, 255, 0);
else if (fRed == 0 && fGreen == 255 && fBlue < 255)
color = sf::Color(0, 255, fBlue + 25 * fTime);
else if (fRed == 0 && fGreen > 0 && fBlue == 255)
color = sf::Color(0, fGreen - 25 * fTime, 255);
else if (fRed < 255 && fGreen == 0 && fBlue == 255)
color = sf::Color(fRed + 25 * fTime, 0, 255);
else if (fRed == 255 && fGreen == 0 && fBlue > 0)
color = sf::Color(255, 0, fBlue - 25 * fTime);
// now use the color somehow
// combine the color components into a single integer
sf::Uint32 c = (color.r << 16) | (color.g << 8) | color.b;
// increment it
c += 25 * fTime;
// loop when max (white) is reached
if (c > 0x00ffffff)
c = 0;
// decompose into a new color
color.r = static_cast<sf::Uint8>((c & 0xff0000) >> 16);
color.g = static_cast<sf::Uint8>((c & 0xff00) >> 8);
color.b = static_cast<sf::Uint8>(c & 0xff);
Ok i gave your method a try, but all I got was a black screen. I wrote a minimal code (a coloured Circle follows the mouse), but its still all black. Do you know why?
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
sf::Clock FrameClock;
float fTime;
sf::CircleShape m_Circle(5);
m_Circle.setFillColor(sf::Color::Red);
sf::Color color;
sf::Uint32 c;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// Close Event
if (event.type == sf::Event::Closed)
window.close();
// Escape key pressed
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
}
fTime = FrameClock.restart().asSeconds();
// combine the color components into a single integer
c = (color.r << 16) | (color.g << 8) | color.b;
// increment it
c += 25 * fTime;
// loop when max (white) is reached
if (c > 0x00ffffff)
c = 0;
// decompose into a new color
color.r = static_cast<sf::Uint8>((c & 0xff0000) >> 16);
color.g = static_cast<sf::Uint8>((c & 0xff00) >> 8);
color.b = static_cast<sf::Uint8>(c & 0xff);
m_Circle.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
m_Circle.setFillColor(color);
window.clear();
window.draw(m_Circle);
window.display();
}
return 0;
}