Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add libgdx to existing project

Tags:

android

libgdx

I have provided an aplication made in android that has a navigation drawer and in it has a list of games. I have to create another game and to put it there. The game that has to be created by my must use libGDX but the original application didn't use this library.

Is it possible to do this ? If Yes, how can I add the libgdx to the exiting project. On github I found only how to start a new project with libGDx, an not how to add it to exising code. thanks.

like image 631
ghita Avatar asked Jun 15 '15 07:06

ghita


People also ask

Is libGDX a library?

libGDX is a cross-platform Java game development framework based on OpenGL (ES) that works on Windows, Linux, macOS, Android, your browser and iOS.


1 Answers

You can achieve this with these 2 steps:

1. Add LibGDX to your project

On your android gradle file: build.gradle (app or android module)

dependencies {
    ...

    // Add this line, it is recommended to use the latest LibGDX version
    api "com.badlogicgames.gdx:gdx-backend-android:1.9.10"
}

2. Initialize LibGDX for a view or use a LibGDX managed activity

Option 1: Initialize for a view
Since you have a navigation drawer and more code native to android this option fits your needs better. From this question (How to add LibGDX as a sub view in android):

The AndroidApplication class (which extends activity) has a method named initializeForView(ApplicationListener, AndroidApplicationConfiguration) that will return a View you can add to your layout.

-- Matsemann

Also here's documentation on how to implement this (Fragment based LibGDX).

Option 2: Use a managed activity
This is the default/most common use of LibGDX, you will need to change your code as follows:

  • Your activity needs to extend Android application:
public class MainActivity extends AndroidApplication {
  • Create a class extending Game or ApplicationAdapter:
import com.badlogic.gdx.Game;

public class LibGDXGame extends Game {

    @Override
    public void create() {
        System.out.println("Success!");
    }
}
  • Initialize the LibGDX managed activity:
import android.os.Bundle;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;

public class MainActivity extends AndroidApplication {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new LibGDXGame(), config);
    }
}
like image 94
Luis Fernando Frontanilla Avatar answered Oct 01 '22 01:10

Luis Fernando Frontanilla