Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GLFW get screen height/width?

Tags:

c++

opengl

glfw

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?

like image 508
Byzantian Avatar asked Jul 04 '12 20:07

Byzantian


3 Answers

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

Example:

GLFWvidmode return_struct;

glfwGetDesktopMode( &return_struct );

int height = return_struct.Height;

For GLFW they use glfwGetVideoMode, which has a different call but the return structure can be used in the same way.

like image 169
Tim Avatar answered Sep 23 '22 17:09

Tim


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

like image 26
Yahia Avatar answered Sep 19 '22 17:09

Yahia


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.

like image 8
iKlsR Avatar answered Sep 20 '22 17:09

iKlsR