For a game I'm developing I must use large background images. These images are around 5000x3000. When attempting to display that as a single texture I would get black boxes in GWT and Android. I created a class that splits the image into a grid of textures:
public class LargeImage extends LoadableGroup {
private boolean smooth=true;
public String filename;
private int trueWidth,trueHeight;
private ArrayList<Image> tiles=new ArrayList<Image>();
private ArrayList<Texture> textures=new ArrayList<Texture>();
private final int maxTextureSize = 512;
public LargeImage(String filename, CallbackAssetManager manager)
{
super();
this.filename=filename;
PixmapParameter param=new PixmapParameter();
param.format=Pixmap.Format.RGBA8888;
addAsset(new AssetDescriptor(filename, Pixmap.class,param));
manager.add(this);
}
public LargeImage(Pixmap map, boolean smooth)
{
super();
this.smooth=smooth;
fromPixMap(map);
}
public void loaded(CallbackAssetManager manager)
{
if(!manager.isLoaded(filename))
{
Gdx.app.log("Load Error",filename);
return;
}
Pixmap source = manager.get(filename, Pixmap.class);
fromPixMap(source);
}
private void fromPixMap(Pixmap source)
{
for (int x = 0;x < source.getWidth(); x+=maxTextureSize) {
for (int y = 0;y < source.getHeight(); y+=maxTextureSize) {
Pixmap p = new Pixmap(maxTextureSize, maxTextureSize, Pixmap.Format.RGBA8888);
p.drawPixmap(source, 0, 0, x, y, maxTextureSize, maxTextureSize);
Texture t=new Texture(new PixmapTextureData(p, Pixmap.Format.RGBA8888, true, false, true));
textures.add(t);
if(smooth)
t.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
UncoloredImage tile = new UncoloredImage(t);
tile.setTouchable(Touchable.disabled);
tile.setX(x);
tile.setY(source.getHeight()-y-tile.getHeight());
p.dispose();
this.addActor(tile);
tiles.add(tile);
}
}
super.setWidth(source.getWidth());
super.setHeight(source.getHeight());
trueWidth=source.getWidth();
trueHeight=source.getHeight();
}
@Override
public void setWidth(float width) {
setScaleX(width/trueWidth);
super.setWidth(Math.abs(width));
}
public void setHeight(float height) {
setScaleY(height / (float)trueHeight);
super.setHeight(Math.abs(height));
}
@Override
public void dispose(CallbackAssetManager m) {
super.dispose(m);
for(int i=0;i<textures.size();i++)
textures.get(i).dispose();
}
}
The issue with this comes when I need to load other levels. After about 6 loads I get GL out or memory errors. I am keeping a reference to each texture I create and I dispose of everything when loading a new level. How come I am running out of memory even though I dispose the old textures?