How to reload libgdx unmanaged Texture after OpenGL context loss(OpenGL上下文丢失后如何重新加载libgdx非托管纹理)
问题描述
我正在通过网络下载图像并将它们作为图像演员添加到我的 libgdx UI 中:
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
到上面的代码片段,在 resume()
我做:
to the above snippet, and in resume()
I do:
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);
}
}
但这无济于事 - 我最终仍然会卸载纹理和白色区域而不是图像.
But this doesn't help - I still end up with the texture being unloaded and white areas instead of the image.
这应该怎么做?我找不到任何代码来证明这一点!
How should this be done? I haven't been able to find any code demonstrating this!
推荐答案
添加我的解决方案作为参考.我现在向我的经理注册 Image 对象和 Pixmap 对象,在 reload() 上,从 Pixmap 重新创建纹理,并为旧图像设置新纹理.对我有用,但欢迎使用更优雅的解决方案.
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);
}
}
这篇关于OpenGL上下文丢失后如何重新加载libgdx非托管纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OpenGL上下文丢失后如何重新加载libgdx非托管纹理


- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01