SFML: keep a RenderWindow's partial region unrefreshed

289 Views Asked by At

I am looking to get the same effect as the GraphicsPath from Winforms which allows to keep some particular areas of myForm unrefreshed. f.i.:

myForm.Invalidate(new Region(graphicsPath));

My final goal is to draw things at the unrefreshed location, using a HDC (Device context handle) that I will provide to an external application. (This currently works using winforms).

I am using SFML.Net 2.4 and I create my window this way:

 SFML.Graphics.RenderWindow  mySfmlWindow = new RenderWindow(myForm.Handle, settings);

I can still create the HDC on myForm, however, even without calling :

mySfmlWindow.Clear(color);

, the things drawn by the external application are still instantly cleared.

1

There are 1 best solutions below

3
alseether On

Manual approach

You can draw your desired background yourself. I've got an example, where I draw a half of the window background manually, while the other half is not cleared.

enter image description here

The left half is "cleared" in gray just to show the point.

In the code, I use a sf::RectangleShape to clear the window, but you can use a sf::VertexArray if your shape is more complex.


Full Code

int main(){
    sf::RenderWindow win(sf::VideoMode(640, 480), "SFML Test");

    sf::RectangleShape r1;
    r1.setOrigin(sf::Vector2f(25, 25));
    r1.setPosition(50, 50);
    r1.setSize(sf::Vector2f(50, 50));
    r1.setFillColor(sf::Color::Red);

    sf::RectangleShape r2;
    r2.setOrigin(sf::Vector2f(25, 25));
    r2.setPosition(400, 50);
    r2.setSize(sf::Vector2f(50, 50));
    r2.setFillColor(sf::Color::Blue);


    sf::RectangleShape updatedRegion;
    updatedRegion.setPosition(0, 0);
    updatedRegion.setSize(sf::Vector2f(320, 480));  // Half window
    updatedRegion.setFillColor(sf::Color(30,30,30));    // Dark gray just for the sake of the example

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

        //win.clear();              // We skip the clearing process
        win.draw(updatedRegion);    // And do our own "clear"
        win.draw(r1);
        win.draw(r2);
        win.display();

        // Just some movement to test the concept
        r1.rotate(0.01);
        r2.rotate(0.01);

    }

    return 0;
}