1

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.

braX
  • 11,506
  • 5
  • 20
  • 33
KwentRell
  • 416
  • 1
  • 4
  • 17

1 Answers1

0

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;
}
alseether
  • 1,889
  • 2
  • 24
  • 39
  • 1
    The code in the answer is [tag:c++], but the concept is reachable with [tag:.net] too – alseether Apr 02 '19 at 13:34
  • With this technique using SFML draw calls I can draw persitant things too but when I call WIN32's API SetPixel method it gets cleared for instance. – KwentRell Apr 02 '19 at 14:48
  • Yeah, this is a *pure* SFML solution, I'm not sure if you can mix both and draw on a SFML window partially. You can always create your own class, making it inherit from `sf::RenderTarget` and `sf::Window`, and some way managing that *invalidated* areas, but that's way more complicated. – alseether Apr 02 '19 at 15:05