Tuesday 13 January 2009

The Display Manager

Finally, we come to the DisplayManager - the last item in the list of
Java classes.

The DisplayManager handles the graphics, using an off-screen buffer and
a clean version of the background. At each frame change, the previous
dirty rectangles are cleaned by writing the background into them, and
then the sprites are copied to the off-screen buffer, and the new dirty
rectangles redisplayed.

In a paint() call, only the dirty rectangles are displayed. In an
update(), the whole screen is written.

I think an unnecessary DirtyRectSet is defined in Bridge. Priority is
not being taken into account in sprite write sequence. There may still
be some confusion as to when an update and when a full redisplay takes
place.


/**
*
* DisplayManager.java
* @author Mark G. Tacchi (mtacchi@next.com)
* @version 0.8
* Mar 28/1996
* Heavily modified, with permission, by Gil Williamson 2003
*
* DisplayManager provides optimized screen display. Images are drawn to
* an offscreen buffer and blitted out to the display. If images are close
* to one another, they are coalesced and blitted as a single image.
*
* A read only cache is kept which represents an untainted background image.
* This is used in the optimization algorithm as a source for a clean
* background.
*
*/


import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.util.Vector;
import java.awt.Cursor;

public class DisplayManager extends java.lang.Object
{
private Image background;
private Image offScreenBuffer;
private DirtyRectSet dirtyRects = new DirtyRectSet();

/**
* A reference back to the Bridge this manager is servicing.
*/
protected Bridge owner;

public DisplayManager(Bridge theOwner)
{
owner= theOwner;
}

/**
* Set the background image to the specified image.
*/
public void setBackground (Image theImage)
{
background= theImage;
int width = background.getWidth(owner);
int height = background.getHeight(owner);
offScreenBuffer= owner.createImage(width, height);
offScreenBuffer.getGraphics().drawImage (theImage, 0, 0, owner);

dirtyRects.addRect (new Rectangle (0, 0, width,height));

}/*setBackground*/

public void refresh(Graphics g)
{
//gil temp
Rectangle r = new Rectangle(0,0,640,480);
dirtyRects.addRect(r);
paint(g);

}

/**
* Display changed portions of screen.
*/
public void paint(Graphics g)
{
DirtyRectSet flushRects;
Graphics osb;

if( offScreenBuffer == null )
osb = null;
else
osb = offScreenBuffer.getGraphics ();

//
// clear background behind actors...
//
dirtyRects.drawImage (osb, background, owner);

flushRects= dirtyRects;
dirtyRects= new DirtyRectSet();

//
// draw Sprites
//
Vector slist = owner.bridgedata.getCurrentSprites();
for (int ii = 0; ii< slist.size(); ii++)
{
Sprite sprite = (Sprite)slist.elementAt(ii);
// Zilch sprites do not get drawn
if (sprite.getClass() == Zilch.class) continue;
Rectangle r = new Rectangle(sprite.getExtent());
dirtyRects.addRect (r);
flushRects.addRect (r);
Graphics g2= osb.create (r.x, r.y, r.width, r.height);
Image imageii = sprite.getCurrentImage();
g2.drawImage(imageii, 0, 0, owner);
g2.dispose();
}
flushRects.drawImage (g, offScreenBuffer, owner);

} /*paint*/

} /*DisplayManager*/

No comments: