diff --git a/src/java/com/threerings/media/tile/TileManager.java b/src/java/com/threerings/media/tile/TileManager.java index fd8e0530b..40810a1ec 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.2 2001/07/14 00:21:24 shaper Exp $ +// $Id: TileManager.java,v 1.3 2001/07/16 18:59:31 shaper Exp $ package com.threerings.cocktail.miso.tile; @@ -20,42 +20,49 @@ public class TileManager { _tsmgr = tsmgr; } - - public Tile getTile (String setName, String tileName) + + public int getNumTilesInSet (int tsid) { - return new Tile(0, 0); + TileSet tset = _tsmgr.getTileSet(tsid); + if (tset == null) return -1; + return tset.getNumTiles(); } - public Tile getTile (short tsid, short tid) + public String[] getTileSetNames () + { + return _tsmgr.getTileSetNames(); + } + + public Tile getTile (int tsid, int tid) { // the fully unique tile id is the conjoined tile set and tile id int utid = tsid << 16 | tid; // look the tile up in our hash Tile tile = (Tile) _tiles.get(utid); - if (tile == null) { - - // retrieve the tileset containing the tile - TileSet tset = _tsmgr.getTileSet(tsid); - Log.info("Retrieved tileset [tsid=" + tsid + ", tid=" + - tid + ", tset=" + tset + "]."); - - // retrieve the tile image from the tileset - tile = new Tile(tsid, tid); - tile.img = tset.getTileImage(tid); - - _tiles.put(utid, tile); + if (tile != null) { +// Log.info("Retrieved tile from cache [tsid=" + tsid + +// ", tid=" + tid + "]."); + return tile; } + // retrieve the tileset containing the tile + TileSet tset = _tsmgr.getTileSet(tsid); + + // retrieve the tile image from the tileset + tile = new Tile(tsid, tid); + tile.img = tset.getTileImage(tid); + + _tiles.put(utid, tile); + + Log.info("Loaded tile into cache [tsid="+tsid+", tid="+tid+"]."); + return tile; } // mapping from (tsid << 16 | tid) to tile objects protected IntMap _tiles = new IntMap(); - // mapping from tileset ids to tileset objects - protected IntMap _tilesets = new IntMap(); - // our tile set manager protected TileSetManager _tsmgr; } diff --git a/src/java/com/threerings/media/tile/TileSet.java b/src/java/com/threerings/media/tile/TileSet.java index 9eefddfd3..544ca4043 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.3 2001/07/16 00:45:07 shaper Exp $ +// $Id: TileSet.java,v 1.4 2001/07/16 18:59:31 shaper Exp $ package com.threerings.cocktail.miso.tile; @@ -44,6 +44,25 @@ public class TileSet // get the row height and tile count for each row _rowHeight = config.getValue(PREFIX + CONF_ROWHEIGHT, (int[])null); _tileCount = config.getValue(PREFIX + CONF_TILECOUNT, (int[])null); + + // determine the total number of tiles in the set + for (int ii = 0; ii < _tileCount.length; ii++) + _numTiles += _tileCount[ii]; + } + + public int getId () + { + return _tsid; + } + + public String getName () + { + return _name; + } + + public int getNumTiles () + { + return _numTiles; } /** @@ -64,7 +83,7 @@ public class TileSet // find the row number containing the sought-after tile int ridx, tcount, ty, tx; ridx = tcount = ty = tx = 0; - while ((tcount += _tileCount[ridx]) < tid) { + while ((tcount += _tileCount[ridx]) < tid + 1) { ty += _rowHeight[ridx++]; } @@ -72,9 +91,9 @@ public class TileSet int xidx = tid - (tcount - _tileCount[ridx]); tx = Tile.WIDTH * xidx; - Log.info("Retrieving tile image [tid=" + tid + ", ridx=" + - ridx + ", xidx=" + xidx + ", tx=" + tx + - ", ty=" + ty + "]."); +// 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 ImageManager.getImageCropped(_imgTiles, tx, ty, @@ -91,6 +110,7 @@ public class TileSet buf.append("[name=").append(_name); buf.append(", file=").append(_file); buf.append(", tsid=").append(_tsid); + buf.append(", numtiles=").append(_numTiles); buf.append(", rowheight={"); for (int ii = 0; ii < _rowHeight.length; ii++) { @@ -119,6 +139,7 @@ public class TileSet protected int _tsid; // the tileset unique identifier protected int _rowHeight[]; // the height of each row in pixels protected int _tileCount[]; // the number of tiles in each row + protected int _numTiles; // the total number of tiles protected Image _imgTiles; } diff --git a/src/java/com/threerings/media/tile/TileSetManager.java b/src/java/com/threerings/media/tile/TileSetManager.java index 85e323f44..6ac04fb30 100644 --- a/src/java/com/threerings/media/tile/TileSetManager.java +++ b/src/java/com/threerings/media/tile/TileSetManager.java @@ -1,9 +1,27 @@ // -// $Id: TileSetManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $ +// $Id: TileSetManager.java,v 1.2 2001/07/16 18:59:31 shaper Exp $ package com.threerings.cocktail.miso.tile; public interface TileSetManager { + /** + * Return the tileset object corresponding to the specified tileset id. + */ public TileSet getTileSet (int tsid); + + /** + * Return a String array of all tileset names. + */ + public String[] getTileSetNames (); + + /** + * Return the tileset id associated with the named tileset. + */ + public int getTileSetId (String name); + + /** + * Return the total number of tilesets. + */ + public int getNumTileSets (); } diff --git a/src/java/com/threerings/media/tile/TileSetManagerImpl.java b/src/java/com/threerings/media/tile/TileSetManagerImpl.java new file mode 100644 index 000000000..b98102966 --- /dev/null +++ b/src/java/com/threerings/media/tile/TileSetManagerImpl.java @@ -0,0 +1,59 @@ +// +// $Id: TileSetManagerImpl.java,v 1.1 2001/07/16 18:59:31 shaper Exp $ + +package com.threerings.cocktail.miso.tile; + +import com.samskivert.util.Config; +import com.samskivert.util.IntMap; + +import java.util.Enumeration; + +public class TileSetManagerImpl implements TileSetManager +{ + public TileSetManagerImpl (Config config) + { + _config = config; + } + + public TileSet getTileSet (int tsid) + { + TileSet tset = (TileSet)_tilesets.get(tsid); + if (tset != null) return tset; + + _tilesets.put(tsid, tset = new TileSet(_config, tsid)); + return tset; + } + + public String[] getTileSetNames () + { + int size = _tilesets.size(); + if (size == 0) return null; + + String[] names = new String[size]; + Enumeration sets = _tilesets.elements(); + for (int ii = 0; ii < size; ii++) { + names[ii] = ((TileSet)sets.nextElement()).getName(); + } + + return names; + } + + public int getTileSetId (String name) + { + Enumeration sets = _tilesets.elements(); + while (sets.hasMoreElements()) { + TileSet tset = (TileSet)sets.nextElement(); + if (tset.getName().equals(name)) return tset.getId(); + } + + return -1; + } + + public int getNumTileSets () + { + return _tilesets.size(); + } + + protected Config _config; + protected IntMap _tilesets = new IntMap(); +} diff --git a/src/java/com/threerings/miso/tile/CompiledTileSetManager.java b/src/java/com/threerings/miso/tile/CompiledTileSetManager.java index 9ca56a458..677bd4419 100644 --- a/src/java/com/threerings/miso/tile/CompiledTileSetManager.java +++ b/src/java/com/threerings/miso/tile/CompiledTileSetManager.java @@ -1,13 +1,14 @@ // -// $Id: CompiledTileSetManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $ +// $Id: CompiledTileSetManager.java,v 1.2 2001/07/16 18:59:31 shaper Exp $ package com.threerings.cocktail.miso.tile; -public class CompiledTileSetManager implements TileSetManager +import com.samskivert.util.Config; + +public class CompiledTileSetManager extends TileSetManagerImpl { - public TileSet getTileSet (int tsid) + public CompiledTileSetManager (Config config) { - // TBD - return null; + super(config); } } diff --git a/src/java/com/threerings/miso/tile/EditableTileSetManager.java b/src/java/com/threerings/miso/tile/EditableTileSetManager.java index c6cb2c817..8eeec951d 100644 --- a/src/java/com/threerings/miso/tile/EditableTileSetManager.java +++ b/src/java/com/threerings/miso/tile/EditableTileSetManager.java @@ -1,27 +1,14 @@ // -// $Id: EditableTileSetManager.java,v 1.1 2001/07/14 00:02:47 shaper Exp $ +// $Id: EditableTileSetManager.java,v 1.2 2001/07/16 18:59:31 shaper Exp $ package com.threerings.cocktail.miso.tile; import com.samskivert.util.Config; -import com.samskivert.util.IntMap; -public class EditableTileSetManager implements TileSetManager +public class EditableTileSetManager extends TileSetManagerImpl { public EditableTileSetManager (Config config) { - _config = config; + super(config); } - - public TileSet getTileSet (int tsid) - { - TileSet tset = (TileSet)_tilesets.get(tsid); - if (tset != null) return tset; - - _tilesets.put(tsid, tset = new TileSet(_config, tsid)); - return tset; - } - - protected Config _config; - protected IntMap _tilesets = new IntMap(); }