非常大的背景纹理

时间:2015-04-25 21:11:27

标签: android opengl-es libgdx textures

对于我正在开发的游戏,我必须使用大背景图像。这些图像大约是5000x3000。当试图将其显示为单个纹理时,我会在GWT和Android中获得黑盒子。我创建了一个将图像分割成纹理网格的类:

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

当我需要加载其他级别时,问题就出现了。大约6次加载后,我得到GL输出或内存错误。我保持对我创建的每个纹理的引用,并在加载新级别时处理所有内容。为什么即使我丢弃旧纹理,我的内存也会耗尽?

2 个答案:

答案 0 :(得分:0)

可能有这个部分&#34;来源&#34;:

Pixmap source = manager.get(filename, Pixmap.class);
    fromPixMap(source);
如果您不使用更多

,也许可以帮助您

答案 1 :(得分:0)

我发现了问题。我忘了处理1x1背景填充。我忘了我用这个类通过发送Pixmap来创建1x1图像。 1x1被转换为512x512,并且正在累积到水平,直到达到极限。我为正在创建的背景填充图像添加了一个处理,并修复了整个问题。

我还更新了tile生成器以使用所需的最小纹理大小,并且最多也达到了GL_MAX_TEXTURE_SIZE。这样,1x1图像实际上是1x1,而5000x3000是更少的图块,具体取决于图形硬件。这是新代码:

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

}
}