当前位置: 首页 > 知识库问答 >
问题:

LWJGL入门HelloWorld代码java exec窗口未打开

易研
2023-03-14

我在MacOS上使用Intellij。我对编程比较陌生,并从LWJGL网站上学习本教程。我遵循了所有的说明,并在VM选项中添加了-XStartonFirstThread。但是,每当我在IntelliJ中运行程序(HelloWorld.java),我就会看到停靠站中的java exec窗口,并得到“Hello LWJGL 3.2.3 build 13!”在控制台,但窗户打不开,我根本看不到窗户。

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

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

public class HelloWorld {

    // The window handle
    private long window;

    public void run() {
        System.out.println("Hello LWJGL " + Version.getVersion() + "!");

        init();
        loop();

        // Free the window callbacks and destroy the window
        glfwFreeCallbacks(window);
        glfwDestroyWindow(window);

        // Terminate GLFW and free the error callback
        glfwTerminate();
        glfwSetErrorCallback(null).free();
    }

    private void init() {
        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        GLFWErrorCallback.createPrint(System.err).set();

        // Initialize GLFW. Most GLFW functions will not work before doing this.
        if ( !glfwInit() )
            throw new IllegalStateException("Unable to initialize GLFW");

        // Configure GLFW
        glfwDefaultWindowHints(); // optional, the current window hints are already 
        the default
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden 
        after creation
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable

        // Create the window
        window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);
        if ( window == NULL )
            throw new RuntimeException("Failed to create the GLFW window");

        // Setup a key callback. It will be called every time a key is pressed, 
        repeated or released.
        glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
            if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
                glfwSetWindowShouldClose(window, true); // We will detect this in the 
        rendering loop
        });

        // Get the thread stack and push a new frame
        try ( MemoryStack stack = stackPush() ) {
            IntBuffer pWidth = stack.mallocInt(1); // int*
            IntBuffer pHeight = stack.mallocInt(1); // int*

            // Get the window size passed to glfwCreateWindow
            glfwGetWindowSize(window, pWidth, pHeight);

            // Get the resolution of the primary monitor
            GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

            // Center the window
            glfwSetWindowPos(
                    window,
                    (vidmode.width() - pWidth.get(0)) / 2,
                    (vidmode.height() - pHeight.get(0)) / 2
            );
        } // the stack frame is popped automatically

        // Make the OpenGL context current
        glfwMakeContextCurrent(window);
        // Enable v-sync
        glfwSwapInterval(1);

        // Make the window visible
        glfwShowWindow(window);
    }

    private void loop() {
        // This line is critical for LWJGL's interoperation with GLFW's
        // OpenGL context, or any context that is managed externally.
        // LWJGL detects the context that is current in the current thread,
        // creates the GLCapabilities instance and makes the OpenGL
        // bindings available for use.
        GL.createCapabilities();

        // Set the clear color
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

        // Run the rendering loop until the user has attempted to close
        // the window or has pressed the ESCAPE key.
        while ( !glfwWindowShouldClose(window) ) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the 
        framebuffer

            glfwSwapBuffers(window); // swap the color buffers

            // Poll for window events. The key callback above will only be
            // invoked during this call.
            glfwPollEvents();
        }
    }

    public static void main(String[] args) {
        new HelloWorld().run();
    }

}
window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL);

共有1个答案

爱琪
2023-03-14

您需要将Mac OS上的向前兼容性设置为true。在init()中调用

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

另外,您可能需要设置OpenGL版本。

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// This means you are setting the OpenGL version to be 3.3 (version_major.version_minor)
 类似资料:
  • 任务:Main.Main()失败无法创建窗口 执行任务“:main.main()”失败。 进程“命令”c:/program files/java/jdk-16/bin/java.exe“以非零退出值-1结束 null null

  • 我今天下载了LWJGL3,发现它几乎完全重写了。我查阅了一个关于如何创建窗口的教程,但我仍然有创建窗口的问题。 代码运行时没有问题:控制台中没有错误,但窗口没有显示!

  • 我有两台显示器;创建 LWJGL 窗口时: 它总是出现在我的左侧屏幕上。是否有一个参数可以设置以更改它出现在哪个屏幕上,例如:

  • 我不熟悉使用LWJGL(一般来说Java也是如此),因此,我阅读了他们关于入门的页面,他们提供了一个Hello World程序示例。我自己尝试运行它,发现它出现以下错误:<代码>2020-03-18 10:20:02.145 java[19779:1119716]***由于未捕获的异常“nSinternalinconsistenceexception”而终止应用程序,原因:“[NSUndoMana

  • 我试图用Java/LWJGL做一个简单的游戏。我在学习一个在windows上制作的教程,我用的是Mac。我复制了他的代码来打开一个窗口字符,程序立即崩溃了,给了我一个很长很奇怪的错误,可能与指针有关(我实际上不知道)。下面是用于创建窗口的行,我使用该窗口的属性中定义了宽度和高度: 当我在mac上运行它时,它会给我以下错误:

  • 我在调整窗口大小时有问题。当我尝试调整窗口大小时,屏幕没有正确地改变。这是窗口的原始大小: 但是当我调整窗口的大小时,屏幕会变成这样: 每次调整窗口大小但仍然不能工作时,我都会调用updateProjectionMatrix()和glVievport()。 我的代码: