Snake
Snake

Reputation: 14648

Libgdx or native for this

I am looking to make a game and I need to involve native android sdk features such as toast, dialog, in app billing, other google Api, gcm ..etc I am pretty experienced with android sdk when I built tools apps and I used animations and very briefly surface view.

However I have looked into libdgx and looks promising but the only downside I find is the "not so easy integration with Android native sdk". Ie, I can't just start my own activity or call native api unless I am missing it

So I was wondering, should I go with libgdx in this case or should I go with the native route?

Thank you

Upvotes: 1

Views: 1326

Answers (1)

Tenfour04
Tenfour04

Reputation: 93601

If you're sure you're not going to target other platforms, you can just move your code from the default core project into your Android project and work from there, calling any API as you please. But you would lose the ability to test on desktop.

To maintain portability to other platforms and ability to test on desktop, you can create an interface listing all the Android API methods you would like to call. Pass an instance of this interface into your game's constructor in your Android project, so your game can indirectly call them. Your desktop project can pass in an instance of this interface with empty or system logging methods.

Example:

public class MyGdxGame extends ApplicationAdapter {

    public interface AndroidAPIAdapter {
        public void makeToast(String msg);
        public void startActivity(int activityNumber);
    }

    AndroidAPIAdapter androidAPIAdapter;

    public MyGdxGame (AndroidAPIAdapter androidAPIAdapter){
        this.androidAPIAdapter = androidAPIAdapter;
    }

    //Call this from game code in core project as needed
    public void makeToast(String msg){
        if (androidAPIAdapter!=null)
            androidAPIAdapter.makeToast(msg);
    }

    //Call thisfrom game code in core project as needed
    public void startActivity(int activityNumber){
        if (androidAPIAdapter!=null)
            androidAPIAdapter.startActivity(activityNumber);
    }

    //...
}

with:

public class MyGameActivity extends AndroidApplication implements AndroidAPIAdapter {

    public static final int ACTIVITY_SETTINGS = 0;
    public static final int ACTIVITY_ABOUT = 1;
    //etc.

    public void onCreate (Bundle bundle) {
        super.onCreate(bundle);

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        config.useImmersiveMode = true;
        initialize(new MyGdxGame(this), config);
    }

    @Override
    public void makeToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT);
    }

    @Override
    public void startActivity(int activityNumber) {
        switch (activityNumber){
            case ACTIVITY_SETTINGS:
                startActivity(this, MySettingsActivity.class);
                break;
            case ACTIVITY_ABOUT:
                startActivity(this, MyAboutActivity.class);
                break;
        }
    }
}

Upvotes: 5

Related Questions