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

Author Topic: FiveBlocksFall - My first SFML videogame  (Read 55 times)

0 Members and 1 Guest are viewing this topic.

michelemura

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • LinkedIn profile
    • Email
FiveBlocksFall - My first SFML videogame
« on: March 02, 2026, 10:46:04 pm »
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?

Hapax

  • Hero Member
  • *****
  • Posts: 3462
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: FiveBlocksFall - My first SFML videogame
« Reply #1 on: Today at 12:15:11 am »
This code doesn't seem too bad. If it works, it shouldn't matter anyway! ;D

With that said, you could probably do both event loops as just one and just change what is drawn by the current time (whether it's in the splash section or the fade out section).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*