Home Game Development Libgdx Cross-platform Game Development Cookbook

Libgdx Cross-platform Game Development Cookbook

books-svg-icon Book
eBook $32.99 $22.99
Print $54.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $32.99 $22.99
Print $54.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    Diving into Libgdx
About this book
Publication date:
October 2014
Publisher
Packt
Pages
516
ISBN
9781783287291

 

Chapter 1. Diving into Libgdx

In this chapter, we will cover the following recipes:

  • Setting up a cross-platform development environment

  • Creating a cross-platform project

  • Understanding the project structure and application life cycle

  • Updating and managing project dependencies

  • Using source control on a Libgdx project with Git

  • Importing and running the Libgdx official demos

 

Introduction


Before thinking about how to render an animated character onscreen, it is very important that you prepare all the required tools to create cross-platform applications with Libgdx and understand its basic principles. This is, precisely, the purpose of this initial chapter.

First, we will cover how to install everything that is required for the three major operating systems, Windows, Mac, and GNU/Linux. Though we all know you want to go straight to the fun bit, a stable and productive working environment is vital in order to avoid future headaches. After we make sure that all is in order by testing a sample project, it will be time to take a closer look at how all Libgdx projects are structured.

Often, a developer wants to use a newer version of Libgdx or some third-party library because it includes an incredible feature or solves a problem they were losing sleep over. For these reasons, it will prove very useful to know how to properly update a project so as to enjoy some fresh goodies.

Finally, as you are probably very much aware, using source control for every single one of your endeavors is surely a life saver. Not only does it give us a backup system straightaway, but it also empowers us to share and keep track of the changes in the repository. This is extremely useful when you want to blame someone else for something that went wrong! In this chapter, we will show how to efficiently use source control with a Libgdx project using Git as an example.

 

Setting up a cross-platform development environment


Once you go through this recipe, you will be able to enjoy Libgdx in all its glory and start developing games for all the supported platforms.

Let's begin with a short disclaimer. For the most part, Libgdx relies on open source software that is widely available at no cost. This means that anyone can target desktops, Android, and browsers using a Windows, Mac, or GNU/Linux distribution. The only restriction applies to iOS, for which you will specifically need a Mac. Moreover, if you wish to test your work on a real device, an Apple developer account is essential and further costs apply.

Getting ready

You need to be aware of the operating system version you will use to pick the right versions of the software packages we will install. The main explanation thread will focus on Windows 8 64-bit, but further comments will be provided whenever there are differences across systems.

Note

Keep in mind that software versions might change after the release of this book, so think of this recipe as more of a guideline than a sacred text. The names of the downloaded packages will typically include the version number, and they will change over time.

How to do it…

Here is our little software shopping list:

  • Java Development Kit

  • Eclipse IDE

  • The Gradle plugin for Eclipse

  • Android SDK, only for those who want to target Android devices

  • The RoboVM plugin for Eclipse, only if you want to target iOS

  • XCode, only for Mac users that want to target iOS

Java Development Kit

Libgdx is based on Java, and therefore, Java Development Kit is a requirement. The installation step is as follows:

Go to Oracle's download site, http://www.oracle.com/technetwork/java/javase/downloads, and click on the latest release of Java SE Development Kit that corresponds to your operating system. Note that you need to differentiate between the x86 and x64 builds.

Note

Be careful; Java 7 is the minimum JDK required, Java 6 will just not work.

Windows and Mac users

Perform the following installation steps:

  1. Run the installer and follow the provided instructions. The process is quite straightforward, but when using Windows, you will have to remember the destination folder you picked; the default folder is C:\Program Files\Java\jdk_version.

  2. You need to tell the system where the JDK is located.

  3. If you are under Windows, right-click on My Computer, click on System Properties, access the Advanced section, and click on Environment Variables. Select New, and enter JAVA_HOME as the name and your installation path as a value. In my case, the value is C:\Program Files\Java\jdk1.7.0_45.

  4. Mac users will have to edit their ~/.bash_profile file and add the following:

    export JAVA_HOME=`/usr/libexec/java_home –v 1.7`
GNU/Linux users

Perform the following installation steps:

  1. Move the downloaded package to the desired installation folder and decompress it. You can do this from a desktop environment or the much more classic console. We will assume the file is jdk-7u45-linux-x64.gz; it's in the ~/Downloads directory, and the installation folder is ~/dev/jdk1.7.0_45:

    mkdir –p ~/dev/jdk
    cd ~/Downloads
    tar –xzvf jdk-17u45-linux-x64.gz
    mv jdk1.7.0_45 ~/dev
    rm jdk-7u45-linux-x64.gz
    

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. This book is full of rich working examples you can experiment with. Access the following link to download the most up-to-date version: https://github.com/siondream/libgdx-cookbook.

  2. In GNU/Linux, the system also needs to know where the JDK is. In order to do so, open the ~/.bashrc file with your text editor of choice and add the following at the bottom:

    export JAVA_HOME=$HOME/dev/jdk1.7.0_45
    export PATH=$PATH:$JAVA_HOME/bin
    
  3. Close the file, and run the following command to reload the user configuration:

    source ~/.bashrc
    

    Note

    Alternatively, you can install OpenJDK, the open source implementation of the Java platform.

Eclipse

Eclipse is the most popular IDE for Libgdx game development, and it is thus the one we will focus in this book. If it is not of your liking, you can use IntelliJ IDEA, Netbeans, or any editor along the command line. Perform the following installation steps:

  1. Go to the Eclipse downloads section at http://www.eclipse.org/downloads and select Eclipse Standard. The Eclipse 4 codename, Juno, is the minimum version needed to use the required plugins.

  2. Simply pick the right version for your operating system and wait for it to download; be wary that it is also 32/64-bit sensitive.

  3. Once this is complete, extract the compressed file where you want to use Eclipse from and you will be done.

  4. From a GNU/Linux system, you can do the following:

    cd ~/Downloads
    tar –xzvf eclipse-standard-kepler-SR1-linux-gtk-x86_64.tar.gz
    mv eclipse ~/dev
    rm eclipse-standard-kepler-SR1-linux-gtk-x86_64.tar.gz
    

Android SDK

Follow these instructions to install Android Development Kit, which is essential to target Android devices:

  1. Access the download page at http://developer.android.com/sdk.

  2. Scroll down and unfold the View all downloads and sizes section and, again, choose your operating system from the SDK Tools Only section. Google has an easy-to-use installer for Windows users, so if you want to be spared part of the hassle, use the installer.

  3. The installer is really simple. Limit yourself to follow the instructions, and if JDK is properly added to the environment variables, everything should be completely smooth. The installation folder does not really matter.

Mac users

Unzip the package wherever you want, as long as you tell the system where it is. Again, this is done by editing the ~/.bash_profile file and adding something similar to this:

export PATH=$PATH:/dev/android-sdk-mac_x86_64/tools
export PATH=$PATH:/dev/android-sdk-mac_x86_64/platform-tools
GNU/Linux users

Perform the following installation steps:

  1. Unzip the package, move it to the desired installation folder, and add the export location to the PATH environment variable. The commands needed will be something similar to this:

    cd ~/Downloads
    tar –xzvf android-sdk.r22.2.1-linux.tgz
    mv android-sdk-linux ~/dev
    rm xzvf android-sdk.r22.2.1-linux.tgz
    
  2. Just like with JDK, edit the ~/.bashrc file and add the following lines at the end:

    export PATH=$PATH:~/dev/android-sdk-linux/tools
    export PATH=$PATH:~/dev/android-sdk-linux/platform-tools
  3. Again, close the file and reload the ~/.bashrc file:

    source ~/.bashrc
    
  4. After this, go to to the Android SDK folder and run SDK Manager, which will help us install specific packages. On GNU/Linux, you first need to give execution permissions to the user on the SDK folder:

    cd ~/dev/android-sdk-linux
    chmod –R 744 *
    
All users

Perform the following steps:

  1. Create an ANDROID_HOME environment variable pointing to the root of Android SDK. This is done the same way as we did with the JAVA_HOME variable in the previous section.

  2. Run SDK Manager found in the tools folder. GNU/Linux users need to run an Android executable.

  3. Several Android SDK tools will appear selected by default; leave them selected. The Google USB driver is not compatible with GNU/Linux, but you should select it if you can.

  4. The SDK tool corresponding to the latest Android version available will be ticked as well. Feel free to choose whichever SDK you prefer, but keep in mind that Libgdx requires Android 2.2 or later.

    Note

    If you use Android-specific code somewhere in your project, it is advisable to keep SDK for the oldest Android version you want to target. This way, you can ensure compatibility at all times.

  5. Regardless of the Android version you pick, it is always advisable to consider backwards compatibility so as to reach as wide an audience as possible. As a developer, you will want to be thorough when it comes to testing on multiple devices.

  6. Having said this, select Install packages and accept the licenses.

Eclipse plugins

Getting tired? Worry no more, we are getting close to the finish line! We are about to install several plugins that will allow us to manage our build process and target iOS devices:

  • Gradle (mandatory)

  • The Google plugin for Eclipse (mandatory)

  • Developer tools for Android (only to target Android)

  • Google Web Toolkit SDK (only to target browsers)

  • RoboVM for iOS (only to target iOS)

A course on how to use an IDE is out of the scope of this book. However, pertinent explanations will be provided when having to deal with Eclipse-specific issues. The installation steps are as follows:

  1. Run Eclipse and create a new workspace to import your recipe's code into. Just so we are all on the same page, let's name it libgdx-cookbook.

    Once you see the welcome panel, close it and select Help | Install New Software. The Android Developer Tools and the Google Web Toolkit plugins can be found at http://dl.google.com/eclipse/plugin/4.3.

    Note

    Be aware that this will only work with Eclipse 4.3 Kepler. If you use a different Eclipse release, use the matching version number. Google has this URL available on its developers help guide for Eclipse at https://developers.google.com/eclipse/docs/getting_started.

  2. Select Developer Tools, Google Plugin for Eclipse, and Google Web Toolkit SDK, and proceed with the installation. A modal dialog will warn you about installing unsigned content because there is always an inherent risk when installing plugins. Rest assured Google's tools are safe. Upon completion, Eclipse will need to restart, and you will be prompted to enter the Android SDK location.

  3. Now, follow the same process for the RoboVM plugin; you will find it at http://download.robovm.org/eclipse.

  4. There is only one option within the repository, so select it, carry on with the installation, and restart Eclipse once again.

Gradle is an open source build automation system. It will gracefully handle all the dependencies of our projects, doing most of the cumbersome heavy lifting. Perform the following steps:

  1. Once again, go to the Install new software option in Eclipse and introduce the URL http://dist.springsource.com/release/TOOLS/gradle.

  2. Install the Gradle IDE entry and restart Eclipse for the last time.

Great! One more task can be crossed out from our shopping list.

XCode

XCode is the Apple IDE required to develop for their platforms. Mac users who want to target iOS can get hold of XCode free of charge from Apple Store.

Fixing character encoding and line endings

Eclipse has the impolite practice of not using UTF-8 encoding and Unix line endings if you are under a Microsoft operating system. While this will not affect you initially, it will prove to be a huge pain when it comes to using other peers' code as many conflicts will appear to ruin the party. Perform the following steps:

  1. Character encoding is applied on a per-workspace basis, and to fix it, you need to access the libgdx-cookbook workspace you just created.

  2. Click on Window, select Preferences | General, then select Workspace, and make sure UTF-8 encoding and Unix line endings are your choices.

    Note

    Remember to always be aware of how you encode your project files because encoding derived issues is the worst and you do not want to deal with them!

Making sure everything is in order

The time of truth has come as we are about to import a Libgdx project to our workspace for the first time and see whether we have made any mistakes along the way. This is the most basic Libgdx project you can possibly make if you want to target all platforms. Use the source code provided with this book. Perform the following steps:

  1. Once you have Eclipse open in front of you, right-click on Package Explorer, select Import, and choose Gradle project inside the Gradle node.

  2. Select the [cookbook]/environment folder, and click on Build Model.

  3. Make sure you select all listed projects, and click on Finish to start importing the projects. Gradle will now download all the dependencies, which may take a while.

  4. As long as everything goes according to plan, the only error you might see on the Problems pane will be in the Android project. This is because it is set to use an Android SDK different from the one you installed. In such a case, right-click on environment-test-android, go to Properties, and tick your installed Android SDK under the Android tab. You can also install the missing SDK if you prefer to do so.

  5. All the assets are located inside the environment-test-android project and will be shared across all platforms.

  6. We now need to tell the desktop project where the assets are located. Right-click on the desktop project (environment-test-desktop), select Properties | Run/Debug Settings, select DesktopLauncher, and click on Edit.

  7. Open the Arguments tab and select Other as the working directory.

  8. Now enter this in the input box:

    ${workspace_loc:environment-test-android/assets}

As long as you followed this recipe correctly, there should be no errors hanging around; so, it is time to run the project on every platform as a final test.

First, let's try the desktop project, which is the easiest of all.

Right-click on it, select Run As followed by Java application, and then choose the entry point (DesktopLauncher). You will see the following window:

Android is next in the queue.

Note

I strongly advise you against testing a Libgdx game on the emulator because of its extremely poor performance. A device is so much better, and you will run your project through the desktop version most of the time anyway. This rapid iteration cycle is the main point in Libgdx's philosophy.

To pair your Android device, be it a phone or tablet, you need to enable USB debugging on your Android device, which can be a little obscure in later versions. Perform the following steps:

  1. On the device, go to Settings, enter About Phone, and tap the Build Number block seven times to enable developer options. Yes, do not ask why.

  2. Once you get a message saying you just became a developer, you can go to Settings | Developer options and enable USB debugging.

  3. Now, you can run the environment test on your device by right-clicking on the Android project, entering the Run As menu, and selecting Android Application. Finally, choose your device from the list.

    Note

    Note that the device drivers have to be installed in your development machine. We cannot possibly cover all drivers due to their huge variety.

Let's try the HTML project now. Perform the following steps:

  1. Right-click on the environment-test-gwt project and select Run As Gradle build….

  2. On the next window, type Ctrl + Space, and scroll down and double-click on the gwtSuperDev task. Click on Apply followed by Run.

  3. The first time you do this, it will take quite a while. Under the hood, the build process will launch a Jetty web server on your computer.

  4. After a while, you will be able to access it through the following URL:

    http://localhost:8080/gwt
    
  5. A background code server will accept build requests from the browser, so no manual full compilation is needed once you kick it off. You will notice a particular message in the compiler output:

    The code server is ready.
    Next, visit http://localhost:9876/
    
  6. Access the URL and drag the Dev Mode On bookmarklet onto the browser's bar. You will never need to do this again.

  7. Back on your running environment test tab, click on the newly added bookmarklet, and select compile. Any change to the game code will be recompiled and reinjected in the web server.

  8. After a short time, the page will refresh and you can run the most recent version of your code.

Additionally, Mac users can run the iOS project by right-clicking on environment-test-ios and going to the Run As menu. Inside, you will find three options of interest:

  • iOS Device App: This requires you to have an actual connected device and an Apple developer subscription

  • iOS Simulator App (iPad)

  • iOS Simulator App (iPhone)

Pretty much like the HTML5 project, the first build will take a long time; it should be fine after this.

Congratulations! Now, you can run your Libgdx projects on all targetable platforms.

How it works…

The Libgdx development environment installation process is pretty much self-explanatory. However, it is worth mentioning a few details of how it is designed to facilitate cross-platform development and what technologies it relies on. You will at least know why you just installed so many things!

Libgdx has a multiplatform API that allows users to write platform-agnostic Java code once and deploy it on all the supported platforms, while achieving the same behavior. Every platform has a backend that implements low-level subsystems: Application, Graphics, Audio, Input, Files, and Network.

This way, we can happily request to draw a sprite onscreen, play some background music, or read a text file through the common graphics, audio, and file interfaces, respectively, and it will run everywhere. Magic!

Deployment on platforms such as Android, iOS, or HTML5 might not be the fastest process ever, but this is usually mitigated by the ability of the desktop backend to serve as a debugging platform. Remember that this will become increasingly important as you and Libgdx become friends.

The desktop backend mostly relies on LWJGL (Light Weight Java Game Library). At the same time, LWJGL is built on top of the magnanimous OpenGL (Open Graphics Library). A fun fact is that Minecraft was created using LWJGL.

For Android development, Libgdx finds its resources on the official Android SDK as well as the embedded system-specific version of OpenGL, which is called OpenGL ES.

This gets a lot trickier when it comes to HTML5 support because the technologies are quite different. HTML5 can display incredibly visually rich applications through WebGL and JavaScript, but unfortunately, this has little to do with the Libgdx toolchain. Compatibility with browsers is achieved through Google Web Toolkit (GWT), which compiles Java code into optimized JavaScript code, thanks to, what I like to call, black magic.

Last but not least, we have iOS support that relies on RoboVM. This magnificent piece of open source software eats Java code for breakfast and spits out native ARM or x86 code. It also provides full access to Cocoa Touch API, allowing us to deploy on iOS devices, as long as we have a Mac.

There are quite a few more technologies involved to make this happen, but this serves as a broad overview of what goes on under the hood.

There's more…

You can use Android SDK Manager to gain access to more Android APIs such as Google Play Game services, advertisement platforms, or Intel Hardware Accelerated Execution Manager (HAXM). HAXM is an alternative Android emulator, much faster than the default one. Feel free to explore!

See also

  • In the next recipe, you will learn how to create brand new Libgdx-based cross-platform projects. Let the fun begin!

  • For instructions on how to deploy your Libgdx applications, go to the first four recipes in Chapter 13, Giving Back.

 

Creating a cross-platform project


In this recipe, we will lay out a series of very simple steps for you to set up Libgdx cross-platform projects really quickly. With very little hassle, you will have a functional barebones application ready to take your brilliant game logic in.

Getting ready

Make sure you have all Libgdx dependencies installed in your development machine. If you didn't follow all the steps in the Setting up a cross-platform development environment recipe, proceed to do so before carrying on.

How to do it…

Libgdx makes use of Gradle to handle the build process. Gradle is an open source build automation tool, very similar to Apache Ant and Apache Maven. It handles your project's dependencies and downloads external libraries when necessary; you only have to declare that you want to include them in your project.

Luckily enough, you do not need to learn a lot about Gradle to start working on Libgdx projects because our framework bundles a tool that creates a skeleton application with all the basics for you to use.

The gdx-setup tool offers a very straightforward user interface as well as a command-line option. Feel free to use whichever you are most comfortable with; we will explain both here. Perform the following steps:

  1. First, download the latest version of the tool from http://libgdx.badlogicgames.com/nightlies/dist/gdx-setup.jar.

  2. Running the .jar file with no additional arguments opens up the user interface straightaway. Filling the form in does not entail any mystery at all.

  3. Simply enter the project folder name, Java package name, name of the game logic entry point class, folder where the projects will be created, and location of your Android SDK. Once you are ready, click on the Generate button, as shown in the following screenshot:

    For those who fancy the command-line version of the tool, here is its usage:

    java --jar gdx-setup.jar --dir <dir-name> --name <app-name> --package <package_name> --mainClass <main_class> --sdkLocation <sdk_location>
    
    • --dir: This is the destination folder of the projects

    • --name: This is the name of the application, which will determine the project folders' names

    • --package: This is the name of the Java package where the code will live

    • --mainClass: This is the class name for the game code entry point

    • --sdkLocation: This is the path to your Android SDK

    For example, to call the tool from the command line with the settings shown in the previous screenshot, you will have to enter:

    java –jar gdx-setup.jar –dir E:\projects\tools\test –name my-gdx-game –package com.mygdx.game –mainClass MyGdxGame –sdkLocation C:\android
    

    Done! Just like we did with environment-test in the previous recipe, now it is time to import the project in Eclipse.

  4. Right-click on Package Explorer, select Import, and choose the Gradle project inside the Gradle tab.

    Select your destination folder and click on Build Model.

    Note

    Remember that you need to set the working directory for the desktop project so that it can find the assets that are located within the Android project.

  5. Right-click on the desktop project. Go to Properties | Run/Debug Settings, select DesktopLauncher, and click on Edit.

  6. Open the Arguments tab and select Other as the working directory.

  7. Now, enter this in the input box:

    ${workspace_loc:my-gdx-game-android/assets}
  8. You need to override the memory allowance for Gradle and specify the location of Android SDK so that Gradle can pick it up. Add the following lines to the gradle.properties file located under the gradle directory in your user folder:

    org.gradle.jvmargs=-Xms128m -Xmx512msdk.dir=C:/android

    Note

    The Libgdx Gradle build system is also compatible with Intelij IDEA, Netbeans, and the command line. Feel free to explore and look for additional information on these lines.

Your newly created Libgdx project should be fully functional. Gradle will take care of the dependencies, download the necessary libraries, and handle the compilation process. Like we mentioned before, the first build can take quite a while, but it should be significantly smoother from then on.

Happy coding!

How it works…

At this point, you will notice how the Libgdx projects are structured. They are actually made of several projects, one per platform and another core project. The core project contains the actual logic of your game, while the platform-specific projects typically only have a launcher that calls the core entry point.

The resulting directory tree inside the test folder will look as follows:

|- settings.gradle      - project submodules
|- build.gradle         - main Gradle build config file
|- gradlew         - Build script for GNU/Linux
|- gradlew.bat         - Build script for Windows
|- local.properties      - Intellij IDEA only file
|
|- gradle/         - local Gradle
|
|- core
|   |- build.gradle      - Gradle build for core project, do not modify
|   |- src/         - Game code
|   
|- desktop
|   |- build.gradle      - Gradle build for desktop project
|   |- src/         - Desktop specific code
|
|- android
|   |- build.gradle      - Gradle build for Android project
|   |- AndroidManifest.xml   - Android config
|   |- src/         - Android specific code
|   |- res/         - Android icons and other resources
|   |- assets/         - Shared assets
|
|- gwt
|   |- build.gradle      - Gradle build for GWT project
|   |- src/         - GWT specific code
|   |- webapp/         - War template
|
|- ios
|   |- build.gradle      - Gradle build for iOS project
|   |- src/         - iOS specific code

Gradle, our build system, is particularly good with multiproject solutions. It uses domain-specific language (DSL) rather than XML, like Ant and Maven do, to define targets as well as their dependencies. When we tell Gradle to build a project for us, it uses the build.gradle files to create a directed acyclic graph representing the dependencies. Then, it builds the dependencies in the right order.

The dependency graph for the Libgdx project skeleton will look as follows:

There's more…

Gradle is extremely configurable so as to accommodate the needs of a diverse set of developers and their environments. This is done through several gradle.properties files located at various specific places:

  • The project build directory where the main build.gradle file is

  • The user home, which will be C:\Users\User\.gradle\gradle.properties in Windows and ~/.gradle/gradle.properties in GNU/Linux

  • The system properties

These settings are applied in descending order, which means that the lower settings can overwrite the higher settings.

Gradle downloads dependencies from repositories on demand. When your machine is behind a proxy, you need to specify this through one of the gradle.properties files by adding the following settings:

systemProp.http.proxyHost=www.somehost.org
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password

For secure addresses, you only need to replace http with https in the previous properties.

As you surely understand, this is not a book on Gradle. If you wish to know more about it and how to tailor it for your needs, refer to the official user guide at http://www.gradle.org/docs/current/userguide/userguide_single.html.

See also

  • For further details on the Gradle project and dependency management, read the Updating and managing project dependencies recipe of this chapter.

 

Understanding the project structure and application life cycle


Throughout this recipe, we will examine the typical project architecture of Libgdx and how it makes cross-platform development a much less cumbersome quest. We will also learn how to configure platform-specific launchers in order to tweak parameters such as resolution, colors, the OpenGL version, and so on. More importantly, we will go through the Libgdx application life cycle. This is the heart of any game you will ever make using our beloved framework, and therefore, one can imagine it is something worth getting acquainted with.

Getting ready

With the goal of illustrating the contents of this recipe, we will use the same environment test application we used in the Setting up a cross-platform development environment recipe to test that our Libgdx installation is working as expected. Fire up Eclipse and make sure you select your libgdx-cookbook workspace. Now, check you have the projects that compose the test application already available. If not, import the projects under [cookbook]/environment through Gradle, as shown in the previous recipe.

How to do it…

As we already mentioned before, Libgdx applications are typically split in several projects: core, desktop, Android, iOS, and HTML. The platform-specific projects serve as the application's entry points on each platform; their duty basically boils down to invoking the core project's main class and passing in the basic configuration parameters for the game to run.

Note

Imagine you were to target Android exclusively, you could probably get away with one single project containing both the platform-agnostic and Android-specific code. However, this is a bad practice and should be avoided. What happens if you decide to port your game to a different platform later on? No one would like to refactor the project structure to accommodate it to the new circumstances. Regardless of the platform and devices you work with, it is always preferable to keep the two categories as isolated as possible.

Using logging to get to know the application life cycle

Every Libgdx application has a very well-defined lifecycle controlling the states it can be in at a given time. These states are: creation, pausing, resuming, rendering, and disposing. The lifecycle is modeled by the ApplicationListener interface, which we are required to implement as it will serve as the entrance to our game logic. In our recipe's example, the EnvironmentTest class in the core project carries out such roles.

Meet the ApplicationListener interface:

public interface ApplicationListener {
   public void create ();
   public void resize (int width, int height);
   public void render ();
   public void pause ();
   public void resume ();
   public void dispose ();
}

Your ApplicationListener interface implementation can handle each one of these events in the way it deems convenient. Here are the typical usages:

  • create(): This is used to initialize subsystems and load resources.

  • resize(): This is used to handle setting a new screen size, which can be used to reposition UI elements or reconfigure camera objects.

  • render(): This is used to update and render the game elements. Note that there is no update() method as render() is supposed to carry out both tasks.

  • pause(): This is the save game state when it loses focus, which does not involve the actual gameplay being paused unless the developer wants it to.

  • resume(): This is used to handle the game coming back from being paused and restores the game state.

  • dispose(): This is used to free resources and clean up.

When do each of these methods get called? Well, that's a really good question! Before we start looking at cryptic diagrams, it is much better to investigate and find out for ourselves. Shall we? We will simply add some logging to know exactly how the flow works.

Take a look at the EnvironmentTest.java file:

public class EnvironmentTest implements ApplicationListener {
   private Logger logger;
   private boolean renderInterrupted = true;
   
   @Override
   public void create() {
      logger = new Logger("Application lifecycle", Logger.INFO);
      logger.info("create");
   }

   @Override
   public void dispose() {
      logger.info("dispose");
   }

   @Override
   public void render() {
      if (renderInterrupted) {
         logger.info("render");
         renderInterrupted = false;
      }
   }

   @Override
   public void resize(int width, int height) {
      logger.info("resize");
      renderInterrupted = true;
   }

   @Override
   public void pause() {
      logger.info("pause");
      renderInterrupted = true;
   }

   @Override
   public void resume() {
      logger.info("resume");
      renderInterrupted = true;
   }
}

The renderInterrupted member variable avoids printing render for every game loop iteration.

Note

Whenever Eclipse complains about missing imports, hit Ctrl + Shift + O to automatically add the necessary modules.

The Logger class helps us show useful debug information and errors on the console. Not only does it work on desktops but also on external devices, as long as they are connected to Eclipse. Remember this little new friend as it will be truly useful for as long as you work with Libgdx. The constructor receives a string that will be useful to identify the messages in the log as well as on a logging level.

In order of increasing severity, these are the available logging levels: Logger.INFO, Logger.DEBUG, Logger.ERROR, and Logger.NONE. Several methods can be used to log messages:

  • info(String message)

  • info(String message, Exception exception)

  • debug(String message)

  • debug(String message, Exception exception)

  • error(String message)

  • error(String message, Throwable exception)

Logging levels can be retrieved and set with the getLevel() and setLevel() methods, respectively. Both the level and the method used to log a message will determine whether they will actually be printed on the console. For example, if the level is set to Logger.INFO, only messages sent through info() and error() will appear, and those sent through debug() will be ignored.

Now, run the application on all the platforms and pay attention to the console. Depending on how you play with the focus, the output will vary, but it should be similar to this:

Application lifecycle: create
Application lifecycle: resize
Application lifecycle: render
Application lifecycle: pause
Application lifecycle: render
Application lifecycle: resume
Application lifecycle: render
Application lifecycle: dispose

This should give you a pretty decent handle of how the application lifecycle works.

Placing breakpoints on each ApplicationListener overridden method is also a good way of discovering what is going on. Instruction breakpoints allow you to debug an application and stop the execution flow that reaches the said instruction. At this point, you can run the code instruction by instruction and examine the current state of the active variables. To set a breakpoint, double-click next to the corresponding line; a blue dot will confirm that the breakpoint is set. Once you are done, you can debug the application by right-clicking on the desired project and selecting the Debug As menu.

The Eclipse Debug view will then enter the stage with all its shiny panels. The Debug tab shows the current execution callstack, the Variables tab contains the current state of the variables within scope, and in the following screenshot, you can see the code with the current line highlighted. The arrow buttons in the upper toolbar can be used to step over the next instruction (F6) or move on to the next method (F5), where applicable, or out of the current method (F7), as shown in the following screenshot:

Starter classes and configuration

Every platform project consists of a starter class (or entry point). This class is responsible for constructing the platform-specific application backend. Each backend implements the Application interface. The starter class also passes a new instance of our ApplicationListener implementation to the application backend. This implementation typically lives in the core project and serves as an entry point to our cross-platform game code. Finally, it also submits a configuration object, and by doing so, it provides a mechanism to customize general parameters, as we will see later.

Desktop starter

The entry point of the desktop project is the static main method of the DesktopLauncher starter class:

public class DesktopLauncher {
   public static void main (String[] arg) {
      LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
      new LwjglApplication(new EnvironmentTest(), config);
   }
}

As you can see, this creates LwjglApplicationConfiguration. Then, it instantiates a LwjglApplication object passing in a new EnvironmentTest instance along the recently created config object. Some of the most useful attributes of the configuration class are listed as follows:

  • r, g, b, and a: This is the number of bits to be used per color channel, which are red, green, blue, and alpha, respectively.

  • disableAudio: This is to set whether audio should be used. If it should, requesting the audio subsystem will return null.

  • width and height: This is the size of the application window in pixels.

  • fullScreen: This is to set whether the application should start in the full screen or windowed mode.

  • vSyncEnabled: This is to set whether vertical synchronization should be enabled. This ensures that the render operations are in sync with the monitor refresh rate, avoiding potential partial frames.

  • title: This is the string with the desired title of the window.

  • resizable: This is to set whether the user should be able to resize the application window.

  • foregroundFPS and backgroundFPS: This is the number of desired frames per second when the application is active and inactive, respectively.

Android starter

The Android starter can be found in AndroidLauncher.java:

public class AndroidLauncher extends AndroidApplication {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
      initialize(new EnvironmentTest(), config);
   }

Android starters use the Android SDK Activity framework, which those who have developed for this platform before will be familiar with. In this case, an AndroidApplicationConfiguration instance is used. Some of the most useful attributes are listed as follows:

  • r, g, b, and a: Just as with the desktop project, these refer to the number of bits used per color channel.

  • hideStatusBar: This is to set whether the application should hide the typical Android status bar that shows up right at the top of the screen.

  • maxSimultaneousSounds: This is the number of maximum sound instances that can play at a given time. As you can see in Chapter 5, Audio and File I/O, dedicated to audio, they only refer to short sound effects as opposed to long streams of audio.

  • useAccelerometer: This is to set whether the application should care about the accelerometer; it defaults to true.

  • useCompass: This is to set whether the Android application should update the compass values; it also defaults to true.

HTML starter

The HTML5 starter resides inside the GwtLauncher.java file and follows the pattern we already know:

public class GwtLauncher extends GwtApplication {
   @Override
   public GwtApplicationConfiguration getConfig () {
      GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(480, 320);
      return cfg;
   }

   @Override
   public ApplicationListener getApplicationListener () {
      return new EnvironmentTest();
   }
}

A GwtApplicationConfiguration object is used to configure the HTML5 backend. Its most important parameters are as follows:

  • antialiasing: This is to set whether to enable antialiasing, which is computationally expensive, but helps to avoid rough edges when rendering.

  • canvasId: This is the identifier for the HTML element to embed the game canvas in. When not specified, the system will create a canvas element inside the <body> element.

  • fps: This is the target frames per second at which we desire to run the game.

  • width and height: These are the dimensions of the drawing area in pixels.

iOS starter

Finally, the iOS starter is hosted by the IOSLauncher.java file:

public class IOSLauncher extends IOSApplication.Delegate {
    @Override
    protected IOSApplication createApplication() {
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        return new IOSApplication(new EnvironmentTest(), config);
    }

    public static void main(String[] argv) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(argv, null, IOSLauncher.class);
        pool.drain();
    }
}

The configuration object for this backend belongs to the IOSApplicationConfiguration class and here are its main parameters:

  • accelerometerUpdate: This is the update interval to update the accelerometer values in seconds

  • orientationLandscape and orientationPortrait: This is to set whether the application supports the landscape or portrait orientation, respectively

  • preferredFramesPerSecond: This is the number of frames per second we try to reach while running the application

  • useAccelerometer: Just as on Android, this sets whether to update the accelerometer values

  • useCompass: This is to set whether to update the compass sensor values

How it works…

So far, you autonomously experienced how a Libgdx application is organized and the mechanism it uses to run across platforms. Also, you tested how the application lifecycle works and which events are triggered as a consequence of an event. Now, it is time to get a higher-level overview of all these systems and see how they fit together.

Here is an UML class diagram showing every piece of the puzzle that is involved in any way with game startups on specific platforms and in the application lifecycle. After a quick glance, we can observe how EnvironmentTest, our ApplicationListener implementation, is used by every launcher class along the various configuration classes:

The next diagram depicts the mighty Libgdx application lifecycle. Every time the game starts, the create() method is called. Immediately after, there is a call to resize() so as to accommodate the current screen dimensions. Next, the application enters its main loop, where it calls render() continuously, while processing the input and other events, as required.

When the application loses focus (for example, the user receives a call on Android), pause() is invoked. Once the focus is recovered, resume() is called, and we enter the main loop again.

The resize() method is called every time the application surface dimensions change (for example, the user resizes the window).

Finally, it's called when the player gets bored of our game. Sorry, this will never happen! When the player runs out of time to play our game and exits, pause() will be called, followed by dispose().

There's more…

After looking at the basic concepts behind a simple Libgdx project, let's move on to a couple of tricks to improve your quality of life.

Living comfortably with ApplicationAdapter

As you already know, every Libgdx game needs to have an ApplicationListener interface implementation in its core project for the launchers to use. We also saw how the developer is forced to implement the create(), dispose(), render(), resize(), pause(), and resume() methods of such an interface. However, these overridden methods might end up completely empty. What a waste of digital ink, and more importantly, our precious time!

Luckily enough, Libgdx provides a useful ApplicationAdapter class that already contains an empty implementation for each ApplicationListener interface method. This means that you can simply inherit from ApplicationAdapter and only override the methods you really need. This comes particularly in handy when writing small tests rather than big games. These small adapter classes are quite common within the API, and they are really comfortable to use as long as we do not need to inherit from anything else. Remember that Java does not support multiple inheritance.

The following will be perfectly valid if we want a completely empty application:

public class MyGame extends ApplicationAdapter {}

Managing a multiscreen application with Game

Most games are made out of several screens the player can navigate through. The main menu, level selection settings, or levels are some of the most common examples. Though this completely depends on the nature of each project, most of them definitely share the structure. Libgdx comes with an utterly minimalistic screen system built-in, which might just be enough for your requirements, so why not use it? Reinventing the wheel is rarely a good idea.

The two main components of this system are the Game abstract class and Screen interface. Game implements the well-known ApplicationListener interface, so you will only need your main class to inherit from Game.

The Game class holds a reference to the current Screen and provides the getter and setter methods for it. Game requires you to implement the create() method, but already provides implementations for the rest of the application lifecycle methods. Be aware that if you override any of the other methods, you will need to call the parent version so as to maintain screen behavior correctness. The helpful bit comes with the render() method, which will automatically update and render the active Screen reference, as long as it is not null.

What follows is an UML class diagram illustrating a sample game architecture based on the Game/Screen model. The user implemented MyGame as a Game derived class, and the SettingsScreen, GameScreen, LevelSelectionScreen, and MainMenuScreen classes were derived from Screen:

The Game public API looks like this. Note that method implementation has been omitted for space reasons:

public abstract class Game implements ApplicationListener {
   public void dispose ();
   public void pause ();
   public void resume ();
   public void render ();
   public void resize (int width, int height);
   public void setScreen (Screen screen);
   public Screen getScreen ();
}

The Screen interface is quite similar to the ApplicationListener interface, but its equivalent methods will only be called when it is the active screen. It also adds the hide() and show() methods that will be called when changed to and from a screen, respectively. In the following code, you will find an overview of the interface:

public interface Screen {
   public void render (float delta);
   public void resize (int width, int height);
   public void show ();
   public void hide ();
   public void pause ();
   public void resume ();
   public void dispose ();
}

Note

Just like we saw before with ApplicationListener and ApplicationAdapter, the Screen interface has a convenient implementation, unsurprisingly called ScreenAdapter. You can just inherit from it and override the methods that you need only.

See also

  • Jump to Chapter 2, Working with 2D Graphics, to start rendering textures onscreen or carry on with the Updating and managing project dependencies recipe to learn more about Libgdx project configuration.

 

Updating and managing project dependencies


This recipe will show you how to leverage Gradle in order to maintain your project and its dependencies. By the end of the recipe, you will be able to upgrade to a newer version of Libgdx and add third-party extensions and arbitrary Java libraries.

Oftentimes, people tend to be reluctant to learn new technologies, especially build systems such as Gradle. However, they actually tremendously simplify the process, thus helping us make even more awesome games.

Getting ready

Let's start with the environment-test project we created in the Setting up a cross-platform development environment recipe. At this point, you should have the project up and running within Eclipse or the IDE of your choice.

How to do it…

Your application's dependencies are expressed in the build.gradle text file, which is pretty much the only file we will manipulate. Be advised against tinkering with the project-specific Gradle files as you stand a really good chance of making all hell break loose.

Note

This is not supposed to be a full Gradle manual; it's a mere introduction for you to get by and move on to making actual games.

Gradle build file primer

Throughout this primer, and for space reasons, we will only show small snippets from the build file.

Go ahead and open the build.gradle file from Eclipse. The first thing you will come across is the buildscript element, which defines the list of repositories and basic dependencies. Repositories act as a library-serving system. We can reference libraries by name, and Gradle will ask the list of repositories for a library that matches the name:

buildscript {
    repositories {
        maven {
            url 'https://github.com/steffenschaefer/gwt-gradle-plugin/raw/maven-repo/'
        }
        ...
    }

    dependencies {
        ...
    }
}

The allprojects element contains a string with the application version. Additionally, it defines appName as well as the Libgdx and roboVM versions it should build against. It also provides a list of repositories to fetch from:

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"
    
    version = "1.0"
    ext {
        appName = "environment-test"
        gdxVersion = "1.2.0"
        roboVMVersion = "0.0.13"
    }
    
    repositories {
        ...
    }
}

Every project has a project element indicating its name. A skeleton application's core project will only depend on the previously defined Libgdx version:

project(":core") {
    apply plugin: "java"
    
    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}

Platform-specific projects, such as a desktop project, will depend on core as well as their corresponding backend and potentially native libraries:

project(":desktop") {
    apply plugin: "java"
    
    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

Do not panic if you do not fully understand everything that goes on inside the build script. However, you should at least have a very basic grasp on how the general structure holds itself together.

Now, let's get on to some useful business.

Updating existing dependencies

Dependencies that are pulled from repositories take the following typical Maven format:

compile '<groupId>:<artifactId>:<version>:<classifier>'

Intuitively, whenever you desire to change a dependency version, you just need to go and modify the version component of its declaration. Now, imagine the Libgdx team has released a new version and we are all very excited with the new features; it is time to try them out. Conveniently enough, throughout the script, the Libgdx version points to the gdxVersion variable. We only need to find the allprojects element and change the following:

gdxVersion = "1.1.0"

To the following, you are free to choose whichever version you like:

gdxVersion = "1.2.0"

To make Gradle fetch the new dependencies, select all the projects, right-click on Gradle, and then click on Refresh All.

Note

Libgdx has stable and nightly builds. Stable builds are well tested, planned builds that can be identified by their version number, 1.2.0, for instance. Nightly builds, in turn, are generated overnight from whatever the Git repository contains. To use nightly builds in your project, you need to set 1.2-SNAPSHOT as the version identifier. Nightly builds are good to test and get the latest features as they are introduced; however, they are considerably riskier and prone to breaking. Use the stable builds if peace of mind is what you seek. Luckily enough, you can switch between them just by changing the gdxVersion variable.

Adding Libgdx extensions

Libgdx comes with several additional libraries that provide a ton of extra features. The reason they are not part of the core is because either not everyone is likely to need them or because they might not work on all backends. These extensions have been mavenized and can be fetched from the repositories.

Note

Linking against libraries you do not need will unnecessarily increase the size of your distributable package. Desktop downloads are not too big of a problem as we have AAA game downloads going up to 50 GB nowadays. However, mobile games need to be careful about this since some 3G connections have bandwidth limits.

Currently, the following are the Libgdx Gradle-ready extensions along with their required dependencies for each of the projects. The core dependency will add the interfaces for you to use within the game code, whilst the platform-specific dependencies will contain the implementation of such interfaces. You will need to add them inside the corresponding dependencies element.

Bullet

A bullet is a wrapper for the popular open source 3D physics library. Note that it is not compatible with the HTML5 backend as it needs to run native code.

Core

Here is the code for the core:

compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
Desktop

To use it with desktop, we use the following:

compile "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-desktop"
Android

Similarly, for Android, we use the following:

compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-v7a"
iOS

Furthermore, we use the following code for iOS:

compile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-ios"
FreeTypeFont

The FreeTypeFont extension helps you generate bitmaps from TTF fonts on the fly. It is not compatible with the HTML5 backend.

Core

Here is the code for the core:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
Desktop

To use it with desktop, we use the following:

compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
Android

For Android, this is what we use:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
iOS

Lastly, for iOS, we use the following:

compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
Controllers

The Controllers extension provides an API to get events from the game controllers. It is not compatible with the iOS backend; although the project still compiles and runs, it will just not detect any controller or event. For more information on controllers, see Chapter 4, Detecting User Input.

Core

Here is the code for the core:

compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
Desktop

To use it with desktop, we use the following:

compile "com.badlogicgames.gdx:gdx-controllers-desktop:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-platform:$gdxVersion:natives-desktop"
Android

To use this extension with Android , we use the following:

compile "com.badlogicgames.gdx:gdx-controllers-android:$gdxVersion"
HTML5

For HTML5, the following code is used:

compile "com.badlogicgames.gdx:gdx-controllers:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion"
compile "com.badlogicgames.gdx:gdx-controllers-gwt:$gdxVersion:sources"
Box2D

The Box2D extension will provide you with a full-blown rigid body physics engine compatible with all backends. Read more about it in Chapter 10, Rigid Body Physics with Box2D.

Core

Here is the code for the core:

compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
Desktop

We run the following code to use it with desktop:

compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
Android

For Android, we use the following code:

compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
iOS

Likewise, for iOS, we will use the following code:

compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
HTML5

To use it with HTML5, we use the following code:

compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
Tools

The Tools extension provides texture packing, font generation, and particle editor functionalities, only compatible with the desktop backend.

Core
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
Desktop
compile "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
AI

Artificial Intelligence systems: steering behaviors, finite state machines, and behavior trees.

Core

Here is the code for the core:

compile "com.badlogicgames.gdx:gdx-ai:$gdxVersion"
Android

Similarly, for android, we use the following:

compile "com.badlogicgames.gdx:gdx-ai:$gdxVersion"
HTML

Lastly, for HTML, we use the following:

compile "com.badlogicgames.gdx:gdx-ai:$gdxVersion:sources"

Adding external repositories

It is possible to add extra repositories to Gradle to look for the files you need by adding them to the allprojects section. Gradle supports Maven- and Ivy-formatted repositories:

allprojects {
    ...
    
    repositories {
        ivy { url "https://some-ivy-repo.com/repo" }
        maven { url "https://some-maven-repo.com/repo" }
    }
}

Adding additional file dependencies

The library you want to use so desperately might not be in any Maven or Ivy repository. Is everything lost? Of course not! You can make projects depend on local files such as arbitrary JAR packages.

For instance, you can place the JAR files you need inside a lib folder in each project. Then, you will need to add the following entry to the dependencies section of the projects:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

Managing GWT dependencies

HTML5 projects will require extra attention when adding dependencies. The GWT compiler needs to know about the modules the application will use. This information needs to be specified in both the GdxDefinition.gwt.xml and GdxDefinitionSuperdev.gwt.xml files located in the class path. The following snippet shows a typical gwt.xml file highlighting the addition of the popular Universal Tween Engine dependency:

<module rename-to="html">
    <inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
    <inherits name='com.cookbook.environment.EnvironmentTest' />
    <inherits name='aurelienribon.tweenengine'/>
   
    <entry-point class='com.cookbook.environment.client.GwtLauncher' />

    <set-configuration-property name="gdx.assetpath" value="../android/assets" />
</module>

The Box2D extension will require you to inherit from the following module:

<inherits name='com.badlogic.gdx.physics.box2d.box2d-gwt' />

The Controllers extension will ask for the following module:

<inherits name='com.badlogic.gdx.controllers.controllers-gwt' />

The AI extension will require the following:

<inherits name='com.badlogic.gdx.ai' />

Managing GWT projects can be a bit fiddly, and we will cover this topic more thoroughly in the Making libraries compatible with GWT recipe of Chapter 11, Third-party Libraries and Extras.

Every time you add or update a dependency, it is advisable to rebuild the Gradle model so that everything is up to date and you can carry on working normally. Select Gradle and Refresh All from the project by right-clicking on the contextual menu.

There's more…

Telling Gradle to refresh a project's dependencies will automatically make the system download those that have changed when the dependency is a snapshot. For example, this is what happens with the Libgdx nightly builds:

gdxVersion = "1.2.0"

However, you might want to force Gradle to redownload a specific dependency or even all of them. This can come in handy when a library changes, but the version number is still the same. Though rare, this can very well happen if someone makes a mistake.

Gradle downloads and puts dependencies in a .gradle directory inside the user's home folder. It is possible to delete either the whole folder or specific libraries to make Gradle download them again the next time it tries to build the project.

Digging a bit deeper, you can tell Gradle that a particular dependency is prone to change often. It will then check every 24 hours whether it has changed. If it has, it will redownload the dependency. This is achieved with the changing property:

dependencies {
    compile group: "group", name: "project", version: "1.1-SNAPSHOT", changing: true
}

Once more, for further information on how to tune Gradle to your taste, refer to the official user guide.

See also

  • If you want to know more about libraries and tools that work well with Libgdx and can help you in your game development adventure, do not forget to read Chapter 11, Third-party Libraries and Extras.

 

Using source control on a Libgdx project with Git


Writing software, in general, and making games, in particular, is a hard endeavor, which is why we should avoid unnecessary complications whenever we can using tools that will save us from despair. There are so many things that can go wrong during development; luckily for us, we can use source control as the first step to a better night's sleep.

What if your hard drive breaks, bursts in flames, or gets stolen? Yes, the right answer is continuous backups, tons of them, and then some more! Surely, online cloud storage services such as Dropbox or Google Drive provide this for you out of the box. They even let you share folders with others, which might lure you into thinking that it is a good way of working as a team, but it just stops cutting it the second things go a bit beyond trivial.

What if you come back home on a Saturday night after too many drinks and decide it is a great time to get some coding done? After all, you feel incredibly inspired! What follows is that you will wake up the next morning in sweat, tears and full of regret. Surely, Dropbox lets you revert changes on a per-file basis, but a lot of fiddling is required when the changes are spread across multiple systems, and you risk worsening things even more.

Finally, what if two members in your team make changes to the same file? Conflict solving in Dropbox is flaky at best; you will most likely enter the realms of hell in these cases. Manually merge two files every time this happens, and believe me, it will happen constantly, but this is not something you will enjoy.

Any good source control system gracefully solves each one of these little nightmares for you. A repository keeps track of every important file, and every time you make a set of changes, it is dead easy to create a new revision or snapshot of the whole project tree. These changes can be shared with your team members, and there is no problem in going back a few revisions whenever you realize a mistake has been made. Most source control systems also provide very intelligent mechanisms to merge files several people have modified.

After this little rant, it is time to proceed with the recipe. We will use Git to put only the essential files of our Libgdx project under source control, and in this way sleep a lot better at night. Keep in mind that the intent is neither to provide a detailed guide on how revision control works nor how to fully understand Git, but just the bare minimum to get you started.

Note

Why Git? Well, according to Linus Torvalds, if you do not use Git, you are ugly. For those who don't know him, Linus Torvalds is the father of both the Linux kernel and Git revision control system. He does not have a huge appreciation towards alternatives such as CVS or SVN. Check Linus' talk, as it is very interesting, at http://www.youtube.com/watch?v=4XpnKHJAok8.

Getting ready

First, you need to install the Git client on your computer, which was originally conceived as a command-line tool by Linus Torvalds. However, nowadays we have visual clients at our disposal, which make life much easier. On both Windows and Mac, I will personally recommend SourceTree because it is quite intuitive and has everything you are likely to need at hand. However, there are alternatives such as Tortoise Git. Both are free, and the latter is also completely open source. SourceTree's installer can be found on its official site at http://sourcetreeapp.com.

The package installation process varies across GNU/Linux distributions. In Debian-based distributions (most common ones), users can install the Git client using the command-line and apt-get:

sudo apt-get install git

This is a program capture; the cropped text is not relevant

How to do it…

Every time you start a project, the first step should be to create a repository to keep track of everything that goes on with it. Services such as GitHub, Gitorious, and Google Code offer free repositories but require you to disclose the source. However, GitHub also allows you to create paid private repositories. Bitbucket is a competitive alternative if you seek a private repository at no cost. Last but not least, you can always host your own Git server at home, but then you will have to deal with problems such as backups, availability from the outside, and so on. Perform the following steps:

  1. Choose your poison, and create an account and a repository to start with, such as:

  2. The next step will be to clone your newly created repository so as to have a local copy to work with.

  3. From SourceTree, click on Clone/New, select the Clone Repository tab, and enter the URL you received from the service of your choice.

  4. After entering the destination path, you can simply click on Clone, and the empty repository will be available on the leftmost panel, as shown in the following screenshot:

  5. GNU/Linux users can get away with the following command:

    git clone <REPO-URL> <DESTINATION-FOLDER>
    
  6. Have you already started working on your awesome game? Then, what you need to do is clone your brand new repository into an empty folder and pour all the project files there. Git requires an empty folder to either create or clone a repository.

  7. With your repository selected on SourceTree, click on Working Copy and take a close look at the Working Copy Changes panel; there will be tons of files that are not under source control asking you to include them, as shown in the following screenshot:

  8. The list of candidates for addition, deletion, and modified files can also be queried from the following command line:

    git status
    

We do not need all this nonsense! If you already compiled something, there will be tons of clutter that will certainly become a huge waste of cloud storage. Though this comes cheap these days, it will be painful to upload and download changes further down the line. Ideally, we only want to keep track of whatever is absolutely necessary for someone to download a snapshot from the other end of the world and be able to continue working normally.

Luckily enough, we can get rid of all the noise from git status and the Working Copy Changes panel by creating a .gitignore text file in the root folder of the repository with the following content:

bin/
target/
obj/
.gwt/
gwt-unitCache/
war/
gen/

*.class

It is time to add the .gitignore file to our repository. Perform the following steps:

  1. From the Working Copy Changes panel, right-click on the file and select Add to Index.

  2. Then, click on Commit, add a meaningful message such as Adds .gitignore file to make repository management easier, and finish by clicking on the Commit button on the modal window, as shown in the following screenshot:

  3. On the command line, it looks something like this:

    git add .gitignore
    git commit –m "Adds .gitignore file to make repository management easier"
    
  4. Now, you can safely proceed to add every remaining file and commit again. From GUI, follow the same process as with the .gitignore file. However, if you are a command-line lover, you can stage and commit all the files at once like this:

    git add *
    git commit –m "Adds project basic files"
    

Congratulations! Git is now keeping track of your project locally. Whenever you make changes, you simply need to add them again to the set of files about to be committed, and actually commit them. Obviously, you can achieve this with both the visual and command-line variants of the client.

Note

Writing meaningful commit messages might seem unimportant, but nothing is further away from the truth. Whenever you examine the history in search for the origin of a bug, you will want all the help you can get to understand what the person who made each commit was trying to do at that point in time.

It is time to push all the commits you have made from your local repository to the remote repository hosted by the provider of your choice. This is the way your teammates, and potentially the whole world in case the repository is public, will be granted access to the changes. Perform the following steps:

  1. From SourceTree, click on Push and select the origin and destiny branches; typically, both will be named master. Once you are ready, click on OK and wait for the operation to finish, as shown in the following screenshot:

  2. Here is the command-line version of the same song:

    git push origin master
    

    Let's imagine that our game developer buddies have been sweating blood to add new shiny features. Naturally, you are eager to check what they have been up to and help them out. Assuming they pushed their changes to origin, you now need to pull every new commit and merge them with your working branch. Fortunately, Git will magically and gracefully take care of most conflicts. Perform the following steps:

  3. From SourceTree, you need to click on the Pull button and select the remote repository and branch you are pulling from. For a simplistic approach, these will be origin and master. Once you are ready, click on the OK button, as shown in the following screenshot, and sit back and enjoy:

  4. A pull operation can also be triggered from the command line:

    git pull origin
    

Note

Sometimes, Eclipse will not be too happy after sudden external project modifications, and misbehave a little bit. If this happens, you just need to refresh the projects by selecting them, pressing F5, or restarting IDE.

How it works…

Git is a distributed revision control system. This means that, in principle, there is no central repository everyone commits to. Instead, developers have their own local repository containing the full history of the project.

The way Git keeps track of your files is quite special. A complete snapshot of all the files within the repository is kept per version. Every time you commit a change, Git takes a brand new picture and stores it. Thankfully, Git is smart enough to not store a file twice, unless it changes from one version to another. Rather, it simply links the versions together. You can observe this process in the following diagram:

As per the previous diagram, Version 1 has two files, A and B. Then, a commit is made updating A, so Git stores the new file and creates a link to B because it has not changed. The next commit modifies B, so Version 3 stores this file and a link to the unchanged revision of A. Finally, Version 4 contains modifications to A, a link to B (unchanged), and the first revision of the newly created file C.

Basic operations

Most times you work with Git, you will operate locally because your repository contains everything that you need. It lets you check the full history, commit, and revert changes, among many others. This makes Git lightning fast and very versatile as it does not attach you to any Internet connection.

The typical usage of Git is as follows:

  • Clone a remote repository and make it locally available with git clone

  • Modify, add, or remove files

  • Add files to the staging area for them to be committed to your local repository with git add

  • Commit the files and create a new version within your local repository using git commit

  • Receive changes from a remote repository with git pull

  • Send your local changes to a remote repository with git push

Git branches

Though we didn't use them, the way to experience Git's true glory is through branching and merging. You can think of the history of your repository as a series of directory-tree snapshots linked together. A commit is a small object that points to the corresponding snapshot and contains information about its ancestors. The first commit will not have a parentand subsequent commits will have a parent, but when a commit is the result of merging several changes together, it will have several parents.

A branch is simply a movable pointer to one of these commits; you already know master, the default branch. The way we have been working, master will point to the last commit you made, as shown in the following diagram:

Branching in Git has a minimal overhead as the only thing it does is create a new pointer to a certain commit. In the following diagram, we create a new branch called test:

We can work on test for a while, go back to master, and then work on something different. Our repository will look something like this:

Someone in the team decides it is time to bring the awesome changes made on test over to master; it is now merging time! A new commit object is created, with the last commits from master and test as parents. Finally, the master branch pointer moves forward so that it points to the new commit. Easy as pie!

As we mentioned, this is not intended to be a comprehensive guide on Git but a mere introduction instead. If you are dying out of curiosity, I will wholeheartedly recommend Pro Git, which is freely available online under a Creative Commons license at http://git-scm.com/book.

Gitignore files

The .gitignore file we created earlier contains a list of rules that tell Git which files we are not interested in. Files under this branch of the directory tree that match the patterns and are not committed to the repository just yet, will not appear as candidates when running git status on the Source Tree Working Copy Changes panel.

Typically, you will want to ignore specific files, that is, every file with a specific extension or anything that is under a certain folder. The syntax for these, in the same order, is quite simple:

/game/doc/doc.pdf
*.jar
/game/game-core/bin

SourceTree lets us add files and paths to .gitignore very easily. Select the file you want to add to the ignore list from the Working Copy Changes panel, and right-click and select Ignore. A new modal window will appear giving you the options to ignore just this file, all the files with this extension, or everything placed under a certain folder from the file's path. Once you select the option you want, click on OK, as shown in the following screenshot:

There's more…

This recipe was aimed at covering the very basics of the relationship between Libgdx and revision control systems. If you are interested in broadening your knowledge on the matter, read on.

Regardless of what Linus Torvalds might tell you, Git is not the only revision control system out in the wild, and it might not be the best fit in your case. The following are other alternatives:

  • Mercurial: This is cross-platform, open source, and completely distributed, just like Git. It is available at http://mercurial.selenic.com.

  • Subversion: This is also cross-platform and open source, but it's a centralized system. It is usually perceived as easier to learn than Git and works for most projects. However, it is much less flexible. It is available at subversion.trigris.org.

  • Perforce: This is a proprietary centralized system, but free for up to 20 users. It is widely used in the games industry for its binary file management. It is available at http://perforce.com.

Pro Git is a freely available book on how to master the version control system. It is available at http://git-scm.com/book.

See also

The Libgdx community is built around Git and hosted on GitHub. The following recipes are highly related to this revision control system:

  • The Working from sources recipe in Chapter 13, Giving Back

  • The Sending a pull request on GitHub recipe in Chapter 13, Giving Back

 

Importing and running the Libgdx official demos


Libgdx comes with a few full games to serve as example projects. Developers are encouraged to check them out, try them, and read the source code. It is a fantastic way to learn how things are done the Libgdx way.

In this recipe, you will learn how to get and run the official demos.

Getting ready

You only need to make sure your development environment works. The process to get up and running is explained in the Setting up a cross-platform development environment at the beginning of this chapter.

How to do it…

There are eight official Libgdx demos. They are as follows:

We will work with Super Jumper, but the process is identical for any other project; just follow these steps:

  1. Clone the repository using Git. The URL is available in the project page. Super Jumper's Git repository URL is git@github.com:libgdx/libgdx-demo-superjumper.git. If you do not know how to clone a Git repository, read the Using source control on a Libgdx project with Git recipe.

  2. Import the project from its folder into Eclipse following the instructions in the Setting up a cross-platform development environment recipe.

  3. Run each platform-specific project like any other Libgdx project.

Now, you can run Super Jumper, as shown in the following screenshot:

How it works…

All the Libgdx official demos use Gradle as a build system, so the process of importing them into an IDE, getting them to compile, and running them is exactly the same as we saw in the past.

As you progress through this book, it will be a great exercise to go back to the demos' source code and try to identify the concepts and techniques explained here.

There's more…

Luckily enough, the Libgdx community is big, active, and generous. This results in a great number of open source projects for people to study and learn from. Some of them can be found in the Libgdx gallery at http://libgdx.badlogicgames.com/gallery.html.

Latest Reviews (8 reviews total)
Nothing to complain about purchase.
El software avanza y el libro queda algo desfasado.
Redacción y contenido excelentes, altamente recomendable.
Libgdx Cross-platform Game Development Cookbook
Unlock this book and the full library FREE for 7 days
Start now