libgdx Getting started with libgdx Adding support for platform-specific code


Example

LibGDX is designed in a way that you can write the same code and deploy it on several different platforms. Yet, there are times when you want to get access to platform specific code. For an instance, if you have leaderboards and achievements in your game, you may want to use platform-specific tools (like Google Play Games) in addition to storing them locally. Or you want to use a database, or something completely different.

You can't add this kind of code into the core module. So the first step is to create an Interface. Create it in the core module. This first one is a utility to manage other interfaces:

public interface PlatformWrapper{
    //here you can also add other things you need that are platform specific.
    //If you want to create your own file saver for an instance, a JSON based database,
    //achievements, leaderboards, in app purchases and anything else you need platform specific code for.
    SQLWrapper getSQLSaver();//This one will be used in this example
    AchievementWrapper getAchievementHandler();//this line is here as an example
}

Then, we need to create a second interface, the SQLWrapper. This one also goes in the core module.

public interface SQLWrapper{
    void init(String DATABASE);
    void saveSerializable(int id, Object o);
    Object loadSerializable(int id, Object o);
    void saveString(int id, String s);
    //.... and other methods you need here. This is something that varies
    //with usage. You may not need the serializable methods, but really need a custom method for saving 
    //an entire game world. This part is entirely up to you to find what methods you need
   
    //With these three methods, always assume it is the active database in question. Unless 
    //otherwise specified, it most likely is
    String getActiveDatabase();
    void deleteDatabase();
    void deleteTable(String table);//delete the active table
}

Now, you need to go into every project and create a class to implement PlatformWrapper and one to implement SQLWrapper. In each project you add the necessary code, like instances, constructors and so on.

After you have overridden all of the interfaces you made, make sure they all have an instance in the class that implements PlatformWrapper (and that there is a getter). Finally, you change the constructor in the main class. The main class is the class you reference from the launcher. It either extends ApplicationAdapter, implements ApplicationListener or extends Game. Edit the constructor and add PlatformWrapper as an argument. Inside the platform wrapper you have some utilities (if you added any) in addition to all the other wrappers (sql, achievements, or any else you added).

Now, if everything is correctly set up, you can make a call to the PlatformWrapper and get any of the cross-platform interfaces. Call any method and (assuming the executed code is correct) you will see on any platform, it does what it is supposed to do with platform specific code