Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p

Double buffering in game development in java

Game programming tags

Game programming in java, Professional game, Game design, Java for game, Code in java, java development, java platform, language for game, programming language, android games.


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 again move the pointer head to that 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 full-screen API

Whichever buffer is available to draw the next frame 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 for the next frame 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 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 of 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

2 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.


No comments:

Post a Comment