GLFW get screen height/width?
Asked Answered
C

3

15

Playing around with OpenGL for a while, using the freeglut library, I decided that I will use GLFW for my next training project instead, since I was told that GLUT was only designed for learning purposes and should not be used professionally. I had no problems with linking the lib to my NetBeans project and it compiles just fine, using mingw32 4.6.2.

However, I am running into difficulties trying to position the window at the center of the screen. Under freeglut, I previously used:

glutInitWindowPosition ( 
                         (glutGet(GLUT_SCREEN_WIDTH)-RES_X)  / 2,
                         (glutGet(GLUT_SCREEN_HEIGHT)-RES_Y) / 2 
                       );

I can't find any glfw function that would return the screen size or width. Is such a function simply not implemented?

Capping answered 4/7, 2012 at 20:48 Comment(0)
P
13

How about glfwGetDesktopMode, I think this is what you want.

Example:

GLFWvidmode return_struct;

glfwGetDesktopMode( &return_struct );

int height = return_struct.Height;

In GLFW 3.0 they switched to glfwGetVideoMode, which has a different call but the return structure can be used in the same way.

Pascoe answered 4/7, 2012 at 20:55 Comment(10)
I am kind of confused, in glfw.h it says:typedef struct { int Width, Height; int RedBits, BlueBits, GreenBits; } GLFWvidmode; What am I supposed to use as an argument if I want to get the height, for instance?Capping
@CyberpunC: You don't ask for the width and height separately. You get both in one call. You pass an empty struct, and it fills it in with all of those fields. You read the fields you care about, and ignore the rest.Stocky
Do I have to use the heap? Isn't this all a bit expensive? Why even the struct? Is there a way to pass this as an anonymous object? EDIT: std::cout << return_struct->Height << " "<< return_struct->Width << std::endl; just prints "0 0"Capping
@Nicol Bolas: And how do I do that?Capping
I've changed it to not use heap, been doing way too much Java lately :) @CyberpunCPascoe
@Tim: Now it's printing "1988975548 1988628834 " But how is this my screen height/width (1920x1080)?Capping
Sorry, I don't know. I've never actually used that function, I just pulled it from the documentation. I'm not sure why it would report that. @CyberpunCPascoe
Great, you know what? I am going to link freeglut now just for that function.Capping
@CyberpunC: Are you calling glfwInit before glfwGetDesktopMode? The GLFW User Guide say that: "Before using any of the GLFW functions, it is necessary to call glfwInit"Hug
This is obsolete now, current versions have glfwGetVideoMode which returns a pointer to memory owned by GLFW. You pass it a monitor.Barometrograph
G
10

first you need two variables to store your width and height.

int width, height;

then as described on page 14 of the reference.

glfwSetWindowPos(width / 2, height / 2);

and as a bonus you can then call

glfwGetWindowSize(&width, &height);

this a void function and does not return any value however it will update the two previously declared variables.. so place it in the mainloop or the window reshape callback function.

you can verify this in the official manual here on page 15.

Geophysics answered 14/7, 2012 at 22:15 Comment(2)
This would not center the window, use glfwSetWindowPos(ScreenWidth / 2 - WindowWidth / 2, ...); instead.Lederhosen
@sharethis i did not said it would.. read his question again.. he asked as part of his question for the function that would return the window size..Geophysics
A
5

This might help somebody...

void Window::CenterTheWindow(){
            GLFWmonitor* monitor = glfwGetPrimaryMonitor();
            const GLFWvidmode* mode = glfwGetVideoMode(monitor);
            glfwSetWindowPos(m_Window, (mode->width - m_Width) / 2, (mode->height - m_Height) / 2);
}

m_Width and m_Height are variables that have the width and the height of the window.

Reference: http://www.glfw.org/docs/latest/monitor.html

Aracelis answered 18/5, 2015 at 14:48 Comment(1)
This solution is correct, but when the window is not opened in the primary monitor this solution is not going to work.Nutlet

© 2022 - 2024 — McMap. All rights reserved.