Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2012 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3389202 to your computer and use it in GitHub Desktop.
Save anonymous/3389202 to your computer and use it in GitHub Desktop.
More sophisticated double-buffered rendering
private VolatileImage drawImage = null;
private int drawImageWidth = 800;
private int drawImageHeight = 600;
public void render(BufferStrategy strategy) {
if (drawImage == null) {
drawImage = createVolatileImage(drawImageWidth, drawImageHeight);
}
Graphics2D drawGraphics = (Graphics2D) drawImage.getGraphics();
// TODO: use `drawGraphics` to draw game graphics (background, platforms, player, etc.)
// render a single frame:
do {
do {
Graphics2D renderGraphics = (Graphics2D) strategy.getDrawGraphics();
renderGraphics.drawImage(drawImage, 0, 0, null);
renderGraphics.dispose();
} while (strategy.contentsRestored());
strategy.show();
} while (strategy.contentsLost());
Toolkit.getDefaultToolkit().sync();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment