主线程上的LWJGL窗口

时间:2016-01-18 17:15:47

标签: java macos lwjgl

我已经做了一段时间的基础java了,最近我尝试在我的mac上开始使用LWJGL(轻量级Java游戏库)。我正确地为日食火星安装它,并通过一些在线教程建立程序的基本大纲。

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
)

任何事情都会非常有用,我运行Mac OSX Yosemite,Eclipse Mars,我从2016年1月16日起每晚都有LWJGL

1 个答案:

答案 0 :(得分:0)

您收到此错误的原因是您在单独的线程上运行GLFW窗口。在主线程上运行窗口,它将起作用。

正如评论Here

中的此主题所述
  

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

意味着使用除主线程之外的任何线程都会导致错误。