我正在使用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
}
//在这一点之后还有更多的代码,但它们都经过了测试和运作。
答案 0 :(得分:1)
天哪,我才刚刚意识到对我的主循环的调用被注释掉了。
/捂脸