[CLOSED] How to get window size with GLFW?

Started by TheBoneJarmer, November 17, 2014, 00:20:30

Previous topic - Next topic

TheBoneJarmer

Hey

I cannot figure out how to retrieve the window size with the glfw functions. According to the docs, I need to pass through bytebuffers as parameters. It works fine but the buffer's limit is always 0, which means no data has written. This is my code:
PointerBuffer bufferW = new PointerBuffer(4);
PointerBuffer bufferH = new PointerBuffer(4);

glfwGetFramebufferSize(window,bufferW.getBuffer(),bufferH.getBuffer());

bufferW.flip();
		
System.out.println(bufferW);

Cornix

If you flip a buffer the position and limit are manipulated. Are you sure you want to flip the buffer after calling the method?

spasi

Quote from: TheBoneJarmer on November 17, 2014, 00:20:30Hey

I cannot figure out how to retrieve the window size with the glfw functions. According to the docs, I need to pass through bytebuffers as parameters. It works fine but the buffer's limit is always 0, which means no data has written. This is my code:
PointerBuffer bufferW = new PointerBuffer(4);
PointerBuffer bufferH = new PointerBuffer(4);

glfwGetFramebufferSize(window,bufferW.getBuffer(),bufferH.getBuffer());

bufferW.flip();
		
System.out.println(bufferW);

There are 3 problems with the above code:

- You're using PointerBuffers, which are meant to be used for pointer data (arrays of pointers). You should be using ByteBuffers, or IntBuffers.
- glfwGetFramebufferSize returns the framebuffer size, not the window size. Technically these are not the same, the framebuffer size is in pixels, whereas the window (client area) size is in screen coordinates.
- You should never call flip after an LWJGL call. LWJGL may read the current buffer position and limit, but never modifies them.

This is the correct code:

ByteBuffer w = BufferUtils.createByteBuffer(4);
ByteBuffer h = BufferUtils.createByteBuffer(4);
glfwGetWindowSize(window, w, h);
int width = w.getInt(0);
int height = h.getInt(0);


or with IntBuffers:

IntBuffer w = BufferUtils.createIntBuffer(1);
IntBuffer h = BufferUtils.createIntBuffer(1);
glfwGetWindowSize(window, w, h);
int width = w.get(0);
int height = h.get(0);

TheBoneJarmer

Thanks! Yes, I got confused a bit. I did not found any examples in Java, only in C++. But those use pointers. I did almost the same though in my third attempt, but I did not used the BufferUtils.createByteBuffer(4) method. I used ByteBuffer.allocate(4) instead. It works like a charm now!