Lately, I've been reading a lot about 2D lighting systems in a game, and have observed just how much lighting can add to a game's graphics appeal. However, as for implementing one of these in SFML, I am clueless. The lighting system I would like doesn't have to be too complex - I don't need things such as shadows. What I would like is something like this, which I made in Java using swing: An example that I made in Java using Swing.

How this works:

  • I draw a black rectangle over the whole image with an alpha that changes based on the in-game time of day - 0 during the day, 128 during the night.
  • On top of that rectangle, I draw a gradient on top of the player's x and y coordinates - from (255, 255, 255, 100) to (0, 0, 0, 0)

What would be the ideal way to impement this in SFML?

  • \$\begingroup\$ I have no idea about SFML, maybe it possess sophisticated light system or something but an agnostic approach could be using a black PNG with the transparent gradients for the light (alpha) as you commented you made with java swing. But the fact that a PNG is bitmap you don't need calculations so it will be quite performant. the PNG could be even bigger that you viewport and you could rotate or anything you need. Cheers. e.g. answers.unity.com/storage/attachments/32076-hallow.png \$\endgroup\$
    – salc2
    Commented Apr 11, 2018 at 9:21
  • \$\begingroup\$ Let There be Light is a nice library to use if you don't want to code everything yourself or want a overview on how it could be done. en.sfml-dev.org/forums/index.php?topic=16895.0 \$\endgroup\$
    – Cobrapitz
    Commented Apr 12, 2018 at 22:33

1 Answer 1


Without shadows

The most direct approach is to have a PNG image with the texture of a light. If you don't want to use an external file, you can even create it yourself inside the program:

  1. Create a new white sf::Image of dimensions radius*radius.
  2. Iterate through all its pixels, setting their alpha value as 1 minus the normalized distance to the center of the image.

I recommend you drawing it with additive blend mode sf::BlendAdd. Also, check this substractAlpha mode; you can use this if you have a sf::RenderTexture acting as a layer of darkness. Draw the light in that layer using this blend mode to see through.

sf::BlendMode substractAlpha(
        sf::BlendMode::Factor::Zero,              // color src
        sf::BlendMode::Factor::One,               // color dst
        sf::BlendMode::Equation::Add,             // color eq
        sf::BlendMode::Factor::Zero,              // alpha src
        sf::BlendMode::Factor::OneMinusSrcAlpha,  // alpha dst
        sf::BlendMode::Equation::Add);            // alpha eq

With shadows

Although you don't need shadows, I'll leave this here for other people that visit this question.

Dynamic 2D light is a more complex topic, as it involves raycasting algorithms and such. So instead of explaining anything here, I'll point you to a great tutorial (in fact I would recommend anyone to watch all the videos in this channel, they're all fantastic).

Line Of Sight or Shadow Casting in 2D by javidx9.

Although he doesn't use SFML, it is pretty framework-agnostic and everything he does in the OLC engine you can do it with sf::VertexArray.

Alternative: use an existing library

There already exist open source libraries for lighting and dynamic light casting in SFML.

Let there be light (LTBL2)

GitHub repository
SFML forum thread

  • Pros
    • Relies on GLSL shaders, so it's probably more efficient.
    • Provides soft shadows (with penumbras/antumbras).
  • Cons
    • It has very little documentation.
    • Although the project had some updates in 2020, the development stopped in 2016 and doesn't seem to be currently active.


GitHub repository
SFL forum thread

  • Pros
    • It is currently actively developed and maintained.
    • It is extensively documented and comes with an out-of-the-box demo to test it (without external dependencies).
  • Cons
    • As a more recent library, it is not exhaustively tested and might still have glitches.
    • Depending on your needs, it might not be the best performant option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .