0
\$\begingroup\$

Do I have to clear the entire target with a single color every frame? I thought the newly drawn ones will overwrite the old ones.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ What do you observe when you don't clear the target with a single colour? Does the result visibly differ from what you want? \$\endgroup\$
    – DMGregory
    Jul 11 at 11:06
  • \$\begingroup\$ I saw nothing. The result did not visibly differ from what I want. Because no objects were drawn on the screen. \$\endgroup\$ Jul 11 at 11:15
  • \$\begingroup\$ You don't want any objects drawn on the screen? \$\endgroup\$
    – DMGregory
    Jul 11 at 11:19
  • 1
    \$\begingroup\$ I am learning the basics of SFML, and I want to test things, DMGregory. \$\endgroup\$ Jul 11 at 11:32

1 Answer 1

2
\$\begingroup\$

From the documentation (emphasis mine):

Calling clear before drawing anything is mandatory, otherwise the contents from previous frames will be present behind anything you draw. The only exception is when you cover the entire window with what you draw, so that no pixel is not drawn to. In this case you can avoid calling clear (although it won't have a noticeable impact on performance).

So you're right, if you plan to redraw the whole screen, every frame, then you don't have to call clear.

...but since calling clear is cheap, it is highly recommended to do so: it will prevent some eventual WTF?!!!? during future development (e.g. WTF, why are there weird shapes when I debug my character movement code ?!!!?). Also, since it is generally expected to be there, you'll get less weird looks at you when you have other devs look at your code. Without going into details, it can help you find some bugs with your code that would be harder to track if you didn't do it.

\$\endgroup\$
2
  • \$\begingroup\$ Why do you know that calling clear is cheap? \$\endgroup\$ Jul 22 at 10:48
  • 1
    \$\begingroup\$ "although it won't have a noticeable impact on performance" \$\endgroup\$
    – Vaillancourt
    Jul 22 at 11:45

You must log in to answer this question.

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