【问题标题】:How to reload libgdx unmanaged Texture after OpenGL context lossOpenGL上下文丢失后如何重新加载libgdx非托管纹理
【发布时间】:2013-02-10 23:17:34
【问题描述】:

我正在通过网络下载图像并将它们作为 Image Actor 添加到我的 libgdx UI 中:

Pixmap pm = new Pixmap(data, 0, data.length);
Texture t = new Texture(pm);
TextureRegion tr = new TextureRegion(t,200,300);
TextureRegionDrawable trd = new TextureRegionDrawable(tr);
Image icon = new Image();
icon.setDrawable(trd);

鉴于此,我需要某种方式重新加载纹理数据,因为当 OpenGL 上下文丢失时(例如,因为屏幕进入睡眠状态),纹理数据也会丢失。

我尝试制作自己的经理类,添加

DynamicTextureManager.register(t, pm); // Register texture together with the source pixmap

到上面的sn-p,在resume()我做:

DynamicTextureManager.reload();

经理类:

public class DynamicTextureManager {
    private static LinkedHashMap<Texture, Pixmap> theMap = new
      LinkedHashMap<Texture,Pixmap>();
    public static void reload() {
        Set<Entry<Texture,Pixmap>> es = theMap.entrySet();
        for(Entry<Texture,Pixmap> e : es) {
            Texture t = e.getKey();
            Pixmap p = e.getValue();

            t.draw(p, 0, 0);
        }   
    }

    public static void register(Texture t, Pixmap p) {
        theMap.put(t, p);
    }
}

但这无济于事 - 我最终仍然会卸载纹理和白色区域而不是图像。

这应该怎么做?我找不到任何代码来证明这一点!

【问题讨论】:

  • 我的猜测是Texture 不会神奇地重新创建底层纹理句柄。尝试重新创建Texture 本身,或者切换到所谓的托管 纹理数据(检查TextureData 的子类;PixmapTextureData 不支持这个概念。
  • 我有一个与此非常相似的任务。我还没有完成,但如果我让它工作(我认为这将涉及从二进制数据重建 Pixmap)我会用答案更新它。
  • @Jyro117 我添加了我当前的解决方案作为答案,请随时改进!
  • 啊,太好了,感谢您添加解决方案。我一定会在接下来的几天里更详细地查看它!

标签: android libgdx


【解决方案1】:

添加我的解决方案作为参考。我现在向我的经理注册 Image 对象和 Pixmap 对象,在 reload() 上,从 Pixmap 重新创建纹理,并为旧图像设置新纹理。对我有用,但欢迎使用更优雅的解决方案。

import java.util.Map.Entry;
public class DynamicTextureManager {
    private static final class MapData {
        Pixmap pixmap;
        int width;
        int height;
    }

    private static WeakHashMap<Image, MapData> theMap = new WeakHashMap<Image, MapData>();

    public static void reload() {
        Set<Entry<Image, MapData>> es = theMap.entrySet();
        for (Entry<Image, MapData> e : es) {
            Image i = e.getKey();
            MapData d = e.getValue();

            Texture t = new Texture(d.pixmap);
            TextureRegion tr;
            if(d.width == -1 || d.height == -1) {
                tr = new TextureRegion(t);
            }
            else {
                tr = new TextureRegion(t,d.width, d.height);                
            }
            TextureRegionDrawable trd = new TextureRegionDrawable(tr);
            i.setDrawable(trd);
        }
    }

    public static void register(Image i, Pixmap p) {
        MapData d = new MapData();
        d.pixmap = p;
        d.width = -1;
        d.height = -1;
        theMap.put(i, d);
    }

    public static void register(Image i, Pixmap p, int width, int height) {
        MapData d = new MapData();
        d.pixmap = p;
        d.width = width;
        d.height = height;

        theMap.put(i, d);
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多