Libgdx / java性能崩溃与box2d光

时间:2016-02-03 20:09:02

标签: java libgdx box2d light

背景资讯

嘿伙计们,我目前正在制作一个小小的RPG :)今天我试图实现一些简单的灯光......

主要问题

当我在我的项目中使用Point灯时,fps将变得越来越慢......我有一台非常好的电脑,所以它不能成为我的gpu或cpu ...那么我错过了什么?

继承人屏幕截图enter image description here

你可以在左下角看到fps:6。 顺便说一句,我禁用了vsync,我的gpu是一个gtx 960 ...所以我真的不知道为什么我有这么低的fps ......

玩家类:

package Mobs;


public class Player {

AnimatedSprite animatedSprite;
SpriteBatch batch;

Light licht = new Light(Color.WHITE,500);


public int state = 0;
public int netState = 1;
float speed = 2f;
public Vector2 position = new Vector2(256,256);
public Vector2 networkPosition = new Vector2(0,0);



public Player(){



}


public void update(){

       state = 0;

       if(Gdx.input.isKeyPressed(Keys.A)){ 
           position.x -= Gdx.graphics.getDeltaTime() * 100f;


           state = 1;
           //System.out.println(currentState);
       }
       if(Gdx.input.isKeyPressed(Keys.D)){
           position.x += Gdx.graphics.getDeltaTime() * 100f;    


           state = 2;
           //System.out.println(currentState);
       }
       if(Gdx.input.isKeyPressed(Keys.W)){ 
           position.y += Gdx.graphics.getDeltaTime() * 100f;


           state = 3;
           //System.out.println(currentState);
       }
       if(Gdx.input.isKeyPressed(Keys.S)){ 
           position.y -= Gdx.graphics.getDeltaTime() * 100f;


           state = 4;
           //System.out.println(currentState);
       }



}

public void setX(float x){

    position.x = x;

}

public void setY(float y){

    position.y = y;

}

public void draw(float f, float g, OrthographicCamera camera){

    position.x = f;
    position.y = g;

    //System.out.println("In beforeSetState : "+currentState);
    animatedSprite.setState(state);
    //System.out.println("In after : "+currentState);
    animatedSprite.createAnimation();

    camera.position.set(f,g,0);
    camera.update();

    licht.drawLight(camera, f+25 , g+25);


    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(animatedSprite.convertAnimationTOframes(),f,g, Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15);   
    batch.end();



}


public void doSetup(){


    batch = new SpriteBatch();
    animatedSprite = new AnimatedSprite();


}



public float getX(){


    return position.x;


}

public float getY(){


    return position.y;


}


}

我的“光”课:

package Screen;


public class Light {


World world;
RayHandler rayHandler;
PointLight pointLight;
Body player;
Box2DDebugRenderer debugRenderer;



public Light(Color farbe, int radius) {

    world = new World(new Vector2(0,0),false);
    rayHandler = new RayHandler(world);

    pointLight =  new PointLight(rayHandler, 500 , farbe , radius, 0, 0);
    pointLight.setSoftnessLength(0f);

    debugRenderer = new Box2DDebugRenderer();


}


public void drawLight(OrthographicCamera playerCam, float x, float y){

    world.step(1 / 60f, 8, 3);


    debugRenderer.render(world, playerCam.combined);

    pointLight.setPosition(x,y);

    rayHandler.setCombinedMatrix(playerCam.combined);
    rayHandler.updateAndRender();




}

public void removeLights(){

    rayHandler.removeAll();
    pointLight.remove();


}


}

因为我还有滞后,继承我的MainClass:

public class LauncherScreen implements Screen{

//-----------------------------------------------------------
//-----------------idle Animation----------------------------
//-----------------------------------------------------------

Map duengon;

AnimatedSprite animationForMultiplayer;
SpriteBatch spriteBatch;   
Player mySelf;

OrthographicCamera mpPlayerCam;
OrthographicCamera camera;

static Client client = new Client();
Launcher launcher = new Launcher();


int[][] map = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, 
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
               {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};


@Override
public void render(float delta) {
    // TODO Auto-generated method stub

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


    launcher.update();

    //------------------------------------------------------------------------
    //------------------------Draws the Map-----------------------------------
    //------------------------------------------------------------------------


    duengon.ubdate(map, camera);



    //------------------------------------------------------------------------
    //------------------------Draws the Players-------------------------------
    //------------------------------------------------------------------------



    for(MPPlayer mpPlayer : launcher.getPlayersValue()){   

        animationForMultiplayer.setState(mpPlayer.state);

        animationForMultiplayer.createAnimation();          

        camera.position.set(mpPlayer.x,mpPlayer.y,0);


        spriteBatch.setProjectionMatrix(camera.combined);

        spriteBatch.begin();                
        spriteBatch.draw(animationForMultiplayer.convertAnimationTOframes(), mpPlayer.x, mpPlayer.y,Gdx.graphics.getWidth()/25,Gdx.graphics.getHeight()/15);     // #6
        spriteBatch.end();

        System.out.println("mpPlayer : "+mpPlayer.x+" "+mpPlayer.y);


    }


    mySelf.update();
    mySelf.draw(launcher.getPlayerX(), launcher.getPlayerY(), camera);

    camera.update();

    System.out.println(Gdx.graphics.getFramesPerSecond());  

    System.out.println("player : "+launcher.getPlayerX()+" "+launcher.getPlayerY());



}




@Override
public void show() {
    // TODO Auto-generated method stub
    animationForMultiplayer = new AnimatedSprite();
    spriteBatch = new SpriteBatch();              
    mySelf = new Player();

    mySelf.doSetup();

    mpPlayerCam = new OrthographicCamera(0,0);
    mpPlayerCam.setToOrtho(false);


    camera = new OrthographicCamera(0, 0);
    camera.setToOrtho(false);

    duengon = new Map();



}


@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}


@Override
public void hide() {
    // TODO Auto-generated method stub

}




@Override
public void pause() {
    // TODO Auto-generated method stub

}




@Override
public void resume() {
    // TODO Auto-generated method stub

}




@Override
public void dispose() {
    // TODO Auto-generated method stub

    spriteBatch.dispose();
    mySelf.doDispose();
    animationForMultiplayer.doDispose();
    duengon.doDispose();


}






 }

我的mainClass中的.doDispose()是从类中处理资源的方法是使用

感谢您的帮助和时间:)

1 个答案:

答案 0 :(得分:2)

你没有处置任何东西,你可能想要查看LibGDX的dispose()函数 链接在这里 https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html

如果不是问题,请告诉我们。