sfml Window Basics Creating an OpenGL window


Example

Windows in SFML are represented by one of two classes:

  • sf::Window is a generic window provided by the operating system including an OpenGL render context.
  • sf::RenderWindow is a specialized version of sf::Window that also acts as a sf::RenderTarget, allowing SFML's primitives to be rendered to it.

The basic usage is the same in both cases.

#include <SFML/Window.hpp>

int main(int argc, char *argv) {
    // Create and initialize a window object
    sf::Window window(sf::VideoMode(640, 480), "My SFML Window");

    // Repeat this as long as the window is open
    while (window.isOpen()) {
        // Handle window events ("event loop")
        sf::Event event;
        while (window.pollEvent(event)) {
            switch(event.type) {
                case sf::Event::Closed: // User tries to close the window
                    window.close(); // Actually close the window
                    break;
            }
        }

        // Render logic would be placed here
        
        // Swap buffers and update the window
        window.display();
    }
    return 0;
}