Karim Amin's Blog

Yeah I complain. So what!

LibGDX Tutorial #2 My first sample app »« MBTA Planner video posted on YouTube

LibGDX Tutorial #1

Hi, welcome to my first LibGDX tutorial.  There are a lot of example code on the web but there is not much documentation to help newbies such as myself.  Thus I decided to create a blog for everything I learn about LibGDX!

Before we start, let me explain a little about LibGDX.  What is LibGDX?

Well their website says the following:

“LibGDX is a cross-platform 2D and 3D game development framework written in Java/C/C++. It’s free for commercial and non-commercial use, pretty powerful and lots of fun to work with! Or at least no-one complained yet. Write your game once, deploy to Windows, Linux, Mac OSX and Android”

Basically, write a game and you’ll be able to run it a variety of platforms with just a few lines of code.  2 lines to be exact!  Wow that’s awesome!  In the following example, i’ll show you how you can create both a desktop and android application simultaneously.

OK let’s go on with the show!

I. Installing LibGDX

  1. To install LibGDX, you will want to download the latest nightly build from the site http://libgdx.l33tlabs.org/.
  2. Unzip the file into any folder of your choice.  In my example, I downloaded the files into a folder named “LibGDX”

II. Configuring your Eclipse Project

  1. Open up Eclipse and create a new Java Project.  Call it HelloLibGDX.  This is where we will create the desktop application and related files.
  2. Create a folder and label it “libs”. The folder must be called labeled “libs” as this is a requirement by LibGDX
  3. Inside this folder, you want to copy the following files from your LibGDX folder you extracted your files.
    • gdx.jar
    • gdx-backend-jogl.jar
    • gdx-backend-jogl-natives.jar
    • gdx-natives.jar
  4. Next, we need to link your project to the newly copied Jar files.  Do this by going into your project properties->Libraries->Add Jars and select all the jars you just copied over.

III. Writing the Code for a Desktop Application

  1. Still in your Java Project, create a new package under your source folder and name it com.hellolibgdx
  2. Create a new class HelloLibGDX and have it implement com.badlogic.gdx.ApplicationListener or you can just write ApplicationListener.  When the class is created, you should see something similar to

    package com.hellolibgdx;

    import com.badlogic.gdx.ApplicationListener;

    public class HelloLibGDX implements ApplicationListener {

    @Override
    public void create() {

    }

    @Override
    public void dispose() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void render() {

    }

    @Override
    public void resize(int arg0, int arg1) {

    }

    @Override
    public void resume() {

    }

    }

  3. Next, for each platform that we want to run our application on, we have to create a “Launcher” class.  This class sole responsibility is to launch the application on that platform.  So let’s create a new class named “DesktopLauncher” and create the normal public static void main function used to run java applications.
  4. Inside this function, we want to create a new instance of a JoglApplication which takes five(5) parameters.  The first parameter is an application listener  and in this case, we pass in a new instance of the  HelloLibGDX class.  The next argument is a string which represents the title of our application.  The next two arguments represent the width and height of our application in pixels.  We’ll set this to 480×320.    Finally, the last argument tells libgdx if we want to use OpenGL 2.0.  We’re going to use 1.0 for the majority of this tutorial so set this parameter to false.  You should be left with the following
    package com.hellolibgdx;
    import com.badlogic.gdx.backends.jogl.JoglApplication;/* Author: Karim Amin * This class will launch our application on our desktop environment */ public class DesktopLauncher {

    public static void main(String[] args) { new JoglApplication(new HelloLibGDX(), "Application Title", 480, 320, false);

    }

    }

  5. That’s it for the desktop portion.  If you save your work and right-click on your project and select Run->Java Application.  You should see the following:

    Sample Desktop Libgdx application

    Figure 1.1

IV. Writing Code for an Android Application

  1. Now, we’re going to create our android application.  All the work is pretty much done for us in the desktop portion of this tutorial.  So first, let’s create a new Android Project in Eclipse and name is HelloLibGDXAndroid
  2. We’ll use API 3 (Android 1.5) for the build target.  Set the application name to be “HelloLibGdx”.  Package name is com.hellolibgdx.  Set the activity name to be HelloLibGdxAndroidActivity
  3. As with the desktop project, we need to add a few jars to the project.  Again, create the libs folder as required and then copy the following files/folders into it:
    • gdx.jar
    • gdx-backend-android.jar
    • The entire folder armeabi
    • The entire folder armeabi-v7a
  4. Next, go to the project properties and hook up the jars again.  You will also need to link this project to the desktop project.  So go to the Projects tab, click Add and select the desktop project.
  5. Now we need to modify the HelloLibGdxAndroidActivity class to extend AndroidApplication instead of Activity.  The class AndroidApplication extends Activity already so specifying that is not necessary.
  6. Get rid of the setContentView statement and replace it with the line
    a call to initialize which takes two (2) parameters.  The first parameter is the main class which we will run.  You should instantiate a new instance of HelloLibGDX.  The second parameter again tells libgdx if we should use OpenGL 2.0 or not.  You will set this to false.  When done, you should have the following:

    package com.hellolibgdx;
    /* Author: Karim Amin
    * This is a sample android libgdx application
    */
    import android.os.Bundle;

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

    public class HelloLibGdxAndroidActivity extends AndroidApplication {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initialize(new HelloLibGDX(), false);
    }
    }

  7. Now you can run this by right clicking on your android project and going to Run As->Android Application, you should see the following:

    Sample Android libgdx application

  8. That’s it.  You have now created your first LibGdx application that runs on the desktop and on the android platforms.  Take a bow!

You can download a copy of the source files here in zip format.


October 8, 2011 at 3:07 pm