Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do double buffering in SDL 2.0?

Tags:

sdl-2

I have a tile tile map array [50][50].

When I click the mouse to move the x and y of every single tile in that array I see white spaces between each tile - I believe this is called 'tearing', it isn't desired.

I googled double buffering and read the wiki, http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics .

How is it done in SDL2, specifically with large tile arrays created from 'for loops'?

...I suspect you copy tile textures onto a buffer texture and copy that to the renderer once its fully loaded - though I couldn't get this to work, would love to see a full working example!

This is my previous post with current my current code: SDL2 double buffer not working, still tearing

like image 388
doomglhfcn Avatar asked Feb 05 '15 10:02

doomglhfcn


People also ask

What is double buffer mode?

The Double Buffer mode allows to set two different Memory addresses from/to which the DMA controller will access alternatively (after completing transfer to/from target memory 0, it will start transfer to/from target memory 1). This allows to reduce software overhead for double buffering and reduce the CPU access time.

What does enable double buffering do?

Double buffering uses a memory buffer to address the flicker problems associated with multiple paint operations. When double buffering is enabled, all paint operations are first rendered to a memory buffer instead of the drawing surface on the screen.

What is double and triple buffer?

Double buffering renders the next frame while displaying the current. That results in latency of 1 frame since input. Triple buffering adds another frame to the queue, resulting in a 2 frame lag.


1 Answers

SDL2 handles double buffering automatically for you:

  • SDL_RenderClear will clear the current rendering buffer.
  • SDL_RenderCopy will render textures to the current rendering buffer.
  • SDL_RenderPresent will swap the buffers, showing everything that was rendered using SDL_RenderCopy since the last SDL_RenderClear call.
like image 144
emlai Avatar answered Sep 17 '22 01:09

emlai