MakeTiles: a little program to assemble the image based on tiles.dat

git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@696 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
jason@long.name 2013-06-25 17:39:24 +00:00
parent 9f5584c124
commit 0018fe13ae
2 changed files with 1063 additions and 960 deletions

103
graphics/MakeTiles.java Normal file
View file

@ -0,0 +1,103 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
public class MakeTiles
{
static HashMap<String,String> tileData = new HashMap<String,String>();
static HashMap<String,Image> loadedImages = new HashMap<String,Image>();
static final int NTILES = 960;
static final int TILE_SIZE = 16;
public static void main(String [] args)
throws Exception
{
File recipeFile = new File(args[0]);
File outputFile = new File(args[1]);
BufferedReader in = new BufferedReader(
new FileReader(recipeFile));
String l;
while ( (l=in.readLine()) != null )
{
if (l.startsWith("#")) {
continue;
}
String[] parts = l.split("\\s+");
String tileName = parts[0];
String tileImage = parts[1];
tileData.put(tileName, tileImage);
}
in.close();
// actually assemble the image
BufferedImage buf = new BufferedImage(TILE_SIZE,TILE_SIZE*NTILES,BufferedImage.TYPE_INT_RGB);
Graphics2D gr = buf.createGraphics();
for (int i = 0; i < NTILES; i++) {
String tileImage = tileData.get(Integer.toString(i));
assert tileImage != null;
ImageRef ref = parseImageRef(tileImage);
if (!loadedImages.containsKey(ref.fileName)) {
loadedImages.put(ref.fileName,
loadImage(ref.fileName));
}
gr.drawImage(
loadedImages.get(ref.fileName),
0, TILE_SIZE*i,
TILE_SIZE, TILE_SIZE*(i+1),
ref.offsetX, ref.offsetY,
ref.offsetX + (ref.width != 0 ? ref.width : TILE_SIZE),
ref.offsetY + (ref.height != 0 ? ref.height : TILE_SIZE),
null);
}
ImageIO.write(buf, "png", outputFile);
}
static class ImageRef
{
String fileName;
int offsetX;
int offsetY;
int width;
int height;
}
static ImageRef parseImageRef(String s)
{
ImageRef rv = new ImageRef();
String [] parts = s.split("@", 2);
rv.fileName = parts[0];
String offsetInfo = parts[1];
parts = offsetInfo.split(",");
if (parts.length >= 1) {
rv.offsetX = Integer.parseInt(parts[0]);
}
if (parts.length >= 2) {
rv.offsetY = Integer.parseInt(parts[1]);
}
if (parts.length >= 3) {
rv.width = Integer.parseInt(parts[2]);
}
if (parts.length >= 4) {
rv.height = Integer.parseInt(parts[3]);
}
return rv;
}
static Image loadImage(String fileName)
throws IOException
{
ImageIcon ii = new ImageIcon(fileName+".png");
return ii.getImage();
}
}

File diff suppressed because it is too large Load diff