Detecting screen resolution?

begre1929 wrote on Sunday, April 13, 2014:

Hi,
is it possible to detect screen resolution with glfw ?
I want to use

glfwCreateWindow(w, h, “I will smoke your bones”, glfwGetPrimaryMonitor(), NULL);

where w and h depend of the current screen resolution

dougbinks wrote on Monday, April 14, 2014:

See the documentation on monitor handling:
http://www.glfw.org/docs/latest/group__monitor.html

begre1929 wrote on Monday, April 14, 2014:

hi Doug and thank you for the answer.

I tried,
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
if(!monitor) exit(1);
glfwGetMonitorPhysicalSize(monitor,&width,&height);

std::cout << “wi=” << width <<" hei=" << height << std::endl;

I got
wi=0 hei=0

so I guees on my OS I can’t use glfwGetMonitorPhysicalSize…

arampl wrote on Tuesday, April 15, 2014:

Hi, begre1929!
Doug shows to you correct documentation link. You’ve just used wrong function. You need to use “glfwGetVideoMode”, which returns videomode structure with width and height members.

#include "glfw3.h"
#include <stdlib.h>
#include <stdio.h>

static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
	glfwSetWindowShouldClose(window, GL_TRUE);
}

int main(int argc, const char * argv[])
{
	GLFWvidmode	*mode;
	GLFWwindow	*window;

	if(!glfwInit())
		exit(EXIT_FAILURE);

	mode = (GLFWvidmode*)glfwGetVideoMode(glfwGetPrimaryMonitor());

	window = glfwCreateWindow(mode->width, mode->height, "Screen resolution window", NULL, NULL);

	glfwMakeContextCurrent(window);

	glfwSetKeyCallback(window, key_callback);

	while(!glfwWindowShouldClose(window))
    {
		glClear(GL_COLOR_BUFFER_BIT);
		glfwSwapBuffers(window);
 		glfwPollEvents();
    }

	glfwTerminate();
	exit(EXIT_SUCCESS);
}

Regards :slight_smile:

begre1929 wrote on Tuesday, April 15, 2014:

thx arample, this line,
mode = (GLFWvidmode*)glfwGetVideoMode(glfwGetPrimaryMonitor());
solved my problem.

Hi this did solve my problem, but I wanted to ask, how did you know that mode must have two further variables as width and height?

I mean these guys:
mode->width
mode->height

GLFWvidmode is documented here: https://www.glfw.org/docs/latest/structGLFWvidmode.html

In addition to looking at the documentation, you can search the GLFW header in your IDE or by opening it in an editor, for example here I’ve searched for GLFWvidmode in the online code:

Some IDEs (for example Visual Studio, XCode, Eclipse etc.) will allow you to look at the declaration of a struct or function by right clicking on where you use it, and go to it’s declaration to view members etc.

Yeah I did find that after a while, didn’t remove my comment. But anyway you took the initiative for helping. Thank you @dougbinks and @mmozeiko for the 100% helpful reply.