graphics: MakeTiles: support layering two or more images together

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@705 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-06-26 13:12:52 +00:00
parent 57fd5c0847
commit 2e217755a3
2 changed files with 76 additions and 37 deletions

View file

@ -1,3 +1,23 @@
===============
The Recipe File
===============
The `tiles.dat' file is the recipe file for generating the tiles.
It is a plain-text file, where each line begins with a tile id,
followed by an image specification.
The image specification consists of one or more image layers separated
by the pipe character (|). The layers are drawn in the order specified.
A layer is a file name (excluding the .png extension), optionally followed
by @X,Y,W,H, where X,Y specify the upper-left corner of the rectangle to
draw, and W,H specify the width and height of the rectangle to draw.
==============================
Generating the composite image
==============================
To build a new tiles.png file, do the following: To build a new tiles.png file, do the following:
1. Open a command-prompt, and cd to the graphics subdirectory. 1. Open a command-prompt, and cd to the graphics subdirectory.

View file

@ -42,7 +42,20 @@ public class MakeTiles
String tileImage = tileData.get(Integer.toString(i)); String tileImage = tileData.get(Integer.toString(i));
assert tileImage != null; assert tileImage != null;
ImageRef ref = parseImageRef(tileImage); ImageSpec ref = parseImageSpec(tileImage);
drawTo(ref, gr, 0, TILE_SIZE*i);
}
ImageIO.write(buf, "png", outputFile);
}
static void drawTo(ImageSpec ref, Graphics2D gr, int destX, int destY)
throws IOException
{
if (ref.background != null) {
drawTo(ref.background, gr, destX, destY);
}
if (!loadedImages.containsKey(ref.fileName)) { if (!loadedImages.containsKey(ref.fileName)) {
loadedImages.put(ref.fileName, loadedImages.put(ref.fileName,
loadImage(ref.fileName)); loadImage(ref.fileName));
@ -54,19 +67,17 @@ public class MakeTiles
gr.drawImage( gr.drawImage(
sourceImg, sourceImg,
0, TILE_SIZE*i, destX, destY,
TILE_SIZE, TILE_SIZE*(i+1), destX+TILE_SIZE, destY+TILE_SIZE,
ref.offsetX, ref.offsetY, ref.offsetX, ref.offsetY,
ref.offsetX + (ref.width != 0 ? ref.width : srcWidth), ref.offsetX + (ref.width != 0 ? ref.width : srcWidth),
ref.offsetY + (ref.height != 0 ? ref.height : srcHeight), ref.offsetY + (ref.height != 0 ? ref.height : srcHeight),
null); null);
} }
ImageIO.write(buf, "png", outputFile); static class ImageSpec
}
static class ImageRef
{ {
ImageSpec background;
String fileName; String fileName;
int offsetX; int offsetX;
int offsetY; int offsetY;
@ -74,11 +85,17 @@ public class MakeTiles
int height; int height;
} }
static ImageRef parseImageRef(String s) static ImageSpec parseImageSpec(String chain)
{ {
ImageRef rv = new ImageRef(); ImageSpec result = null;
String [] parts = s.split("@", 2); for (String layerStr : chain.split("\\|")) {
ImageSpec rv = new ImageSpec();
rv.background = result;
result = rv;
String [] parts = layerStr.split("@", 2);
rv.fileName = parts[0]; rv.fileName = parts[0];
if (parts.length >= 2) { if (parts.length >= 2) {
@ -98,7 +115,9 @@ public class MakeTiles
} }
}//endif something given after '@' in image specifier }//endif something given after '@' in image specifier
return rv; }//end foreach layer in image specification
return result;
} }
static Image loadImage(String fileName) static Image loadImage(String fileName)