Double buffering in game development in java

Concept of Double buffering

If your game app draws and clears the content directly to the screen then
the user can see the intermediate pictures.

For example, at the time when the background is drawn but the player is going to be drawn definitely, you'll see only the background until the player is drawn. I hope you are getting my point.

As above mentioned this is often not a behaviour of a good game.

We are going to overcome this drawback.

To this, Game developers use a way referred to as double buffering (sounds interesting!).

Double buffering

double buffer in game development in java

What happens by using it?

Well, the user cannot see the method of rendering in turns no flickering impact happens.

What's the idea behind the double buffering

Actually, the game app makes and manages 2 buffers, it uses them to hide
the steps of rendering the stuff from the user.

Once the drawing is complete on one buffer, then it shows that complete
image to the user by copying data from the buffer to screen.

When one buffer is drawn or copying to the screen, it rapidly starts drawing on
second and the same will happen again (copying data to screen from it)

One important thing is that the buffer's content should be erased before
drawing.

The benefit of using the double buffering technique is the user doesn't see the rendering process and no flickering effect happens.

What is page-flipping?

game development programming
game development in java

Page-flipping can be used in full-screen mode, it also uses a similar way like double buffering but instead of copying from buffer it moves the video pointer head to that buffer. See the above image.

This way whichever image is not pointed by the video pointer we can draw the next frame to that and on completion of the frame, we'll swap the pointer to buffer.

java program example for double buffering

class DoubleBufferExample

To use double buffering and page-flipping, we can use an instance of java.awt.image.BufferStrategy class.

By using a BufferStrategy object, you don't have to care that the program is in full-screen and using page-flipping, or it is windowed and using
double-buffering.

The bufferStrategy object relies on bufferCapabilities object and does the work according to it.

learn more about full-screen API

Whichever buffer is available for drawing, just call the getDrawGraphics() from BufferStrategy. It will create and return an object of Graphics connected to that buffer.

This graphics object will draw stuff  to buffer to which it is connected.

It is important to call BufferStrategy.contentsLost() to ensure that the off-screen surface is available.

The surface might get unavailable if the app is running in full screen and uses the page flipping and the user hit the alt plus tab together to switch between apps.

This will cause the graphics device to take back its memory which has been given to your app.  

So to ensure the surface is available it is important.

Depending on the capabilities the show() from BufferStretegy performs either
double-buffering/image copying or page-flipping/pointer swapping to display
the image.

Read java docs for more information about
buffer strategy and
Buffer Capabilities.

Notice that the rendering code is wrapped in a try/finally block in the below example.

Because this graphics object has been created therefore it must be disposed off when the rendering is complete.

Not invoking the Graphics.dispose() method can cause a memory leak and crash the app.

Here is an example of how to employ the BufferStrategy to draw and show a single frame.

java code snippet

The two classes allow you to create and use the BufferStrategy object named as Window and Canvas.

Thus by adding the canvas, the buffer Strategy object will be accessible to you and Canvas can be added to JFrame.

To get the instance of BufferStrategy for canvas just call the Canvas.createBufferStrategy() method and pass in the desired number of buffer and then you can access it thru Canvas.getBufferStrategy().

Because the application is handling the drawing, there is no need to respond to repaint messages from the system so set ignore-repaint flag to true by setIgnoreRepaint() method.

The complete code for DoubleBufferExample is below.


Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *