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.
-
2What do you observe when you don't clear the target with a single colour? Does the result visibly differ from what you want? – DMGregory Jul 11 '22 at 11:06
-
I saw nothing. The result did not visibly differ from what I want. Because no objects were drawn on the screen. – Đạt Phạm Jul 11 '22 at 11:15
-
You don't want any objects drawn on the screen? – DMGregory Jul 11 '22 at 11:19
-
1I am learning the basics of SFML, and I want to test things, DMGregory. – Đạt Phạm Jul 11 '22 at 11:32
1 Answers
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.

- 134,153
- 22
- 242
- 357

- 16,325
- 17
- 55
- 61