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

主线程上的LWJGL窗口

彭宜人
2023-03-14
package main;

import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwGetPrimaryMonitor;
import static org.lwjgl.glfw.GLFW.glfwGetVideoMode;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPollEvents;
import static org.lwjgl.glfw.GLFW.glfwSetWindowPos;
import static org.lwjgl.glfw.GLFW.glfwShowWindow;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import static org.lwjgl.opengl.GL11.GL_TRUE;
import static org.lwjgl.system.MemoryUtil.NULL;

import java.nio.ByteBuffer;

import org.lwjgl.glfw.GLFWVidMode;

public class Main implements Runnable{

    private Thread thread;
    public boolean running = true;

    private long window;

    private int width = 1200, height = 800;

    public static void main(String args[]){
        Main game = new Main();
        game.start();
    }

    public void start(){
        running = true;
        thread = new Thread(this, "EndlessRunner");
        thread.start();
    }

    public void init(){
        // Initializes our window creator library - GLFW 
        // This basically means, if this glfwInit() doesn't run properlly
        // print an error to the console
        if(glfwInit() != GL_TRUE){
            // Throw an error.
            System.err.println("GLFW initialization failed!");
        }

        // Allows our window to be resizable
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

        // Creates our window. You'll need to declare private long window at the
        // top of the class though. 
        // We pass the width and height of the game we want as well as the title for
        // the window. The last 2 NULL parameters are for more advanced uses and you
        // shouldn't worry about them right now.
        window = glfwCreateWindow(width, height, "Endless Runner", NULL, NULL);

        // This code performs the appropriate checks to ensure that the
        // window was successfully created. 
        // If not then it prints an error to the console
        if(window == NULL){
            // Throw an Error
            System.err.println("Could not create our Window!");
        }

        // creates a bytebuffer object 'vidmode' which then queries 
        // to see what the primary monitor is. 
        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        // Sets the initial position of our game window. 
        glfwSetWindowPos(window, 100, 100);
        // Sets the context of GLFW, this is vital for our program to work.
        glfwMakeContextCurrent(window);
        // finally shows our created window in all it's glory.
        glfwShowWindow(window);
    }

    public void update(){
        // Polls for any window events such as the window closing etc.
        glfwPollEvents();
    }

    public void render(){
        // Swaps out our buffers
        glfwSwapBuffers(window);
    }

    @Override
    public void run() {
        // All our initialization code
        init();
        // Our main game loop
        while(running){
            update();
            render();
            // Checks to see if either the escape button or the
            // red cross at the top were pressed.
            // if so sets our boolean to false and closes the
            // thread.
            if(glfwWindowShouldClose(window) == GL_TRUE){
                running = false;
            }
        }
    }
}
2016-01-18 12:09:36.761 java[21882:475722] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /SourceCache/Foundation/Foundation-1153.20/Misc.subproj/NSUndoManager.m:340
2016-01-18 12:09:36.762 java[21882:475722] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2016-01-18 12:09:36.762 java[21882:475722] (
    0   CoreFoundation                      0x00007fff9160903c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff91b8a76e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff91608e1a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff99e518cb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   Foundation                          0x00007fff99dd357f +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 156
    5   AppKit                              0x00007fff8cc0dc95 -[NSApplication run] + 756
    6   libglfw.dylib                       0x000000012944a17e initializeAppKit + 1342
    7   libglfw.dylib                       0x0000000129449845 _glfwPlatformCreateWindow + 37
    8   libglfw.dylib                       0x00000001294454b1 glfwCreateWindow + 513
    9   ???                                 0x000000011129e954 0x0 + 4582926676
    10  ???                                 0x0000000111290760 0x0 + 4582868832
    11  ???                                 0x0000000111290760 0x0 + 4582868832
    12  ???                                 0x0000000111290760 0x0 + 4582868832
    13  ???                                 0x0000000111290c4d 0x0 + 4582870093
    14  ???                                 0x0000000111290c92 0x0 + 4582870162
)

共有1个答案

余铭晨
2023-03-14

出现此错误的原因是因为您在一个单独的线程上运行GLFW窗口。在主线程上运行窗口,它就可以工作了。

正如在这里的评论中读到的

做了快速检查。在同一个程序中创建多个显示会导致错误,即使它们是在分开的线程中创建的。您可以做的一件事是为您需要的两个(或更多)显示创建分离的java程序,并让它们使用套接字和/或流进行通信

 类似资料:
  • 任务:Main.Main()失败无法创建窗口 执行任务“:main.main()”失败。 进程“命令”c:/program files/java/jdk-16/bin/java.exe“以非零退出值-1结束 null null

  • 给定Clojure的并发模型,我如何确保所有LWJGL OpenGL更新函数都是从同一个线程调用的? 我熟悉C/C++中的线程。我熟悉Clojure的agent/atom/ref模型。然而,我并不熟悉Clojure中的“手动”并发/线程处理。 谢谢!

  • 我的程序中运行了两个线程。当我尝试在非主线程中使用OpenGL函数时,它抛出了一个IllegalStateException:当前线程中没有当前的OpenGL上下文。所以我的问题是,如何将上下文从主线程传递到另一个线程?我正在使用LWJGL 3。

  • 我试图用Java/LWJGL做一个简单的游戏。我在学习一个在windows上制作的教程,我用的是Mac。我复制了他的代码来打开一个窗口字符,程序立即崩溃了,给了我一个很长很奇怪的错误,可能与指针有关(我实际上不知道)。下面是用于创建窗口的行,我使用该窗口的属性中定义了宽度和高度: 当我在mac上运行它时,它会给我以下错误:

  • 我在Android上使用RxJava和Retrofit2.0来处理网络请求。 在创建可观察对象时,我向其添加以下内容: 是否有一个优雅的解决方案来解决这个问题,而不必手动地用一个块包装我的实现,以便将一些东西发布到主线程?

  • 我今天下载了LWJGL3,发现它几乎完全重写了。我查阅了一个关于如何创建窗口的教程,但我仍然有创建窗口的问题。 代码运行时没有问题:控制台中没有错误,但窗口没有显示!