如何重装libgdx后OpenGL上下文损失非托管纹理 [英] How to reload libgdx unmanaged Texture after OpenGL context loss

查看:146
本文介绍了如何重装libgdx后OpenGL上下文损失非托管纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载的图片在网络上,并将它们添加到我的libgdx的用户界面与使用该图片的演员:

I'm downloading images over the network and add them to my libgdx UI as Image actors using this:

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上下文丢失是失去了一些方法(例如,因为屏幕进入睡眠状态)。

Given this I need some way of reloading the texture data since it is lost when the OpenGL context is lost (e.g. because the screen goes to sleep).

我试图让自己的管理器类,添加

I've tried making my own manager class, adding

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

以上代码片段,并在简历()我做的:

DynamicTextureManager.reload();

该管理器类:

The manager class:

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

不过,这并没有帮助 - 我还是结束了,而不是像被卸载的质地和白色区域

But this doesn't help - I still end up with the texture being unloaded and white areas instead of the image.

应该如何进行?我一直没能找到任何code证明这个!

How should this be done? I haven't been able to find any code demonstrating this!

推荐答案

加入我的解决方案作为参考。我现在注册的Image对象,并与我的经理的像素图的对象,就重装()的纹理是从像素图重新创建,我设置的旧形象,新的纹理。对我的作品,但更优雅的解决方案是值得欢迎的。

Adding my solution as a reference. I now register the Image object and the Pixmap object with my manager, on reload() the Texture is re-created from the Pixmap and I set the new Texture for the old Image. Works for me, but more elegant solutions are welcome.

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

}

这篇关于如何重装libgdx后OpenGL上下文损失非托管纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆