当前位置: 首页 > 知识库问答 >
问题:

glfwSetCursorPos()不能在windows上工作

钮安歌
2023-03-14
    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在某个更新中损坏了一些东西,因为几个月前,当我用LWJGL3.1.6做一个项目时,我100%确定setCursorPos()当时既可以在linux上工作,也可以在windows上工作。

共有1个答案

赫连冠玉
2023-03-14

好了,我找到了解决问题的方法:

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

这有点奇怪,因为它没有在windows上导致分段错误,但是从同一个线程调用该方法对windows和Linux都有效。

 类似资料:
  • 我遵照这些指示: Windows全局安装PHAR的过程与在Windows上手动安装Composer的过程相同: 为PHP二进制文件创建目录;例如,C:\bin 追加;C:\bin到您的PATH环境变量(相关帮助) 下载https://phar.phpunit.de/phpunit-6.2.phar 并将文件另存为C:\bin\phpunit。法尔 打开命令行(例如,按Windows R»键入cmd

  • 我试图制作一个跨平台的JavaFX应用程序,它在Windows和OSX机器上工作得很好,但在Linux上不行。 jar是在Intellij思想中使用基本的JavaFX配置构建的。 有人帮忙吗?

  • 所以基本上我有一个使用 Webpack 的项目,如果我使用 构建,使用另一个编辑器编辑文件将触发手表;但是,如果我使用 Webstorm 编辑文件,则不会发生任何事情。 我遇到了这篇文章,似乎我不是唯一一个,但是该解决方案适用于 Ubuntu,所以我想知道 Windows 是否有类似的东西? 谢谢

  • 我有一段非常简单的Java代码,在那里我尝试从Java连接到我的Oracle DB。 在Windows下一切正常,但当我尝试在Ubuntu上运行时,我得到了一个错误。 我读了很多书,也试过很多解决方法。这是我的代码: 当我运行它时,我收到一个错误: 连接失败Java.sql.sqlRecoverable异常:IO错误:网络适配器无法在oracle.jdbc.driver.T4CConnection

  • 这段代码工作正常(并与我们更大的应用程序联系在一起),并为我们的应用程序打开了配置屏幕。 以下是等待空闲方法: 在Windows10机器上,clickOnTable()方法失败,因为方法实际上没有单击选项卡,因此屏幕上没有显示我们要查找的表。 null 更新1 在失败的笔记本电脑和工作的Windows10机器上尝试了不同版本的Java。两者都没有区别。 出现故障的笔记本电脑运行的是Windows

  • 我实现了一个简单的Java实用程序类,使用aes/gcm/nopadding进行加密和解密。我使用这段代码: IV总是一个12字节数组,key是32字节数组,SecureRandom使用种子生成。我知道在不同的OS上SecureRandom是不同的,但是加密和解密是在同一个OS上执行的,所以应该没有问题。 是不是很线性,对吧?它在Windows上工作得很好,加密和破译返回相同的文本。然而,在Doc