LWJGL: glfwCreateWindow return null

怎甘沉沦 提交于 2019-12-10 15:57:14

问题


I was trying LWJGL library in Java seeing the tutorial, but the creation of the window I will return null.

This is the code:

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.ByteBuffer;
import org.lwjgl.glfw.GLFWvidmode;

public class Main implements Runnable {
    private int height = 720, width = height / 9 * 16;
    private String title = "Game";

    private Thread thread;
    private boolean running = false;

    private long window;

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

    private void init() {
        if(glfwInit() != GL_TRUE) {
            System.err.println("Non riesco ad inizializzare GLFW!");
            return;
        }

        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
        window = glfwCreateWindow(width, height, title, NULL, NULL);

        if(window == NULL) {
            System.err.println("Non riesco a creare una finestra GLFW!");
            return;
        }

        ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - width) / 2, (GLFWvidmode.height(vidmode) - height) / 2);

        glfwMakeContextCurrent(window);
        glfwShowWindow(window);
    }

    @Override
    public void run() {
        init();
        while(running) {
            update();
            render();

            if(glfwWindowShouldClose(window) == GL_TRUE)
                running = false;
        }
    }

    private void update() {
        glfwPollEvents();
    }

    private void render() {
        glfwSwapBuffers(window);
    }

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

Returns the following error:

Non riesco a creare una finestra GLFW!
Exception in thread "Game" java.lang.NullPointerException
    at org.lwjgl.system.Checks.checkPointer(Checks.java:66)
    at org.lwjgl.glfw.GLFW.glfwSwapBuffers(GLFW.java:2546)
    at com.michele.flappybird.Main.render(Main.java:66)
    at com.michele.flappybird.Main.run(Main.java:54)
    at java.lang.Thread.run(Unknown Source)

How can I fix? Thanks to anyone trying to help me.


回答1:


My first attempt would be to simplify the program by removing the Thread, since LWJGL sometime has problems calling methods from outside the main thread. The second attempt would be to remove the glfwWindowHint(...) methods and instead call glfwDefaultWindowHints()



来源:https://stackoverflow.com/questions/28929736/lwjgl-glfwcreatewindow-return-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!