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.
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.
Here is our little software shopping list:
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.
Perform the following installation steps:
C:Program FilesJavajdk_version
.JAVA_HOME
as the name and your installation path as a value. In my case, the value is C:Program FilesJavajdk1.7.0_45
.~/.bash_profile
file and add the following:export JAVA_HOME=`/usr/libexec/java_home –v 1.7`
Perform the following installation steps:
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
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.
~/.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
source ~/.bashrc
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:
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
Follow these instructions to install Android Development Kit, which is essential to target Android devices:
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
Perform the following installation steps:
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
~/.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
~/.bashrc
file:source ~/.bashrc
cd ~/dev/android-sdk-linux chmod –R 744 *
Perform the following steps:
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.tools
folder. GNU/Linux users need to run an Android executable.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:
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:
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.
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.
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:
Great! One more task can be crossed out from our shopping list.
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.
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:
libgdx-cookbook
workspace you just created.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:
[cookbook]/environment
folder, and click on Build Model.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.environment-test-android
project and will be shared across all platforms.environment-test-desktop
), select Properties | Run/Debug Settings, select DesktopLauncher, and click on Edit.${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.
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:
Let's try the HTML project now. Perform the following steps:
environment-test-gwt
project and select Run As Gradle build….http://localhost:8080/gwt
The code server is ready.
Next, visit http://localhost:9876/
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:
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.
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.
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!
3.142.166.31