DEV Community

dwarfŧ
dwarfŧ

Posted on • Updated on

creating a window in SFML

step 1
first of all you need to install SFML. I am using ubuntu and i found the linux installation guide here.

step 2
next, you need to create a window you can do this by doing:

#include <SFML/Graphics.hpp>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Hello window!");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        // window.draw(...);

        // end the current frame
        window.display();
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

this will give us a black window named hello window!

step 3
to compile we cannot just do g++ main.cpp. We have to include some library's we can compile this by doing:

g++ main.cpp -o main -lsfml-graphics -lsfml-window -lsfml-system
Enter fullscreen mode Exit fullscreen mode

step 4
you can run it by doing: ./main

step 6
enjoy and have a good day

Top comments (0)