Setting up a basic LibGDX project

This topic came up on one of my favourite game development communities, r/gamedev and I thought I should make a quick tutorial on setting up Libgdx for someone new in game development, so you don't have to worry.

You can use the gdx-setup-ui.jar, but I'd recommend against it. It segregates projects, creating a core, an android one, and more if you check it off. This is confusing and unnecessary for someone new to the library. To just get a desktop project in eclipse, here's what you do:

  1. Create a new Java Project under File > New > Java Project
  2. Give it a name, and click finish.
  3. Right click on the project folder on the left hand screen.
  4. In the right-click dialogue, go into New > Folder. Give it the name "assets"
  5. Do this again, and create a "libs". You can call them whatever you wish, it's just what i like to use. Assets is for images and such, libs will store the LibGDX jars.

Here's how your project should look:

project1.jpg

Download libgdx from here. Be Sure to grab the latest stable build. Unzip the folder. There's a lot of stuff in here, what you'll generally need are the following files:

  • gdx-backend-lwjgl-natives.jar
  • gdx-backend-lwjgl.jar
  • gdx-natives.jar
  • gdx-openal.jar
  • gdx.jar

You can grab other ones as you need them, for example if you want to use Tiled

  • extensions/gdx-tiled-preprocessor.jar
  • extensions/gdx-tools.jar

Copy the .jar files, and put them into the libs directory in your java project folder.

Now add the jars in eclipse:

  1. Right click on the project name
  2. Click refresh so the folders get updated with the Jar files.
  3. Right click again & select build path
  4. In the sub menu, select Configure Build Path
  5. In the now open window, click Add Jars on the right.
  6. Expand the project folder, and then expand libs
  7. Shift click all the jar files in the folder.
  8. Click OK
project2.jpg

Now that you have all the necessary binaries, time to create a simple window to start.

Add a new class to the src package. Be sure to name it the same as the project, as it's where we'll add the main method.

In the class, add the following import statements:

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.Game;

LWJGL will be used to create the window. The import of the Game interface is for later. Setup your main method inside the class:

public static void main(String args[]) {
    LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
    cfg.width = 800;
    cfg.height = 640;
    cfg.useGL20 = false;
    LwjglApplication app = new LwjglApplication(new MyLibgdxGame(), cfg);
}

The setup here is pretty straight forward. We create a new configuration object to store the resolution of the window. We are not using OpenGL2.0, so set that to false. The last line is then creating a new application window, by passing this a new object of this class as the game. Eclipse will now complain there's a compiler error. To fix, extend your class with Game:

public class MyLibGdxGame extends Game {

Then add the interface method so the interface can be satisfied:

@Override
public void create() {

}

At this point you can build the game and a window will pop up. But it will flicker and such, which is not what we want. So what you can do now is add a new class.

  1. Call it GameScreen
  2. Add an interface to it in the creation wizard.
  3. The interface is called Screen and is part of com.badlogic.gdx package.
  4. Click finish

This class creates a lot of nice functionality for us to manage a game screen. You can use the screen interface for different states of the game. Such as menus, intro, game over, etc. It gives you initialization, a render method, pause, and other handy methods.

To solve our above problem, add the following line to the render method:

Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

This simply clears the screen with a black colour. It ensures that we won't get that flickering nonsense. Back in your main game class, add the screen to the game via:

 GameScreen gameScreen = new GameScreen();
 setScreen(gameScreen);

Put those in the create method, and try running the application. That's your basis on getting an application setup for libgdx. Definitely check out their documentation on how to use the framework. Just remember to start simple. Even just do some movement with shapes and such!

A note about assets. A lot of the tutorials refer to using the setup ui. Because of that, the assets load a bit differently. If you have a file in the assets folder called player.png, you can resolve that path simply using: Gdx.files.internal("assets/player.png")