From b7d43b786f24dcd9d089ac3ba3a73e8685d44557 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 17 Aug 2004 00:48:40 +0000 Subject: [PATCH] Pulled tile calculations out into a handy utility method. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3082 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/geom/GeomUtil.java | 25 ++++++++++++++++++- .../threerings/media/tile/UniformTileSet.java | 20 ++++----------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/java/com/threerings/geom/GeomUtil.java b/src/java/com/threerings/geom/GeomUtil.java index dec1b3431..02dc39db9 100644 --- a/src/java/com/threerings/geom/GeomUtil.java +++ b/src/java/com/threerings/geom/GeomUtil.java @@ -1,5 +1,5 @@ // -// $Id: GeomUtil.java,v 1.7 2004/02/25 14:42:30 mdb Exp $ +// $Id: GeomUtil.java,v 1.8 2004/08/17 00:48:40 mdb Exp $ package com.threerings.geom; @@ -207,4 +207,27 @@ public class GeomUtil } return source; } + + /** + * Returns the rectangle containing the specified tile in the supplied + * larger rectangle. Tiles go from left to right, top to bottom. + */ + public static Rectangle getTile ( + int width, int height, int tileWidth, int tileHeight, int tileIndex) + { + // figure out from whence to crop the tile + int tilesPerRow = width / tileWidth; + + // if we got a bogus region, return bogus tile bounds + if (tilesPerRow == 0) { + return new Rectangle(0, 0, width, height); + } + + int row = tileIndex / tilesPerRow; + int col = tileIndex % tilesPerRow; + + // crop the tile-sized image chunk from the full image + return new Rectangle( + tileWidth*col, tileHeight*row, tileWidth, tileHeight); + } } diff --git a/src/java/com/threerings/media/tile/UniformTileSet.java b/src/java/com/threerings/media/tile/UniformTileSet.java index 1c0170f57..c4388d7e8 100644 --- a/src/java/com/threerings/media/tile/UniformTileSet.java +++ b/src/java/com/threerings/media/tile/UniformTileSet.java @@ -1,11 +1,13 @@ // -// $Id: UniformTileSet.java,v 1.14 2003/05/13 21:33:58 ray Exp $ +// $Id: UniformTileSet.java,v 1.15 2004/08/17 00:48:40 mdb Exp $ package com.threerings.media.tile; import java.awt.Rectangle; import java.awt.image.BufferedImage; +import com.threerings.geom.GeomUtil; + /** * 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 @@ -60,20 +62,8 @@ public class UniformTileSet extends TileSet protected Rectangle computeTileBounds (int tileIndex) { BufferedImage tsimg = getRawTileSetImage(); - - // figure out from whence to crop the tile - int tilesPerRow = tsimg.getWidth() / _width; - - // if we got a bogus image, return bogus tile bounds - if (tilesPerRow == 0) { - return new Rectangle(0, 0, tsimg.getWidth(), tsimg.getHeight()); - } - - int row = tileIndex / tilesPerRow; - int col = tileIndex % tilesPerRow; - - // crop the tile-sized image chunk from the full image - return new Rectangle(_width*col, _height*row, _width, _height); + return GeomUtil.getTile(tsimg.getWidth(), tsimg.getHeight(), + _width, _height, tileIndex); } // documentation inherited