First pass at just in time compositing (JITC!). Things aren't quite as
fast as I'd have hoped, but I'm optimistic that the image compression business that Walter and I are embarking upon will solve that handily. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1333 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: CharacterManager.java,v 1.17 2002/04/15 23:06:49 mdb Exp $
|
// $Id: CharacterManager.java,v 1.18 2002/05/04 06:57:24 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.cast;
|
package com.threerings.cast;
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ import java.util.Iterator;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import com.samskivert.util.Tuple;
|
import com.samskivert.util.Tuple;
|
||||||
|
import org.apache.commons.collections.LRUMap;
|
||||||
|
|
||||||
import com.threerings.media.sprite.MultiFrameImage;
|
import com.threerings.media.sprite.MultiFrameImage;
|
||||||
|
|
||||||
@@ -168,6 +169,8 @@ public class CharacterManager
|
|||||||
|
|
||||||
// if that failed, we'll just have to generate the danged things
|
// if that failed, we'll just have to generate the danged things
|
||||||
if (frames == null) {
|
if (frames == null) {
|
||||||
|
Log.info("Creating frames [descrip=" + descrip +
|
||||||
|
", action=" + action + "].");
|
||||||
// do the compositing
|
// do the compositing
|
||||||
frames = createCompositeFrames(descrip, action);
|
frames = createCompositeFrames(descrip, action);
|
||||||
// cache the result on disk if we've got such a cache
|
// cache the result on disk if we've got such a cache
|
||||||
@@ -194,8 +197,6 @@ public class CharacterManager
|
|||||||
CharacterDescriptor descrip, String action)
|
CharacterDescriptor descrip, String action)
|
||||||
throws NoSuchComponentException
|
throws NoSuchComponentException
|
||||||
{
|
{
|
||||||
MultiFrameImage[] frames = new MultiFrameImage[DIRECTION_COUNT];
|
|
||||||
|
|
||||||
int[] cids = descrip.getComponentIds();
|
int[] cids = descrip.getComponentIds();
|
||||||
int ccount = cids.length;
|
int ccount = cids.length;
|
||||||
Colorization[][] zations = descrip.getColorizations();
|
Colorization[][] zations = descrip.getColorizations();
|
||||||
@@ -214,12 +215,20 @@ public class CharacterManager
|
|||||||
// sort them into the proper rendering order
|
// sort them into the proper rendering order
|
||||||
Arrays.sort(components);
|
Arrays.sort(components);
|
||||||
|
|
||||||
// now composite the component frames, one atop the next; using
|
// create the multiframe image and colorization arrays that we'll
|
||||||
// the colorizations if we have them
|
// supply to the lazy compositing result multiframe image
|
||||||
for (int i = 0; i < ccount; i++) {
|
CompositedFrameImage[] frames =
|
||||||
ColorizedComponent cc = components[i];
|
new CompositedFrameImage[DIRECTION_COUNT];
|
||||||
TileUtil.compositeFrames(
|
zations = new Colorization[ccount][];
|
||||||
frames, cc.component.getFrames(action), cc.zations);
|
for (int orient = 0; orient < DIRECTION_COUNT; orient++) {
|
||||||
|
MultiFrameImage[] sources = new MultiFrameImage[ccount];
|
||||||
|
for (int cc = 0; cc < ccount; cc++) {
|
||||||
|
ColorizedComponent ccomp = components[cc];
|
||||||
|
sources[cc] = ccomp.component.getFrames(action)[orient];
|
||||||
|
zations[cc] = ccomp.zations;
|
||||||
|
}
|
||||||
|
frames[orient] = new CompositedFrameImage(
|
||||||
|
action + orient, sources, zations);
|
||||||
}
|
}
|
||||||
|
|
||||||
return frames;
|
return frames;
|
||||||
@@ -250,11 +259,15 @@ public class CharacterManager
|
|||||||
protected HashMap _actions = new HashMap();
|
protected HashMap _actions = new HashMap();
|
||||||
|
|
||||||
/** A cache of composited animation frames. */
|
/** A cache of composited animation frames. */
|
||||||
protected HashMap _frames = new HashMap();
|
protected LRUMap _frames = new LRUMap(ACTION_CACHE_SIZE);
|
||||||
|
|
||||||
/** The character class to be created. */
|
/** The character class to be created. */
|
||||||
protected Class _charClass = CharacterSprite.class;
|
protected Class _charClass = CharacterSprite.class;
|
||||||
|
|
||||||
/** The action animation cache, if we have one. */
|
/** The action animation cache, if we have one. */
|
||||||
protected ActionCache _acache;
|
protected ActionCache _acache;
|
||||||
|
|
||||||
|
/** The number of actions to cache before we start clearing them
|
||||||
|
* out. */
|
||||||
|
protected static final int ACTION_CACHE_SIZE = 5;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
//
|
||||||
|
// $Id: CompositedActionFrames.java,v 1.1 2002/05/04 06:57:24 mdb Exp $
|
||||||
|
|
||||||
|
package com.threerings.cast;
|
||||||
|
|
||||||
|
import java.awt.Image;
|
||||||
|
import com.threerings.media.sprite.MultiFrameImage;
|
||||||
|
import com.threerings.cast.util.TileUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An implementation of the {@link MultiFrameImage} interface that is used
|
||||||
|
* to lazily create composited character frames when they are requested.
|
||||||
|
*/
|
||||||
|
public class CompositedFrameImage implements MultiFrameImage
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Constructs a composited frame image with the supplied source
|
||||||
|
* component images and colorization configuration. The actual
|
||||||
|
* component frame images will not be composited until they are
|
||||||
|
* requested.
|
||||||
|
*/
|
||||||
|
public CompositedFrameImage (
|
||||||
|
String action, MultiFrameImage[] sources, Colorization[][] zations)
|
||||||
|
{
|
||||||
|
_action = action;
|
||||||
|
_sources = sources;
|
||||||
|
_zations = zations;
|
||||||
|
|
||||||
|
// the sources must all have the same frame count, so we just use
|
||||||
|
// the count from the first one
|
||||||
|
int frameCount = _sources[0].getFrameCount();
|
||||||
|
_images = new Image[frameCount];
|
||||||
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
|
public int getFrameCount ()
|
||||||
|
{
|
||||||
|
return _images.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// documentation inherited
|
||||||
|
public Image getFrame (int index)
|
||||||
|
{
|
||||||
|
// create the frame if we don't already have it
|
||||||
|
if (_images[index] == null) {
|
||||||
|
Log.info("Compositing [action=" + _action +
|
||||||
|
", index=" + index + "].");
|
||||||
|
_images[index] =
|
||||||
|
TileUtil.compositeFrames(index, _sources, _zations);
|
||||||
|
}
|
||||||
|
return _images[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String _action;
|
||||||
|
|
||||||
|
/** Our source frame images. */
|
||||||
|
protected MultiFrameImage[] _sources;
|
||||||
|
|
||||||
|
/** The colorizations for our source components. */
|
||||||
|
protected Colorization[][] _zations;
|
||||||
|
|
||||||
|
/** The frame images. */
|
||||||
|
protected Image[] _images;
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: TileUtil.java,v 1.12 2002/03/16 03:15:05 shaper Exp $
|
// $Id: TileUtil.java,v 1.13 2002/05/04 06:57:24 mdb Exp $
|
||||||
|
|
||||||
package com.threerings.cast.util;
|
package com.threerings.cast.util;
|
||||||
|
|
||||||
@@ -21,98 +21,112 @@ import com.threerings.cast.Colorization;
|
|||||||
public class TileUtil
|
public class TileUtil
|
||||||
implements DirectionCodes
|
implements DirectionCodes
|
||||||
{
|
{
|
||||||
|
// /**
|
||||||
|
// * Renders each of the given <code>src</code> component frames into
|
||||||
|
// * the corresponding frames of <code>dest</code>, allocating blank
|
||||||
|
// * image frames for <code>dest</code> if none yet exist. If
|
||||||
|
// * <code>zations</code> is not null, the provided list of
|
||||||
|
// * colorizations will be applied as well.
|
||||||
|
// *
|
||||||
|
// * @throws IllegalArgumentException if the frame count of the source
|
||||||
|
// * and destination images don't match.
|
||||||
|
// */
|
||||||
|
// public static void compositeFrames (
|
||||||
|
// MultiFrameImage[] dest, MultiFrameImage[] src,
|
||||||
|
// Colorization[] zations)
|
||||||
|
// {
|
||||||
|
// for (int orient = 0; orient < DIRECTION_COUNT; orient++) {
|
||||||
|
// MultiFrameImage sframes = src[orient];
|
||||||
|
// MultiFrameImage dframes = dest[orient];
|
||||||
|
|
||||||
|
// // create blank destination frames if needed
|
||||||
|
// if (dframes == null) {
|
||||||
|
// dframes = (dest[orient] = new BlankFrameImage(sframes));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // sanity check
|
||||||
|
// int dsize = dframes.getFrameCount();
|
||||||
|
// int ssize = sframes.getFrameCount();
|
||||||
|
// if (dsize != ssize) {
|
||||||
|
// String errmsg = "Can't composite images with inequal " +
|
||||||
|
// "frame counts [dest=" + dsize + ", src=" + ssize + "].";
|
||||||
|
// throw new IllegalArgumentException(errmsg);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // slap the images together
|
||||||
|
// for (int ii = 0; ii < dsize; ii++) {
|
||||||
|
// Image dimg = dframes.getFrame(ii);
|
||||||
|
// BufferedImage simg = (BufferedImage)sframes.getFrame(ii);
|
||||||
|
|
||||||
|
// // recolor the source image
|
||||||
|
// if (zations != null) {
|
||||||
|
// for (int i = 0; i < zations.length; i++) {
|
||||||
|
// Colorization cz = zations[i];
|
||||||
|
// if (zations[i] != null) {
|
||||||
|
// simg = ImageUtil.recolorImage(
|
||||||
|
// simg, cz.rootColor, cz.range, cz.offsets);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // now splat the recolored image onto the target
|
||||||
|
// Graphics g = dimg.getGraphics();
|
||||||
|
// g.drawImage(simg, 0, 0, null);
|
||||||
|
// g.dispose();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders each of the given <code>src</code> component frames into
|
* Renders and returns the <code>index</code>th image from each of the
|
||||||
* the corresponding frames of <code>dest</code>, allocating blank
|
* supplied source multi-frame images to a newly created buffered
|
||||||
* image frames for <code>dest</code> if none yet exist. If
|
* image. This is used to render a single frame of a composited
|
||||||
* <code>zations</code> is not null, the provided list of
|
* character action, and accordingly, the source image array should be
|
||||||
* colorizations will be applied as well.
|
* already sorted into the proper rendering order.
|
||||||
*
|
|
||||||
* @throws IllegalArgumentException if the frame count of the source
|
|
||||||
* and destination images don't match.
|
|
||||||
*/
|
*/
|
||||||
public static void compositeFrames (
|
public static Image compositeFrames (
|
||||||
MultiFrameImage[] dest, MultiFrameImage[] src,
|
int index, MultiFrameImage[] sources, Colorization[][] zations)
|
||||||
Colorization[] zations)
|
|
||||||
{
|
{
|
||||||
for (int orient = 0; orient < DIRECTION_COUNT; orient++) {
|
Image dest = null;
|
||||||
MultiFrameImage sframes = src[orient];
|
Graphics g = null;
|
||||||
MultiFrameImage dframes = dest[orient];
|
|
||||||
|
|
||||||
// create blank destination frames if needed
|
long start = System.currentTimeMillis();
|
||||||
if (dframes == null) {
|
for (int ii = 0; ii < sources.length; ii++) {
|
||||||
dframes = (dest[orient] = new BlankFrameImage(sframes));
|
BufferedImage simg = (BufferedImage)sources[ii].getFrame(index);
|
||||||
|
|
||||||
|
// create the image now that we know how big it should be
|
||||||
|
if (dest == null) {
|
||||||
|
dest = ImageUtil.createImage(
|
||||||
|
simg.getWidth(null), simg.getHeight(null));
|
||||||
|
g = dest.getGraphics();
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanity check
|
// recolor the source image
|
||||||
int dsize = dframes.getFrameCount();
|
if (zations != null) {
|
||||||
int ssize = sframes.getFrameCount();
|
int zcount = zations[ii].length;
|
||||||
if (dsize != ssize) {
|
for (int zz = 0; zz < zcount; zz++) {
|
||||||
String errmsg = "Can't composite images with inequal " +
|
Colorization cz = zations[ii][zz];
|
||||||
"frame counts [dest=" + dsize + ", src=" + ssize + "].";
|
if (cz != null) {
|
||||||
throw new IllegalArgumentException(errmsg);
|
simg = ImageUtil.recolorImage(
|
||||||
}
|
simg, cz.rootColor, cz.range, cz.offsets);
|
||||||
|
|
||||||
// slap the images together
|
|
||||||
for (int ii = 0; ii < dsize; ii++) {
|
|
||||||
Image dimg = dframes.getFrame(ii);
|
|
||||||
BufferedImage simg = (BufferedImage)sframes.getFrame(ii);
|
|
||||||
|
|
||||||
// recolor the source image
|
|
||||||
if (zations != null) {
|
|
||||||
for (int i = 0; i < zations.length; i++) {
|
|
||||||
Colorization cz = zations[i];
|
|
||||||
if (zations[i] != null) {
|
|
||||||
simg = ImageUtil.recolorImage(
|
|
||||||
simg, cz.rootColor, cz.range, cz.offsets);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// now splat the recolored image onto the target
|
|
||||||
Graphics g = dimg.getGraphics();
|
|
||||||
g.drawImage(simg, 0, 0, null);
|
|
||||||
g.dispose();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// now splat the recolored image onto the target
|
||||||
* An implementation of the {@link MultiFrameImage} interface that
|
g.drawImage(simg, 0, 0, null);
|
||||||
* initializes itself to contain a specified number of blank
|
|
||||||
* frames of the requested dimensions.
|
|
||||||
*/
|
|
||||||
protected static class BlankFrameImage implements MultiFrameImage
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Constructs a blank frame image based on the dimensions of
|
|
||||||
* template multi-frame image.
|
|
||||||
*/
|
|
||||||
public BlankFrameImage (MultiFrameImage template)
|
|
||||||
{
|
|
||||||
int frameCount = template.getFrameCount();
|
|
||||||
_images = new Image[frameCount];
|
|
||||||
for (int i = 0; i < frameCount; i++) {
|
|
||||||
Image img = template.getFrame(i);
|
|
||||||
_images[i] = ImageUtil.createImage(
|
|
||||||
img.getWidth(null), img.getHeight(null));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
// clean up after ourselves
|
||||||
public int getFrameCount ()
|
if (g != null) {
|
||||||
{
|
g.dispose();
|
||||||
return _images.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// documentation inherited
|
long now = System.currentTimeMillis();
|
||||||
public Image getFrame (int index)
|
Log.info("Composited " + sources.length + " frames in " +
|
||||||
{
|
(now-start) + " millis.");
|
||||||
return _images[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The frame images. */
|
return dest;
|
||||||
protected Image[] _images;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The image file name suffix appended to component image file names. */
|
/** The image file name suffix appended to component image file names. */
|
||||||
|
|||||||
Reference in New Issue
Block a user