Content of BufferStrategy

Hi,

I’ve been programming Java Game for 6 months now, building a Java Game framework for my thesis. The information on this board has been a big help. First time, I’ve posted thought, so I hope I don’t raise a common issue.

I was wondering if there is a way to retrieve the content of a bufferstrategy. Let me explain myself.

I’m currently using a bufferstrategy(2) on a JFrame. Once I’ve drawn everything I need, before calling show(), i would like to be able to copy the content of the backbuffer to a BufferedImage. This allow me to:

  1. Cache parts of the image. This cache could be useful for the next render, or a any other render.
  2. Save the buffered image to a file. Thus, it would give me an efficient screenshot (recording?) engine.

Right now, I now that I can save the content of the window using the Robot class. However, this seems very inefficient. Any ideas?

Thanks for the help,

Firebane

Try flipping your thinking sround a bit. Draw to a buffered image and then blit that into the backbuffer. that way you always have a copy of the current screen state.

I’m not positive but I don’t think there is another way to get the backbuffer image.

You need to consider carefully if you actually want to do what you’re proposing. Yanking images from video memory is an EXTREMELY slow process, and is likely to slow your game(s) down considerably. With that in mind, here’s my answer:

If you’re only trying to save the existing image, then set the BufferCapabilities.FlipContents to COPIED or PRIOR. That should keep you from having to refill the entire buffer.

If you REALLY need the image, then I recommend that you manually double-buffer. Create an instance of BufferedImage and use that Graphics context for drawing. When you’re ready to draw to the screen, simply draw the BufferedImage to the BufferStrategy.

Any questions?

I had already thought of doing the double-buffering manually. However, I did not want to lose the speed advantage of BufferedStrategy.

As retrieving data from the video memory, I already knew that it was a slow process. However, I was wondering if Java cached the information somewhere where it would be easily accessible. Just when I think I know the Java 2D very well, I discover a new method. :slight_smile:

If the data only “lives” in the video memory, then getting it back it pretty much a lost cause (speed wise).

[quote]If the data only “lives” in the video memory, then getting it back it pretty much a lost cause (speed wise).
[/quote]
Unfortunately, BufferStrategy is something of a thin wrapper around VolatileImages that just happen to point to the active VRAM. This isn’t true in all circumstances (e.g. if you have insufficient VRAM, BufferStrategy will use a main RAM memory buffer), but the rule is that the images are in VRAM.

I pondered this issue a while ago, and, using a custom Composite class it is possible to read back data from a BufferStrategy’s back buffer[s].

As has been mentioned earlier however, unless this is simply for debug purposes, I doubt you want to do it this way.

If its for a screenshot feature for example, you may want to simply re-render the current frame to an offscreen image that you do have pixel readback access to.