diff --git a/src/java/com/threerings/cast/util/TileUtil.java b/src/java/com/threerings/cast/util/TileUtil.java index 3bd08dfb4..d05432a8e 100644 --- a/src/java/com/threerings/cast/util/TileUtil.java +++ b/src/java/com/threerings/cast/util/TileUtil.java @@ -1,5 +1,5 @@ // -// $Id: TileUtil.java,v 1.1 2001/11/02 15:28:20 shaper Exp $ +// $Id: TileUtil.java,v 1.2 2001/11/08 03:04:44 mdb Exp $ package com.threerings.cast.util; @@ -84,7 +84,7 @@ public class TileUtil } // get the number of frames of animation - int frameCount = tset.getNumTiles() / Sprite.NUM_DIRECTIONS; + int frameCount = tset.getTileCount() / Sprite.NUM_DIRECTIONS; for (int dir = 0; dir < Sprite.NUM_DIRECTIONS; dir++) { // retrieve all images for the sequence and direction diff --git a/src/java/com/threerings/media/tile/ObjectTileSet.java b/src/java/com/threerings/media/tile/ObjectTileSet.java new file mode 100644 index 000000000..347e91231 --- /dev/null +++ b/src/java/com/threerings/media/tile/ObjectTileSet.java @@ -0,0 +1,65 @@ +// +// $Id: ObjectTileSet.java,v 1.1 2001/11/08 03:04:44 mdb Exp $ + +package com.threerings.media.tile; + +import java.awt.Image; +import java.awt.Point; + +import com.samskivert.util.HashIntMap; + +import com.threerings.media.Log; +import com.threerings.media.ImageManager; + +/** + * The objcet tileset supports the specification of object information for + * object tiles in addition to all of the features of the swiss army + * tileset. + */ +public class ObjectTileSet extends SwissArmyTileSet +{ + /** + * Constructs a tileset with all of the swiss army configuration + * parameters, with the addition of object information for those tiles + * in the set that are object tiles. + * + * @param objects object information for those tiles that are objects. + * + * @see SwissArmyTileSet#SwissArmyTileSet + */ + public ObjectTileSet ( + ImageManager imgmgr, String imgPath, String name, int tsid, + int[] tileCount, int[] rowWidth, int[] rowHeight, + Point offsetPos, Point gapDist, HashIntMap objects) + { + super(imgmgr, imgPath, name, tsid, tileCount, rowWidth, rowHeight, + offsetPos, gapDist); + + // keep this for later + _objects = objects; + } + + /** + * Creates instances of {@link ObjectTile} which can span more than a + * single tile's space in a display. + */ + protected Tile createTile (int tileId) + { + // default object dimensions to (1, 1) + int wid = 1, hei = 1; + + // retrieve object dimensions if known + if (_objects != null) { + int size[] = (int[])_objects.get(tileId); + if (size != null) { + wid = size[0]; + hei = size[1]; + } + } + + return new ObjectTile(_tsid, tileId, wid, hei); + } + + /** Mapping of object tile ids to object dimensions. */ + protected HashIntMap _objects; +} diff --git a/src/java/com/threerings/media/tile/SwissArmyTileSet.java b/src/java/com/threerings/media/tile/SwissArmyTileSet.java new file mode 100644 index 000000000..2819828a4 --- /dev/null +++ b/src/java/com/threerings/media/tile/SwissArmyTileSet.java @@ -0,0 +1,129 @@ +// +// $Id: SwissArmyTileSet.java,v 1.1 2001/11/08 03:04:44 mdb Exp $ + +package com.threerings.media.tile; + +import java.awt.Image; +import java.awt.Point; + +import com.threerings.media.Log; +import com.threerings.media.ImageManager; + +/** + * The swiss army tileset supports a diverse variety of tiles in the + * tileset image. Each row can contain varying numbers of tiles and each + * row can have its own width and height. Tiles can be separated from the + * edge of the tileset image by some border offset and can be separated + * from one another by a gap distance. + */ +public class SwissArmyTileSet extends TileSet +{ + /** + * The full monty. Constructs a swiss army tile set object with the + * full panoply of parameters, specifying everything under the sun. + * Each row in the tileset image must contain tiles of the same width + * and height, but those values can vary from row to row. Each row can + * contain an arbitrary number of tiles. The tiles can be offset from + * the upper left of the image and can have a uniform horizontal and + * vertical distance between each tile (horizontal doesn't have to be + * the same as vertical but all horizontal distances must be the same, + * for example). + * + * @param imgmgr an image manager from which the tileset image can be + * loaded. + * @param imgPath the path to the tileset image. + * @param name the name of the tileset (optional, can be null). + * @param tsid the unique integer identifier of the tileset (optional, + * can be zero if the tileset is not to be loaded by id). + * @param tileCount an array containing the number of tiles in each + * row. + * @param rowWidth an array containing the width of the tiles in each + * row. + * @param rowHeight an array containing the height of the tiles in + * each row. + * @param offsetPos the offset to the upper left of the first tile. + * @param gapDist the number of pixels (x for horizontally and y for + * vertically) in between each tile in the tileset image. + */ + public SwissArmyTileSet ( + ImageManager imgmgr, String imgPath, String name, int tsid, + int tileCount[], int rowWidth[], int rowHeight[], + Point offsetPos, Point gapDist) + { + super(imgmgr, imgPath, name, tsid); + + // keep these around + _tileCount = tileCount; + _rowWidth = rowWidth; + _rowHeight = rowHeight; + _offsetPos = offsetPos; + _gapDist = gapDist; + + // compute our number of tiles + for (int i = 0; i < tileCount.length; i++) { + _numTiles += tileCount[i]; + } + } + + // documentation inherited + public int getTileCount () + { + return _numTiles; + } + + // documentation inherited + protected Image getTileImage (int tileId) + { + Image tsimg = getTilesetImage(); + if (tsimg == null) { + return null; + } + + // find the row number containing the sought-after tile + int ridx, tcount, ty, tx; + ridx = tcount = 0; + + // start tile image position at image start offset + tx = _offsetPos.x; + ty = _offsetPos.y; + + while ((tcount += _tileCount[ridx]) < tileId + 1) { + // increment tile image position by row height and gap distance + ty += (_rowHeight[ridx++] + _gapDist.y); + } + + // determine the horizontal index of this tile in the row + int xidx = tileId - (tcount - _tileCount[ridx]); + + // final image x-position is based on tile width and gap distance + tx += (xidx * (_rowWidth[ridx] + _gapDist.x)); + + // Log.info("Retrieving tile image [tileId=" + tileId + ", ridx=" + + // ridx + ", xidx=" + xidx + ", tx=" + tx + + // ", ty=" + ty + "]."); + + // crop the tile-sized image chunk from the full image + return _imgmgr.getImageCropped( + tsimg, tx, ty, _rowWidth[ridx], _rowHeight[ridx]); + } + + /** The number of tiles in each row. */ + protected int[] _tileCount; + + /** The number of tiles in the tileset. */ + protected int _numTiles; + + /** The width of the tiles in each row in pixels. */ + protected int[] _rowWidth; + + /** The height of the tiles in each row in pixels. */ + protected int[] _rowHeight; + + /** The offset distance (x, y) in pixels from the top-left of the + * image to the start of the first tile image. */ + protected Point _offsetPos = new Point(); + + /** The distance (x, y) in pixels between each tile in each row + * horizontally, and between each row of tiles vertically. */ + protected Point _gapDist = new Point(); +} diff --git a/src/java/com/threerings/media/tile/TileManager.java b/src/java/com/threerings/media/tile/TileManager.java index 63ce618ea..57aa17c85 100644 --- a/src/java/com/threerings/media/tile/TileManager.java +++ b/src/java/com/threerings/media/tile/TileManager.java @@ -1,5 +1,5 @@ // -// $Id: TileManager.java,v 1.19 2001/11/01 01:40:42 shaper Exp $ +// $Id: TileManager.java,v 1.20 2001/11/08 03:04:44 mdb Exp $ package com.threerings.media.tile; @@ -10,21 +10,70 @@ import java.util.ArrayList; import com.samskivert.util.HashIntMap; import com.threerings.media.Log; +import com.threerings.media.ImageManager; /** * The tile manager provides a simplified interface for retrieving and - * caching tiles. + * caching tiles. Tiles can be loaded in two different ways. An + * application can load a tileset by hand, specifying the path to the + * tileset image and all of the tileset metadata necessary for extracting + * the image tiles, or it can provide a tileset repository which loads up + * tilesets using whatever repository mechanism is implemented by the + * supplied repository. In the latter case, tilesets are loaded by a + * unique identifier. + * + *

Loading tilesets by hand is intended for things like toolbar icons + * or games with a single set of tiles (think Stratego, for example). + * Loading tilesets from a repository supports games with vast numbers of + * tiles to which more tiles may be added on the fly (think the tiles for + * an isometric-display graphical MUD). + * + *

When the tile manager is used to load tiles via the tileset + * repository, it caches the resulting tile instance so that they can be + * fetched again without rebuilding the tile image. Tilesets that are + * fetched by hand are not cached and it is assumed that the requesting + * application will cache the tile objects itself (probably by retaining + * references directly to the tile instances in which it is interested). + * The tile creation process is not hugely expensive, but does involve + * extracting the tile image from the larger tileset image. */ public class TileManager { /** - * Initializes the tile manager. - * - * @param tilesetrepo the tile set repository. + * Creates a tile manager and provides it with a reference to the + * image manager from which it will load its tileset images. */ - public TileManager (TileSetRepository tsrepo) + public TileManager (ImageManager imgmgr) { - _tsrepo = tsrepo; + // keep this guy around for later + _imgmgr = imgmgr; + } + + /** + * Loads up a tileset from the specified image with the specified + * metadata parameters. + */ + public TileSet loadTileSet ( + String imgPath, int count, int width, int height) + { + return new UniformTileSet(_imgmgr, imgPath, count, width, height); + } + + /** + * Sets the tileset repository that will be used by the tile manager + * when tiles are requested by tileset id. + */ + public void setTileSetRepository (TileSetRepository tsrepo) + { + _tsrepo = tsrepo; + } + + /** + * Returns the tileset repository currently in use. + */ + public TileSetRepository getTileSetRepository () + { + return _tsrepo; } /** @@ -39,6 +88,11 @@ public class TileManager public Tile getTile (int tsid, int tid) throws NoSuchTileSetException, NoSuchTileException { + // make sure we have a repository configured + if (_tsrepo == null) { + throw new NoSuchTileSetException(tsid); + } + // the fully unique tile id is the conjoined tile set and tile id int utid = (tsid << 16) | tid; @@ -61,13 +115,8 @@ public class TileManager return tile; } - /** - * Returns the tile set repository used by this tile manager. - */ - public TileSetRepository getTileSetRepository () - { - return _tsrepo; - } + /** The entity through which we load images. */ + protected ImageManager _imgmgr; /** Cache of tiles that have been requested thus far. */ protected HashIntMap _tiles = new HashIntMap(); diff --git a/src/java/com/threerings/media/tile/TileSet.java b/src/java/com/threerings/media/tile/TileSet.java index 749967e09..4089de465 100644 --- a/src/java/com/threerings/media/tile/TileSet.java +++ b/src/java/com/threerings/media/tile/TileSet.java @@ -1,5 +1,5 @@ // -// $Id: TileSet.java,v 1.18 2001/11/01 01:40:42 shaper Exp $ +// $Id: TileSet.java,v 1.19 2001/11/08 03:04:44 mdb Exp $ package com.threerings.media.tile; @@ -7,62 +7,45 @@ import java.awt.Image; import java.awt.Point; import java.awt.image.*; -import com.samskivert.util.HashIntMap; -import com.samskivert.util.StringUtil; - import com.threerings.media.Log; import com.threerings.media.ImageManager; /** - * A tile set stores information on a single logical set of tiles. It - * provides a clean interface for the {@link TileManager} to retrieve - * individual tiles from the tile set. + * A tileset stores information on a single logical set of tiles. It + * provides a clean interface for the {@link TileManager} or other + * entities to retrieve individual tiles from the tile set and + * encapsulates the potentially sophisticated process of extracting the + * tile image from a composite tileset image. * - *

Tiles are referenced by their tile id. The tile id is - * essentially the tile number, assuming the tile at the top-left of - * the image is tile id 0 and tiles are numbered left to right, top to - * bottom, in ascending order. + *

Tiles are referenced by their tile id. The tile id is essentially + * the tile number, assuming the tile at the top-left of the image is tile + * id zero and tiles are numbered, in ascending order, left to right, top + * to bottom. */ -public class TileSet implements Cloneable +public abstract class TileSet implements Cloneable { /** - * Constructs a tile set object with the given image manager as - * the source for retrieving tile images. + * Provides the basic information needed to load a tileset image to + * the tileset base class. + * + * @param imgmgr an image manager from which the tileset image can be + * loaded. + * @param imgPath the path to the tileset image. + * @param name the name of the tileset (optional, can be null). + * @param tsid the unique integer identifier of the tileset (optional, + * can be zero if the tileset is not to be loaded by id). */ - public TileSet ( - ImageManager imgmgr, int tsid, String name, String imgFile, - int tileCount[], int rowWidth[], int rowHeight[], - int numTiles, Point offsetPos, Point gapDist, - boolean isObjectSet, HashIntMap objects) + public TileSet (ImageManager imgmgr, String imgPath, + String name, int tsid) { _imgmgr = imgmgr; - _tsid = tsid; + _imgPath = imgPath; _name = name; - _imgFile = imgFile; - _tileCount = tileCount; - _rowWidth = rowWidth; - _rowHeight = rowHeight; - _numTiles = numTiles; - _offsetPos = offsetPos; - _gapDist = gapDist; - _isObjectSet = isObjectSet; - _objects = objects; + _tsid = tsid; } /** - * Returns a new tile set that is a clone of this tile set with - * the image file updated to reference the given file name. - */ - public TileSet clone (String imgFile) - throws CloneNotSupportedException - { - TileSet dup = (TileSet)clone(); - dup.setImageFile(imgFile); - return dup; - } - - /** - * Returns the tile set identifier. + * Returns the tileset identifier. */ public int getId () { @@ -70,7 +53,7 @@ public class TileSet implements Cloneable } /** - * Returns the tile set name. + * Returns the tileset name. */ public String getName () { @@ -78,43 +61,64 @@ public class TileSet implements Cloneable } /** - * Returns the number of tiles in the tile set. + * Returns the number of tiles in the tileset. */ - public int getNumTiles () + public abstract int getTileCount (); + + /** + * Sets the image file to be used as the source for the tile + * images produced by this tileset. + */ + public void setImageFile (String imgPath) { - return _numTiles; + _imgPath = imgPath; + _tilesetImg = null; } /** - * Returns the {@link Tile} object from this tile set - * corresponding to the specified tile id, or null if an error + * Returns a new tileset that is a clone of this tileset with the + * image file updated to reference the given file name. Useful for + * configuring a single tileset and then generating additional + * tilesets with new images with the same configuration. + */ + public TileSet clone (String imgPath) + throws CloneNotSupportedException + { + TileSet dup = (TileSet)clone(); + dup.setImageFile(imgPath); + return dup; + } + + /** + * Creates a @link Tile} object from this tileset corresponding to the + * specified tile id and returns that tile, or null if an error * occurred. * - * @param tid the tile identifier. + * @param tileId the tile identifier. Tile identifiers start with zero + * as the upper left tile and increase by one as the tiles move left + * to right and top to bottom over the source image. * * @return the tile object, or null if an error occurred. + * + * @exception NoSuchTileException thrown if the specified tile id is + * out of range for this tileset. */ - public Tile getTile (int tid) + public Tile getTile (int tileId) throws NoSuchTileException { - if (_imgmgr == null) { - Log.warning("No default image manager [tsid=" + _tsid + - ", tid=" + tid + "]."); - return null; - } - // bail if there's no such tile - if (tid < 0 || tid > (_numTiles - 1)) { - throw new NoSuchTileException(tid); + if (tileId < 0 || tileId > (getTileCount() - 1)) { + throw new NoSuchTileException(tileId); } // create and populate the tile object - Tile tile = createTile(tid); + Tile tile = createTile(tileId); // retrieve the tile image - tile.img = getTileImage(_imgmgr, tile.tid); + tile.img = getTileImage(tile.tid); if (tile.img == null) { Log.warning("Null tile image [tile=" + tile + "]."); + return null; } // populate the tile's dimensions @@ -129,60 +133,30 @@ public class TileSet implements Cloneable } /** - * Sets the image file to be used as the source for the tile - * images produced by this tile set. - */ - public void setImageFile (String imgFile) - { - _imgFile = imgFile; - _imgTiles = null; - } - - /** - * Return a string representation of the tileset information. - */ - public String toString () - { - StringBuffer buf = new StringBuffer(); - buf.append("[name=").append(_name); - buf.append(", file=").append(_imgFile); - buf.append(", tsid=").append(_tsid); - buf.append(", numtiles=").append(_numTiles); - return buf.append("]").toString(); - } - - /** - * Construct and return a new tile object for further population - * with tile-specific information. Derived classes can override - * this method to create their own sub-class of Tile. + * Construct and return a new tile object for further population with + * tile-specific information. Derived classes can override this method + * to create their own sub-class of {@link Tile}. * - * @param tid the tile id for the new tile. + * @param tileId the tile id for the new tile. * * @return the new tile object. */ - protected Tile createTile (int tid) + protected Tile createTile (int tileId) { - // construct an object tile if the tile set was specified as such - if (_isObjectSet) { - // default object dimensions to (1, 1) - int wid = 1, hei = 1; - - // retrieve object dimensions if known - if (_objects != null) { - int size[] = (int[])_objects.get(tid); - if (size != null) { - wid = size[0]; - hei = size[1]; - } - } - - return new ObjectTile(_tsid, tid, wid, hei); - } - // construct a basic tile - return new Tile(_tsid, tid); + return new Tile(_tsid, tileId); } + /** + * Returns the image corresponding to the specified tile within this + * tileset. + * + * @param tileId the index of the tile to be retrieved. + * + * @return the tile image. + */ + protected abstract Image getTileImage (int tileId); + /** * Populates the given tile object with its detailed tile * information. Derived classes can override this method to add @@ -197,91 +171,65 @@ public class TileSet implements Cloneable } /** - * Returns the image corresponding to the specified tile id within - * this tile set. + * Returns the tileset image (which is loaded if it has not yet been + * loaded). * - * @param imgmgr the image manager. - * @param tid the tile id. - * - * @return the tile image. + * @return the tileset image or null if an error occurred loading the + * image. */ - protected Image getTileImage (ImageManager imgmgr, int tid) + protected Image getTilesetImage () { - // load the full tile image if we don't already have it - if (_imgTiles == null) { - if ((_imgTiles = imgmgr.getImage(_imgFile)) == null) { - Log.warning("Failed to retrieve full tileset image " + - "[file=" + _imgFile + "]."); - return null; - } + // return it straight away if it's already loaded + if (_tilesetImg != null) { + return _tilesetImg; + } + + // load up the tileset image via the image manager + if ((_tilesetImg = _imgmgr.getImage(_imgPath)) == null) { + Log.warning("Failed to retrieve tileset image " + + "[tsid=" + _tsid + ", path=" + _imgPath + "]."); } - // find the row number containing the sought-after tile - int ridx, tcount, ty, tx; - ridx = tcount = 0; - - // start tile image position at image start offset - tx = _offsetPos.x; - ty = _offsetPos.y; - - while ((tcount += _tileCount[ridx]) < tid + 1) { - // increment tile image position by row height and gap distance - ty += (_rowHeight[ridx++] + _gapDist.y); - } - - // determine the horizontal index of this tile in the row - int xidx = tid - (tcount - _tileCount[ridx]); - - // final image x-position is based on tile width and gap distance - tx += (xidx * (_rowWidth[ridx] + _gapDist.x)); - - // Log.info("Retrieving tile image [tid=" + tid + ", ridx=" + - // ridx + ", xidx=" + xidx + ", tx=" + tx + - // ", ty=" + ty + "]."); - - // crop the tile-sized image chunk from the full image - return imgmgr.getImageCropped( - _imgTiles, tx, ty, _rowWidth[ridx], _rowHeight[ridx]); + return _tilesetImg; } + /** + * Generates a string representation of the tileset information. + */ + public String toString () + { + StringBuffer buf = new StringBuffer("["); + toString(buf); + return buf.append("]").toString(); + } + + /** + * Tileset derived classes should override this, calling + * super.toString(buf) and then appending additional + * information to the buffer. + */ + protected void toString (StringBuffer buf) + { + buf.append("name=").append(_name); + buf.append(", tsid=").append(_tsid); + buf.append(", path=").append(_imgPath); + buf.append(", tileCount=").append(getTileCount()); + } + + /** The default image manager for retrieving tile images. */ + protected ImageManager _imgmgr; + + /** The path to the file containing the tile images. */ + protected String _imgPath; + /** The tileset name. */ protected String _name; /** The tileset unique identifier. */ protected int _tsid; - /** The file containing the tile images. */ - protected String _imgFile; - - /** The width of the tiles in each row in pixels. */ - protected int[] _rowWidth; - - /** The height of the tiles in each row in pixels. */ - protected int[] _rowHeight; - - /** The number of tiles in each row. */ - protected int[] _tileCount; - - /** The number of tiles in the tileset. */ - protected int _numTiles; - - /** Whether this set produces object tiles. */ - protected boolean _isObjectSet = false; - - /** The offset distance (x, y) in pixels from the top-left of the - * image to the start of the first tile image. */ - protected Point _offsetPos = new Point(); - - /** The distance (x, y) in pixels between each tile in each row - * horizontally, and between each row of tiles vertically. */ - protected Point _gapDist = new Point(); - - /** Mapping of object tile ids to object dimensions. */ - protected HashIntMap _objects; - - /** The image containing all tile images for this set. */ - protected Image _imgTiles; - - /** The default image manager for retrieving tile images. */ - protected ImageManager _imgmgr; + /** The image containing all tile images for this set. This is private + * because it should be accessed via {@link #getTilesetImage} even by + * derived classes. */ + private Image _tilesetImg; } diff --git a/src/java/com/threerings/media/tile/UniformTileSet.java b/src/java/com/threerings/media/tile/UniformTileSet.java new file mode 100644 index 000000000..223df39fc --- /dev/null +++ b/src/java/com/threerings/media/tile/UniformTileSet.java @@ -0,0 +1,74 @@ +// +// $Id: UniformTileSet.java,v 1.1 2001/11/08 03:04:44 mdb Exp $ + +package com.threerings.media.tile; + +import java.awt.Image; +import com.threerings.media.Log; +import com.threerings.media.ImageManager; + +/** + * A uniform tileset is one that is composed of tiles that are all the + * same width and height and are arranged into rows, with each row having + * the same number of tiles except possibly the final row which can + * contain the same as or less than the number of tiles contained by the + * previous rows. + */ +public class UniformTileSet extends TileSet +{ + /** + * Constructs a tile set object with the specified tileset + * configuration parameters. + * + * @param imgmgr the image manager via which to load the tileset + * image. + * @param imgPath the path to supply to the image manager when loading + * the tile (which will fetch the image using the resource manager). + * @param count the number of tiles in the tile image. + * @param width the width of each tile, in pixels. + * @param height the height of each tile, in pixels. + */ + public UniformTileSet (ImageManager imgmgr, String imgPath, + int count, int width, int height) + { + super(imgmgr, imgPath, null, 0); + + // keep these for later + _count = count; + _width = width; + _height = height; + } + + // documentation inherited + public int getTileCount () + { + return _count; + } + + // documentation inherited + protected Image getTileImage (int tileId) + { + Image tsimg = getTilesetImage(); + if (tsimg == null) { + return null; + } + + // figure out from whence to crop the tile + int tilesPerRow = tsimg.getWidth(null) / _width; + int row = tilesPerRow / tileId; + int col = tilesPerRow % tileId; + + // crop the tile-sized image chunk from the full image + return _imgmgr.getImageCropped( + tsimg, col * _width, row * _height, _width, _height); + } + + /** The total number of tiles in this tileset. */ + protected int _count; + + /** The width (in pixels) of the tiles in this tileset. */ + protected int _width; + + /** The height (in pixels) of the tiles in this tileset. */ + protected int _height; +} diff --git a/src/java/com/threerings/media/tile/XMLTileSetParser.java b/src/java/com/threerings/media/tile/XMLTileSetParser.java index 111bb3b58..11e77c23c 100644 --- a/src/java/com/threerings/media/tile/XMLTileSetParser.java +++ b/src/java/com/threerings/media/tile/XMLTileSetParser.java @@ -1,5 +1,5 @@ // -// $Id: XMLTileSetParser.java,v 1.20 2001/11/01 01:40:42 shaper Exp $ +// $Id: XMLTileSetParser.java,v 1.21 2001/11/08 03:04:44 mdb Exp $ package com.threerings.media.tile; @@ -78,11 +78,6 @@ public class XMLTileSetParser } else if (qName.equals("tilecount")) { _info.tileCount = StringUtil.parseIntArray(data); - // calculate the total number of tiles in the tileset - for (int ii = 0; ii < _info.tileCount.length; ii++) { - _info.numTiles += _info.tileCount[ii]; - } - } else if (qName.equals("offsetpos")) { parsePoint(data, _info.offsetPos); @@ -119,7 +114,13 @@ public class XMLTileSetParser // documentation inherited protected InputStream getInputStream (String fname) throws IOException { - return ConfigUtil.getStream(fname); + InputStream is = ConfigUtil.getStream(fname); + if (is == null) { + String errmsg = "Can't load tileset description file from " + + "classpath [path=" + fname + "]."; + throw new FileNotFoundException(errmsg); + } + return is; } /** @@ -129,10 +130,18 @@ public class XMLTileSetParser */ protected TileSet createTileSet () { - return new TileSet( - _imgmgr, _info.tsid, _info.name, _info.imgFile, _info.tileCount, - _info.rowWidth, _info.rowHeight, _info.numTiles, _info.offsetPos, - _info.gapDist, _info.isObjectSet, _info.objects); + if (_info.isObjectSet) { + return new ObjectTileSet( + _imgmgr, _info.imgFile, _info.name, _info.tsid, + _info.tileCount, _info.rowWidth, _info.rowHeight, + _info.offsetPos, _info.gapDist, _info.objects); + + } else { + return new SwissArmyTileSet( + _imgmgr, _info.imgFile, _info.name, _info.tsid, + _info.tileCount, _info.rowWidth, _info.rowHeight, + _info.offsetPos, _info.gapDist); + } } /** @@ -160,7 +169,6 @@ public class XMLTileSetParser public String name; public String imgFile; public int tileCount[], rowWidth[], rowHeight[]; - public int numTiles; public Point offsetPos = new Point(); public Point gapDist = new Point(); public boolean isObjectSet; diff --git a/src/java/com/threerings/miso/tile/BaseTileSet.java b/src/java/com/threerings/miso/tile/BaseTileSet.java index ad1a2071b..65858b6a1 100644 --- a/src/java/com/threerings/miso/tile/BaseTileSet.java +++ b/src/java/com/threerings/miso/tile/BaseTileSet.java @@ -1,5 +1,5 @@ // -// $Id: BaseTileSet.java,v 1.4 2001/11/01 01:40:42 shaper Exp $ +// $Id: BaseTileSet.java,v 1.5 2001/11/08 03:04:45 mdb Exp $ package com.threerings.miso.tile; @@ -13,23 +13,31 @@ import com.threerings.media.tile.*; import com.threerings.miso.scene.MisoScene; /** - * The miso tile set extends the base tile set to add support for tile - * passability. Passability is used to determine whether {@link - * com.threerings.miso.scene.Traverser} objects can traverse a - * particular tile in a {@link com.threerings.miso.scene.MisoScene}. + * The miso tile set extends the swiss army tile set to add support for + * tile passability. Passability is used to determine whether {@link + * com.threerings.miso.scene.Traverser} objects can traverse a particular + * tile in a {@link MisoScene}. */ -public class MisoTileSet extends TileSet +public class MisoTileSet extends SwissArmyTileSet { + /** + * Constructs a Miso tileset with the swiss army tile set + * configuration information and additional information about tile + * passability. + * + * @param layer the layer to which this tileset is assigned. + * @param passable info on each tile indicating whether or not the + * tile is passable (can be walked on by sprites). + * + * @see SwissArmyTileSet#SwissArmyTileSet + */ public MisoTileSet ( - ImageManager imgmgr, int tsid, String name, String imgFile, - int tileCount[], int rowWidth[], int rowHeight[], - int numTiles, Point offsetPos, Point gapDist, - boolean isObjectSet, HashIntMap objects, - int layer, int passable[]) + ImageManager imgmgr, String imgFile, String name, int tsid, + int[] tileCount, int[] rowWidth, int[] rowHeight, + Point offsetPos, Point gapDist, int layer, int[] passable) { - super(imgmgr, tsid, name, imgFile, tileCount, rowWidth, - rowHeight, numTiles, offsetPos, gapDist, - isObjectSet, objects); + super(imgmgr, imgFile, name, tsid, tileCount, + rowWidth, rowHeight, offsetPos, gapDist); _layer = layer; _passable = passable; diff --git a/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java b/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java index 34ecc66d6..427b38d2b 100644 --- a/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java +++ b/src/java/com/threerings/miso/tile/tools/xml/XMLMisoTileSetParser.java @@ -1,5 +1,5 @@ // -// $Id: XMLMisoTileSetParser.java,v 1.5 2001/11/01 01:40:42 shaper Exp $ +// $Id: XMLMisoTileSetParser.java,v 1.6 2001/11/08 03:04:45 mdb Exp $ package com.threerings.miso.tile; @@ -45,6 +45,7 @@ public class XMLMisoTileSetParser extends XMLTileSetParser if (qName.equals("passable")) { _passable = StringUtil.parseIntArray(data); + } else if (qName.equals("tileset")) { _passable = null; _layer = -1; @@ -54,13 +55,24 @@ public class XMLMisoTileSetParser extends XMLTileSetParser // documentation inherited protected TileSet createTileSet () { - return new MisoTileSet( - _imgmgr, _info.tsid, _info.name, _info.imgFile, _info.tileCount, - _info.rowWidth, _info.rowHeight, _info.numTiles, _info.offsetPos, - _info.gapDist, _info.isObjectSet, _info.objects, - _layer, _passable); + if (_info.isObjectSet) { + return new ObjectTileSet( + _imgmgr, _info.imgFile, _info.name, _info.tsid, + _info.tileCount, _info.rowWidth, _info.rowHeight, + _info.offsetPos, _info.gapDist, _info.objects); + + } else { + return new MisoTileSet( + _imgmgr, _info.imgFile, _info.name, _info.tsid, + _info.tileCount, _info.rowWidth, _info.rowHeight, + _info.offsetPos, _info.gapDist, _layer, _passable); + } } - protected int _passable[]; + /** Info on whether or not the tiles are passable (can be walked on by + * sprites). */ + protected int[] _passable; + + /** The layer to which the tiles are assigned. */ protected int _layer = -1; } diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java b/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java index 43f5c2c69..2016b72ad 100644 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java +++ b/src/java/com/threerings/miso/tools/xml/XMLSceneParser.java @@ -1,5 +1,5 @@ // -// $Id: XMLSceneParser.java,v 1.23 2001/10/25 16:36:43 shaper Exp $ +// $Id: XMLSceneParser.java,v 1.24 2001/11/08 03:04:44 mdb Exp $ package com.threerings.miso.scene.xml; @@ -58,7 +58,7 @@ public class XMLSceneParser extends SimpleParser Log.warning( "Unrecognized scene file format version, will attempt " + "to continue parsing file but your mileage may vary " + - "[fname=" + _fname + ", version=" + version + + "[version=" + version + ", known_version=" + XMLSceneVersion.VERSION + "]."); } @@ -268,14 +268,28 @@ public class XMLSceneParser extends SimpleParser * Parse the specified XML file and return a miso scene object with * the data contained therein. * - * @param fname the file name. + * @param path the full path to the scene description file. * * @return the scene object, or null if an error occurred. */ - public EditableMisoScene loadScene (String fname) throws IOException + public EditableMisoScene loadScene (String path) throws IOException + { + return loadScene(getInputStream(path)); + } + + /** + * Parse the specified XML file and return a miso scene object with + * the data contained therein. + * + * @param stream the input stream from which the XML scene description + * can be read. + * + * @return the scene object, or null if an error occurred. + */ + public EditableMisoScene loadScene (InputStream stream) throws IOException { _info = new SceneInfo(); - parseFile(fname); + parseStream(stream); // place shadow tiles for any objects in the scene _info.scene.generateAllObjectShadows(); // return the final scene object diff --git a/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java b/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java index c8dca4844..fef4916d1 100644 --- a/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java +++ b/src/java/com/threerings/miso/tools/xml/XMLSceneRepository.java @@ -1,10 +1,11 @@ // -// $Id: XMLSceneRepository.java,v 1.13 2001/11/02 02:52:16 shaper Exp $ +// $Id: XMLSceneRepository.java,v 1.14 2001/11/08 03:04:44 mdb Exp $ package com.threerings.miso.scene.xml; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import com.samskivert.util.Config; @@ -48,27 +49,31 @@ public class XMLSceneRepository } /** - * Returns the path to the scene root directory. + * Loads and returns a miso scene object for the scene described in + * the specified XML file. + * + * @param path the full pathname to the file. + * + * @return the scene object. */ - public String getScenePath () + public EditableMisoScene loadScene (String path) throws IOException { - return _root + File.separator + _sceneRoot + File.separator; + Log.info("Loading scene [path=" + path + "]."); + return _parser.loadScene(path); } /** * Loads and returns a miso scene object for the scene described in - * the specified XML file. The filename should be relative to the - * scene root directory. + * the XML file available via the supplied input stream. + * + * @param path the full pathname to the file. * - * @param fname the full pathname to the file. * @return the scene object. */ - public EditableMisoScene loadScene (String fname) throws IOException + public EditableMisoScene loadScene (InputStream stream) + throws IOException { - String path = getScenePath() + fname; - Log.info("Loading scene [path=" + path + "]."); - - return _parser.loadScene(path); + return _parser.loadScene(stream); } /** @@ -77,13 +82,11 @@ public class XMLSceneRepository * the scene root directory. * * @param scene the scene to save. - * @param fname the file to write the scene to. + * @param path the full path of the file to write the scene to. */ - public void saveScene (MisoScene scene, String fname) throws IOException + public void saveScene (MisoScene scene, String path) throws IOException { - String path = getScenePath() + fname; Log.info("Saving scene [path=" + path + "]."); - _writer.saveScene(scene, path); } diff --git a/src/java/com/threerings/miso/util/MisoUtil.java b/src/java/com/threerings/miso/util/MisoUtil.java index 6917dc318..202ba35df 100644 --- a/src/java/com/threerings/miso/util/MisoUtil.java +++ b/src/java/com/threerings/miso/util/MisoUtil.java @@ -1,5 +1,5 @@ // -// $Id: MisoUtil.java,v 1.13 2001/11/02 02:52:16 shaper Exp $ +// $Id: MisoUtil.java,v 1.14 2001/11/08 03:04:45 mdb Exp $ package com.threerings.miso.util; @@ -12,8 +12,6 @@ import com.samskivert.util.*; import com.threerings.cast.CharacterManager; -import com.threerings.resource.ResourceManager; - import com.threerings.media.ImageManager; import com.threerings.media.tile.*; @@ -122,19 +120,6 @@ public class MisoUtil } } - /** - * Creates an ImageManager object. - * - * @param frame the root frame to which images will be rendered. - * - * @return the new tile manager object or null if an error occurred. - */ - public static ImageManager createImageManager (Frame frame) - { - ResourceManager rmgr = createResourceManager(); - return new ImageManager(rmgr, frame); - } - /** * Creates a TileManager object. * @@ -147,20 +132,11 @@ public class MisoUtil Config config, ImageManager imgmgr) { TileSetRepository tsrepo = createTileSetRepository(config, imgmgr); - TileManager tilemgr = new TileManager(tsrepo); + TileManager tilemgr = new TileManager(imgmgr); + tilemgr.setTileSetRepository(tsrepo); return tilemgr; } - /** - * Creates a ResourceManager object. - * - * @return the new resource manager object or null if an error occurred. - */ - protected static ResourceManager createResourceManager () - { - return new ResourceManager("rsrc"); - } - /** * Creates a TileSetRepository object, reading the * class name to instantiate from the config object.