我正在开发纸牌游戏,目前基础不错,但在 eclipse 中运行时遇到错误。我也在使用 slick 2d。
这是来自控制台的错误。
Exception in thread "main" java.lang.RuntimeException: No OpenGL context found in the current thread. at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124) at org.lwjgl.opengl.GL11.glGetError(GL11.java:1277) at org.newdawn.slick.opengl.renderer.ImmediateModeOGLRenderer.glGetError(ImmediateModeOGLRenderer.java:387) at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:337) at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:275) at org.newdawn.slick.Image.(Image.java:270) at org.newdawn.slick.Image.(Image.java:244) at org.newdawn.slick.Image.(Image.java:232) at org.newdawn.slick.Image.(Image.java:198) at Cards.Card.(Card.java:18)
我认为错误来源的代码(Card 类)
package Cards;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
public class Card
{
final int numCards = 52;
Image[] card = new Image [numCards];
Card (int c)
{
String fileLocation = new String ();
for (int i = 1 ; i <= 52 ; i++)
{
fileLocation = "res/cards/" + i + ".png";
try
{
card [i] = new Image (fileLocation); //line
}
catch (SlickException e)
{
e.printStackTrace ();
}
}
}
public Image getImage (int cardlocation)
{
return card [cardlocation];
}
}
以前有人遇到过这种问题吗?我该如何解决?
最佳答案
这种错误对于 LWJGL 初学者来说很常见。 OpenGL 上下文在创建时绑定(bind)到线程。所以你只能从同一个线程访问这个上下文。
由于看起来您没有使用不同的线程,因此可能还有其他原因。 Slick2D 似乎需要为其 Image
类提供有效的 OpenGL 上下文。
所以我的第一个尝试是在初始化卡片图像之前初始化 OpenGL 上下文。
关于java - 在当前线程中找不到 OpenGL 上下文,如何修复此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347340/