Jensen
Jensen

Reputation: 31

LWJGL Display crashing when implemented with Jframe and canvas

I'm working on creating a 2D Java game with a lighting engine with OpenGL using LWJGL, but I've hit a wall when trying to link up keyboard inputs.

The render loop works fine, but as soon as I tried to implement a JFrame/canvas and the getParent/KeyListener combo The application crashes immediately after starting up. I have to shut the application down in netbeans - the window doesn't respond to right clicking application's entry in the start toolbar.

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
}

//There's more code after this point of course, but it's all already been tested and works.

Upvotes: 2

Views: 295

Answers (1)

Jensen
Jensen

Reputation: 31

Oh god I only just realized the call to my main loop was commented out.

/facepalm

Upvotes: 1

Related Questions