I'm using VS2013 with SFML Simple and Fast Installer for VS2013 (
http://en.sfml-dev.org/forums/index.php?topic=14127.0 ), and I have tried some types of codes for fading, any of which ended up with freezing:
(1)
bool MsgBox::showTitleLogo(sf::RenderWindow &window)
{
window.clear(sf::Color::White);
sf::Sprite spr;
sf::Texture logo;
if (!logo.loadFromFile("splash/logo.png"))
return false;
spr.setTexture(logo);
spr.setPosition(Config::center(spr));
sf::Clock clk;
while (clk.getElapsedTime() < sf::milliseconds(1000));
bool fade = true;
sf::Color c = spr.getColor();
while (fade)
{
if (c.a = 0)
{
fade = false;
break;
}
window.draw(spr);
if (c.a >= 5)
c.a -= 5;
else
c.a = 0;
spr.setColor(c);
}
return true;
}
(2)
bool MsgBox::fade(sf::RenderWindow &window, bool isIn, sf::Time t, sf::Color c)
{
sf::Color _c = c;
if (isIn)
_c.a = 255;
else
_c.a = 0;
sf::Time grade = sf::seconds(t.asSeconds() / 256.0f);
sf::Clock clk;
bool loop = true;
sf::RectangleShape screen;
screen.setFillColor(_c);
screen.setPosition(0, 0);
screen.setSize(sf::Vector2f(Config::scrWidth(), Config::scrHeight()));
while (loop)
{
window.draw(screen);
clk.restart();
if (clk.getElapsedTime() == grade)
{
if (isIn)
--_c.a;
else
++_c.a;
}
if (isIn)
if (_c.a == 0)
loop = false;
else
if (_c.a == 255)
loop = false;
}
return true;
}
(3)
int main()
{
sf::Sprite sprt;
sf::Texture img;
img.loadFromFile("image.png");
sprt.setTexture(img);
sf::RenderWindow App(sf::VideoMode(1600, 800, 32), "SFML Alpha Demo");
App.setFramerateLimit(60);
App.setVerticalSyncEnabled(false);
int a = 255;
while (App.isOpen())
{
if (a<0){ a = 255; }
sprt.setColor(sf::Color(255, 255, 255, a));
a--;
App.clear(sf::Color::Green);
App.draw(sprt);
App.display();
}
return EXIT_SUCCESS;
}
// This one came from http://en.sfml-dev.org/forums/index.php?topic=2693.msg17638#msg17638 , modified to 2.1 style by myself.
Was there any problem on these codes?
Edit: I intended to make a splash screen, which would be shown up at the startup.