genaray
genaray

Reputation: 1180

Libgdx/java Performance crash with box2d light

Background Info

Hey guys i am currently working on a little rpg :) and today i tried to implement some easy light ....

Main Question

When i use Point light in my Project the fps will get slower and slower ... i have got a very good pc, so it cant be my gpu or cpu ... So what did i miss?

Heres an Screenshot enter image description here

You can see in the left bottom corner the fps : 6. By the way, i disabled vsync and my gpu is an gtx 960 ... so i dont really know why i have so low fps ...

Player class :

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;


}


}

And my "Light" class :

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();


}


}

Because i still got laggs, heres my 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();


}






 }

The .doDispose() in my mainClass are methods which disposes the resource from the classes is use

Thanks for your help and your time :)

Upvotes: 1

Views: 291

Answers (1)

Dennis
Dennis

Reputation: 121

You are not disposing anything, you might want to look into the dispose() function of LibGDX Here is the link https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html

if thats not the problem please let us know.

Upvotes: 2

Related Questions