Sachin Vijaykumar
Sachin Vijaykumar

Reputation: 45

How to update the screen without clearing the renderer in SDL2

I am trying to render a square after SDL_RenderPresent() as I want to render the rectangle to the screen without disturbing other stuff or clearing the screen.

  SDL_Event event;
  SDL_RenderClear(renderer);

  SDL_Rect rect;
  rect.x = 10;
  rect.y = 10;
  rect.w = 100;
  rect.h = 100;

  SDL_Rect nice;
  nice.h = 100;
  nice.w = 100;
  nice.x = 130;
  nice.y = 123;

  SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);

  SDL_RenderFillRect(renderer, &rect);

  SDL_RenderPresent(renderer);

  SDL_RenderFillRect(renderer, &nice);

  SDL_RenderPresent(renderer);

  while (SDL_WaitEvent(&event)) {
    if (event.type == SDL_QUIT)
      break;
  }

only the second square get rendered , the first square does not appear on the screen

Upvotes: 0

Views: 947

Answers (1)

keltar
keltar

Reputation: 18409

As documentation says:

The backbuffer should be considered invalidated after each present; do not assume that previous contents will exist between frames.

Most often rendering utilises double buffering, so at best, even if contents are preserved, your backbuffer will be lagging behind 2 frames. There is almost no sane (and reliable) way to update only changed parts, except for very static scenes.

Your options could be either to redraw everything again, or to maintain your own intermediate render texture, by drawing everything into your texture and then displaying it on screen.

Upvotes: 0

Related Questions