This piece of C++ code is the first which runs after the game has been loaded:
void _showSplashScreen() {
sf::RenderWindow windowSplash(sf::VideoMode(512, 512), "FiveBlocksFall", sf::Style::None);
// Load your splash texture
sf::Texture splashTexture;
splashTexture.loadFromFile("assets/pics/splash_screen.png");
sf::Sprite splashSprite(splashTexture);
splashSprite.setPosition(0.f, 0.f);
// Clock to measure the 3 seconds
sf::Clock clock;
// --- MAIN SPLASH LOOP (3 seconds) ---
while (windowSplash.isOpen())
{
sf::Event event;
while (windowSplash.pollEvent(event))
{
if (event.type == sf::Event::Closed)
windowSplash.close();
}
// Close splash after 3 seconds
if (clock.getElapsedTime().asSeconds() > 3.f)
break; // or switch to your main game state
windowSplash.clear(sf::Color::Black);
windowSplash.draw(splashSprite);
windowSplash.display();
}
// --- FADE OUT (1 second) ---
sf::RectangleShape fadeRect(sf::Vector2f(512.f, 512.f));
fadeRect.setFillColor(sf::Color(0, 0, 0, 0)); // fully transparent
sf::Clock fadeClock;
while (windowSplash.isOpen()) {
sf::Event event;
while (windowSplash.pollEvent(event)) {
if (event.type == sf::Event::Closed)
windowSplash.close();
}
float t = fadeClock.getElapsedTime().asSeconds();
if (t > 1.f) break; // Alpha goes from 0 → 255 over 1 second
sf::Uint8 alpha = static_cast<sf::Uint8>(255 * (t / 1.f));
fadeRect.setFillColor(sf::Color(0, 0, 0, alpha));
windowSplash.draw(splashSprite);
windowSplash.draw(fadeRect);
windowSplash.display();
}
}
The game has been published on itch.io but there's always space for improvement..
What do you think about this fade-out routine?