gpt4 book ai didi

android - 如何在 OpenGL 上下文丢失后重新加载 libgdx 非托管纹理

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:55 25 4
gpt4 key购买 nike

我正在通过网络下载图像并将它们作为图像 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

对于上面的代码 fragment ,在 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);
}
}

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

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

最佳答案

添加我的解决方案作为引用。我现在向我的管理器注册 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);
}

}

关于android - 如何在 OpenGL 上下文丢失后重新加载 libgdx 非托管纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com