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

编程入门 行业动态 更新时间:2024-10-09 08:36:03
本文介绍了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非托管纹理

本文发布于:2023-11-16 03:50:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1600733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   纹理   加载   OpenGL   libgdx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!