SFML C++ Quick Start

SFML is a handy little library I’ve used mainly for graphics. I made an OpenGL and Vulkan graphics library myself cause what’s the fun in using some existing library? Suck the fun out of everything. It was interesting to see that SFML had a similar architecture to mine which made it kind of convenient to pick up.

There’s plenty of resources here: https://www.sfml-dev.org/learn.php

In any case, here’s the crash course project I would keep as a quick reference:

main.cpp:

C++
#include <SFML/Graphics.hpp>

int main()
{
    // NOTE: The key here is this is a RenderWindow as opposed to a Window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My Window");

    sf::CircleShape circle(10.0f, 30);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // A RenderWindow has these functions to draw, but a Window does not
        window.clear(sf::Color::Black);
        window.draw(circle);
        window.display();
    }

    return 0;
}

CMakeLists.txt:

CMake
cmake_minimum_required(VERSION 3.25)
project(sfml_example)

set(CMAKE_CXX_STANDARD 20)

find_package(SFML COMPONENTS graphics)

add_executable(sfml_example main.cpp)

target_link_libraries(sfml_example
        PUBLIC
        sfml-graphics)

A few items of interest:

  • RenderWindow is not the same as Window — you can only draw in a RenderWindow
  • It has a number of components, but graphics is a simple place to start

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *