wyvern.lib
Class ImageCache

java.lang.Object
  extended bywyvern.common.util.ClientImageCache
      extended bywyvern.lib.ImageCache

public class ImageCache
extends ClientImageCache

Server-side cache for mapping tile numbers to images.

The cache provides methods for extracting 32x32 pieces of an image, and it will cache the pieces.

Version:
1.0, Oct 4, 1998
Author:
Steve Yegge

Field Summary
 
Fields inherited from class wyvern.common.util.ClientImageCache
cache_, images_, itemList_, maxSize_, QMARK, TILE_WIDTH, tiles_
 
Constructor Summary
protected ImageCache()
          Prevent instantiation.
 
Method Summary
static java.awt.Image extractImage(int tile, int x, int y)
          Loads (and caches) a piece of an image given its tile number and (x,y) offsets.
static java.awt.Image getImage(Archetype arch)
          Retrieves (and caches) the image for the specified archetype.
static java.awt.Image getImage(int tile)
          Loads an image from its tile number.
 java.lang.String getImageNameFromTile(int tile)
          Looks up the image name in our cache.
static java.awt.Image loadImage(java.lang.String path)
          Loads an image from a resource (a file or jar file).
static java.awt.Image loadImageFromRelativePath(java.lang.String path)
          Loads an image, given a "normal" image path that's relative to a wiz directory or the built-in game art directory.
protected  java.awt.Image loadImageResource(java.lang.String path)
          Loads resource headlessly.
 
Methods inherited from class wyvern.common.util.ClientImageCache
addTileNumber, clear, extractPiece, extractSpecial, getInstance, iextractImage, igetImage, setInitialTileMappings, setMaxSize, storeImage, uncacheImage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImageCache

protected ImageCache()
Prevent instantiation. This class is a Singleton.

Method Detail

getImage

public static java.awt.Image getImage(int tile)
Loads an image from its tile number.

Parameters:
tile - the database tile number
Returns:
the Image for that tile, or null if not found

extractImage

public static java.awt.Image extractImage(int tile,
                                          int x,
                                          int y)
Loads (and caches) a piece of an image given its tile number and (x,y) offsets.

The (x,y) offsets correspond to the tile location within the image to extract. For instance, if the image is 64x64, i.e. 2x2 tiles, then an (x,y) offset of (0,1) will extract and return the lower-left corner of the image.

Parameters:
tile - the TileDatabase number of the image
x - the x tile offset into the image
y - the y tile offset into the image
Returns:
the cropped 32x32 image, or null if the original image wasn't found.
Throws:
java.lang.IllegalArgumentException - if either offset is invalid

getImage

public static java.awt.Image getImage(Archetype arch)
Retrieves (and caches) the image for the specified archetype. Images are keyed using their unique tile IDs, so they can be shared among archetypes. We need to get the tile ID from the archetype before looking it up in the table.

Parameters:
arch - the Archetype whose image to load

getImageNameFromTile

public java.lang.String getImageNameFromTile(int tile)
Looks up the image name in our cache.

This version of the ImageCache is used by the map editor, so load the image synchronously when it's requested. Our superclass returns null unless the image is available.

Overrides:
getImageNameFromTile in class ClientImageCache
Parameters:
tile - the tile database number
Returns:
the Image for that tile, or null if it doesn't exist

loadImage

public static java.awt.Image loadImage(java.lang.String path)
Loads an image from a resource (a file or jar file).

Parameters:
path - the path to the image, relative to the system classpath. Example: "wyvern/art/game/foo/bar". Don't pass in the extension unless it's a PNG file, in which case pass the ".png" extension.
Returns:
the Image, if it could be loaded, else null
Throws:
java.lang.NullPointerException - if the passed path is null

loadImageResource

protected java.awt.Image loadImageResource(java.lang.String path)
Loads resource headlessly.

Overrides:
loadImageResource in class ClientImageCache
Parameters:
path - the relative path to the image, using the naming conventions required by wyvern.common.config.Wyvern.loadImage()

loadImageFromRelativePath

public static java.awt.Image loadImageFromRelativePath(java.lang.String path)
Loads an image, given a "normal" image path that's relative to a wiz directory or the built-in game art directory.

Parameters:
path - the relative image path, e.g. "wiz/foobar/blah.gif", or "monsters/goblin/troll.S.gif"
Returns:
the Image, if it could be loaded, else null
Throws:
java.lang.NullPointerException - if you pass a null path