From 47e38b4fa862b91c4ab2ddb43ca12f2c3127d875 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 4 May 2002 19:35:31 +0000 Subject: [PATCH] Changed MultiFrameImage around so that it retains total control over its internal images and doesn't necessarily need to decode them into multiple images or even have actual images at all. This cleans things up and paves the way for our "trimmed" image support which is forthcoming. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1335 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/sprite/ImageSprite.java | 44 +++++-------------- .../media/util/MultiFrameImage.java | 24 ++++++++-- .../media/util/MultiFrameImageImpl.java | 31 ++++++++++--- .../media/util/SingleFrameImageImpl.java | 29 ++++++++++-- 4 files changed, 82 insertions(+), 46 deletions(-) diff --git a/src/java/com/threerings/media/sprite/ImageSprite.java b/src/java/com/threerings/media/sprite/ImageSprite.java index d27f5fdf4..3de6b4cf6 100644 --- a/src/java/com/threerings/media/sprite/ImageSprite.java +++ b/src/java/com/threerings/media/sprite/ImageSprite.java @@ -1,5 +1,5 @@ // -// $Id: ImageSprite.java,v 1.4 2002/04/23 01:16:28 mdb Exp $ +// $Id: ImageSprite.java,v 1.5 2002/05/04 19:35:31 mdb Exp $ package com.threerings.media.sprite; @@ -95,25 +95,11 @@ public class ImageSprite extends Sprite // first check to see that we're in the sprite's bounds and that // we've got a frame image (if we've got no image, there's nothing // to be hit) - if (!super.hitTest(x, y) || _frame == null) { + if (!super.hitTest(x, y) || _frames == null) { return false; } - if (_frame instanceof BufferedImage) { - BufferedImage bimage = (BufferedImage)_frame; - int argb = bimage.getRGB(x - _bounds.x, y - _bounds.y); - int alpha = argb >> 24; - // Log.info("Checking [x=" + x + ", y=" + y + - // ", bounds=" + _bounds + ", " + alpha); - - // it's only a hit if the pixel is non-transparent - return (argb >> 24) != 0; - - } else { - Log.warning("Can't check for transparent pixel " + - "[image=" + _frame + "]."); - return true; - } + return _frames.hitTest(_frameIdx, x - _bounds.x, y - _bounds.y); } /** @@ -164,10 +150,10 @@ public class ImageSprite extends Sprite _frames = frames; _frameIdx %= _frames.getFrameCount(); - _frame = _frames.getFrame(_frameIdx); // determine our drawing offsets and rendered rectangle size - accomodateFrame(_frame); + accomodateFrame(_frames.getWidth(_frameIdx), + _frames.getHeight(_frameIdx)); // add our new bounds dirty.add(_bounds); @@ -188,23 +174,17 @@ public class ImageSprite extends Sprite * will need to override this method and adjust bounds accordingly for * the new frame (which can be null). */ - protected void accomodateFrame (Image frame) + protected void accomodateFrame (int width, int height) { - if (frame == null) { - _bounds.width = 0; - _bounds.height = 0; - - } else { - _bounds.width = frame.getWidth(null); - _bounds.height = frame.getHeight(null); - } + _bounds.width = width; + _bounds.height = height; } // documentation inherited public void paint (Graphics2D gfx) { - if (_frame != null) { - gfx.drawImage(_frame, _bounds.x, _bounds.y, null); + if (_frames != null) { + _frames.paintFrame(gfx, _frameIdx, _bounds.x, _bounds.y); } else { super.paint(gfx); } @@ -248,7 +228,6 @@ public class ImageSprite extends Sprite // only update the sprite if our frame index changed if (nfidx != _frameIdx) { _frameIdx = nfidx; - _frame = _frames.getFrame(_frameIdx); // dirty our rectangle since we've altered our display image invalidate(); } @@ -264,9 +243,6 @@ public class ImageSprite extends Sprite /** The images used to render the sprite. */ protected MultiFrameImage _frames; - /** The current frame being rendered. */ - protected Image _frame; - /** The current frame index to render. */ protected int _frameIdx; diff --git a/src/java/com/threerings/media/util/MultiFrameImage.java b/src/java/com/threerings/media/util/MultiFrameImage.java index ea71df92c..348b1a132 100644 --- a/src/java/com/threerings/media/util/MultiFrameImage.java +++ b/src/java/com/threerings/media/util/MultiFrameImage.java @@ -1,8 +1,9 @@ // -// $Id: MultiFrameImage.java,v 1.1 2001/08/14 23:35:22 mdb Exp $ +// $Id: MultiFrameImage.java,v 1.2 2002/05/04 19:35:31 mdb Exp $ package com.threerings.media.sprite; +import java.awt.Graphics; import java.awt.Image; /** @@ -17,7 +18,24 @@ public interface MultiFrameImage public int getFrameCount (); /** - * Returns the image for the specified frame index. + * Returns the width of the specified frame image. */ - public Image getFrame (int index); + public int getWidth (int index); + + /** + * Returns the height of the specified frame image. + */ + public int getHeight (int index); + + /** + * Renders the specified frame into the specified graphics object at + * the specified coordinates. + */ + public void paintFrame (Graphics g, int index, int x, int y); + + /** + * Returns true if the specified frame contains a non-transparent + * pixel at the specified coordinates. + */ + public boolean hitTest (int index, int x, int y); } diff --git a/src/java/com/threerings/media/util/MultiFrameImageImpl.java b/src/java/com/threerings/media/util/MultiFrameImageImpl.java index 75f83b3b7..775551399 100644 --- a/src/java/com/threerings/media/util/MultiFrameImageImpl.java +++ b/src/java/com/threerings/media/util/MultiFrameImageImpl.java @@ -1,10 +1,13 @@ // -// $Id: MultiFrameImageImpl.java,v 1.1 2001/10/25 18:06:17 shaper Exp $ +// $Id: MultiFrameImageImpl.java,v 1.2 2002/05/04 19:35:31 mdb Exp $ package com.threerings.media.sprite; +import java.awt.Graphics; import java.awt.Image; +import com.threerings.media.util.ImageUtil; + /** * A basic implementation of the {@link MultiFrameImage} interface * intended to facilitate the creation of sprites whose display frames @@ -15,7 +18,7 @@ public class MultiFrameImageImpl implements MultiFrameImage /** * Constructs a multiple frame image object. */ - public MultiFrameImageImpl (Image imgs[]) + public MultiFrameImageImpl (Image[] imgs) { _imgs = imgs; } @@ -26,10 +29,28 @@ public class MultiFrameImageImpl implements MultiFrameImage return _imgs.length; } - // documentation inherited - public Image getFrame (int index) + // documentation inherited from interface + public int getWidth (int index) { - return _imgs[index]; + return _imgs[index].getWidth(null); + } + + // documentation inherited from interface + public int getHeight (int index) + { + return _imgs[index].getHeight(null); + } + + // documentation inherited from interface + public void paintFrame (Graphics g, int index, int x, int y) + { + g.drawImage(_imgs[index], x, y, null); + } + + // documentation inherited from interface + public boolean hitTest (int index, int x, int y) + { + return ImageUtil.hitTest(_imgs[index], x, y); } /** The frame images. */ diff --git a/src/java/com/threerings/media/util/SingleFrameImageImpl.java b/src/java/com/threerings/media/util/SingleFrameImageImpl.java index 65e6a7033..04bdf6b9e 100644 --- a/src/java/com/threerings/media/util/SingleFrameImageImpl.java +++ b/src/java/com/threerings/media/util/SingleFrameImageImpl.java @@ -1,10 +1,13 @@ // -// $Id: SingleFrameImageImpl.java,v 1.1 2001/10/25 18:06:17 shaper Exp $ +// $Id: SingleFrameImageImpl.java,v 1.2 2002/05/04 19:35:31 mdb Exp $ package com.threerings.media.sprite; +import java.awt.Graphics; import java.awt.Image; +import com.threerings.media.util.ImageUtil; + /** * The single frame image class is a basic implementation of the * {@link MultiFrameImage} interface intended to facilitate the @@ -27,10 +30,28 @@ public class SingleFrameImageImpl implements MultiFrameImage return 1; } - // documentation inherited - public Image getFrame (int index) + // documentation inherited from interface + public int getWidth (int index) { - return _img; + return _img.getWidth(null); + } + + // documentation inherited from interface + public int getHeight (int index) + { + return _img.getHeight(null); + } + + // documentation inherited from interface + public void paintFrame (Graphics g, int index, int x, int y) + { + g.drawImage(_img, x, y, null); + } + + // documentation inherited from interface + public boolean hitTest (int index, int x, int y) + { + return ImageUtil.hitTest(_img, x, y); } /** The frame image. */