Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to clear the screen even if I intend to overwrite every single pixel?

Tags:

sdl

sdl-2

In the SDL2 documentation it is mentioned that :

You are strongly encouraged to call SDL_RenderClear() to initialize the backbuffer before starting each new frame's drawing, even if you plan to overwrite every pixel.

What is the reason behind that? Isn't it just a useless call to fill the screen one particular color when you know you're going to overwrite it anyway?

like image 770
DrunkenBeard Avatar asked Sep 23 '16 18:09

DrunkenBeard


2 Answers

If you're sure that you will draw over every pixel with a fully nontransparent color you don't need to use SDL_RenderClear().

SDL_RenderClear() is for when you you have a backround color in your frame thats actual visible.

like image 55
just_some_dude Avatar answered Sep 28 '22 01:09

just_some_dude


You don't need to call SDL_RenderClear() in every frame if you are updating every pixel. You can even make some nice effects by using semi-transparent pixels (e.g, "motion blur" by painting a black semi-transparent rectangle before painting another frame).

If this is just your own code, yeah, it might seem useless, but you knever know once your code is in the hands of someone else. They might use different images, things that are transparent and they're going to have a hard time figuring out why everything is repeated in the screen.

Thats one good reason to be encouraged to clear the screen. You are not required to do so. Also, its worth noting that clearing the screen with a color is different than filling the screen with a color by painting every pixel (and a lot faster), thus, your performance lost is neglible compared to the potential future headache.

You can check the RenderClear code for every platform and see how it does different things to do what you said (fill the screen with a color), but with clear method rather than paint method:

  • SW_RenderClear
  • PSP_RenderClear
  • GLES2_RenderClear
  • GLES_RenderClear
  • GL_RenderClear
  • D3D11_RenderClear
  • D3D_RenderClear

You can see that the only implementation of SDL_RenderClear that actually fills the screen is the SW_RenderClear one, but other than that, there is no harm in clearing the screen, but no obligation either. SDL works just fine without it.

like image 22
Leonardo Avatar answered Sep 28 '22 00:09

Leonardo