kw7
kw7

Reputation: 63

libgdx not initialized yet

I have been coding my libgdx application (for desktop only) for a while and after deciding to cleanup the code part by part iv'e gotten into a problem i cant seem to solve myself..

exception:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()J
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
Caused by: java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()J
    at com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape(Native Method)
    at com.badlogic.gdx.physics.box2d.PolygonShape.<init>(PolygonShape.java:29)
    at com.mygdx.game.handler.BodyEditorLoader.<init>(BodyEditorLoader.java:41)
    at com.mygdx.game.util.GameUtils.init(GameUtils.java:23)
    at com.mygdx.game.DungeonLife.create(DungeonLife.java:168)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

after googling for a while i figured my error is as mentioned in this thread as in

Another problem might be that you instantiate a SpriteBatch (or something else which internally uses a SpriteBatch) too early (looked a bit like this in the stacktrace).

but as the answer mentions

Instead, create such things in the create/show methods of your game.

I cant seem to understand when libgdx is initialized and ready for use, and where to place my GameUtils.init() method to assure libgdx is initialized

My code is as follows: (i have taken out e-relevant methods)

Application Class

package com.mygdx.game;

import box2dLight.RayHandler;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.mygdx.game.entity.MobEntity;
import com.mygdx.game.entity.PhysicalEntity;
import com.mygdx.game.entity.Player;
import com.mygdx.game.entity.Weapon;
import com.mygdx.game.handler.*;
import com.mygdx.game.util.CollisionConstants;
import com.mygdx.game.util.GameUtils;
import com.mygdx.game.util.TileObjectUtil;
import com.mygdx.game.util.WorldConstants;
import com.mygdx.game.valtype.WeaponDefinition;

public class DungeonLife extends ApplicationAdapter implements WorldConstants {

    OrthographicCamera camera;

    float width , height;

    Texture texture;
    TextureRegion[] enttex;

    //TEMP
    MobEntity demo;

    Player thePlayer;
    PlayerInputProcessor playerInputProcessor;

    ScreenUI ui;

    int mapWidth , mapHeight;

    //========================================
    GameMap gameMap;
    //========================================


    @Override
    public void create () {

        texture = new Texture("maps/img/tileset_entity.png");

        enttex = new TextureRegion[(int) ((texture.getWidth()*texture.getHeight()) / (BLOCK*BLOCK))];

        enttex[0] = new TextureRegion(texture , 0 , 0 , (int)BLOCK , (int)BLOCK);
        enttex[1] = new TextureRegion(texture , (int)BLOCK , 0 , (int)BLOCK , (int)BLOCK);

        width = Gdx.graphics.getWidth()/5;
        height = Gdx.graphics.getHeight()/5;
        camera = new OrthographicCamera(width,height);
        camera.position.set(width / 2, height / 2, 0);

        camera.update();

        GameUtils.init(); // <------this guy

        //init();


    } ...

GameUtils

package com.mygdx.game.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.game.entity.PhysicalEntity;
import com.mygdx.game.handler.*;

import java.util.PriorityQueue;

public class GameUtils {

    public static void init(){

        weapon_bodyEditorLoader = new BodyEditorLoader(Gdx.files.internal("textures/weapons/dungeonlife_weapons.json"));
        resourceManager = new ResourceManager();
        resourceManager.addRes("friendlyhealth" , new Texture("textures/ui/friendlyhealth.png"));
        resourceManager.addRes("enemyhealth" , new Texture("textures/ui/enemyhealth.png"));
        tmxMapLoader = new TmxMapLoader();
        gameContactListender = new GameContactListender();


    }


    //GLOBAL
    public static BodyEditorLoader weapon_bodyEditorLoader;
    public static GameContactListender gameContactListender;
    public static ResourceManager resourceManager;
    public static TmxMapLoader tmxMapLoader;


    //CURRENTS ============================

    public static GameMap CURRENT_GAMEMAP;

}

Desktop launcher (the usual)

package com.mygdx.game.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.DungeonLife;
import com.mygdx.game.util.GameUtils;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.useGL30 = false;
        config.width=640;
        config.height=360;
        DungeonLife dungeonLife = new DungeonLife();
        new LwjglApplication(dungeonLife, config);
    }
}

Help is much appriciated! :D

Upvotes: 2

Views: 739

Answers (1)

noone
noone

Reputation: 19776

As I've already wrote in the other linked answer, make sure that your project is correctly set up. Box2D is an extension and needs its own native libraries to be able to run.

It seems that it's a specific problem with the Box2D natives which are not loaded yet. They are not loaded automatically when LibGDX is initializing, but you have to trigger that yourself (see wiki).

The code you've posted looks correct, but before you can use anything Box2D related, you have to call Box2D.init(). Alternatively, creating a World object does the same, but isn't the nicest way to do it.

Upvotes: 1

Related Questions