Why is getCursorPosition() double?

yj1214 wrote on Sunday, August 16, 2015:

I thought screen cooridinates don’t have decimal points…

This is my code,

    double x;
    double y;

	glfwGetCursorPos(window, &x, &y);

	printf("%d, %d", x, y);
	printf("\n");

And this is what I get,

0, 1082245120
0, 1082263552
0, 1082284032
0, 1082304512
0, 1082331136
0, 1082357760
0, 1082384384
0, 1082404864
0, 1082419200
0, 1082419200

What’s happening here? Why do I get zero for my x position?

arampl wrote on Sunday, August 16, 2015:

You are applying printf’s decimal integer formatting (%d) to the values of type “double”, thus getting weird result. Use %f instead.

To get integers floor the result.

Doubles are used because mouse presicion is much higher than screen.

yj1214 wrote on Monday, August 17, 2015:

Thanks that worked! but I have another question.

Is there way to get my cursor position relative to my monitor not window?

glfwGetCursorPos();

This function returns the position of my cursor relative to my window. But I need to get my cursor position relative to my monitor. I think SFML has this function but does GLFW have it too?

dougbinks wrote on Monday, August 17, 2015:

Use glfwGetWindowPos to get the position of the window and add this to the cursor position.

yj1214 wrote on Monday, August 17, 2015:

Thanks that works! but I have one last question…

Is there way to get my cursor position even if my cursor isn’t inside the window?

dougbinks wrote on Monday, August 17, 2015:

I think you can only get the cursor position inside the window if the cursor is displayed. To get unlimited virtual cursor position you need to call glfwSetInputMode with GLFW_CURSOR, GLFW_CURSOR_DISABLED

Is that just for internal use though? I have never seen the cursor position reported as anything other than integer values, despite being returned as doubles.

The double type is used because this covers the range of types returned by all OSs. For example on MacOS the type returned is a double, and on Windows a 32bit integer (the range of which can be expressed as an integer by a double).

I don’t have a Mac with a Retina display, but it’s possible that this would return fractional coordinates for the mouse position - indeed I think it would need to in order to render mouse movement smoothly.

It does, for two reasons: the size of the coordinate system for the user interface is smaller than the pixel size of the screen; this allows applications to work independently of the screen’s resolution, and as such, if the mouse position was reported as unscaled integers, pixels would be skipped. In addition to that, it allows reporting full-resolution mouse positions without having to use a different API for this, for when the mouse can provide finer movement than per-pixel.

Reporting mouse position/movement per pixel only ever makes sense if you need to know exactly where on the screen it is, and nothing else. It’s a barrier in your way if you want it for pretty much everything else.