LWJGL / OpenGL实现导致NullPointerException错误

时间:2015-05-01 22:35:08

标签: java eclipse opengl lwjgl

package com.flappygame.flappy;

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

import java.nio.ByteBuffer;

import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GLContext;

public class Main implements Runnable {

    private int width = 1280;
    private int height = 720;

    private Thread thread;
    private boolean running = false;

    private long window;


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

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

        //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
        //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
        //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
        window = glfwCreateWindow(width, height, "Flappy", NULL, NULL);
        if (window == NULL) {
            System.err.println("Could not create GLFW window!");
            return;
        }

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

        glfwMakeContextCurrent(window);
        glfwShowWindow(window);     
        //System.out.println("OpenGL version: " + glGetString(GL_VERSION));
    }

    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();
    }

}

(这是我的整个代码)

首先,我使用了本教程中的代码https://www.youtube.com/watch?v=527bR2JHSR0(GitHub repo:https://github.com/TheCherno/Flappy)。我一步一步地编写这段代码,但我坚持......运行它(荒谬的是,我只做了主文件)。重要的是,IMO,我在Mac OS X 10.10.3上使用 Eclipse 。 OFC我已经添加了所有LWJGL库。这是控制台错误我在运行代码的每一次(fu ...)时间:

Could not create GLFW window!
Exception in thread "Game" java.lang.NullPointerException
at org.lwjgl.system.Checks.checkPointer(Checks.java:66)
at org.lwjgl.glfw.GLFW.glfwSwapBuffers(GLFW.java:2578)
at com.radnok.flappy.Main.render(Main.java:74)
at com.radnok.flappy.Main.run(Main.java:60)
at java.lang.Thread.run(Thread.java:745)

这里有什么问题? 我有OpenGL版本(注释行)的问题。也许是因为它?

0 个答案:

没有答案