libGDX 101

Yair Barak
4 min readJan 27, 2017

libGDX is a library for creating games. Write it once (Java), and run on every platform you wish: Android, iOS, Windows, Linux, Web Browsers.

libGDX has easy-to-use tools, physical engine, and most important — an active developers community.

In this post I want to demonstrate how to create and configure a new libGDX project, and run it in Android, iOS, desktop and browser.

Create Project

To create a new project all you need is to download this jar. Execute it, and that what you get:

The installer will create a gradle project with all the chosen components, so they will be downloaded on build (or when opening the IDE).

Fill the fields like this:

Name: LibGDX101

Package: com.yair.libgdx101

Game Class: LibGDX101

We’ll leave the sub-projects area as is, with all the desired targets checked.

Extensions are for extra libraries we can add to our project. Uncheck Box2d — which used for physical engine — we don’t need it in this tutorial.

And… press Generate.

Configurations

Now, after we created the project, we can load it with our favorite IDE. I use Android Studio, but you can use whatever you like (well, if you don’t use AS or IntelliJ — the rest of this tutorial won’t be very helpful...).

Let’s start: File -> New -> Import Project — pick build.gradle file that in the root folder. First-time-loading can take lots of time, DON’T PANIC!

After we waited patiently, this is what we get:

As you can see, there is a module for each platform, and another extra module called ‘core’. This is the main module, where we’ll do all the hard work. The other modules are only for specific configuration for each platform.

By default the project configured only for Android — we’ll need to create the others by ourselves.

We’ll start with Desktop. The ability to run in desktop is one of the biggest advantages of libGDX. It’s very comfortable to re-run the game after each small change on desktop, instead of reload it to device.

Click on Edit configurations (it showed when you press the arrow beside ‘Run’).

On the new window click on ‘+’ -> Add new configuration and choose Application.

Fill the fields according to this:

Notice to Working directory — when we try to load assets in the game, this will be the starting point, the ‘assets’ directory. We use the same assets for all platforms, and by default it stored under Android folder.

Now we can run the Desktop configuration, and that’s what we get:

Great!

Let’s move to HTML5 config, which is a bit more complicated.

Go again to Add new configuration, but this time choose Gradle:

And this is the config:

What we do here is calling build.gradle file of the HTML module, and run there a task called superDev.

Run the new configuration. It’ll take some time, and then we’ll get in terminal the message “The code server is ready”. Let’s ignore the url showing there, instead we’ll try this one: localhost:8080/html.

This is what we’ll see:

What’s left is only iOS config. Before we continue — if you thought that it’s magically possible in non-MacOS env… sorry to disappoint.

So, for the next trick I’ll need a Mac with XCode installed, and apple developer certificate set.

Go to Preferences -> Plugins. Click on Browser repositories and look for RoboVM:

After install and restart AS, we’ll create a new configuration of RoboVM iOS:

You need to choose with what certificate you want to build, and pick target — real-device/simulator — and then hit ‘run’.

Summary

Okay. You have a well-configured libGDX project. Now you only need to write your awesome game…

(Will be continued, soon)

Originally posted here (hebrew)

--

--

Yair Barak

Geek, programmer, husband and father, in no particular order.