【发布时间】:2015-05-04 14:07:55
【问题描述】:
我正在使用 LWJGL 使用带有 OpenGL 的照明引擎创建一个 2D Java 游戏,但是在尝试链接键盘输入时遇到了困难。
渲染循环工作正常,但是当我尝试实现 JFrame/canvas 和 getParent/KeyListener 组合时,应用程序在启动后立即崩溃。 我必须在 netbeans 中关闭应用程序 - 窗口不响应在开始工具栏中右键单击应用程序的条目。
public static void main(String[] args) {
Main main = new Main();
main.run();
}
public void run() {
initialize();
//animLoop();
}
private void initialize() {
try {
Frame theFrame = new Frame("Inlight");
Canvas theCanvas = new Canvas();
theCanvas.setMinimumSize(new Dimension(windowWidth, windowHeight));
theFrame.setSize(windowWidth, windowHeight);
theCanvas.requestFocusInWindow();
theFrame.add(theCanvas);
theFrame.setVisible(true);
//before doing the following, I need to create the canvas within which openGL does it's rendering
//create it before applying keylistener
Display.setDisplayMode(new DisplayMode(windowWidth, windowHeight));
Display.setParent(theCanvas);
Display.getParent().addKeyListener(new InlightKeyListener());
Display.create(new PixelFormat(0, 16, 1));
} catch (Exception e) {
e.printStackTrace();
}
shaderProgram = glCreateProgram();
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
StringBuilder fragmentShaderSource = new StringBuilder();
try {
String line;
BufferedReader reader = new BufferedReader(new FileReader("src/shader.frag"));
//points to the shader doc
while ((line = reader.readLine()) != null) {
fragmentShaderSource.append(line).append("\n");
}
} catch (IOException e) {}
glShaderSource(fragmentShader, fragmentShaderSource);
glCompileShader(fragmentShader);
if (glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Fragment shader not compiled!");
}
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glValidateProgram(shaderProgram);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, windowWidth, windowHeight, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_STENCIL_TEST);
glClearColor(0, 0, 0, 0);
System.out.println("Done initialize");
}
public synchronized void animLoop() {
//This method will loop the render
long startTime = System.currentTimeMillis();
//sets the starting time to the current time
long curTime = startTime;
//The current time measurement, so at thestart the curTime = starting time
while (curTime - startTime < 1800) {
long timePassed = System.currentTimeMillis() - curTime;
//Makes the timePassed variable equal to the System's current time - the last measured current time.
curTime += timePassed;
//updates the measurement of the current time to the actual current time. (I imagine some small amount to time is lost while it is updated. this is negligible.)
organiseTitle();
//sets up the ojects to display the title screen for 1800 milliseconds
render();
//draws the new screen scene to the display
clearObj();
//cleans up the items built for that last render
}
while (!Display.isCloseRequested()) {
long timePassed = System.currentTimeMillis() - curTime;
//Makes the timePassed variable equal to the System's current time - the last measured current time.
curTime += timePassed;
//updates the measurement of the current time to the actual current time. (I imagine some small amount to time is lost while it is updated. this is negligible.)
Organiselevel1(20, 200);
render();
//draws the new screen scene to the display
clearObj();
//cleans up the items built for that last render
}
glDeleteShader(fragmentShader);
glDeleteProgram(shaderProgram);
Display.destroy();
//closes the window
}
//当然在这之后还有更多的代码,但它们都已经过测试并且可以工作了。
【问题讨论】:
-
您是否收到任何错误消息?
-
不,它只是在 Sout 打印“完成初始化”后立即崩溃。我必须停止 IDE 中的进程才能关闭窗口。
-
您是否在代码中的任何位置调用 Display.update()?它处理窗口事件,所以如果你不调用它,你将无法关闭或移动窗口。
-
是的,在我的渲染()结束时;在 animLoop 方法中调用的模块,以及 Display.sync(60);
标签: java swing opengl jframe lwjgl