Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headless/CLI LibGDX

Tags:

java

libgdx

I'm coding the server-side for a small LibGDX powered game, and have stumbled across an issue. Every time I try and use any Gdx.files.* methods, I'm met with a NullPointerException.

Apparently this is because I'm not implementing the ApplicationListener, so LibGDX has not been initialized.

Is there any way of initializing LibGDX in a headless/CLI way? I need to be able to load TiledMap objects on the server-side.

MapLoader(Request request)
{
    TiledMap tmp = new TmxMapLoader().load("maps/" + request.name + ".tmx");
}

Exception in thread "Server" java.lang.NullPointerException at com.tester.Example.server.ExampleServer$2.received(MapLoader.java:83) at com.esotericsoftware.kryonet.Server$1.received(Server.java:60) at com.esotericsoftware.kryonet.Connection.notifyReceived(Connection.java:246) at com.esotericsoftware.kryonet.Server.update(Server.java:202) at com.esotericsoftware.kryonet.Server.run(Server.java:350) at java.lang.Thread.run(Thread.java:722)

like image 870
Ben Poulson Avatar asked Apr 17 '13 19:04

Ben Poulson


2 Answers

I would not recommend using libGDX for a headless environment, it was simply not designed to be used that way and you may encounter issues in the future as the libGDX team changes the framework. However, as Rod pointed out, it is entirely possible to do so and below is a snippet of how you would go about it. To initialize the Gdx.files global you will need to create a class in the backend package and setup the globals yourself:

package com.badlogic.gdx.backends.lwjgl;

import com.badlogic.gdx.Gdx;

public class Headless {
    public static void loadHeadless() {
        LwjglNativesLoader.load();
        Gdx.files = new LwjglFiles();
    }
}

The rest should be fairly simple. Just call Headless.loadHeadless(); at the start and then you should be able to use parts of the framework you require.

As I stated before, I would not suggest doing this, but I haven't found any nice solutions for using libgdx with a client/server architecture.

Edit:

A little while back (after I wrote this answer originally) libgdx added a headless backend which is designed for this kind of purpose. It is the correct and proper way to use libgdx in a headless environment and works very well for creating a server with libgdx.

like image 142
Jyro117 Avatar answered Nov 09 '22 03:11

Jyro117


As of Dec 23, 2013 (pull request #1018), libGDX has a headless backend that you can use for this purpose.

like image 40
Travis Avatar answered Nov 09 '22 04:11

Travis