0
votes

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?

2
Please add the error log and show exactly what is happening (Maybe a screen shot?)Fish

2 Answers

0
votes

maybe have that make dispose this part "source":

Pixmap source = manager.get(filename, Pixmap.class);
    fromPixMap(source);

maybe help you.if you do not use more

0
votes

I found the problem. I forgot to dispose a 1x1 background fill. I forgot that I used this class to create the 1x1 image by sending it the Pixmap. The 1x1 was converted to a 512x512 and that was accumulating through the levels until it hit the limit. I added a dispose for that background fill image where it is being created and that fixed this whole issue.

I also updated the tile generator to use the smallest required texture size and to also go up to GL_MAX_TEXTURE_SIZE. This way the 1x1 images are really 1x1 and the 5000x3000 is less tiles depending on the graphics hardware. Here is the new code:

public class LargeImage extends LoadableGroup {
private boolean smooth=true;
public String filename;
public int trueWidth,trueHeight;
private ArrayList<Image> tiles=new ArrayList<Image>();
private ArrayList<Texture> textures=new ArrayList<Texture>();
private int maxTextureSize;
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 draw(Batch batch, float parentAlpha)
{
    //setCullingArea(new Rectangle(0,0,200,200));
    batch.setColor(getColor());
    super.draw(batch,parentAlpha);
}
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)
{
    if(Gdx.app.getType()== Application.ApplicationType.WebGL)
        maxTextureSize=512;
    else
        maxTextureSize=Gdx.gl.GL_MAX_TEXTURE_SIZE;
    for (int x = 0;x < source.getWidth(); x+=maxTextureSize) {
        for (int y = 0;y < source.getHeight(); y+=maxTextureSize) {
            int tSize=getTextureSize(source.getWidth()-x,source.getHeight()-y);
            Pixmap p = new Pixmap(tSize, tSize, Pixmap.Format.RGBA8888);
            p.drawPixmap(source, 0, 0, x, y, tSize, tSize);
            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();
}
public int getTextureSize(int width, int height)
{
    int n=Math.max(width,height);
    if(n>=maxTextureSize)
        return maxTextureSize;
    n--;
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    n++;
    return n;
}


@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();
    }

}

public void softDispose(CallbackAssetManager m) {
    for(int i=0;i<textures.size();i++) {
        textures.get(i).dispose();
    }

}
}