gpt4 book ai didi

java - glfwSetCursorPos() 在 Windows 上不起作用

转载 作者:行者123 更新时间:2023-12-02 09:46:35 24 4
gpt4 key购买 nike

所以我使用 lwjgl 提供的 GLFW 的 java 绑定(bind)在 java 中编写了一个窗口。它在 Linux 上运行良好,但由于某种原因,某些方法在 Windows 上不起作用。

我已经注册了一个在 Linux 和 Windows 上都可以正常工作的按键回调,但是例如 char 回调只能在 Linux 上工作。

我遇到的另一个问题是 setCursorPos() 不起作用,它破坏了我的相机旋转,因为我无法将鼠标居中以计算从那里的偏移量。

这就是我创建窗口的方式:

    public void init(int selectedMonitor, int glMajor, int glMinor, int windowHints) {
if (this.initialized) return;
GLFWErrorCallback.createPrint(System.err).set();

if (!GLFW.glfwInit())
throw new WindowCreationException("Unable to initialize GLFW.");

// Configure window hints
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, (windowHints & 1));
GLFW.glfwWindowHint(GLFW.GLFW_FOCUSED, (windowHints >> 1) & 1);
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, (windowHints >> 2) & 1);
GLFW.glfwWindowHint(GLFW.GLFW_DECORATED, (windowHints >> 3) & 1);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, glMajor);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, glMinor);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, (windowHints >> 7));

// Set video mode
PointerBuffer monitors = GLFW.glfwGetMonitors();
if (monitors == null)
throw new WindowCreationException("Could not create window, there are no monitors.");

if (selectedMonitor >= monitors.capacity() || selectedMonitor < 0) {
this.currentMonitor = GLFW.glfwGetPrimaryMonitor();
} else {
this.currentMonitor = monitors.get(selectedMonitor);
}
this.vidMode = GLFW.glfwGetVideoMode(this.currentMonitor);

// Create window handle
this.windowHandle = GLFW.glfwCreateWindow(this.width, this.height, this.title, this.isFullscreen ? this.currentMonitor : 0L, 0L);
if (this.windowHandle == 0L)
throw new RuntimeException("Failed to create GLFW Window.");

// Center window if configured
if (((windowHints >> 4) & 1) == 1) this.centerWindow();

// Set key callbacks
this.addKeyCallback((window, keyCode, scanCode, action, mods) -> Key.byKeyCode(keyCode).update(action));
this.addMouseButtonCallback((window, keyCode, action, mods) -> Key.byKeyCode(keyCode).update(action));

glfwSetKeyCallback(this.windowHandle, (window, keyCode, scanCode, action, mods) -> {
for (GLFWKeyCallbackI callback : this.keyCallbacks) callback.invoke(window, keyCode, scanCode, action, mods);
});

glfwSetCharCallback(this.windowHandle, (window, unicode) -> {
for (GLFWCharCallbackI callback : this.charCallbacks) callback.invoke(window, unicode);
});

glfwSetMouseButtonCallback(this.windowHandle, (window, keyCode, action, mods) -> {
for (GLFWMouseButtonCallbackI callback : this.mouseButtonCallbacks) callback.invoke(window, keyCode, action, mods);
});

glfwSetScrollCallback(this.windowHandle, (window, xOffset, yOffset) -> {
for (GLFWScrollCallbackI callback : this.scrollCallbacks) callback.invoke(window, xOffset, yOffset);
});

// Make the OpenGL context current
GLFW.glfwMakeContextCurrent(this.windowHandle);
if (((windowHints >> 5) & 1) == 1) GLFW.glfwSwapInterval(1);

// Configure cursor
this.mouseX = MemoryUtil.memAllocDouble(1);
this.mouseY = MemoryUtil.memAllocDouble(1);
if (((windowHints >> 6) & 1) == 1) this.hideCursor(true);

this.initialized = true;
}

当我执行这段代码时:

GLFW.glfwSetCursorPos(this.windowHandle, 0, 0);
GLFW.glfwGetCursorPos(this.windowHandle, this.mouseX, this.mouseY);
System.out.println("Mouse Pos: " + this.mouseX.get(0) + ", " + this.mouseY.get(0));

Windows 和 Linux 的结果有所不同。
Linux:鼠标位置:0, 0
Windows:鼠标位置:623.0、367.0

我不知道为什么它在Windows上不起作用,甚至看起来与lwjgl版本完全无关,因为我尝试了3.1.6、3.2.1、3.2.2和3.2.3-SNAPSHOT,并且所有这些版本都是一样的。因此,问题要么是我在创建窗口时忘记了某些内容,要么是 Windows 在某些更新中破坏了某些内容,因为几个月前,当我使用 lwjgl 3.1.6 制作一个项目时,我 100% 确定 setCursorPos() 在 Linux 和 Windows 上都可以工作那么。

最佳答案

好的,所以我找到了问题的解决方案:

出于某种原因,glfwSetCursorPos() 必须在 Windows 上创建窗口的同一线程中调用,而不是在 Linux 上。

这有点奇怪,因为它不会在 Windows 上导致段错误,但从同一线程调用该方法适用于 Windows 和 Linux。

关于java - glfwSetCursorPos() 在 Windows 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com