Buffer
Cause a series of sequential operations to appear instantaneous or simultaneous.
A computer must be able to render different frames, one frame at a time.
If we were to paint a frame, and the video driver pulls our frame half drawn, what ends up happening is half the image renders onto the screen.
Imagine a play, where there is some setup required between scenes. While the audience is captivated by the play on one stage, the team can prep for the next scene. When the first scene is done, the audience’s attention is diverted to the second stage, where they can watch the next scene without any waiting. While that scene plays out, the team preps the first stage for the next scene.
This pattern requires a swap step that is atomic. No code can access either state while they are being swapped.
The other consequence is increased memory usage. You need two copies of the buffer at any time.
class Scene
{
public:
() : current_(&buffers_[0]), next_(&buffers_[1]) {}
Scene
void draw() {
next_->clear();
next_->draw(1, 1);
next_->draw(4, 3);
();
swap}
& getBuffer() { return *current_; }
Framebuffer
private:
void swap() {
// Just switch the pointers.
* temp = current_;
Framebuffercurrent_ = next_;
next_ = temp;
}
buffers_[2];
Framebuffer * current_;
Framebuffer* next_;
Framebuffer};
We create a scene class with two buffers, and have them swap buffers. We always paint to the next buffer, and swap when appropriate.
Copy data between the buffers