【问题标题】:Java lwjgl&slick2d game gui and hud?Java lwjgl&slick2d 游戏 gui 和 hud?
【发布时间】:2014-04-30 16:53:04
【问题描述】:

我对 Java 还很陌生,目前正在开发一个小游戏,只是为了测试我能用 Java 做什么。但是遇到了一个随机问题:我不知道如何制作 GUI(单人游戏、多人游戏、选项...的菜单)和 HUD(值 [生命、法力、经验...] 的图形显示)已经说过:我正在使用库 jinput、lwjgl、lwjgl_util 和 Slick2D。我的窗口是通过 lwjgl 的 Display 类呈现的。这是我的 Main.class 代码:

public class Main 
{
    public static int disWidth = 800;
    public static int disHeight =600;


    public static void main(String[] args)
    {
        initDisplay();
        initGL();
        initGame();


        gameLoop();
//        initGui();


        cleanUp();
    }

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

    private static void getInput()
    {
        Game.game.getInput();
    }

    private static void initGui()
    {
//Render the gui
    }

    private static void update()
    {
       Game.game.update();
//       GUI.gui.update() or something
    }

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


        Game.game.render();


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

    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 gameLoop()
    {        
        Time.init();

        int frames = 0;
        long lastTime = System.nanoTime();
        long totalTime = 0;

        while(!Display.isCloseRequested())
        {
            long now = System.nanoTime();
            long passed = now - lastTime;
            lastTime = now;
            totalTime += passed;

            if(totalTime >= 1000000000)
            {
                System.out.println(frames);
                totalTime = 0;
                frames = 0;
            }

            Time.update();
            getInput();
            update();
            render();
            frames++;
        }



    }

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


    private static void initDisplay()
    {
        try 
        {
            Display.setDisplayMode(new DisplayMode(disWidth, disHeight));
            Display.create();
            Keyboard.create();
        }
        catch (LWJGLException ex) 
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }    
}

【问题讨论】:

    标签: java user-interface interface 2d lwjgl


    【解决方案1】:

    您应该尝试检查 TWL。我认为这是在 LWJGL 环境中实现 GUI 的一种方法,尽管这种方法非常困难(可能是由于 xml 部分)并且它的文档记录不太好(在我看来)

    您也可以尝试将 LWJGL 的主 Display 类放在标准 Swing Canvas 中,使用该函数设置其父级。

    我自己需要一个 GUI,我正在学习 TWL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多