Started tooling about with Java2D BufferedImages. Retrieve tileset
config information from miso.properties for now. Don't pass around ImageObservers since we're hoping to be able to address that differently. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@49 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
//
|
||||
// $Id: Tile.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
// $Id: Tile.java,v 1.2 2001/07/14 00:21:24 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
/**
|
||||
* 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
|
||||
public BufferedImage 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;
|
||||
@@ -46,4 +47,13 @@ public class Tile
|
||||
this.tsid = (short) tsid;
|
||||
this.tid = (short) tid;
|
||||
}
|
||||
|
||||
public String toString ()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("[tsid=").append(tsid);
|
||||
buf.append(", tid=").append(tid);
|
||||
buf.append(", img=").append(img);
|
||||
return buf.append("]").toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
//
|
||||
// $Id: TileManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
// $Id: TileManager.java,v 1.2 2001/07/14 00:21:24 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
import com.threerings.cocktail.miso.Log;
|
||||
|
||||
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 class TileManager
|
||||
{
|
||||
public TileManager (TileSetManager tsmgr)
|
||||
{
|
||||
@@ -38,43 +37,25 @@ public class TileManager implements ImageObserver
|
||||
|
||||
// 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, this);
|
||||
tile.img = tset.getTileImage(tid);
|
||||
|
||||
_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;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: TileSet.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
// $Id: TileSet.java,v 1.2 2001/07/14 00:21:24 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.tile;
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.threerings.cocktail.miso.media.ImageManager;
|
||||
import com.samskivert.util.Config;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.*;
|
||||
|
||||
/**
|
||||
@@ -49,13 +50,19 @@ public class TileSet
|
||||
* Return the image corresponding to the specified tile id within
|
||||
* this tile set.
|
||||
*/
|
||||
public Image getTileImage (int tid, ImageObserver obs)
|
||||
public BufferedImage getTileImage (int tid)
|
||||
{
|
||||
// load the full tile image if we don't already have it
|
||||
if (_imgTiles == null) {
|
||||
_imgTiles = ImageManager.getImage(_file, obs);
|
||||
Log.info("Getting full tileset image [file=" + _file + "].");
|
||||
if (_imgTiles == null) {
|
||||
if ((_imgTiles = ImageManager.getImage(_file)) == null) {
|
||||
Log.warning("Failed to retrieve full tileset image.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// find the row number containing the sought-after tile
|
||||
int ridx, tcount, ty, tx;
|
||||
ridx = tcount = ty = tx = 0;
|
||||
@@ -71,17 +78,9 @@ public class TileSet
|
||||
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;
|
||||
// crop the tile-sized image chunk from the full image
|
||||
return ImageManager.getImageCropped(_imgTiles, tx, ty,
|
||||
Tile.WIDTH, _rowHeight[ridx]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,7 +109,7 @@ public class TileSet
|
||||
return buf.append("}]").toString();
|
||||
}
|
||||
|
||||
protected static final String PREFIX = "tileset.";
|
||||
protected static final String PREFIX = "miso.tileset.";
|
||||
|
||||
protected static final String CONF_FILE = "file";
|
||||
protected static final String CONF_NAME = "name";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// $Id: DisplayMisoSceneImpl.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
// $Id: DisplayMisoSceneImpl.java,v 1.2 2001/07/14 00:21:23 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class Scene
|
||||
|
||||
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
|
||||
|
||||
Tile tile = _tmgr.getTile(DEF_TILESET_NAME, DEF_TILE_NAME);
|
||||
Tile tile = _tmgr.getTile(DEF_TSID, DEF_TID);
|
||||
for (int xx = 0; xx < TILE_WIDTH; xx++) {
|
||||
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
|
||||
for (int ii = 0; ii < NUM_LAYERS; ii++) {
|
||||
@@ -214,8 +214,8 @@ public class Scene
|
||||
|
||||
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 static final short DEF_TSID = 0;
|
||||
protected static final short DEF_TID = 1;
|
||||
|
||||
protected String _name; // the scene name
|
||||
protected short _sid; // the unique scene id
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
//
|
||||
// $Id: IsoSceneView.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
// $Id: IsoSceneView.java,v 1.2 2001/07/14 00:21:23 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
import com.threerings.cocktail.miso.Log;
|
||||
import com.threerings.cocktail.miso.media.ImageManager;
|
||||
import com.threerings.cocktail.miso.tile.Tile;
|
||||
import com.threerings.cocktail.miso.tile.TileManager;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.*;
|
||||
|
||||
/**
|
||||
* The IsoSceneView provides an isometric graphics view of a
|
||||
@@ -26,16 +29,23 @@ public class IsoSceneView implements SceneView
|
||||
|
||||
public void paint (Graphics g)
|
||||
{
|
||||
Graphics2D g2 = (Graphics2D)g;
|
||||
|
||||
_offGraphics.setColor(Color.white);
|
||||
// Image img = ImageManager.getImage("/home/shaper/workspace/cocktail/rsrc/media/miso/tiles-base.png");
|
||||
// _scene.tiles[10][10][0].img =
|
||||
// ImageManager.getImageCropped(img, 32, 0, 32, 16);
|
||||
|
||||
_offGraphics.setColor(Color.red);
|
||||
_offGraphics.fillRect(0, 0, _bounds.width, _bounds.height);
|
||||
|
||||
// draw the full scene into the offscreen image buffer
|
||||
renderScene(_offGraphics, _viewX, _viewY);
|
||||
|
||||
g.drawImage(_offImage, 0, 0, null);
|
||||
// copy offscreen buffer to the given graphics context
|
||||
g2.drawImage(_offImg, null, 0, 0);
|
||||
}
|
||||
|
||||
protected void renderScene (Graphics g, int x, int y)
|
||||
protected void renderScene (Graphics2D g2, int x, int y)
|
||||
{
|
||||
int mapX = x / Tile.HALF_WIDTH;
|
||||
int xOff = x & (Tile.HALF_WIDTH - 1);
|
||||
@@ -67,8 +77,11 @@ public class IsoSceneView implements SceneView
|
||||
// TODO: draw layers L1+.
|
||||
Tile tile = _scene.tiles[tx][ty][Scene.LAYER_BASE];
|
||||
|
||||
g.drawImage(tile.img, screenX, screenY, null);
|
||||
|
||||
g2.drawImage(tile.img, screenX, screenY, null);
|
||||
//Log.info("Drawing tile [tx=" + tx + ", ty=" + ty +
|
||||
// ", sx=" + screenX + ", sy=" + screenY +
|
||||
// ", img=" + tile.img + "].");
|
||||
|
||||
screenX += Tile.WIDTH;
|
||||
|
||||
if ((tx += 1) > Scene.TILE_WIDTH - 1) tx = 0;
|
||||
@@ -89,11 +102,17 @@ public class IsoSceneView implements SceneView
|
||||
{
|
||||
_scene = scene;
|
||||
}
|
||||
|
||||
|
||||
public void setTarget (Component target)
|
||||
{
|
||||
_offImage = target.createImage(_bounds.width, _bounds.height);
|
||||
_offGraphics = _offImage.getGraphics();
|
||||
_target = target;
|
||||
|
||||
int width = _bounds.width, height = _bounds.height;
|
||||
_offImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
_offGraphics = _offImg.createGraphics();
|
||||
|
||||
Log.info("Creating offscreen [width=" + width + ", height=" +
|
||||
height + "].");
|
||||
}
|
||||
|
||||
protected static final int OFF_X = -Tile.WIDTH;
|
||||
@@ -102,14 +121,12 @@ public class IsoSceneView implements SceneView
|
||||
protected static final int DEF_BOUNDS_WIDTH = 600;
|
||||
protected static final int DEF_BOUNDS_HEIGHT = 600;
|
||||
|
||||
protected BufferedImage _offImg;
|
||||
protected Graphics2D _offGraphics;
|
||||
|
||||
protected Rectangle _bounds;
|
||||
|
||||
protected int _viewX, _viewY;
|
||||
|
||||
protected Graphics _offGraphics;
|
||||
protected Image _offImage;
|
||||
|
||||
protected int _viewX, _viewY;
|
||||
protected Scene _scene;
|
||||
|
||||
protected Component _target;
|
||||
protected TileManager _tmgr;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user