I'm currently trying to make a basic monitor utility class for getting and printing info on monitors. Im using LWJGL in java for this. When i call the function glfwGetMonitorPhysicalSize, i always get a 0 returned for both x and y. and the glfwGetVideoMode function only returns "- ' ". I can't find what i'm doing wrong here!
我目前正在尝试创建一个基本的监视器实用程序类,用于在监视器上获取和打印信息。我在java中使用LWJGL。当我调用函数glfwGetMonitorPhysicalSize时,我总是为x和y返回0。并且glfwGetVideoMode函数只返回“ - '”。我在这里找不到我做错的事!
Also the monitorID seems to be different each time i run the program. Is this normal?
每次运行程序时,monitorID似乎也不同。这是正常的吗?
Note that this code is just a test snippet:
请注意,此代码只是一个测试代码段:
private static GLFWErrorCallback errorCallback = Callbacks.errorCallbackPrint(System.out);
private static PointerBuffer monitors = null;
public static long getPrimaryMonitor(){
glfwSetErrorCallback(errorCallback);
if(glfwInit() == GL_FALSE)
System.out.println("error");
monitors = glfwGetMonitors();
long monitorId = monitors.get(0);
// Monitor name
System.out.println(glfwGetMonitorName(monitorId));
// Monitor physical size
IntBuffer xSize = IntBuffer.allocate(4);
IntBuffer ySize = IntBuffer.allocate(4);
glfwGetMonitorPhysicalSize(monitorId, xSize, ySize);
System.out.print("Pos X: ");
while(xSize.hasRemaining())
System.out.print(xSize.get());
System.out.println();
System.out.print("Pos Y: ");
while(ySize.hasRemaining())
System.out.print(ySize.get());
System.out.println();
// Monitor video mode
ByteBuffer videoMode = glfwGetVideoMode(monitorId);
System.out.print("Video mode: ");
while(videoMode.hasRemaining())
System.out.print(videoMode.getChar());
System.out.println();
return monitorId;
}
2 个解决方案
#1
I found the solution! The functions were actually returning valid value's, but apparently i was reading them the wrong way!
我找到了解决方案!这些函数实际上是返回有效值,但显然我是以错误的方式读取它们!
The correct way for video mode:
视频模式的正确方法:
ByteBuffer vidmode = glfwGetVideoMode(monitorId);
System.out.println("Video mode width: " + GLFWvidmode.width(vidmode));
System.out.println("Video mode height: " + GLFWvidmode.height(vidmode));
#2
You probably should use glfwGetPrimaryMonitor()
instead of glfwGetMonitors().get(0)
. For what glfwGetMonitorPhysicalSize
gives you back I only can say I get the same result. As for documentation it says:
您可能应该使用glfwGetPrimaryMonitor()而不是glfwGetMonitors()。get(0)。对于glfwGetMonitorPhysicalSize给你的回复,我只能说我得到了相同的结果。至于文档,它说:
Returns the size, in millimetres, of the display area of the specified monitor. Any or all of the size arguments may be NULL. If an error occurs, all non-NULL size arguments will be set to zero. Notes: This function may only be called from the main thread. Some systems do not provide accurate monitor size information, either because the EDID data is incorrect, or because the driver does not report it accurately.
返回指定监视器的显示区域的大小(以毫米为单位)。任何或所有大小参数都可以为NULL。如果发生错误,则所有非NULL大小参数都将设置为零。注意:此功能只能从主线程调用。某些系统无法提供准确的监视器大小信息,因为EDID数据不正确,或者因为驱动程序未准确报告。
So maybe your system just doesn't support it, as well as mine.
所以也许你的系统不支持它,也不支持它。
And, yes, it's normal that the monitorID is different because its like a pointer and points to dynamically allocated content.
而且,是的,monitorID是不同的,因为它像指针一样指向动态分配的内容。
#1
I found the solution! The functions were actually returning valid value's, but apparently i was reading them the wrong way!
我找到了解决方案!这些函数实际上是返回有效值,但显然我是以错误的方式读取它们!
The correct way for video mode:
视频模式的正确方法:
ByteBuffer vidmode = glfwGetVideoMode(monitorId);
System.out.println("Video mode width: " + GLFWvidmode.width(vidmode));
System.out.println("Video mode height: " + GLFWvidmode.height(vidmode));
#2
You probably should use glfwGetPrimaryMonitor()
instead of glfwGetMonitors().get(0)
. For what glfwGetMonitorPhysicalSize
gives you back I only can say I get the same result. As for documentation it says:
您可能应该使用glfwGetPrimaryMonitor()而不是glfwGetMonitors()。get(0)。对于glfwGetMonitorPhysicalSize给你的回复,我只能说我得到了相同的结果。至于文档,它说:
Returns the size, in millimetres, of the display area of the specified monitor. Any or all of the size arguments may be NULL. If an error occurs, all non-NULL size arguments will be set to zero. Notes: This function may only be called from the main thread. Some systems do not provide accurate monitor size information, either because the EDID data is incorrect, or because the driver does not report it accurately.
返回指定监视器的显示区域的大小(以毫米为单位)。任何或所有大小参数都可以为NULL。如果发生错误,则所有非NULL大小参数都将设置为零。注意:此功能只能从主线程调用。某些系统无法提供准确的监视器大小信息,因为EDID数据不正确,或者因为驱动程序未准确报告。
So maybe your system just doesn't support it, as well as mine.
所以也许你的系统不支持它,也不支持它。
And, yes, it's normal that the monitorID is different because its like a pointer and points to dynamically allocated content.
而且,是的,monitorID是不同的,因为它像指针一样指向动态分配的内容。