Initial working version of many miso classes. The beginnings of some
of our tile- and scene-related machinations. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@44 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// $Id: Tile.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
/**
|
||||
* A tile represents a single square in a single layer in a scene.
|
||||
*/
|
||||
public class Tile
|
||||
{
|
||||
public Image img; // the tile image
|
||||
public short tsid; // the tile set identifier
|
||||
public short tid; // the tile identifier within the set
|
||||
|
||||
// height and width of a tile image in pixels
|
||||
public static final int HEIGHT = 16;
|
||||
public static final int WIDTH = 32;
|
||||
|
||||
// halved values of tile width/height in pixels for use in common
|
||||
// tile-dimension-related calculations
|
||||
public static final int HALF_HEIGHT = HEIGHT / 2;
|
||||
public static final int HALF_WIDTH = WIDTH / 2;
|
||||
|
||||
/**
|
||||
* Construct a new tile with the specified identifiers. Intended
|
||||
* only for use by the TileManager. Do not use this method.
|
||||
*
|
||||
* @see com.threerings.cocktail.miso.TileManager#getTile
|
||||
*/
|
||||
public Tile (short tsid, short tid)
|
||||
{
|
||||
this.tsid = tsid;
|
||||
this.tid = tid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new tile with the specified identifiers. Intended
|
||||
* only for use by the TileManager. Do not use this method.
|
||||
*
|
||||
* @see com.threerings.cocktail.miso.TileManager#getTile
|
||||
*/
|
||||
public Tile (int tsid, int tid)
|
||||
{
|
||||
this.tsid = (short) tsid;
|
||||
this.tid = (short) tid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// $Id: TileManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
import com.samskivert.util.IntMap;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.ImageObserver;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* Provides a simplified interface for retrieving tile objects from
|
||||
* various tilesets, by name or identifier, and manages caching of
|
||||
* tiles and related resources as appropriate.
|
||||
*/
|
||||
public class TileManager implements ImageObserver
|
||||
{
|
||||
public TileManager (TileSetManager tsmgr)
|
||||
{
|
||||
_tsmgr = tsmgr;
|
||||
}
|
||||
|
||||
public Tile getTile (String setName, String tileName)
|
||||
{
|
||||
return new Tile(0, 0);
|
||||
}
|
||||
|
||||
public Tile getTile (short tsid, short 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);
|
||||
|
||||
// retrieve the tile image from the tileset
|
||||
tile = new Tile(tsid, tid);
|
||||
tile.img = tset.getTileImage(tid, this);
|
||||
_tiles.put(utid, tile);
|
||||
}
|
||||
|
||||
return tile;
|
||||
}
|
||||
|
||||
public void registerObserver (ImageObserver obs)
|
||||
{
|
||||
if (_observers == null) _observers = new Vector();
|
||||
_observers.addElement(obs);
|
||||
}
|
||||
|
||||
public boolean imageUpdate (Image img, int infoflags, int x, int y,
|
||||
int width, int height)
|
||||
{
|
||||
int size = _observers.size();
|
||||
for (int ii = 0; ii < size; ii++) {
|
||||
ImageObserver obs = (ImageObserver)_observers.elementAt(ii);
|
||||
obs.imageUpdate(img, infoflags, x, y, width, height);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
// registered tile image observers
|
||||
protected Vector _observers;
|
||||
|
||||
// our tile set manager
|
||||
protected TileSetManager _tsmgr;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// $Id: TileSet.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
import com.threerings.cocktail.miso.Log;
|
||||
import com.threerings.cocktail.miso.media.ImageManager;
|
||||
|
||||
import com.samskivert.util.Config;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.*;
|
||||
|
||||
/**
|
||||
* A tileset stores information on a single logical set of tiles. It
|
||||
* provides a clean interface for the TileManager to retrieve
|
||||
* individual tile images from a particular tile in the tileset.
|
||||
*
|
||||
* The width of each tile in every tileset is a constant Tile.WIDTH 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.
|
||||
*
|
||||
* 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 tiles are
|
||||
* numbered left to right, top to bottom, in ascending order.
|
||||
*
|
||||
* @see com.threerings.cocktail.miso.TileManager
|
||||
*/
|
||||
public class TileSet
|
||||
{
|
||||
public TileSet (Config config, int tsid)
|
||||
{
|
||||
_tsid = tsid;
|
||||
|
||||
// get the tileset name
|
||||
_name = config.getValue(PREFIX + CONF_NAME, (String)null);
|
||||
|
||||
// get the tile image file name
|
||||
_file = config.getValue(PREFIX + CONF_FILE, (String)null);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the image corresponding to the specified tile id within
|
||||
* this tile set.
|
||||
*/
|
||||
public Image getTileImage (int tid, ImageObserver obs)
|
||||
{
|
||||
// load the full tile image if we don't already have it
|
||||
if (_imgTiles == null) {
|
||||
_imgTiles = ImageManager.getImage(_file, obs);
|
||||
}
|
||||
|
||||
// find the row number containing the sought-after tile
|
||||
int ridx, tcount, ty, tx;
|
||||
ridx = tcount = ty = tx = 0;
|
||||
while ((tcount += _tileCount[ridx]) < tid) {
|
||||
ty += _rowHeight[ridx++];
|
||||
}
|
||||
|
||||
// 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 + "].");
|
||||
|
||||
// retrieve the appropriate image chunk from the full image
|
||||
CropImageFilter crop =
|
||||
new CropImageFilter(tx, ty, Tile.WIDTH, _rowHeight[ridx]);
|
||||
|
||||
FilteredImageSource prod =
|
||||
new FilteredImageSource(_imgTiles.getSource(), crop);
|
||||
|
||||
Image img = ImageManager.tk.createImage(prod);
|
||||
ImageManager.tk.prepareImage(img, -1, -1, obs);
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string representation of the tileset information.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
|
||||
buf.append("[name=").append(_name);
|
||||
buf.append(", file=").append(_file);
|
||||
buf.append(", tsid=").append(_tsid);
|
||||
|
||||
buf.append(", rowheight={");
|
||||
for (int ii = 0; ii < _rowHeight.length; ii++) {
|
||||
if (ii > 0) buf.append(",");
|
||||
buf.append(_rowHeight[ii]);
|
||||
}
|
||||
|
||||
buf.append("}, tilecount={");
|
||||
for (int ii = 0; ii < _tileCount.length; ii++) {
|
||||
if (ii > 0) buf.append(",");
|
||||
buf.append(_tileCount[ii]);
|
||||
}
|
||||
|
||||
return buf.append("}]").toString();
|
||||
}
|
||||
|
||||
protected static final String PREFIX = "tileset.";
|
||||
|
||||
protected static final String CONF_FILE = "file";
|
||||
protected static final String CONF_NAME = "name";
|
||||
protected static final String CONF_ROWHEIGHT = "rowheight";
|
||||
protected static final String CONF_TILECOUNT = "tilecount";
|
||||
|
||||
protected String _name; // the tileset name
|
||||
protected String _file; // the file containing the tile images
|
||||
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 Image _imgTiles;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//
|
||||
// $Id: TileSetManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
public interface TileSetManager
|
||||
{
|
||||
public TileSet getTileSet (int tsid);
|
||||
}
|
||||
Reference in New Issue
Block a user