From f91e1b41d1f0482c23af4b7f3939699844004476 Mon Sep 17 00:00:00 2001 From: Walter Korman Date: Wed, 18 Jul 2001 22:45:35 +0000 Subject: [PATCH] Fixed up image loading to use the now-non-static ImageManager class. Modified TileManager to store the height of a tile in the Tile object's member data when the tile is initially created since we would like to be able to reference it speedily. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@63 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/media/image/ImageManager.java | 49 ++++++++++-------- src/java/com/threerings/media/tile/Tile.java | 10 ++-- .../threerings/media/tile/TileManager.java | 14 ++--- .../com/threerings/media/tile/TileSet.java | 10 ++-- .../threerings/media/tile/TileSetManager.java | 8 ++- .../media/tile/TileSetManagerImpl.java | 17 +++++- .../threerings/miso/client/IsoSceneView.java | 5 +- .../miso/tile/CompiledTileSetManager.java | 9 +++- .../miso/tile/EditableTileSetManager.java | 8 ++- tests/rsrc/config/miso/tilesets.xml | 8 +-- tests/rsrc/media/miso/tiles-wall.png | Bin 0 -> 1183 bytes 11 files changed, 84 insertions(+), 54 deletions(-) create mode 100644 tests/rsrc/media/miso/tiles-wall.png diff --git a/src/java/com/threerings/media/image/ImageManager.java b/src/java/com/threerings/media/image/ImageManager.java index 1ee030c4a..19374c145 100644 --- a/src/java/com/threerings/media/image/ImageManager.java +++ b/src/java/com/threerings/media/image/ImageManager.java @@ -1,12 +1,14 @@ // -// $Id: ImageManager.java,v 1.1 2001/07/18 21:45:42 shaper Exp $ +// $Id: ImageManager.java,v 1.2 2001/07/18 22:45:34 shaper Exp $ package com.threerings.media; -import com.threerings.cocktail.miso.Log; +import com.threerings.miso.Log; +import com.threerings.resource.ResourceManager; import java.awt.*; import java.awt.image.*; +import java.io.IOException; import java.util.Hashtable; /** @@ -20,14 +22,16 @@ import java.util.Hashtable; */ public class ImageManager { - public static Toolkit tk = Toolkit.getDefaultToolkit(); + public Toolkit tk = Toolkit.getDefaultToolkit(); /** - * Initialize the ImageManager with a root component to which - * images will later be rendered. + * Construct an ImageManager object with the ResourceManager from + * which it will obtain its data, and a root component to which + * images will be rendered. */ - public static void init (Component root) + public ImageManager (ResourceManager rmgr, Component root) { + _rmgr = rmgr; _root = root; tk = root.getToolkit(); } @@ -36,19 +40,13 @@ public class ImageManager * Load the image from the specified filename and cache it for * faster retrieval henceforth. */ - public static Image getImage (String fname) + public Image getImage (String fname) { if (_root == null) { Log.warning("Attempt to get image without valid root component."); return null; } - // TODO: fix this to properly find the file within the - // classpath. getResourceAsStream() returns an InputStream, - // but java.awt.Toolkit.createImage() can only take one of a - // byte[], String, URL, or ImageProducer. - fname = "/home/shaper/workspace/cocktail/" + fname; - Image img = (Image)_imgs.get(fname); if (img != null) { Log.info("Retrieved image from cache [fname=" + fname + "]."); @@ -57,11 +55,12 @@ public class ImageManager Log.info("Loading image into cache [fname=" + fname + "]."); - img = tk.createImage(fname); - MediaTracker tracker = new MediaTracker(_root); - tracker.addImage(img, 0); - try { + byte[] data = _rmgr.getResourceAsBytes(fname); + img = tk.createImage(data); + MediaTracker tracker = new MediaTracker(_root); + tracker.addImage(img, 0); + tracker.waitForID(0); if (tracker.isErrorAny()) { Log.warning("Error loading image [fname=" + fname + "]."); @@ -71,8 +70,11 @@ public class ImageManager _imgs.put(fname, img); } - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException ioe) { + Log.warning("Exception loading image [ioe=" + ioe + "]."); + + } catch (InterruptedException ie) { + Log.warning("Interrupted loading image [ie=" + ie + "]."); } return img; @@ -82,8 +84,8 @@ public class ImageManager * Creates a new image representing the specified rectangular * section cropped from the specified full image object. */ - public static BufferedImage getImageCropped (Image fullImg, int x, int y, - int width, int height) + public Image getImageCropped (Image fullImg, int x, int y, + int width, int height) { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); @@ -94,6 +96,7 @@ public class ImageManager return img; } - protected static Component _root; - protected static Hashtable _imgs = new Hashtable(); + protected ResourceManager _rmgr; + protected Component _root; + protected Hashtable _imgs = new Hashtable(); } diff --git a/src/java/com/threerings/media/tile/Tile.java b/src/java/com/threerings/media/tile/Tile.java index dc0c31ab6..55e400045 100644 --- a/src/java/com/threerings/media/tile/Tile.java +++ b/src/java/com/threerings/media/tile/Tile.java @@ -1,19 +1,19 @@ // -// $Id: Tile.java,v 1.4 2001/07/18 21:45:42 shaper Exp $ +// $Id: Tile.java,v 1.5 2001/07/18 22:45:35 shaper Exp $ package com.threerings.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 BufferedImage img; // the tile image - public short tsid; // the tile set identifier - public short tid; // the tile identifier within the set + public Image img; // the tile image + public short tsid; // the tile set identifier + public short tid; // the tile identifier within the set + public short height; // the tile height in pixels // height and width of a tile image in pixels public static final int HEIGHT = 16; diff --git a/src/java/com/threerings/media/tile/TileManager.java b/src/java/com/threerings/media/tile/TileManager.java index 199e990bf..1c9a7aaa9 100644 --- a/src/java/com/threerings/media/tile/TileManager.java +++ b/src/java/com/threerings/media/tile/TileManager.java @@ -1,5 +1,5 @@ // -// $Id: TileManager.java,v 1.6 2001/07/18 21:45:42 shaper Exp $ +// $Id: TileManager.java,v 1.7 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.tile; @@ -9,6 +9,7 @@ import com.samskivert.util.ConfigUtil; import com.samskivert.util.IntMap; import java.awt.*; +import java.awt.image.BufferedImage; import java.io.*; import java.util.ArrayList; @@ -60,19 +61,12 @@ public class TileManager return tile; } - // retrieve the tileset containing the tile - TileSet tset = _tsmgr.getTileSet(tsid); - if (tset == null) { - Log.warning("Can't create tile due to unknown tileset " + - "[tsid=" + tsid + ", tid=" + tid + "]."); - return null; - } - // retrieve the tile image from the tileset tile = new Tile(tsid, tid); - if ((tile.img = tset.getTileImage(tid)) == null) { + if ((tile.img = _tsmgr.getTileImage(tsid, tid)) == null) { Log.warning("Null tile image [tsid="+tsid+", tid="+tid+"]."); } + tile.height = (short)((BufferedImage)tile.img).getHeight(); _tiles.put(utid, tile); diff --git a/src/java/com/threerings/media/tile/TileSet.java b/src/java/com/threerings/media/tile/TileSet.java index 9042b9dbe..9a0c1de24 100644 --- a/src/java/com/threerings/media/tile/TileSet.java +++ b/src/java/com/threerings/media/tile/TileSet.java @@ -1,5 +1,5 @@ // -// $Id: TileSet.java,v 1.7 2001/07/18 21:45:42 shaper Exp $ +// $Id: TileSet.java,v 1.8 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.tile; @@ -62,11 +62,11 @@ public class TileSet * Return the image corresponding to the specified tile id within * this tile set. */ - public BufferedImage getTileImage (int tid) + public Image getTileImage (ImageManager imgr, int tid) { // load the full tile image if we don't already have it if (_imgTiles == null) { - if ((_imgTiles = ImageManager.getImage(_imgFile)) == null) { + if ((_imgTiles = imgr.getImage(_imgFile)) == null) { Log.warning("Failed to retrieve full tileset image " + "[file=" + _imgFile + "]."); return null; @@ -89,8 +89,8 @@ public class TileSet // ", ty=" + ty + "]."); // crop the tile-sized image chunk from the full image - return ImageManager.getImageCropped(_imgTiles, tx, ty, - Tile.WIDTH, _rowHeight[ridx]); + return imgr.getImageCropped(_imgTiles, tx, ty, + Tile.WIDTH, _rowHeight[ridx]); } /** diff --git a/src/java/com/threerings/media/tile/TileSetManager.java b/src/java/com/threerings/media/tile/TileSetManager.java index 9ebd6371a..673b3229d 100644 --- a/src/java/com/threerings/media/tile/TileSetManager.java +++ b/src/java/com/threerings/media/tile/TileSetManager.java @@ -1,8 +1,9 @@ // -// $Id: TileSetManager.java,v 1.4 2001/07/18 21:45:42 shaper Exp $ +// $Id: TileSetManager.java,v 1.5 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.tile; +import java.awt.Image; import java.io.InputStream; import java.io.IOException; import java.util.ArrayList; @@ -14,6 +15,11 @@ public interface TileSetManager */ public TileSet getTileSet (int tsid); + /** + * Return the image corresponding to the specified tileset and tile id. + */ + public Image getTileImage (int tsid, int tid); + /** * Return a list of all tilesets available for use. */ diff --git a/src/java/com/threerings/media/tile/TileSetManagerImpl.java b/src/java/com/threerings/media/tile/TileSetManagerImpl.java index bfc397bc0..59152e76c 100644 --- a/src/java/com/threerings/media/tile/TileSetManagerImpl.java +++ b/src/java/com/threerings/media/tile/TileSetManagerImpl.java @@ -1,20 +1,34 @@ // -// $Id: TileSetManagerImpl.java,v 1.3 2001/07/18 21:45:42 shaper Exp $ +// $Id: TileSetManagerImpl.java,v 1.4 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.tile; +import com.threerings.media.ImageManager; + import com.samskivert.util.IntMap; +import java.awt.Image; import java.util.ArrayList; import java.util.Enumeration; public abstract class TileSetManagerImpl implements TileSetManager { + public TileSetManagerImpl (ImageManager imgr) + { + _imgr = imgr; + } + public TileSet getTileSet (int tsid) { return (TileSet)_tilesets.get(tsid); } + public Image getTileImage (int tsid, int tid) + { + TileSet tset = getTileSet(tsid); + return (tset != null) ? tset.getTileImage(_imgr, tid) : null; + } + public ArrayList getTileSets () { int size = _tilesets.size(); @@ -35,5 +49,6 @@ public abstract class TileSetManagerImpl implements TileSetManager return _tilesets.size(); } + protected ImageManager _imgr; protected IntMap _tilesets = new IntMap(); } diff --git a/src/java/com/threerings/miso/client/IsoSceneView.java b/src/java/com/threerings/miso/client/IsoSceneView.java index 1f7f623fd..6660e158b 100644 --- a/src/java/com/threerings/miso/client/IsoSceneView.java +++ b/src/java/com/threerings/miso/client/IsoSceneView.java @@ -1,5 +1,5 @@ // -// $Id: IsoSceneView.java,v 1.6 2001/07/18 21:45:42 shaper Exp $ +// $Id: IsoSceneView.java,v 1.7 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.scene; @@ -7,7 +7,6 @@ import com.threerings.miso.Log; import com.threerings.miso.tile.Tile; import com.threerings.miso.tile.TileManager; import com.threerings.miso.util.MathUtil; -import com.threerings.media.ImageManager; import java.awt.*; import java.awt.image.*; @@ -89,7 +88,7 @@ public class IsoSceneView implements SceneView // TODO: draw layers L1+. Tile tile = _scene.tiles[tx][ty][Scene.LAYER_BASE]; - int ypos = screenY - (tile.img.getHeight() - Tile.HEIGHT); + int ypos = screenY - (tile.height - Tile.HEIGHT); g2.drawImage(tile.img, screenX, ypos, null); //paintCoords(g2, tx, ty, screenX, screenY); diff --git a/src/java/com/threerings/miso/tile/CompiledTileSetManager.java b/src/java/com/threerings/miso/tile/CompiledTileSetManager.java index ec93856d2..c3044dcc7 100644 --- a/src/java/com/threerings/miso/tile/CompiledTileSetManager.java +++ b/src/java/com/threerings/miso/tile/CompiledTileSetManager.java @@ -1,13 +1,20 @@ // -// $Id: CompiledTileSetManager.java,v 1.4 2001/07/18 21:45:42 shaper Exp $ +// $Id: CompiledTileSetManager.java,v 1.5 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.tile; +import com.threerings.media.ImageManager; + import java.io.InputStream; import java.io.IOException; public class CompiledTileSetManager extends TileSetManagerImpl { + public CompiledTileSetManager (ImageManager imgr) + { + super(imgr); + } + public void loadTileSets (InputStream tis) throws IOException { // TBD diff --git a/src/java/com/threerings/miso/tile/EditableTileSetManager.java b/src/java/com/threerings/miso/tile/EditableTileSetManager.java index 803f84597..4a28f75b2 100644 --- a/src/java/com/threerings/miso/tile/EditableTileSetManager.java +++ b/src/java/com/threerings/miso/tile/EditableTileSetManager.java @@ -1,9 +1,10 @@ // -// $Id: EditableTileSetManager.java,v 1.4 2001/07/18 21:45:42 shaper Exp $ +// $Id: EditableTileSetManager.java,v 1.5 2001/07/18 22:45:35 shaper Exp $ package com.threerings.miso.tile; import com.threerings.miso.Log; +import com.threerings.media.ImageManager; import java.io.IOException; import java.io.InputStream; @@ -17,6 +18,11 @@ import java.util.ArrayList; */ public class EditableTileSetManager extends TileSetManagerImpl { + public EditableTileSetManager (ImageManager imgr) + { + super(imgr); + } + public void loadTileSets (InputStream tis) throws IOException { // read all tileset descriptions from the XML input stream diff --git a/tests/rsrc/config/miso/tilesets.xml b/tests/rsrc/config/miso/tilesets.xml index be204b96a..e1ebdecd5 100644 --- a/tests/rsrc/config/miso/tilesets.xml +++ b/tests/rsrc/config/miso/tilesets.xml @@ -1,6 +1,6 @@ - + @@ -9,7 +9,7 @@ Ground 1000 - rsrc/media/miso/tiles-base.png + media/miso/tiles-base.png 16, 16 10, 10 @@ -17,9 +17,9 @@ Wall 1001 - rsrc/media/miso/tiles-wall.png + media/miso/tiles-wall.png 45, 45 - 2, 2 + 3, 3 diff --git a/tests/rsrc/media/miso/tiles-wall.png b/tests/rsrc/media/miso/tiles-wall.png new file mode 100644 index 0000000000000000000000000000000000000000..80594aeb54f452fda32188ff922ecea5496341df GIT binary patch literal 1183 zcmeAS@N?(olHy`uVBq!ia0vp^2|ygh!3HGVD}z4+DVB6cUq=Rpjs4tz5?O)VFi#i9 zkcv5P=h)_mxJ$IjOHVr5wV*)wqk4q{!zvG?P;^X2CEYrft69*kT45>a1M&t>*`5p*a9QZ4`Icn`Ro6FxVZ~pISJ@{}_ z(f&I&Ust8y=i7g0&yH7-O&k87WC;j>W)?ay18G3{g{v_d~xmnB&I6eniE;# z8}Iyc7dOaw=o~h?2G z53q!M=GehdbFRPq5!=fZGa8EI9^GS%dvg4Bp}*TjL!imb=a~OLJ$|3T{_(w@#gl6? zA91DgSuok0?LXddc*pTeo}CWD7f$BhIx5dz$E2qiQX`+Vr845jUd|m&2M$CZVF@v2 z%$dk%$T;`S;@HWR57w0|;C%7KV1w@ezjEf>R?P=wCLCue*(CX6?zb<8vmdB@*dDTg zGvW;A19OGb{r4G`7kqZQxPQXThNC}TzQ}yA`_CQq2Ydx0-`5H73J081Dd5}D^*e?$ zFY1@-yKN%8)q>Hp;tL)hYW%aKwc5U;Etb*SnB62p|MCBMU0ypnJmBl;M!Ro`>YPFYbbiwSkHaDT40vA5#!SwhB}6QtYr_ya$Wa+ zuYMyIa<1%w*gNGvVJzt`SJyQ>Vw04bohqVvay~=dk-Jqc%NI|MXWX~>;ih*Xvr}7^ z+*_lpGP7aTq3SK%3#aPqEr@>T?B?4tqalu4-bhPn5x2ya4~mPG7Udu6)ed?h3QU(_ iQFW=!ZmQ;G|HOYr$AI1HYyelF{r5}E*#Zy4_Y literal 0 HcmV?d00001