1

SFML only allows creating a window of a rectangular(boxy) shape and all other actions are done within it. I am making a Monopoly game and I basically want Monopoly Logo to flash on a screen when the user clicks the executable file and it han’t has to be inside any window(just logo with transparent background).After the logo, the rectangular window then appears. Is there any way to do this?

alseether
  • 1,889
  • 2
  • 24
  • 39

1 Answers1

3

SFML doesn't have any integrated feature to make window background transparent.

In order to achieve this, you should use some OS specific functions. But this won't work either. If you change window attributes to have a transparent window, SFML will render correctly, but you won't see anything because everything, background and foreground, will be transparent.

What's the solution then? The easiest way to do this is by not having any background, making your logo fit perfectly into your window. Then you only need to remove the title bar and borders, with sf::Style::None.

This is how I reach this:

int main()
{
    // First, I load the logo and create an sprite
    sf::Texture logo;

    if (!logo.loadFromFile("monopoly.png")){
        exit(1);
    }

    sf::Sprite sp;
    sp.setTexture(logo);
    sp.scale(0.2, 0.2); // My logo is quite big in fact (1st google result btw)

    int logoWidth = sp.getGlobalBounds().width;
    int logoHeight = sp.getGlobalBounds().height;

    // With the logo loaded, I know its size, so i create the window accordingly
    sf::RenderWindow window(sf::VideoMode(logoWidth, logoHeight), "SFML", sf::Style::None); // <- Important!! Style=None removes title

    // To close splash window by timeout, I just suppose you want something like this, don't you?
    sf::Clock timer;
    sf::Time time = timer.restart();

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            // event loop is always needed
        }
        // Window closed by time
        time += timer.restart();
        if (time >= sf::seconds(2.f)){
            window.close();
        }

        window.clear();
        window.draw(sp);
        window.display();
    }

    // Then, reuse the window and do things again
    window.create(sf::VideoMode(600, 400), "SFML");

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){

        }

        window.clear();
        window.draw(sp);
        window.display();
    }

    return 0;
}

Note that you can re-use your window again just by re-creating with create method.

Results:

enter image description here

and then the other window :

enter image description here

alseether
  • 1,889
  • 2
  • 24
  • 39
  • 1
    Looks very nice, except I'd still use a single loop and call create from inside the loop: "Create (or recreate) the window. If the window was already created, it closes it first". – smoothware Jan 20 '18 at 02:10