如何使用lwjgl切换显示

时间:2013-10-28 03:22:10

标签: screen buffer panel lwjgl

我想知道如何在lwjgl中切换显示。你做swapBuffers(name_of_display)吗?我的意思是菜单屏幕,我想切换到选项屏幕而不关闭显示窗口,只关闭窗口内的面板。

1 个答案:

答案 0 :(得分:0)

这是我处理菜单的游戏容器之一,也许你可以从中获得有用的东西。

package catt.main;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

import catt.gameMode.Arcade;
import catt.gameMode.Campaign;
import catt.gameMode.Editor;
import catt.gameMode.gameMode;
import catt.gameMode.menu.ArcadeMenu;
import catt.gameMode.menu.CampaignMenu;
import catt.gameMode.menu.ExitMenu;
import catt.gameMode.menu.ExitToMainMenu;
import catt.gameMode.menu.LoadScreen;
import catt.gameMode.menu.MainMenu;
import catt.gameMode.menu.editor.EditorMenu;
import catt.gameMode.menu.editor.LoadMenu;
import catt.gameMode.menu.editor.OverwriteMenu;
import catt.gameMode.menu.editor.SaveMenu;



public class Game {

    enum GameState{
        GAME, MENU;
    }
    enum GameType{

        //Gamemodes
        ARCADE, CAMPAIGN, EDITOR, LOADING,

        //Menus
        M_MAIN, M_ARCADE, M_EDITOR, M_EXIT, M_EXIT_MAIN, M_CAMPAIGN, M_LOAD, M_SAVE, M_OVERWRITE;
    }
    public static gameMode gameMode;
    public static gameMode gameMenu;

    private static GameState GameState;

    private static GameType GameType;
    private static GameType lastGameType;
    private static GameType lastMenu;

    private Boolean isRunning = true;

    private static String saveFile;

    @SuppressWarnings("static-access")
    public Game(){
        GameState = GameState.MENU;
        GameType = GameType.M_MAIN;
        lastMenu = GameType;
    }

    //Gameloop
    public void isRunning(){
        gameLoad();

        while(!Display.isCloseRequested()&&isRunning != false){
            glClear(GL_COLOR_BUFFER_BIT);

            //Subloops
            escape();
            update();


            Display.update(); 
            Display.sync(60);
        }
        setGameType("M_EXIT");
    }

    /**
     * Sets the gameMode or gameMenu to the right active state and loads it.
     */
    @SuppressWarnings("static-access")
    public static void gameLoad(){

        if(GameState == GameState.GAME){
            if(GameType == GameType.ARCADE){
                gameMode = new Arcade();
            }else if(GameType == GameType.CAMPAIGN){
                gameMode = new Campaign();
            }else if(GameType == GameType.EDITOR){
                gameMode = new Editor();
            }else if(GameType == GameType.LOADING){
                gameMode = new LoadScreen();
            }
            gameMode.load();
        }
        else if(GameState == GameState.MENU){
            if(GameType == GameType.M_MAIN){
                gameMenu = new MainMenu();
            }else if(GameType == GameType.M_ARCADE){
                gameMenu = new ArcadeMenu();
            }else if(GameType == GameType.M_CAMPAIGN){
                gameMenu = new CampaignMenu();
            }else if(GameType == GameType.M_EDITOR){
                gameMenu = new EditorMenu();
            }else if(GameType == GameType.M_EXIT){
                gameMenu = new ExitMenu();
            }else if(GameType == GameType.M_SAVE){
                gameMenu = new SaveMenu();
            }else if(GameType == GameType.M_LOAD){
                gameMenu = new LoadMenu();
            }else if(GameType == GameType.M_OVERWRITE){
                gameMenu = new OverwriteMenu(saveFile);
            }else if(GameType == GameType.M_EXIT_MAIN){
                gameMenu = new ExitToMainMenu();
            }
            gameMenu.load();
        }
    }

    /**
     * Checks if the menu should be opened or closed if the escape button is pressed;
     */
    @SuppressWarnings("static-access")
    public void escape(){
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
            if(Keyboard.next()){
                if(GameState == GameState.MENU&&GameType == GameType.M_MAIN){
                    setGameType("M_EXIT");
                }else if(GameState == GameState.MENU&&GameType != GameType.M_MAIN){
                    if(GameType == GameType.M_SAVE
                            ||GameType == Game.GameType.M_LOAD){
                        setGameType("M_LAST");
                    }else{                      
                        setState("GAME");
                    }
                }else if(GameState == GameState.GAME){

                    if(GameType == GameType.ARCADE){
                        setGameType("M_ARCADE");
                    }else if(GameType == GameType.CAMPAIGN){
                        setGameType("M_CAMPAIGN");
                    }else if(GameType == GameType.EDITOR){
                        setGameType("M_EDITOR");
                    }
                }
            }
        }
    }

    /**
     * Switches the game state to the asigned state (Menu or Game)
     */
    @SuppressWarnings("static-access")
    public void update(){
        if(GameState == GameState.MENU){
            gameMenu.update();
            gameMenu.render();
        }else if(GameState == GameState.GAME){
            gameMode.update();
            gameMode.render();
        }
    }

    @SuppressWarnings("static-access")
    public static void setState(String state){
        if(state == "GAME"){
            GameState = GameState.GAME;
            GameType = lastGameType;
        }
        else if(state == "MENU"){
            GameState = GameState.MENU;
            GameType = lastMenu;
        }
    }


    /**
     * Sets the (enum) GameType and GameState to the next GameType/State.
     * @param type
     */
    @SuppressWarnings("static-access")
    public static void setGameType(String type){
        //Gamemodes
        if (type == "ARCADE") {

            GameState = GameState.GAME;
            GameType = GameType.ARCADE;
            lastGameType = GameType.ARCADE;
        } 

        else if (type ==  "CAMPAIGN") {

            GameState = GameState.GAME;
            GameType = GameType.CAMPAIGN;
            lastGameType = GameType.CAMPAIGN;
        } 

        else if (type ==  "EDITOR") {

            GameState = GameState.GAME;
            GameType = GameType.EDITOR;
            lastGameType = GameType.EDITOR;
        }

        //Menus
        else if (type ==  "M_MAIN") {

            GameState = GameState.MENU;
            GameType = GameType.M_MAIN;
            lastMenu = GameType.M_MAIN;
        }

        else if (type ==  "M_ARCADE") {

            GameState = GameState.MENU;
            GameType = GameType.M_ARCADE;
            lastMenu = GameType.M_ARCADE;
        }

        else if (type ==  "M_CAMPAIGN") {

            GameState = GameState.MENU;
            GameType = GameType.M_CAMPAIGN;
            lastMenu = GameType.M_CAMPAIGN;
        }
        else if (type ==  "M_EDITOR") {

            GameState = GameState.MENU;
            GameType = GameType.M_EDITOR;
            lastMenu = GameType.M_EDITOR;
        }
        else if (type ==  "M_LOAD") {

            GameState = GameState.MENU;
            GameType = GameType.M_LOAD;
        }
        else if (type ==  "M_SAVE") {

            GameState = GameState.MENU;
            GameType = GameType.M_SAVE;
        }
        else if (type == "M_OVERWRITE"){
            GameState = GameState.MENU;
            GameType = GameType.M_OVERWRITE;
        }

        else if(type == "M_EXIT_MAIN"){
            GameState = GameState.MENU;
            GameType = GameType.M_EXIT_MAIN;
        }
        else if (type ==  "M_EXIT") {

            GameState = GameState.MENU;
            GameType = GameType.M_EXIT;
        }
        else if(type == "M_LAST"){
            GameState = GameState.MENU;
            GameType = lastMenu;
        }
        gameLoad();
    }