Quick example - how to make a resizable window

Started by Evan407, April 14, 2016, 11:12:22

Previous topic - Next topic

Evan407

You probably know this code that makes the window resizable
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE,GLFW.GLFW_TRUE);// the window will be resizable

but that doesn't do the whole job because opengl is still using the old window size.

Here is how you can make opengl work with resizing the window.
Make a callback like so
  /* for resizing window */
  private static GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){
	@Override
	public void invoke(long window, int width, int height){
	  GL11.glViewport(0,0,width,height);
	  //update any other window vars you might have (aspect ratio, MVP matrices, etc)
	}
  };


register the callback during init code

GLFW.glfwSetFramebufferSizeCallback(windowHandle, resizeWindow);


And that's it! Now you can resize the window properly.

kappa

Do keep in mind that on some platforms the window size is not always the same as the framebuffer size (such as retina macs).

Kai

It is pretty obvious that you have to reset the viewport when your window resizes.
Nothing new about that. And using the framebuffer resize event with its width/height is also fine under Retina.