sukramx
sukramx

Reputation: 13

Getting Problems with GLFW under MAC OS

i have to run a java lwjgl game on a Mac but there are problems with GLFW.

> Exception in thread "main" java.lang.ExceptionInInitializerError
at org.lwjgl.glfw.GLFW.glfwShowWindow(GLFW.java:1612)
at assignment7.windowmanager.Window.init(Window.java:65)
at assignment7.windowmanager.Window.start(Window.java:74)
at assignment7.windowmanager.Window.<init>(Window.java:35)
at assignment7.windowmanager.MyWindow.<init>(MyWindow.java:20)
at assignment7.MainGameLoop7.main(MainGameLoop7.java:14)

Caused by: java.lang.IllegalStateException: Please run the JVM with -XstartOnFirstThread. at org.lwjgl.system.macosx.EventLoop.(EventLoop.java:17) ... 6 more

this is my error code

the implementation for the windows class:

package assignment7.windowmanager;

import assignment7.entitites.Scene;
import assignment7.utilities.Input;
import org.lwjgl.opengl.GL;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;

import java.awt.Dimension;



/**
 * Created by Dennis Dubbert on 06.09.16.
 */
public abstract class Window {
    protected int   width, height;
    protected long  window;

    /*
     * In ScreenSize wird die Gr��e des aktuellen Bildschirms abgespeichert.
     * Daraufhin wird die initiale x- und y-Koordinate auf die Mitte des Bildschirmes gesetzt.
     * Da aber die linkere obere Ecke des Fensters zentriert w�re und nicht die Mitte dessen, 
     * wurde 320 von der x-Koordinate und 240 von der y-Koordinate abgezogen. (was der H�lfte des Fensters entspricht. Siehe MyWindow.java)
     */
    protected Dimension ScreenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    protected int xWindowPos = (int) ScreenSize.getWidth()/2 - 320;
    protected int yWindowPos = (int) ScreenSize.getHeight()/2 - 240;

    public Window(int width, int height) {
        this.width = width;
        this.height = height;
        start();
    }

    private void init(){
        if(glfwInit() != GL_TRUE){
            System.err.println("Could not initialize our glfw!");
            return;
        }

        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

        window = glfwCreateWindow(width, height, "assignment7", NULL, NULL);

        glfwSetWindowPos(window, 80, 30);


        if(window == NULL){
            System.err.println("Could not create our Window!");
            return;
        }




        Input.init(window, this);
        glfwMakeContextCurrent(window);
        //here is one Error
        glfwShowWindow(window);

        GL.createCapabilities();

        System.out.println("Your OpenGL version is " + glGetString(GL_VERSION));
        onInit();
    }

    public void start(){
        init();

        while(true){

            update();
            render();

            if(glfwWindowShouldClose(window) == GL_TRUE){
                break;
            }
        }

        glfwDestroyWindow(window);
        glfwTerminate();
    }

    private void update(){
        //here is an Error, too
        glfwPollEvents();
        onUpdate(glfwGetTime());
    }

    private void render(){
        if(Scene.getAmountOfCollisions() == 5){
            glfwDestroyWindow(window);
            glfwTerminate();
            System.exit(1);
        }
        onRender();
        glfwSwapBuffers(window);
    }

    public void moveWindowPos(int x, int y){

        /*
         * Um das leichte ruckeln zu entfernen k�nnte man statt einmal um 1 zu erh�hen/erniedrigen 10 mal um 0.1f erh�hen/erniedrigen.
         * Gedanke dahinter: die Taktrate der GPU ist um einiges Schneller als die einfache erh�hung um 1. 
         * Erste Idee (noch falsch!): 
         * if( x < 0 ){
            for(float i = x; i < 0; i++){
                glfwSetWindowPos(window, xWindowPos-=0.1f, yWindowPos);
                }
            }
        if( x > 0 ){
            for(float i = 0; i < x; i++){
                glfwSetWindowPos(window, xWindowPos+=0.1f, yWindowPos);
            }
        }
        if( y < 0 ){
            for(float i = y; i < 0; i++){
                glfwSetWindowPos(window, xWindowPos, yWindowPos-=0.1f);
            }
        }
        if( y > 0 ){
            for(float i = 0; i < y; i++){
                glfwSetWindowPos(window, xWindowPos, yWindowPos+=0.1f);
            }
        }
         */
                xWindowPos = xWindowPos+x;
                yWindowPos = yWindowPos+y;
                glfwSetWindowPos(window, xWindowPos, yWindowPos);

    }

    protected abstract void onInit();
    protected abstract void onUpdate(double currentTime);
    protected abstract void onRender();
    public abstract void onResize(int width, int height);
    public abstract void onKeyboard(float cursorPositionX, float       cursorPositionY, int pressedKey, int mods);
    public abstract void onMouse(float cursorPositionX, float cursorPositionY, int button, int action);

}

Maybe you can help me

Upvotes: 1

Views: 938

Answers (2)

Jackie Wang
Jackie Wang

Reputation: 21

You just need to configure the Run Configuration in Eclipse, like this:

first choose the run configuration

paste the -XstartOnFirstThread to the run argument

then click run, you won't see this exception any more.

Upvotes: 1

egtoney
egtoney

Reputation: 31

From the error you posted above it seems like you just need to add -XstartOnFirstThread to the command you are using to run the program. So it should be something like this.

java -XstartOnFirstThread -jar executable.jar

If you are using an ide you will have to edit the run configuration to include this argument.

This error is occurring because your frame management must be done in the main thread of your application. Also, be careful because a lot of the GLFW commands can only be called from the main thread. You can tell which ones this applies to in the documentation for each command (in the notes section). example

Upvotes: 2

Related Questions