Search code examples
c++cmakesfmlclionmacos-catalina

Why Does this SFML Code Act like an App, But Does Not Create a Window?


Stack Overflow. I come to you in a time of great need.

I am starting to learn C++, after learning Python, and I want to learn C++ in an entreating way; that is, by making games with SFML. I am following a book which you can find here. I use MacOs Catalina, and Clion with Cmake. The problem is that, when using a basic example, listed below, SFML does not create a window. (Sorry for my use of using namespace)

My Code:

// Include important C++ libraries here
#include <SFML/Graphics.hpp>

// Make code easier to type with "using namespace"
using namespace sf;

int main()
{

    // Create a video mode object
    VideoMode vm(1920, 1080);

    // Create and open a window for the game
    // RenderWindow window(vm, "Timber!!!", Style::Fullscreen);

    // Low res code
    RenderWindow window(vm, "Timber!!!");

    // Create a texture to hold a graphic on the GPU
    Texture textureBackground;

    // Load a graphic into the texture
    textureBackground.loadFromFile("graphics/background.png");

    // Create a sprite
    Sprite spriteBackground;

    // Attach the texture to the sprite
    spriteBackground.setTexture(textureBackground);

    // Set the spriteBackground to cover the screen
    spriteBackground.setPosition(0, 0);


    while (window.isOpen())
    {

        /*
        ****************************************
        Handle the players input
        ****************************************
        */

        if (Keyboard::isKeyPressed(Keyboard::Escape))
        {
            window.close();
        }

        /*
        ****************************************
        Update the scene
        ****************************************
        */


        /*
        ****************************************
        Draw the scene
        ****************************************
        */

        // Clear everything from the last frame
        window.clear();

        // Draw our game scene here
        window.draw(spriteBackground);

        // Show everything we just drew
        window.display();


    }

    return 0;
}

An image of what happens when I build and execute is below The Image

I tried to reinstall SFML, make the window smaller (the book suggests that I create a smaller, 960-pixel-wide VideoMode object and then window.draw() the original 1920/1080 picture*, but this did not work), re-build the project, execute it from XCode, and copy the SFML.dll files into my main project directory (and also I checked for my project directory).

Is this normal? Do you know how I can fix it? Notice how it takes up my screen as if it were the app which I am currently using. Thank you for your time.

  • As a footnote, I ran some other SFML examples to check the correctness of my SFML installation. The ran successfully.

Solution

  • I think it's because you are missing the event loop. Events have to be polled for window to work properly.

    sf::Event event;
    
    // Then in your main loop...
    while (window.pollEvent(event))
    {
      // Handle events.
    }