在当前线程中找不到OpenGL上下文 [英] No OpenGL context found in the current thread

查看:88
本文介绍了在当前线程中找不到OpenGL上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LibGDX 制作游戏.我想同时根据需要动态加载/卸载资产.但是,等待资产加载到主线程中会导致延迟.为了解决这个问题,我创建了一个后台线程,该线程监视需要加载哪些资产(纹理,声音等),并适当地加载/卸载它们.

I am using LibGDX to make a game. I want to simultaneously load/unload assets on the fly as needed. However, waiting for assets to load in the main thread causes lag. In order to remedy this, I've created a background thread that monitors which assets need to be loaded (textures, sounds, etc.) and loads/unloads them appropriately.

不幸的是,当从该线程调用 AssetManager.update()时,出现以下错误.

Unfortunately, I get the following error when calling AssetManager.update() from that thread.

com.badlogic.gdx.utils.GdxRuntimeException: java.lang.RuntimeException: No OpenGL context found in the current thread.

我从一开始就尝试在主线程中运行 run ,并且仅处理前几个屏幕,并且一切正常.我还可以更改算法,以从一开始就将所有内容都加载到内存中,并且效果很好.但是,它们都不在后台线程中工作.

I've tried runing the background thread in the main thread in the beginning and just dealing with the first few screens, and everything works fine. I can also change the algorithm to just load everything into memory from the start in the same thread, and that works as well. However, neither works in the background thread.

当我在带有OpenGL ES 2.0的Android上(以奇怪的方式灵活)而不是Windows上运行此程序时,一切运行良好,我什至可以得到图像的像素尺寸-但是纹理呈现黑色.

When I run this on Android with OpenGL ES 2.0 (which is flexible in odd ways) instead of on Windows, everything runs fine, and I can even get the pixel dimensions of the images - but the textures render black.

我的搜索告诉我,这是 OpenGL上下文绑定到单个线程的问题,但没有其他问题.这解释了为什么当我将其推入主线程时一切正常,而不是当我将其放入其他线程时一切正常.如何解决此上下文问题?

My searches have told me that this is an issue of the OpenGL context being bound to a single thread, but not much else. This explains why everything works when I shove it in the main thread, and not when I put it in a different one. How do I fix this context problem?

推荐答案

首先,您不应该在渲染线程之外访问OpenGL上下文.

我认为您已经查看了这些内容,但是只是为了确保已阅读AssetManager Wiki文章,其中讨论了如何使用AssetManager进行资产的异步管理.除了Wiki文章外,请查看

I assume you have looked at these already, but just to make sure read up on the AssetManager wiki article, which talks a bit about how to use the AssetManager for asynchronous managing of assets. In addition to the wiki article, check out the AssetManagerTest to better understand how to use it. The asset manager test is probably your best bet into loading at how to dynamically load assets.

如果要加载大量东西,则可能需要考虑创建一个加载栏来加载任何较大的预付款.从另一个线程检查资产等(并设置一个标记来调用更新)可能会起作用,但是最终,您将需要调用

If you are loading a ton of stuff, you may want to look into creating a loading bar to load anything large upfront. It might work to check assets and such from another thread (and set a flag to call update), but at the end of the day you will need to call update() on the rendering thread.

请记住,您必须从其他线程调用update(),但我不明白为什么您要让另一个线程检查条件并设置标志.使用另一个线程并同步update()调用可能会比仅在呈现线程上完成所有操作花费更多.另外,由于update()方法会逐步加载文件,因此每次仅暂停几毫秒.通常,您只需调用

Keeping in mind you have to call update() it from a different thread, I don't see why you would want another thread to check conditions and set a flag. There is probably more overhead using another thread and synchronizing the update() call than to just do it all on the rendering thread. Also, the update() method only pauses for a couple milliseconds at a time as it incrementally loads files. Typically, you would simply call load() for your asset, then check isLoaded() on your asset. If it isn't loaded you would then call update() once per frame until isLoaded() returns true. Once it returns true, you can then call get() and get whatever asset you were loading. This can all be done via the main rendering thread without having the app lag while its loading.

如果您确实希望其他线程调用update(),则需要创建一个 Wiki文章有关libGDX的多线程.但是,这使使用其他线程的要点大打折扣,因为与postRunnable一起使用的任何东西都在渲染线程上同步运行.

If you really want your other thread to call update(), you need to create a Runnable object and call postRunnable() such as how they have it described in the wiki article on multi-threading with libGDX. However, this defeats the whole point of using other threads because anything you use with postRunnable runs synchronously on the rendering thread.

这篇关于在当前线程中找不到OpenGL上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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