Hello Guest

[CLOSED] How to get window size with GLFW?

  • 3 Replies
  • 12327 Views
[CLOSED] How to get window size with GLFW?
« on: November 17, 2014, 00:20:30 »
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:
Code: [Select]
PointerBuffer bufferW = new PointerBuffer(4);
PointerBuffer bufferH = new PointerBuffer(4);

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

bufferW.flip();

System.out.println(bufferW);

*

Offline Cornix

  • *****
  • 487
Re: How to get window size with GLFW?
« Reply #1 on: November 17, 2014, 09:01:50 »
If you flip a buffer the position and limit are manipulated. Are you sure you want to flip the buffer after calling the method?

*

Offline spasi

  • *****
  • 2160
    • WebHotelier
Re: How to get window size with GLFW?
« Reply #2 on: November 17, 2014, 09:27:40 »
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:
Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
IntBuffer w = BufferUtils.createIntBuffer(1);
IntBuffer h = BufferUtils.createIntBuffer(1);
glfwGetWindowSize(window, w, h);
int width = w.get(0);
int height = h.get(0);

Re: How to get window size with GLFW?
« Reply #3 on: November 17, 2014, 13:48:17 »
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!