Initial work on reading and writing scenes to XML files.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@113 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-07-23 22:31:48 +00:00
parent bc3d2a4e18
commit f02475b4c2
12 changed files with 285 additions and 116 deletions
@@ -1,5 +1,5 @@
//
// $Id: TileManager.java,v 1.10 2001/07/23 18:52:51 shaper Exp $
// $Id: TileManager.java,v 1.11 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.tile;
@@ -20,9 +20,9 @@ public class TileManager
/**
* Initialize the tile manager with the given TileSetManager object.
*/
public TileManager (TileSetManager tsmgr)
public TileManager (TileSetManager tilesetmgr)
{
_tsmgr = tsmgr;
_tilesetmgr = tilesetmgr;
}
/**
@@ -43,7 +43,7 @@ public class TileManager
// retrieve the tile image from the tileset
tile = new Tile(tsid, tid);
if ((tile.img = _tsmgr.getTileImage(tsid, tid)) == null) {
if ((tile.img = _tilesetmgr.getTileImage(tsid, tid)) == null) {
Log.warning("Null tile image [tsid="+tsid+", tid="+tid+"].");
}
tile.height = (short)((BufferedImage)tile.img).getHeight();
@@ -60,12 +60,12 @@ public class TileManager
*/
public TileSetManager getTileSetManager ()
{
return _tsmgr;
return _tilesetmgr;
}
/** Cache of tiles that have been requested thus far. */
protected IntMap _tiles = new IntMap();
/** Our tileset manager. */
protected TileSetManager _tsmgr;
/** The tileset manager. */
protected TileSetManager _tilesetmgr;
}
@@ -1,5 +1,5 @@
//
// $Id: TileSetManager.java,v 1.8 2001/07/23 18:52:51 shaper Exp $
// $Id: TileSetManager.java,v 1.9 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.tile;
@@ -8,30 +8,54 @@ import com.threerings.media.ImageManager;
import java.awt.Image;
import java.util.ArrayList;
/**
* The TileSetManager provides tileset management functionality
* intended for use by the TileManager. It provides facilities for
* obtaining information about individual tilesets, retrieving an list
* of all tilesets available, and retrieving the image associated with
* a particular tile in a set.
*/
public interface TileSetManager
{
/**
* Return the total number of tiles in the specified tileset.
* Return the total number of tiles in the specified tileset or -1
* if the tileset is not found.
*
* @param tsid the tileset identifier.
* @return the number of tiles.
*/
public int getNumTilesInSet (int tsid);
/**
* Return a list of all tilesets available for use.
* Return an ArrayList containing all TileSet objects available.
*
* @return the list of tilesets.
*/
public ArrayList getAllTileSets ();
/**
* Return the tileset object corresponding to the specified tileset id.
* Return the tileset object corresponding to the specified
* tileset id, or null if the tileset is not found.
*
* @param tsid the tileset identifier.
* @return the tileset object.
*/
public TileSet getTileSet (int tsid);
/**
* Return the image corresponding to the specified tileset and tile id.
*
* @param tsid the tileset identifier.
* @param tid the tile identifier.
*
* @return the tile image.
*/
public Image getTileImage (int tsid, int tid);
/**
* Return the total number of tilesets.
* Return the total number of tilesets available for use.
*
* @return the number of tilesets.
*/
public int getNumTileSets ();
}
@@ -1,5 +1,5 @@
//
// $Id: TileSetManagerImpl.java,v 1.7 2001/07/23 18:52:51 shaper Exp $
// $Id: TileSetManagerImpl.java,v 1.8 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.tile;
@@ -43,9 +43,6 @@ public abstract class TileSetManagerImpl implements TileSetManager
public ArrayList getAllTileSets ()
{
int size = _tilesets.size();
if (size == 0) return null;
ArrayList list = new ArrayList();
CollectionUtil.addAll(list, _tilesets.elements());
return list;
@@ -1,24 +1,23 @@
//
// $Id: TileSetParser.java,v 1.3 2001/07/23 18:52:51 shaper Exp $
// $Id: TileSetParser.java,v 1.4 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.tile;
import java.io.InputStream;
import java.io.IOException;
import java.util.ArrayList;
/**
* An interface to be implemented by classes that wish to provide a
* general interface to load tileset description information from an
* input stream.
* The TileSetParser is a general interface to be implemented by
* classes that load tileset descriptions in a particular format from
* a file.
*/
public interface TileSetParser
{
/**
* Read tileset description data from the given input stream and
* Read tileset description data from the specified file and
* construct TileSet objects to suit. Return an ArrayList of all
* TileSet objects constructed, or a zero-length array if no
* TileSet objects constructed, or a zero-length list if no
* tileset descriptions were fully parsed.
*/
public ArrayList loadTileSets (InputStream tis) throws IOException;
public ArrayList loadTileSets (String fname) throws IOException;
}
@@ -1,20 +1,19 @@
//
// $Id: XMLTileSetParser.java,v 1.5 2001/07/23 18:52:51 shaper Exp $
// $Id: XMLTileSetParser.java,v 1.6 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.tile;
import com.threerings.miso.Log;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.XMLUtil;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import com.samskivert.util.*;
import com.samskivert.xml.XMLUtil;
import com.threerings.miso.Log;
/**
* Parse an XML tileset description file and construct tileset objects
* for each valid description. Does not currently perform validation
@@ -75,13 +74,22 @@ public class XMLTileSetParser extends DefaultHandler
}
}
public ArrayList loadTileSets (InputStream tis) throws IOException
public ArrayList loadTileSets (String fname) throws IOException
{
try {
XMLUtil.parse(this, tis);
return _tilesets;
try {
InputStream tis = ConfigUtil.getStream(fname);
if (tis == null) {
Log.warning("Couldn't find file [fname=" + fname + "].");
return _tilesets;
}
} catch (ParserConfigurationException pce) {
// read all tileset descriptions from the XML input stream
XMLTileSetParser parser = new XMLTileSetParser();
XMLUtil.parse(this, tis);
return _tilesets;
} catch (ParserConfigurationException pce) {
throw new IOException(pce.toString());
} catch (SAXException saxe) {
@@ -1,5 +1,5 @@
//
// $Id: DisplayMisoSceneImpl.java,v 1.9 2001/07/23 18:52:51 shaper Exp $
// $Id: DisplayMisoSceneImpl.java,v 1.10 2001/07/23 22:31:47 shaper Exp $
package com.threerings.miso.scene;
@@ -16,24 +16,25 @@ import java.io.*;
*/
public class Scene
{
/** String translations of each tile layer name. */
public static final String[] XLATE_LAYERS = { "Base", "Object" };
public Tile tiles[][][]; // the tiles comprising the scene
/** The tiles comprising the scene. */
public Tile tiles[][][];
/**
* Construct a new Scene object initialized to a default state.
* Construct a new Scene object initialized to a default state,
* specifying the tile manager from which the scene obtains tiles.
*
* @param tilemgr the tile manager.
*/
public Scene (TileManager tmgr)
public Scene (TileManager tilemgr, int sid)
{
_tmgr = tmgr;
_tilemgr = tilemgr;
_name = DEF_SCENE_NAME;
_sid = 0;
_version = VERSION;
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
Tile tile = _tmgr.getTile(DEF_TSID, DEF_TID);
Tile tile = _tilemgr.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++) {
@@ -44,7 +45,39 @@ public class Scene
}
}
}
/**
* Return the scene name.
*/
public String getName ()
{
return _name;
}
/**
* Return the scene identifier.
*/
public short getId ()
{
return _sid;
}
/**
* Return the scene hot spots array.
*/
public Point[] getHotSpots ()
{
return _hotspots;
}
/**
* Return the scene exit points array.
*/
public ExitPoint[] getExits ()
{
return _exits;
}
/**
* Return the number of actual (non-null) tiles present in the
* specified tile layer for this scene.
@@ -75,12 +108,12 @@ public class Scene
// read scene header information
_name = dis.readUTF();
_sid = dis.readShort();
_version = dis.readShort();
short version = dis.readShort();
// make sure we can understand the file format
if (_version < 0 || _version > VERSION) {
if (version < 0 || version > VERSION) {
throw new IOException("Can't understand scene file format " +
" [version=" + _version + "]");
" [version=" + version + "]");
}
// allocate full tile array. null tiles denote tiles in absentia.
@@ -94,7 +127,7 @@ public class Scene
short tid = dis.readShort();
// retrieve tile from tile manager
tiles[xx][yy][LAYER_BASE] = _tmgr.getTile(tsid, tid);
tiles[xx][yy][LAYER_BASE] = _tilemgr.getTile(tsid, tid);
}
}
@@ -111,7 +144,7 @@ public class Scene
byte ty = dis.readByte();
// retrieve tile from tile manager
tiles[tx][ty][lnum] = _tmgr.getTile(tsid, tid);
tiles[tx][ty][lnum] = _tilemgr.getTile(tsid, tid);
}
}
@@ -145,7 +178,7 @@ public class Scene
// write scene header information
dos.writeUTF(_name);
dos.writeShort(_sid);
dos.writeShort(_version);
dos.writeShort(VERSION);
// write tiles for the base layer
for (int xx = 0; xx < TILE_WIDTH; xx++) {
@@ -190,28 +223,45 @@ public class Scene
}
}
// the latest scene file format version number
/** The latest scene file format version. */
protected static final short VERSION = 1;
// scene width/height in tiles
/** The scene width in tiles. */
protected static final int TILE_WIDTH = 55;
/** The scene height in tiles. */
protected static final int TILE_HEIGHT = 55;
// layer identifiers and total number of layers
/** The base layer id. */
protected static final int LAYER_BASE = 0;
/** The object layer id. */
protected static final int LAYER_OBJECT = 1;
/** The total number of layers. */
protected static final int NUM_LAYERS = 2;
/** The default scene name. */
protected static final String DEF_SCENE_NAME = "Untitled Scene";
/** The default tileset id. */
protected static final short DEF_TSID = 1000;
/** The default tile id. */
protected static final short DEF_TID = 1;
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
/** The scene name. */
protected String _name;
protected TileManager _tmgr;
/** The unique scene id. */
protected short _sid;
/** Hot-spot zone points */
protected Point _hotspots[];
/** Exit points to different scenes. */
protected ExitPoint _exits[];
/** The tile manager. */
protected TileManager _tilemgr;
}
@@ -1,8 +1,68 @@
//
// $Id: EditableSceneManager.java,v 1.5 2001/07/23 18:52:51 shaper Exp $
// $Id: EditableSceneManager.java,v 1.6 2001/07/23 22:31:47 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.util.ArrayList;
import com.threerings.miso.Log;
/**
* The EditableSceneManager extends general scene manager
* functionality to allow reading scenes from and writing scenes to
* XML files.
*/
public class EditableSceneManager extends SceneManagerImpl
{
/**
* Load all scenes described in the specified XML file into the
* hashtable of scenes currently available.
*
* @param fname the file to load scenes from.
*/
public void loadScenes (String fname) throws IOException
{
ArrayList scenes = null;
_sid = 0;
try {
scenes = new XMLSceneParser().loadScenes(fname);
} catch (IOException ioe) {
Log.warning("Exception loading scenes [fname=" + fname +
", ioe=" + ioe + "].");
return;
}
// bail if we didn't find any scenes
int size = scenes.size();
if (size == 0) {
Log.warning("No scenes found [fname=" + fname + "].");
return;
}
// copy new scenes into the main scene hashtable
for (int ii = 0; ii < size; ii++) {
Scene scene = (Scene)scenes.get(ii);
_scenes.put(scene.getId(), scene);
Log.info("Adding scene to cache [scene=" + scene + "].");
}
}
/**
* Write all scenes currently available to the specified file in
* XML format.
*
* @param fname the file to write the scenes to.
*/
public void writeAllScenes (String fname)
{
try {
ArrayList scenes = getAllScenes();
new XMLSceneWriter(scenes).writeToFile(fname);
} catch (IOException ioe) {
Log.warning("Exception writing scenes to file [fname=" +
fname + ", ioe=" + ioe + "].");
}
}
}
@@ -1,10 +1,11 @@
//
// $Id: SceneManager.java,v 1.5 2001/07/23 18:52:51 shaper Exp $
// $Id: SceneManager.java,v 1.6 2001/07/23 22:31:47 shaper Exp $
package com.threerings.miso.scene;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Manages the various scenes that are displayed during the game and
@@ -12,6 +13,20 @@ import java.io.InputStream;
*/
public interface SceneManager
{
/**
* Return an ArrayList containing all Scene objects available.
*
* @return the list of scenes.
*/
public ArrayList getAllScenes ();
/**
* Return a new Scene object with the next unique scene id. The
* scene id is only guaranteed to be unique in the context of the
* scenes currently available via this SceneManager.
*/
public Scene getNewScene ();
/**
* Return the Scene object for the specified scene id.
*/
@@ -1,17 +1,23 @@
//
// $Id: SceneManagerImpl.java,v 1.5 2001/07/23 18:52:51 shaper Exp $
// $Id: SceneManagerImpl.java,v 1.6 2001/07/23 22:31:47 shaper Exp $
package com.threerings.miso.scene;
import com.threerings.miso.Log;
import com.samskivert.util.ConfigUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import com.samskivert.util.*;
import com.threerings.miso.Log;
import com.threerings.miso.tile.TileManager;
public abstract class SceneManagerImpl implements SceneManager
{
public void init (TileManager tilemgr)
{
_tilemgr = tilemgr;
}
public Scene getScene (int sid)
{
// TBD
@@ -22,4 +28,27 @@ public abstract class SceneManagerImpl implements SceneManager
{
return Scene.XLATE_LAYERS;
}
public ArrayList getAllScenes ()
{
ArrayList list = new ArrayList();
CollectionUtil.addAll(list, _scenes.elements());
return list;
}
public Scene getNewScene ()
{
Scene scene = new Scene(_tilemgr, _sid);
_scenes.put(_sid++, scene);
return scene;
}
/** The next monotonically-increasing scene id. */
protected int _sid = 0;
/** The tile manager for use by all scenes. */
protected TileManager _tilemgr;
/** The scenes currently available. */
protected IntMap _scenes = new IntMap();
}
@@ -1,14 +1,12 @@
//
// $Id: EditableTileSetManager.java,v 1.7 2001/07/23 18:52:51 shaper Exp $
// $Id: EditableTileSetManager.java,v 1.8 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.tile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import com.samskivert.util.Config;
import com.samskivert.util.ConfigUtil;
import com.threerings.miso.Log;
import com.threerings.media.ImageManager;
@@ -24,43 +22,29 @@ public class EditableTileSetManager extends TileSetManagerImpl
{
super.init(config, imgmgr);
// load the tileset descriptions
// load the tilesets from the XML description file
String fname = config.getValue("miso.tilesets", (String)null);
loadTileSets(fname);
}
/**
* Load the tilesets described in the specified file into the set
* of available tilesets.
*/
protected void loadTileSets (String fname)
{
try {
InputStream tis = ConfigUtil.getStream(fname);
if (tis == null) {
Log.warning("Couldn't find file [fname=" + fname + "].");
return;
}
// read all tileset descriptions from the XML input stream
XMLTileSetParser parser = new XMLTileSetParser();
ArrayList tsets = parser.loadTileSets(tis);
if (tsets == null) {
Log.warning("No tilesets found [tis=" + tis + "].");
return;
}
// copy new tilesets into the main tileset hashtable
int size = tsets.size();
for (int ii = 0; ii < size; ii++) {
TileSet tset = (TileSet)tsets.get(ii);
_tilesets.put(tset.getId(), tset);
Log.info("Adding tileset to cache [tset=" + tset + "].");
}
ArrayList tilesets = null;
try {
tilesets = new XMLTileSetParser().loadTileSets(fname);
} catch (IOException ioe) {
Log.warning("Exception loading tileset [fname=" + fname +
", ioe=" + ioe + "].");
}
return;
}
// bail if we didn't find any tilesets
int size = tilesets.size();
if (size == 0) {
Log.warning("No tilesets found [fname=" + fname + "].");
return;
}
// copy new tilesets into the main tileset hashtable
for (int ii = 0; ii < size; ii++) {
TileSet tset = (TileSet)tilesets.get(ii);
_tilesets.put(tset.getId(), tset);
Log.info("Adding tileset to cache [tset=" + tset + "].");
}
}
}
@@ -1,5 +1,5 @@
//
// $Id: MisoUtil.java,v 1.2 2001/07/23 18:52:51 shaper Exp $
// $Id: MisoUtil.java,v 1.3 2001/07/23 22:31:48 shaper Exp $
package com.threerings.miso.util;
@@ -39,12 +39,15 @@ public class MisoUtil
*
* @return the new SceneManager object or null if an error occurred.
*/
public static SceneManager createSceneManager (Config config)
public static SceneManager
createSceneManager (Config config, TileManager tilemgr)
{
try {
return (SceneManager)
SceneManagerImpl scenemgr = (SceneManagerImpl)
config.instantiateValue("miso.scenemgr", DEF_SCENEMGR);
scenemgr.init(tilemgr);
return scenemgr;
} catch (Exception e) {
Log.warning("Failed to instantiate scene manager [e=" + e + "].");
return null;