[英]java.lang.RuntimeException: No OpenGL context found in the current thread while generating world in thread
Im making a 2D sandbox game(like terraria) with LibGDX. 我使用LibGDX制作了2D沙盒游戏(例如Terraria)。 I have come across a issue though.
我遇到了一个问题。 I need the world to generated in another thread so the program can render the animated loading screen while it generates.
我需要在另一个线程中生成世界,以便程序可以在生成动画加载屏幕时对其进行渲染。 I put the generation code in a new thread.
我将生成代码放在一个新线程中。 When I run it, I receive this error:
运行它时,出现以下错误:
Exception in thread "Thread-1" java.lang.RuntimeException: No OpenGL context found in the current thread.
Why is this happening and how can i fix it? 为什么会发生这种情况,我该如何解决? When the generator runs it needs to create a Sprite which requires OpenGL, but it seems to not like running in a seperate thread.
生成器运行时,需要创建一个需要OpenGL的Sprite,但似乎不喜欢在单独的线程中运行。 Any insights?
有什么见解吗?
Maybe it is possible to somehow create a GL context in a new thread (but I'm not sure since I haven't done this ever) but why the heck are you trying to achieve this in that way? 也许可以以某种方式在新线程中创建GL上下文(但我不确定,因为我从没做过),但是为什么要尝试通过这种方式实现这一目标呢? :)
:)
Instead of creating new thread to load assets / generate world just render some "loading" label/animation - anything you want and start to render world when it is done. 无需创建新线程来加载资产/生成世界,只需渲染一些“正在加载”标签/动画-任何您想要的东西,并在完成后开始渲染世界。
If you are trying to load some asset you should use AssetManager and it's update method to check if assets are loaded - something like 如果您要加载某些资产,则应使用AssetManager,并且它是update方法来检查资产是否已加载-类似于
AssetManager assetManager = new AssetManager();
//load assets
...
//render method
if(assetManager.update())
{
//render world or even change screen to game screen
}
else
//render "loading"
If you are trying to generate a world just slice this operation for pieces (like generating squares N x N) and have a method to do this just add some control to check if operation is finished - it can be some enum, flag, method... So assuming that you have class WorldGenerator it would be something like 如果您想生成一个世界,只需将此操作切成碎片(例如生成N×N的正方形),并有一种方法可以执行此操作,只需添加一些控件以检查操作是否完成-它可以是一些枚举,标志,方法。 ..因此,假设您拥有WorldGenerator类,它将类似于
WorldGenerator worldGenerator;
//generate world
...
//render method
if(WorldGenerator.isGenerated())
{
//render world or even change screen to game screen
}
else
//render "loading"
//make a step of generation
Avoid multithreading as fire - especially in Libgdx there are really not many cases that you need this and almost every time it is well handled by framework 避免多线程着火-特别是在Libgdx中,实际上并不需要很多情况,几乎每次框架都可以很好地处理它
OK solved. 好了
Libgdx has a sweet util that can post code from a new thread into the main GL thread. Libgdx有一个不错的工具,可以将新线程中的代码发布到主GL线程中。 Here is a example:
这是一个例子:
new Thread(new Runnable(){
@Override
public void run(){
//Here we are telling libgdx to connect a new runnable to the gl thread
Gdx.app.postRunnable(new Runnable(){
//Type thread code here. Will be seperate to main thread but part of it at the same time
}
}
});
Hope this helps :D 希望这会有所帮助:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.