Book Image

LibGDX Game Development By Example

By : James Cook
Book Image

LibGDX Game Development By Example

By: James Cook

Overview of this book

Table of Contents (18 chapters)
LibGDX Game Development By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a project


Follow the given steps to create your own project:

  1. As mentioned earlier, LibGDX comes with a really useful project setup tool. Download the application from here:

    http://libgdx.badlogicgames.com/download.html

    At the time of writing, it is the big red "Download Setup App" button in the middle of your screen.

  2. Once downloaded, open the command line and navigate to the location of the application. You will notice that it is a JAR file type. This means we need to use Java to run it.

  3. Running this will open the setup UI:

Before we hit the Generate button, let's just take a look at what we are creating here:

  • Name: This is the name of our game.

  • Package: This is the Java package our game code will be developed in.

  • Game class: This parameter sets the name of our game class, where the magic happens!

  • Destination: This is the project's directory. You can change this to any location of your choice.

  • Android SDK: This is the location of the SDK. If this isn't set correctly, we can change it here. Going forward, it might be worth setting the ANDROID_HOME environment variable.

Next is the version of LibGDX we want to use. At time of writing, the version is 1.5.4.

Now, let's move on to the subprojects. As we are only interested in desktops at the moment, let's deselect the others.

Finally, we come to extensions. Feel free to uncheck any that are checked. We won't be needing any of them at this point in time. For more information on available extensions, check out the LibGDX wiki (https://github.com/libgdx/libgdx/wiki).

Once all is set, let's hit the Generate button!

There is a little window at the bottom of the UI that will now spring to life. Here, it will show you the setup progress as it downloads the necessary setup files.

Once complete, open that command line, navigate to the directory, and run your preferred tree command (in Windows, it is just "tree").

Hopefully, you will have the same directory layout as the previous image shows.

The astute among you will now ask, "What is this Gradle?" and quite rightly so. I haven't mentioned it yet, although it appears twice in our projects directory.

What is Gradle?

Well, Gradle is a very excellent build tool and LibGDX leverages its abilities to look after the dependencies, build process, and IDE integration. This is especially useful if you are going to be working in a team with a shared code base. Even if you are not, the dependency management aspect is worth it alone.

Anyone who isn't familiar with dependency management may well be used to downloading Java JARs manually and placing them in a libs folder, but they might run into problems later when the JAR they just downloaded needs another JAR, and so on. The dependency management will take care of this for you and even better is that the LibGDX setup application takes care of this for you by already describing the dependencies that you need to run!

Within LibGDX, there is something called the Gradle Wrapper. This is essentially the Gradle application embedded into the project. This allows portability of our project, as now if we want someone else to run it, they can.

I guess this leads us to the question, how do we use Gradle to run our project? In the LibGDX wiki (https://github.com/libgdx/libgdx/wiki/Gradle-on-the-Commandline), you will find a comprehensive list of commands that can be used while developing your game.

However, for now, we will only cover the desktop project.

What you may not have noticed is that the setup application actually generates a very simple "Hello World" game for us. So, we have something we can run from the command line right away!

Let's go for it!

On our command line, let's run the following:

  • On Windows: gradlew desktop:run

  • On Linux and Mac OS X: ./gradlew desktop:run

    The following screen will appear once you execute the preceding command:

You will get an output similar to the preceding screenshot. Don't worry if it suddenly wants to start downloading the dependencies. This is our dependency management in action! All those JARs and native binaries are being downloaded and put on to classpaths. But, we don't care. We are here to create games!

So, after the command prompt has finished downloading the files, it should then launch the "Hello World" game.

Awesome! You have just launched your very first LibGDX game!

Although, before we get too excited, you will notice that not much actually happens here. It is just a red screen with the Bad Logic Games logo.

I think now is the time to look at the code!