Headless/CLI LibGDX

前端 未结 3 980
感情败类
感情败类 2021-01-02 18:06

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

相关标签:
3条回答
  • 2021-01-02 18:32

    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.

    0 讨论(0)
  • 2021-01-02 18:40

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

    0 讨论(0)
  • 2021-01-02 18:56

    Take a look at the LWJGL back end, specifically LwjglApplication.java. The constructor for LwjglApplication initializes all of the Gdx.* globals such as Gdx.files.

    If you were to invoke the steps run in the constructor from your own code, except for the call to initialize(), then that should get you what you want.

    0 讨论(0)
提交回复
热议问题