0

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.

1 Answers1

2

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.

DMGregory
  • 134,153
  • 22
  • 242
  • 357
Vaillancourt
  • 16,325
  • 17
  • 55
  • 61