From 330767e19333f1419ec0cc119423ea0f4d868e94 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 19 Jun 2002 23:30:06 +0000 Subject: [PATCH] Cleaned up a few things related to render origin. If a derived class wants the render origin to be other than (_x, _y) it can update (_rxoff, _ryoff) when it is appropriate to do so, but we don't really need an overridden method to do that. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1500 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/sprite/ImageSprite.java | 55 +++++++++++++------ .../com/threerings/media/sprite/Sprite.java | 20 ++----- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/java/com/threerings/media/sprite/ImageSprite.java b/src/java/com/threerings/media/sprite/ImageSprite.java index b737dd733..b7af397ff 100644 --- a/src/java/com/threerings/media/sprite/ImageSprite.java +++ b/src/java/com/threerings/media/sprite/ImageSprite.java @@ -1,8 +1,12 @@ // -// $Id: ImageSprite.java,v 1.7 2002/06/05 23:38:18 ray Exp $ +// $Id: ImageSprite.java,v 1.8 2002/06/19 23:30:06 mdb Exp $ package com.threerings.media.sprite; +import java.awt.AlphaComposite; +import java.awt.Color; +import java.awt.Composite; + import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; @@ -83,7 +87,7 @@ public class ImageSprite extends Sprite super.init(spritemgr); // now that we have our spritemanager, we can initialize our frames - initFrames(); + setFrameIndex(0, true); } /** @@ -154,29 +158,38 @@ public class ImageSprite extends Sprite // set and init our frames _frames = frames; - initFrames(); + setFrameIndex(0, true); } /** - * Initialize our frames. + * Instructs the sprite to display the specified frame index. */ - protected void initFrames () + protected void setFrameIndex (int frameIdx, boolean forceUpdate) { + // make sure we're displaying a valid frame + frameIdx = (frameIdx % _frames.getFrameCount()); + + // if this is the same frame we're already displaying and we're + // not being forced to update, we can stop now + if (frameIdx == _frameIdx && !forceUpdate) { + return; + } else { + _frameIdx = frameIdx; + } + // start with our old bounds Rectangle dirty = new Rectangle(_bounds); - _frameIdx %= _frames.getFrameCount(); - // determine our drawing offsets and rendered rectangle size accomodateFrame(_frames.getWidth(_frameIdx), _frames.getHeight(_frameIdx)); + // account for any new render offsets + updateRenderOrigin(); + // add our new bounds dirty.add(_bounds); - updateRenderOffset(); - updateRenderOrigin(); - // give the dirty rectangle to the region manager if (_spritemgr != null) { _spritemgr.getRegionManager().addDirtyRegion(dirty); @@ -200,7 +213,16 @@ public class ImageSprite extends Sprite public void paint (Graphics2D gfx) { if (_frames != null) { +// // DEBUG: fill our background with an alpha'd rectangle +// Composite ocomp = gfx.getComposite(); +// gfx.setComposite(ALPHA_BOUNDS); +// gfx.setColor(Color.blue); +// gfx.fill(_bounds); +// gfx.setComposite(ocomp); + + // render our frame _frames.paintFrame(gfx, _frameIdx, _bounds.x, _bounds.y); + } else { super.paint(gfx); } @@ -241,12 +263,9 @@ public class ImageSprite extends Sprite break; } - // only update the sprite if our frame index changed - if (nfidx != _frameIdx) { - _frameIdx = nfidx; - // dirty our rectangle since we've altered our display image - invalidate(); - } + // update our frame (which will do nothing if this is the same as + // our existing frame index) + setFrameIndex(nfidx, false); } // documentation inherited @@ -267,4 +286,8 @@ public class ImageSprite extends Sprite /** For how many milliseconds to display an animation frame. */ protected long _frameDelay; + +// /** DEBUG: The alpha level used when rendering our bounds. */ +// protected static final Composite ALPHA_BOUNDS = +// AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f); } diff --git a/src/java/com/threerings/media/sprite/Sprite.java b/src/java/com/threerings/media/sprite/Sprite.java index 68c89f611..e964e4deb 100644 --- a/src/java/com/threerings/media/sprite/Sprite.java +++ b/src/java/com/threerings/media/sprite/Sprite.java @@ -1,5 +1,5 @@ // -// $Id: Sprite.java,v 1.47 2002/06/18 22:25:33 mdb Exp $ +// $Id: Sprite.java,v 1.48 2002/06/19 23:30:06 mdb Exp $ package com.threerings.media.sprite; @@ -53,8 +53,6 @@ public abstract class Sprite protected void init (SpriteManager spritemgr) { _spritemgr = spritemgr; - - updateRenderOrigin(); } /** @@ -353,16 +351,6 @@ public abstract class Sprite } } - /** - * Updates the sprite's render offset which is used to determine - * where to place the top-left corner of the render bounds. - */ - protected void updateRenderOffset () - { - _rxoff = 0; - _ryoff = 0; - } - /** * Update the coordinates at which the sprite image is drawn to * reflect the sprite's current position. @@ -431,6 +419,8 @@ public abstract class Sprite { buf.append("x=").append(_x); buf.append(", y=").append(_y); + buf.append(", rxoff=").append(_rxoff); + buf.append(", ryoff=").append(_ryoff); } /** The sprite manager. */ @@ -439,7 +429,9 @@ public abstract class Sprite /** The location of the sprite in pixel coordinates. */ protected int _x, _y; - /** The offsets from our location to our rendered origin. */ + /** The offsets from our location to our rendered origin. Derived + * classes may wish to set these values if the sprite's upper left + * corner should not be coincident with its location. */ protected int _rxoff, _ryoff; /** Our rendered bounds in pixel coordinates. */