Added row-based tile width, image offset, and image row/column gap

distance parameters to tile sets.  Removed constant tile width/height
references except, for now, for isometric tile rendering which still
requires tiles that are 32x16.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@130 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-07-28 01:31:51 +00:00
parent e9ab0b343b
commit 92e46c6eae
8 changed files with 175 additions and 118 deletions
+5 -12
View File
@@ -1,5 +1,5 @@
//
// $Id: Tile.java,v 1.7 2001/07/23 18:52:51 shaper Exp $
// $Id: Tile.java,v 1.8 2001/07/28 01:31:51 shaper Exp $
package com.threerings.miso.tile;
@@ -19,18 +19,11 @@ public class Tile
/** The tile identifier within the set. */
public short tid;
/** The tile width in pixels. */
public short width;
/** The tile height in pixels. */
public short height; // the tile height in pixels
/** The height and width of a tile image in pixels. */
public static final int HEIGHT = 16;
public static final int WIDTH = 32;
/** Halved tile width in pixels for use in common calculations. */
public static final int HALF_HEIGHT = HEIGHT / 2;
/** Halved tile height in pixels for use in common calculations. */
public static final int HALF_WIDTH = WIDTH / 2;
public short height;
/**
* Construct a new tile with the specified identifiers. Intended
@@ -1,5 +1,5 @@
//
// $Id: TileManager.java,v 1.11 2001/07/23 22:31:48 shaper Exp $
// $Id: TileManager.java,v 1.12 2001/07/28 01:31:51 shaper Exp $
package com.threerings.miso.tile;
@@ -46,7 +46,9 @@ public class TileManager
if ((tile.img = _tilesetmgr.getTileImage(tsid, tid)) == null) {
Log.warning("Null tile image [tsid="+tsid+", tid="+tid+"].");
}
tile.height = (short)((BufferedImage)tile.img).getHeight();
BufferedImage bimg = (BufferedImage)tile.img;
tile.height = (short)bimg.getHeight();
tile.width = (short)bimg.getWidth();
_tiles.put(utid, tile);
+48 -16
View File
@@ -1,9 +1,10 @@
//
// $Id: TileSet.java,v 1.10 2001/07/23 18:52:51 shaper Exp $
// $Id: TileSet.java,v 1.11 2001/07/28 01:31:51 shaper Exp $
package com.threerings.miso.tile;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.*;
import com.samskivert.util.StringUtil;
@@ -15,11 +16,6 @@ import com.threerings.media.ImageManager;
* provides a clean interface for the TileManager to retrieve
* individual tile images from a particular tile in the tileset.
*
* <p> The width of each tile in every tileset is a constant
* <code>Tile.WIDTH</code> in pixels. The tile count in each row can
* vary. The height of the tiles in each row can also vary. This
* information is obtained from the config object.
*
* <p> Tiles are retrieved from the tile set by the TileManager, and
* are referenced by their tile id (essentially the tile number,
* assuming the tile at the top-left of the image is tile id 0 and
@@ -32,13 +28,17 @@ public class TileSet
* Construct a new TileSet object with the given parameters.
*/
public TileSet (String name, int tsid, String imgFile,
int[] rowHeight, int[] tileCount)
int[] rowWidth, int[] rowHeight, int[] tileCount,
Point offsetPos, Point gapDist)
{
_name = name;
_tsid = tsid;
_imgFile = imgFile;
_rowWidth = rowWidth;
_rowHeight = rowHeight;
_tileCount = tileCount;
_offsetPos = offsetPos;
_gapDist = gapDist;
// determine the total number of tiles in the set
for (int ii = 0; ii < _tileCount.length; ii++)
@@ -86,22 +86,30 @@ public class TileSet
// find the row number containing the sought-after tile
int ridx, tcount, ty, tx;
ridx = tcount = ty = tx = 0;
ridx = tcount = 0;
// start tile image position at image start offset
tx = _offsetPos.x;
ty = _offsetPos.y;
while ((tcount += _tileCount[ridx]) < tid + 1) {
ty += _rowHeight[ridx++];
// 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
// determine the horizontal index of this tile in the row
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 + "].");
// 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 imgr.getImageCropped(_imgTiles, tx, ty,
Tile.WIDTH, _rowHeight[ridx]);
return imgr.getImageCropped(
_imgTiles, tx, ty, _rowWidth[ridx], _rowHeight[ridx]);
}
/**
@@ -116,12 +124,21 @@ public class TileSet
buf.append(", tsid=").append(_tsid);
buf.append(", numtiles=").append(_numTiles);
buf.append(", rowwidth=");
StringUtil.toString(buf, _rowWidth);
buf.append(", rowheight=");
StringUtil.toString(buf, _rowHeight);
buf.append(", tilecount=");
StringUtil.toString(buf, _tileCount);
buf.append(", offsetpos=(").append(_offsetPos.x);
buf.append(", ").append(_offsetPos.y).append(")");
buf.append(", gapdist=(").append(_gapDist.x);
buf.append(", ").append(_gapDist.y).append(")");
return buf.append("]").toString();
}
@@ -134,12 +151,27 @@ public class TileSet
/** The tileset unique identifier. */
protected int _tsid;
/** The width of the tiles in each row in pixels. */
protected int _rowWidth[];
/** The height of each row in pixels. */
protected int _rowHeight[];
/** The number of tiles in each row. */
protected int _tileCount[];
/**
* 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;
/**
* The distance (x, y) in pixels between each tile in each row
* horizontally, and between each row of tiles vertically.
*/
protected Point _gapDist;
/** The total number of tiles. */
protected int _numTiles;
@@ -1,8 +1,9 @@
//
// $Id: XMLTileSetParser.java,v 1.7 2001/07/24 22:52:02 shaper Exp $
// $Id: XMLTileSetParser.java,v 1.8 2001/07/28 01:31:51 shaper Exp $
package com.threerings.miso.tile;
import java.awt.Point;
import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
@@ -34,9 +35,14 @@ public class XMLTileSetParser extends DefaultHandler
// construct the tileset object on tag close
if (qName.equals("tileset")) {
TileSet tset = new TileSet(
_tsName, _tsTsid, _tsImgFile, _tsRowHeight, _tsTileCount);
_tsName, _tsTsid, _tsImgFile, _tsRowWidth, _tsRowHeight,
_tsTileCount, _tsOffsetPos, _tsGapDist);
_tilesets.add(tset);
}
// prepare to read another tileset object
init();
}
// note that we're not within a tag to avoid considering any
// characters during this quiescent time
@@ -66,12 +72,21 @@ public class XMLTileSetParser extends DefaultHandler
} else if (_tag.equals("imagefile")) {
_tsImgFile = str;
} else if (_tag.equals("rowwidth")) {
_tsRowWidth = StringUtil.parseIntArray(str);
} else if (_tag.equals("rowheight")) {
_tsRowHeight = StringUtil.parseIntArray(str);
} else if (_tag.equals("tilecount")) {
_tsTileCount = StringUtil.parseIntArray(str);
}
} else if (_tag.equals("offsetpos")) {
getPoint(str, _tsOffsetPos);
} else if (_tag.equals("gapdist")) {
getPoint(str, _tsGapDist);
}
}
public ArrayList loadTileSets (String fname) throws IOException
@@ -83,6 +98,9 @@ public class XMLTileSetParser extends DefaultHandler
return _tilesets;
}
// prepare to read a new tileset
init();
// read all tileset descriptions from the XML input stream
XMLUtil.parse(this, tis);
@@ -96,6 +114,30 @@ public class XMLTileSetParser extends DefaultHandler
}
}
/**
* Initialize internal member data used to gather tileset
* information during parsing.
*/
protected void init ()
{
_tsOffsetPos = new Point();
_tsGapDist = new Point();
}
/**
* Converts a string containing values as (x, y) into the
* corresponding integer values and populates the given point
* object.
*
* @param str the point values in string format.
* @param point the point object to populate.
*/
protected void getPoint (String str, Point point)
{
int vals[] = StringUtil.parseIntArray(str);
point.setLocation(vals[0], vals[1]);
}
/** The XML element tag currently being processed. */
protected String _tag;
@@ -106,5 +148,6 @@ public class XMLTileSetParser extends DefaultHandler
protected String _tsName;
protected int _tsTsid;
protected String _tsImgFile;
protected int[] _tsRowHeight, _tsTileCount;
protected int[] _tsRowWidth, _tsRowHeight, _tsTileCount;
protected Point _tsOffsetPos, _tsGapDist;
}