【问题标题】:LWJGL window on main thread主线程上的 LWJGL 窗口
【发布时间】:2016-01-18 17:15:46
【问题描述】:

我已经做了一段时间的基本 java,最近我尝试在我的 mac 上使用 LWJGL(轻量级 Java 游戏库)。我为 eclipse mars 正确安装了它,并正在按照一些在线教程为程序构建基本大纲。

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

【问题讨论】:

    标签: java macos lwjgl


    【解决方案1】:

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

    正如在 cmets Here 中的此线程中所读到的那样

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      相关资源
      最近更新 更多