TileImage: teach MakeTiles module how to use LoaderContext

This commit is contained in:
Jason Long 2014-08-15 13:08:26 -07:00
parent 92776621d6
commit 860faa1081
2 changed files with 40 additions and 14 deletions

View file

@ -18,7 +18,6 @@ import static micropolisj.XML_Helper.*;
public class MakeTiles public class MakeTiles
{ {
static HashMap<String,String> tileData = new HashMap<String,String>(); static HashMap<String,String> tileData = new HashMap<String,String>();
static HashMap<String,SourceImage> loadedImages = new HashMap<String,SourceImage>();
static final Charset UTF8 = Charset.forName("UTF-8"); static final Charset UTF8 = Charset.forName("UTF-8");
static int SKIP_TILES = 0; static int SKIP_TILES = 0;
@ -431,6 +430,30 @@ public class MakeTiles
} }
} }
static class MyLoaderContext implements LoaderContext
{
HashMap<String,SourceImage> loadedImages = new HashMap<String,SourceImage>();
//implements LoaderContext
public SourceImage getDefaultImage()
{
throw new UnsupportedOperationException();
}
//implements LoaderContext
public SourceImage getImage(String fileName)
throws IOException
{
if (!loadedImages.containsKey(fileName)) {
loadedImages.put(fileName,
loadImageNoCache(fileName));
}
return loadedImages.get(fileName);
}
}
static MyLoaderContext loaderContext = new MyLoaderContext();
static TileImage loadImage(String fileName) static TileImage loadImage(String fileName)
throws IOException throws IOException
{ {
@ -439,12 +462,7 @@ public class MakeTiles
return loadImageXml(xmlFile); return loadImageXml(xmlFile);
} }
if (!loadedImages.containsKey(fileName)) { return loaderContext.getImage(fileName);
loadedImages.put(fileName,
loadImageNoCache(fileName));
}
return loadedImages.get(fileName);
} }
static SourceImage loadImageReal(File pngFile, int basisSize) static SourceImage loadImageReal(File pngFile, int basisSize)

View file

@ -2,6 +2,7 @@ package micropolisj.graphics;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*; import java.util.*;
import javax.xml.stream.*; import javax.xml.stream.*;
@ -125,20 +126,27 @@ public abstract class TileImage
public interface LoaderContext public interface LoaderContext
{ {
SourceImage getDefaultImage(); SourceImage getDefaultImage()
SourceImage getImage(String name); throws IOException;
SourceImage getImage(String name)
throws IOException;
} }
static SimpleTileImage readSimpleImage(XMLStreamReader in, LoaderContext ctx) static SimpleTileImage readSimpleImage(XMLStreamReader in, LoaderContext ctx)
throws XMLStreamException throws XMLStreamException
{ {
SimpleTileImage img = new SimpleTileImage(); SimpleTileImage img = new SimpleTileImage();
String srcImageName = in.getAttributeValue(null, "src"); try {
if (srcImageName != null) { String srcImageName = in.getAttributeValue(null, "src");
img.srcImage = ctx.getImage(srcImageName); if (srcImageName != null) {
img.srcImage = ctx.getImage(srcImageName);
}
else {
img.srcImage = ctx.getDefaultImage();
}
} }
else { catch (IOException e) {
img.srcImage = ctx.getDefaultImage(); throw new XMLStreamException("image source not found", e);
} }
String tmp = in.getAttributeValue(null, "offsetY"); String tmp = in.getAttributeValue(null, "offsetY");
img.offsetY = tmp != null ? Integer.parseInt(tmp) : 0; img.offsetY = tmp != null ? Integer.parseInt(tmp) : 0;