LibGDX多个摄像头

时间:2015-05-27 09:39:13

标签: java fonts camera libgdx bitmap-fonts

所以,我正在使用LiBGDX和Box2D,使用PixelsPerMeter转换,我有一个跟随播放器的摄像头,但当我打开调试模式时,我需要能够在屏幕上绘制带有fps的字体所以我总能在角落里看到它。问题是,当我尝试缩小BitmapFont时,它看起来很正常,导致其他所有内容都缩小,因为Box2D。我找到了一些关于使用多个摄像头的东西,所以我不必缩小字体,但它没有任何文档。我试着想办法怎么做,但没有运气。我该如何将字体绘制到屏幕上?

这是我尝试多台摄像机的游戏状态:

package com.platformer.gamestates;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.platformer.Entities.Player;
import com.platformer.game.Game;
import com.platformer.generation.MenuTiles;
import com.platformer.generation.TiledMaps;
import com.platformer.managers.GameContacts;
import com.platformer.managers.GameStateManager;

import static com.platformer.managers.B2DVars.PPM;

public class MenuState extends GameState {

    World world;
    Box2DDebugRenderer debugRenderer;

    Texture close, far, house;

    BitmapFont font;
    OrthographicCamera fontCam;

    public static Player player;
    MenuTiles menuTiles;

    SpriteBatch batch;

    float camx,camy;

    float lerp=0.1f,lerpy=0.2f;

    GameContacts gameContacts;


    public MenuState(GameStateManager gsm){
        super(gsm);
    }


    public  void init(){

        close=new Texture(Gdx.files.internal("menu_backgrounds/background_close.png"));
        far=new Texture(Gdx.files.internal("menu_backgrounds/background_far.png"));
        house=new Texture(Gdx.files.internal("menu_backgrounds/house_selected.png"));

        batch=new SpriteBatch();

        font=new BitmapFont();

        fontCam=new OrthographicCamera();
        fontCam.setToOrtho(false,Game.WIDTH,Game.HEIGHT);
        fontCam.position.set(Game.WIDTH/2/PPM,Game.HEIGHT/2/PPM,0);

        world=new World(new Vector2(0,-9.81f),true);
        world.setVelocityThreshold(0);
        gameContacts=new GameContacts();
        world.setContactListener(gameContacts);
        debugRenderer=new Box2DDebugRenderer();

        player=new Player(world,960/2,49+20);
        menuTiles=new MenuTiles(world);

    }
    public  void update(float dt){

        world.step(1/60f,6,2);

        player.update(dt,gameContacts);

        if(player.shouldPlay){gsm.setState(GameStateManager.PLAY);dispose();}

        camx+=(player.x-camx)*lerp;
        camy+=(player.y-camy)*lerpy;

    }

    public  void draw(OrthographicCamera camera){

        camera.position.x=Math.min(Math.max(camx,Game.WIDTH/2/PPM),(MenuTiles.levelWidth/PPM)-(Game.WIDTH/2/PPM));
        camera.position.y=Math.min(Math.max(camy,Game.HEIGHT/2/PPM),(MenuTiles.levelHeight/PPM)-(Game.HEIGHT/2/PPM));

        batch.begin();
        batch.draw(far, 0, 0, 960 / PPM, 720 / PPM);
        batch.draw(close,0,0,960/PPM,720/PPM);
        batch.end();

        //draw map
        menuTiles.draw(camera);

        batch.begin();
        if(gameContacts.isOnHouse())batch.draw(house,192/PPM,48/PPM,2*MenuTiles.tileSize/PPM,2*MenuTiles.tileSize/PPM);
        batch.end();

        //draw player
        player.draw(batch);

        if(player.DebugOn){
            debugRenderer.render(world, camera.combined);
            batch.setProjectionMatrix(fontCam.combined);
            batch.begin();
            font.draw(batch,"fps",0,0);
            batch.end();
            System.out.println(Gdx.graphics.getFramesPerSecond()+" fps");
        }

        camera.update();
        batch.setProjectionMatrix(camera.combined);

    }

    public  void handleInput(){}

    public  void dispose(){

        menuTiles.dispose();
        close.dispose();
        far.dispose();
        house.dispose();

    }
}

1 个答案:

答案 0 :(得分:1)

我认为你不想在这一行中除以PPM ......

fontCam.position.set(Game.WIDTH/2/PPM,Game.HEIGHT/2/PPM,0);

我假设Game.WIDHT和HEIGHT以像素为单位(这在您的代码段中并不清楚)。如果不是这种情况,那么你应该将PPM除以这一行......

fontCam.setToOrtho(false,Game.WIDTH,Game.HEIGHT);

目前它没有得到一贯应用。

此外,您需要在设置其位置后调用fontcam.update()。

另外:这与您的问题本身无关,但我不确定为什么在渲染结束时您有以下几行。在我改变标准相机的位置后,我会把它们放好。

camera.update();
batch.setProjectionMatrix(camera.combined);