Preface
Since personal computers have conquered our private homes, video games have become more and more popular and eventually a multimillion dollar business for big video game companies. With the introduction of mobile devices such as smartphones and tablets, the market for video games has experienced another significant increase; in particular, it has now become open to independent game developers with small budgets.
For game developers, it is essential to have tools at hand that provide fundamentals which allow rapid prototyping and cost-effective implementation of their creative ideas. This is where Libgdx comes into play.
Libgdx, as a Java-based game development framework, provides a unified access layer to handle all the supported platforms. Libgdx also makes use of C/C++ to achieve cross-platform support as well as to boost the application performance for mission critical tasks.
This book will show you how easy it is to develop cross-platform games by walking you through a complete game development cycle using the free and open source library Libgdx. Besides this, you will also learn about common game structure and the involved requirements.
You will be introduced to the key features of Libgdx and you will learn how to develop a game with ease and speed up your development cycles. In ten easy-to-follow chapters, you are going to develop your first Libgdx cross-platform game and add more and more game functionalities as you progress further through this book.
The special features will also make you acquainted with advanced programming techniques such as animations, physics simulations, and shader programs that enhance your games in both their gameplay and visual presentation.
By the end of this book, you will have a fully working 2D game that will run on Windows, Linux, Mac OS X, WebGL-capable browsers, and Android. You will also have all the skills required to extend the game further or to start developing your own cross-platform games.
What this book covers
Chapter 1, Introduction to Libgdx and Project Setup, covers how to install and configure the development environment and introduces you to the project setup tool that comes with Libgdx. Then, we will take a first look at the basics of what a game needs to come alive.
Chapter 2, Cross-platform Development – Build Once, Deploy Anywhere, explains the supported target platforms and how to deploy and run our application on each platform using a demo application. For the first overview of Libgdx's API, we will take a glance at each module. Then, the Application Cycle will be introduced, and we will take a look at how to debug and manipulate our code at runtime.
Chapter 3, Configuring the Game, brings us from our demo application to a real game by setting up a new project called Canyon Bunny. We will work on this project throughout the rest of the book and extend it from chapter to chapter with new features. Since Libgdx is a framework, we will first have to build our program architecture by using UML class diagrams to structure our game engine.
Chapter 4, Gathering Resources, describes how to gather all the resources (assets) needed for Canyon Bunny including graphics, audio files, level data, and so on. We will also find out how to load, track, and organize assets efficiently. Finally, it is time to think about how level data is going to be handled so that we are able to populate our game world with objects.
Chapter 5, Making a Scene, covers how to start implementing the first game objects such as rocks, mountains, and clouds, and how to use the assets that we added in Chapter 4, Gathering Resources. In order to visualize the game scene, a level loader is needed to put our game objects in their right places in the game world. Typically, games have a user interface to give feedback about their current status, so we will add a game GUI to Canyon Bunny that displays the player's high score, remaining extra lives, and so on.
Chapter 6, Adding the Actors, explains how to add the remaining game objects for Canyon Bunny, including the player character and collectible items to complete our game. We will also add simple physics for player movement and basic collision detection. Additionally, the game logic will be extended so that it is able to detect the "life lost" and "game over" conditions.
Chapter 7, Menus and Options, describes how to create a menu system with widgets such as buttons, labels, and checkboxes to enrich the overall game experience. Furthermore, we will add an Options window where the player can adjust the game settings.
Chapter 8, Special Effects, covers how to make use of particle systems and how to apply interpolation algorithms to create impressive effects such as dust clouds; a smooth, following camera; floating rocks; and parallax scrolling for mountains in the background. Using special effects will spice up the appearance of your game.
Chapter 9, Screen Transitions, introduces screen transitions. We will dive into enhanced visual effects using OpenGL's Framebuffer Objects for offscreen rendering into video memory. This will allow us to create seamless transitions for an improved user experience when switching from one screen to another. For Canyon Bunny, we will create a variety of transition effects.
Chapter 10, Managing Music and Sound Effects, will walk you through a list of recommended sound generators and discuss their differences. Then, we will take a look at Libgdx's Audio API and demonstrate how to use it by creating an audio manager. We do this so that handling all our audio playback needs become a breeze.
Chapter 11, Advanced Programming Techniques, introduces you to some advanced programming techniques that will guide you to the next level of game programming. We will build up basic knowledge about the Box2D API that enables us to create believable physics simulations in games. Additionally, we will discuss the topic of shader programs using the example of a monochrome image filter effect. Lastly, we will show you how to make use of the accelerometer hardware commonly available in modern smartphones and tablets, which allows controlling the game by tilting the device.
Chapter 12, Animations, explains how to polish up the game by adding animations. We will cover two different approaches to animate the game menu and the game world. Finally, we will implement a state machine to allow event-based animations for the player character.
What you need for this book
Libgdx is a cross-platform game development framework. For development, you will need a computer running either Windows (Vista/7/8), Linux (for example, Ubuntu), or Mac OS X (10.5+).
Additionally, you will need to download the Libgdx framework for game development. You can download Libgdx from https://code.google.com/p/libgdx/downloads/list. Please download Version 0.9.7 of Libgdx, as this is the version that is used in this book.
The Integrated Development Environment (IDE) used in this book is Eclipse. You can download the Eclipse IDE from http://www.eclipse.org/.
To develop games for the Android platform, you will need an Android device running Android 2.2 (Froyo) or higher and the official Android Software Development Kit (SDK) that can be downloaded from http://developer.android.com/sdk/index.html.
Who this book is for
This book is written for software developers who are new to game development and to Libgdx in particular. It is assumed that you have some experience in Java to be able to follow the discussed code in this book.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "The main class of the demo application is MyDemo.java
."
A block of code is set as follows:
@Override public void dispose() { batch.dispose(); texture.dispose(); }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
public void update (float deltaTime) { updateMotionX(deltaTime); updateMotionY(deltaTime); // Move to new position position.x += velocity.x * deltaTime; position.y += velocity.y * deltaTime; }
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "A new tab titled Development Mode should open at the bottom."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <feedback@packtpub.com>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
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.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <questions@packtpub.com>
if you are having a problem with any aspect of the book, and we will do our best to address it.