java.lang.RuntimeException:当前线程中找不到OpenGL上下文

时间:2013-02-17 22:40:47

标签: java applet lwjgl

我正在尝试为我创建的小游戏创建一个applet。当我正常运行游戏(没有小程序)它工作得很好但是当我试图将它作为小程序运行时它会给我以下错误。

即时通讯使用lwjgl 2.8.4 opengl

java.lang.RuntimeException: No OpenGL context found in the current thread.
java.lang.RuntimeException: Unable to create display
    at com.base.engine.GameApplet.init(GameApplet.java:202)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:722)

这是GameApplet.java的代码

package com.base.engine;

import com.base.game.Game;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

public class GameApplet extends Applet {

    Canvas display_parent;

    /** Thread which runs the main game loop */
    Thread gameThread;

    /** is the game loop running */
    private static boolean running = false;

    private static Game game;

    public void startLWJGL() {
        //gameThread = new Thread() {
                       // @Override
            //public void run() {
                running = true;
                try {
                                        initDisplay();
                    Display.setParent(display_parent);
                    System.out.println("hij loopt");
                                        //Display.create();
                                        System.out.println("hij loopt");

                                        initGL();
                                        initGame();

                                        gameLoop();

                                        cleanUp();

                } catch (LWJGLException e) {
                    return;
                }
                //gameLoop();                                
            //}

        //};
        //gameThread.start();
    }

    private static void initGame()
    {
        game = new Game();
    }

    private static void getInput()
    {
        System.out.print("before input");
        game.getInput();
        System.out.print("after input");
    }

    private static void update()
    {
        game.update();
    }

    private static void render()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();

        game.render();

        Display.update();
        Display.sync(60);
    }

    private static void gameLoop()
    {  

            System.out.print("before while");
            while(!Display.isCloseRequested())
            {
                while(running) {
                    System.out.print("before running shit");
                    Display.sync(60);
                    Display.update();

                    Display.destroy();

                    //getInput();
                    update();
                    render();
                    System.out.println("running gameLoop..");
                }
            }
            System.out.print("after while");
    }

    private static void initGL()
    {        

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
        glMatrixMode(GL_MODELVIEW);

        glDisable(GL_DEPTH_TEST);

        glClearColor(0,0,0,0);
    }

    private static void cleanUp()
    {
        Display.destroy();
        Keyboard.destroy();
    }

    private static void initDisplay()
    { 
        try 
        {

            Display.setDisplayMode(new DisplayMode(800,600));
            Display.create();
            Keyboard.create();
            System.out.println("Keyboard created");
            Display.setVSyncEnabled(true);
        } catch (LWJGLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    public static ArrayList<GameObject> sphereCollide(float x, float y, float radius)
    {
        return game.sphereCollide(x, y, radius);
    }

    /**
     * Tell game loop to stop running, after which the LWJGL Display will 
     * be destoryed. The main thread will wait for the Display.destroy().
     */

    private void stopLWJGL() {
        running = false;

    }

        @Override
    public void start() {
        //startLWJGL();
    }

        @Override
    public void stop() {

    }

    /**
     * Applet Destroy method will remove the canvas, 
     * before canvas is destroyed it will notify stopLWJGL()
     * to stop the main game loop and to destroy the Display
     */
        @Override
    public void destroy() {
        remove(display_parent);
        super.destroy();
    }

        @Override
    public void init() {
        setLayout(new BorderLayout());
        try {
            display_parent = new Canvas() {
                                @Override
                public final void addNotify() {
                    super.addNotify();
                    startLWJGL();
                }
                                @Override
                public final void removeNotify() {
                    stopLWJGL();
                    super.removeNotify();
                }
            };
            display_parent.setSize(800,600);
            add(display_parent);
                        System.out.println("gaat ie lekker");
            display_parent.setFocusable(true);
            display_parent.requestFocus();
            display_parent.setIgnoreRepaint(true);
            setVisible(true);
        } catch (Exception e) {
            System.err.println(e);
            throw new RuntimeException("Unable to create display");
        }
    }

}

有人可以告诉我,我做错了吗?

0 个答案:

没有答案