Skip to main content

drawing on only part of display in fullscreen mode with double buffering?

2 replies [Last post]
boyaci
Offline
Joined: 2006-03-24

I am trying to implement an animation in Fullscreen mode (FSEM) where the background remains static to a large extent and only some small portion of the display should differ from frame to frame. I can do this by drawing both the static background image and the animating foreground image to the back buffer and then display it using BufferStrategy.show():

Suppose the background image is bg (a BufferedImage) and suppose that the small part that changes during the animation is stored in another image fg. For simplicity suppose that the screen position where fg is drawn does not change in successive frames. I can implement this as follows:

public void animate(){
if (getBufferStrategy().contentsLost())
createBufferStrategy(2);
Graphics2D g = (Graphics2D) getBufferStrategy().getDrawGraphics();
g.drawImage(bg,x1,y1,null); // x1,y1,bg never change
g.drawImage(fg,x2,y2,null); // x2,y2 constant, only fg changes
getBufferStrategy().show();
}

But I am a bit concerned with this approach. Because this implementation has to blit/flip the entire back buffer to front even though most part is actually never changing. I am wondering if there is a better way of doing this. For example, is there a way to blit/flip only a portion of the front buffer? Or is this actually taken care of by the BufferStrategy behind the scene?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Dmitri Trembovetski

java2d@javadesktop.org wrote:
> I am trying to implement an animation in Fullscreen mode (FSEM) where the background remains static to a large extent and only some small portion of the display should differ from frame to frame. I can do this by drawing both the static background image and the animating foreground image to the back buffer and then display it using BufferStrategy.show():
>
> Suppose the background image is bg (a BufferedImage) and suppose that the small part that changes during the animation is stored in another image fg. For simplicity suppose that the screen position where fg is drawn does not change in successive frames. I can implement this as follows:
>
> public void animate(){
> if (getBufferStrategy().contentsLost())
> createBufferStrategy(2);
> Graphics2D g = (Graphics2D) getBufferStrategy().getDrawGraphics();
> g.drawImage(bg,x1,y1,null); // x1,y1,bg never change
> g.drawImage(fg,x2,y2,null); // x2,y2 constant, only fg changes
> getBufferStrategy().show();
> }

First of all, your buffer strategy handling is not
entirely correct. You are not supposed to recreate
the buffer strategy when it's lost, it's taken
care for you.
Take a look at the suggested loop in the BS's
javadoc (updated for jdk1.6):

http://java.sun.com/javase/6/docs/api/java/awt/image/BufferStrategy.html

Regarding your question
Unfortunately the buffer strategy api does not provide
partial front-buffer updates. However, if the strategy is hw
accelerated the showing of the back-buffer is very
fast. Most games don't bother with partial updates,
they re-render the whole back-buffer every time.

If you're concerned with re-rendering the whole back-buffer
on every frame, you can also create
a bufferstrategy with FlipContents.COPY capability.
It will ensure that the contents of the back-buffer
do not change after you do the show(), so you
can only re-render stuff you need in the back-buffer,
and then show() it (all).

However, before going to all these troubles, make
sure there's actually an issue - profile the application
and see what really takes time.

Thanks,
Dmitri

>
> But I am a bit concerned with this approach. Because this implementation has to blit/flip the entire back buffer to front even though most part is actually never changing. I am wondering if there is a better way of doing this. For example, is there a way to blit/flip only a portion of the front buffer? Or is this actually taken care of by the BufferStrategy behind the scene?
> [Message sent by forum member 'boyaci' (boyaci)]
>
> http://forums.java.net/jive/thread.jspa?messageID=223663
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

boyaci
Offline
Joined: 2006-03-24

Thank you very much, that was very helpful.