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:
Walter Korman
2001-07-12 22:38:03 +00:00
parent c8af354d2d
commit 672fecf682
16 changed files with 848 additions and 0 deletions
@@ -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);
}
+38
View File
@@ -0,0 +1,38 @@
//
// $Id: Log.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso;
/**
* A placeholder class that contains a reference to the log object used by
* the Spine package.
*/
public class Log
{
public static com.samskivert.util.Log log =
new com.samskivert.util.Log("miso");
/** Convenience function. */
public static void debug (String message)
{
log.debug(message);
}
/** Convenience function. */
public static void info (String message)
{
log.info(message);
}
/** Convenience function. */
public static void warning (String message)
{
log.warning(message);
}
/** Convenience function. */
public static void logStackTrace (Throwable t)
{
log.logStackTrace(com.samskivert.util.Log.WARNING, t);
}
}
@@ -0,0 +1,20 @@
//
// $Id: CompiledSceneManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
public class CompiledSceneManager implements SceneManager
{
// context, rsrc mgr, loads compiled scene files via rsrc mgr
public Scene getScene (String name)
{
// TBD
return null;
}
public Scene getScene (int sid)
{
// TBD
return null;
}
}
@@ -0,0 +1,229 @@
//
// $Id: DisplayMisoSceneImpl.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
import com.threerings.cocktail.miso.tile.Tile;
import com.threerings.cocktail.miso.tile.TileManager;
import java.awt.Point;
import java.io.*;
/**
* A scene represents the data model corresponding to a single screen
* for game play. For instance, one scene might display a portion of
* a street with several buildings scattered about on the periphery.
*/
public class Scene
{
public Tile tiles[][][]; // the tiles comprising the scene
/**
* Construct a new Scene object initialized to a default state.
*/
public Scene (TileManager tmgr)
{
_tmgr = tmgr;
_name = DEF_SCENE_NAME;
_sid = 0;
_version = VERSION;
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
Tile tile = _tmgr.getTile(DEF_TILESET_NAME, DEF_TILE_NAME);
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
for (int ii = 0; ii < NUM_LAYERS; ii++) {
if (ii == LAYER_BASE) {
tiles[xx][yy][ii] = tile;
}
}
}
}
_file = null;
}
public File getFile ()
{
return _file;
}
public void setFile (File file)
{
_file = file;
}
/**
* Return the number of actual (non-null) tiles present in the
* specified tile layer for this scene.
*/
public int getNumLayerTiles (int lnum)
{
if (lnum == LAYER_BASE) return TILE_WIDTH * TILE_HEIGHT;
int numTiles = 0;
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
if (tiles[xx][yy] != null) numTiles++;
}
}
return numTiles;
}
/**
* Populate the scene object by reading the contents from the given
* input stream.
*/
public void readFrom (InputStream in) throws IOException
{
DataInputStream dis = new DataInputStream(in);
// read scene header information
_name = dis.readUTF();
_sid = dis.readShort();
_version = dis.readShort();
// make sure we can understand the file format
if (_version < 0 || _version > VERSION) {
throw new IOException("Can't understand scene file format " +
" [version=" + _version + "]");
}
// allocate full tile array. null tiles denote tiles in absentia.
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
// read all tiles for the base layer
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
// read tile details
short tsid = dis.readShort();
short tid = dis.readShort();
// retrieve tile from tile manager
tiles[xx][yy][LAYER_BASE] = _tmgr.getTile(tsid, tid);
}
}
// read tiles for each of the subsequent layers
for (int lnum = 1; lnum < NUM_LAYERS; lnum++) {
// read the number of tiles in this layer
int numTiles = dis.readShort();
for (int ii = 0; ii < numTiles; ii++) {
// read tile details
short tsid = dis.readShort();
short tid = dis.readShort();
byte tx = dis.readByte();
byte ty = dis.readByte();
// retrieve tile from tile manager
tiles[tx][ty][lnum] = _tmgr.getTile(tsid, tid);
}
}
// read hotspot points
short numSpots = dis.readShort();
_hotspots = new Point[numSpots];
for (int ii = 0; ii < numSpots; ii++) {
_hotspots[ii] = new Point();
_hotspots[ii].x = dis.readByte();
_hotspots[ii].y = dis.readByte();
}
// read exit points
short numExits = dis.readShort();
_exits = new ExitPoint[numExits];
for (int ii = 0; ii < numExits; ii++) {
_exits[ii] = new ExitPoint();
_exits[ii].x = dis.readByte();
_exits[ii].y = dis.readByte();
_exits[ii].sid = dis.readShort();
}
}
/**
* Write this scene object to the specified output stream.
*/
public void writeTo (OutputStream out) throws IOException
{
DataOutputStream dos = new DataOutputStream(out);
// write scene header information
dos.writeUTF(_name);
dos.writeShort(_sid);
dos.writeShort(_version);
// write tiles for the base layer
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
Tile tile = tiles[xx][yy][LAYER_BASE];
dos.writeShort(tile.tsid);
dos.writeShort(tile.tid);
}
}
// write tiles for each of the subsequent layers
for (int lnum = 1; lnum < NUM_LAYERS; lnum++) {
// write the number of tiles in this layer
dos.writeShort(getNumLayerTiles(lnum));
for (int xx = 0; xx < TILE_WIDTH; xx++) {
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
Tile tile = tiles[xx][yy][lnum];
if (tile != null) {
dos.writeShort(tile.tsid);
dos.writeShort(tile.tid);
}
}
}
}
// write hotspot points
int numSpots = (_hotspots == null) ? 0 : _hotspots.length;
dos.writeShort(numSpots);
for (int ii = 0; ii < numSpots; ii++) {
dos.writeByte(_hotspots[ii].x);
dos.writeByte(_hotspots[ii].y);
}
// write exit points
int numExits = (_exits == null) ? 0 : _exits.length;
dos.writeShort(numExits);
for (int ii = 0; ii < numExits; ii++) {
dos.writeByte(_exits[ii].x);
dos.writeByte(_exits[ii].y);
dos.writeByte(_exits[ii].sid);
}
}
// the latest scene file format version number
protected static final short VERSION = 1;
// scene width/height in tiles
protected static final int TILE_WIDTH = 20;
protected static final int TILE_HEIGHT = 40;
// layer identifiers and total number of layers
protected static final int LAYER_BASE = 0;
protected static final int LAYER_OBJECT = 1;
protected static final int NUM_LAYERS = 2;
protected static final String DEF_SCENE_NAME = "Untitled Scene";
protected static final String DEF_TILESET_NAME = "ground";
protected static final String DEF_TILE_NAME = "dirt";
protected String _name; // the scene name
protected short _sid; // the unique scene id
protected short _version; // file format version
protected Point _hotspots[]; // hot spot zone points
protected ExitPoint _exits[]; // exit points to different scenes
protected File _file; // the file last associated with this scene
protected TileManager _tmgr;
}
@@ -0,0 +1,19 @@
//
// $Id: EditableSceneManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
public class EditableSceneManager implements SceneManager
{
public Scene getScene (String name)
{
// TBD
return null;
}
public Scene getScene (int sid)
{
// TBD
return null;
}
}
@@ -0,0 +1,13 @@
//
// $Id: ExitPoint.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
/**
* Represents a point in a scene that leads to a different scene.
*/
public class ExitPoint
{
byte x, y; // coordinates for this exit point
short sid; // scene id this exit transitions to
}
@@ -0,0 +1,115 @@
//
// $Id: IsoSceneView.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
import com.threerings.cocktail.miso.tile.Tile;
import com.threerings.cocktail.miso.tile.TileManager;
import java.awt.*;
/**
* The IsoSceneView provides an isometric graphics view of a
* particular scene.
*/
public class IsoSceneView implements SceneView
{
public IsoSceneView (TileManager tmgr)
{
_tmgr = tmgr;
_viewX = 0;
_viewY = 0;
_bounds = new Rectangle(0, 0, DEF_BOUNDS_WIDTH, DEF_BOUNDS_HEIGHT);
}
public void paint (Graphics g)
{
_offGraphics.setColor(Color.white);
_offGraphics.fillRect(0, 0, _bounds.width, _bounds.height);
renderScene(_offGraphics, _viewX, _viewY);
g.drawImage(_offImage, 0, 0, null);
}
protected void renderScene (Graphics g, int x, int y)
{
int mapX = x / Tile.HALF_WIDTH;
int xOff = x & (Tile.HALF_WIDTH - 1);
int mapY = y / Tile.HEIGHT;
int yOff = y & Tile.HEIGHT - 1;
int xa = xOff - yOff;
int ya = (xOff >> 1) + (yOff >> 1);
int mx = mapX;
int my = mapY;
int screenY = OFF_Y + (Tile.HALF_HEIGHT - ya);
for (int ii = 0; ii < Scene.TILE_HEIGHT; ii++) {
int tx = mx;
int ty = my;
int screenX = OFF_X + (Tile.HALF_WIDTH - xa);
int length = Scene.TILE_WIDTH;
if ((ii & 1) == 1) {
length++;
screenX -= Tile.HALF_WIDTH;
}
for (int j = 0; j < length; j++) {
// TODO: draw layers L1+.
Tile tile = _scene.tiles[tx][ty][Scene.LAYER_BASE];
g.drawImage(tile.img, screenX, screenY, null);
screenX += Tile.WIDTH;
if ((tx += 1) > Scene.TILE_WIDTH - 1) tx = 0;
if ((ty -= 1) < 0) ty = Scene.TILE_HEIGHT - 1;
}
screenY += Tile.HALF_HEIGHT;
if ((ii & 1) == 1) {
if ((mx += 1) > Scene.TILE_WIDTH - 1) mx = 0;
} else {
if ((my += 1) > Scene.TILE_HEIGHT - 1) my = 0;
}
}
}
public void setScene (Scene scene)
{
_scene = scene;
}
public void setTarget (Component target)
{
_offImage = target.createImage(_bounds.width, _bounds.height);
_offGraphics = _offImage.getGraphics();
}
protected static final int OFF_X = -Tile.WIDTH;
protected static final int OFF_Y = 0;
protected static final int DEF_BOUNDS_WIDTH = 600;
protected static final int DEF_BOUNDS_HEIGHT = 600;
protected Rectangle _bounds;
protected int _viewX, _viewY;
protected Graphics _offGraphics;
protected Image _offImage;
protected Scene _scene;
protected TileManager _tmgr;
}
@@ -0,0 +1,15 @@
//
// $Id: SceneManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
/**
* Manages the various scenes that are displayed during the game and
* provides simplified retrieval and caching facilities.
*/
public interface SceneManager
{
public Scene getScene (String name);
public Scene getScene (int sid);
}
@@ -0,0 +1,30 @@
//
// $Id: SceneView.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.scene;
import java.awt.Component;
import java.awt.Graphics;
/**
* An interface to be implemented by classes that provide a view of a
* given scene by drawing the scene contents onto a particular GUI
* component.
*/
public interface SceneView
{
/**
* Render the scene to the given graphics context.
*/
public void paint (Graphics g);
/**
* Set the scene that we're rendering.
*/
public void setScene (Scene scene);
/**
* Set the target component to which we're rendering.
*/
public void setTarget (Component target);
}
@@ -0,0 +1,19 @@
//
// $Id: ImageManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.media;
import java.awt.*;
import java.awt.image.ImageObserver;
public class ImageManager
{
public static Image getImage (String fname, ImageObserver obs)
{
Image img = tk.getImage(fname);
tk.prepareImage(img, -1, -1, obs);
return img;
}
public static Toolkit tk = Toolkit.getDefaultToolkit();
}
@@ -0,0 +1,54 @@
//
// $Id: StripImage.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.media;
import java.awt.*;
import java.awt.image.*;
/**
* StripImage facilitates cutting an image up into individual frames.
*/
public class StripImage
{
public StripImage (Image img, int frameWidth, int frameHeight,
int framesPerRow, int numFrames)
{
_img = img;
_frameWidth = frameWidth;
_frameHeight = frameHeight;
_framesPerRow = framesPerRow;
_numFrames = numFrames;
}
public Image getFrame (int idx, ImageObserver obs)
{
int frameX = (idx % _framesPerRow) * _frameWidth;
int frameY = (idx / _framesPerRow) * _frameHeight;
CropImageFilter crop =
new CropImageFilter(frameX, frameY, _frameWidth, _frameHeight);
FilteredImageSource prod =
new FilteredImageSource(_img.getSource(), crop);
Image img = ImageManager.tk.createImage(prod);
ImageManager.tk.prepareImage(img, -1, -1, obs);
return img;
}
public Image[] getAllFrames (ImageObserver obs)
{
Image allImgs[] = new Image[_numFrames];
for (int ii = 0; ii < _numFrames; ii++) {
allImgs[ii] = getFrame(ii, obs);
}
return allImgs;
}
protected Image _img;
protected int _frameWidth, _frameHeight;
protected int _framesPerRow, _numFrames;
}
@@ -0,0 +1,13 @@
//
// $Id: CompiledTileSetManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.tile;
public class CompiledTileSetManager implements TileSetManager
{
public TileSet getTileSet (int tsid)
{
// TBD
return null;
}
}
@@ -0,0 +1,18 @@
//
// $Id: SwingUtil.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
package com.threerings.cocktail.miso.util;
import java.awt.*;
public class SwingUtil
{
public static void centerFrame (Frame frame)
{
Toolkit tk = frame.getToolkit();
Dimension ss = tk.getScreenSize();
int width = frame.getWidth(), height = frame.getHeight();
frame.setBounds((ss.width-width)/2, (ss.height-height)/2,
width, height);
}
}