Book Image

Learning LibGDX Game Development- Second Edition

Book Image

Learning LibGDX Game Development- Second Edition

Overview of this book

Table of Contents (21 chapters)
Learning LibGDX Game Development Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

As 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 that 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. You will also learn how to develop a game with ease and speed up your development cycles. In ten easy-to-follow chapters, you will 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, Android, and iOS. 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, takes 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. As LibGDX is a framework, we will first have to build our program architecture 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, will implement the game objects such as rocks, mountains, and clouds. We will put the new code into action using a level loader. We will also add a Graphical User Interface (GUI) to the game scene to show the player's score, extra lives and frames per second to measure the games performance.

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 off-screen rendering into video memory. This will allow us to create seamless transitions for an improved user experience while switching from one screen to another. For Canyon Bunny, we will create a variety of transition effects.

Chapter 10, Managing the 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 the LibGDX's Audio API and demonstrate how to use it by creating an audio manager. We do this so that handling our entire 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 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 with the example of a monochrome image filter effect. Lastly, we will show you how to make use of the accelerometer hardware that is commonly available in modern smartphones and tablets, which allows controlling the game by tilting the device.

Chapter 12, Animations, explains how to polish the game by adding animations. In this chapter, 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.

Chapter 13, Basic 3D Programming, introduces the new LibGDX's 3D API. You will learn how to use the 3D API to create basic models such as sphere, cube, cylinder, and so on, and load models exported from modeling software such as Blender. You will also learn about ray picking, an important concept used to develop first person shooter games.

Chapter 14, Bullet Physics, will walk you through the basics of 3D physics using Bullet. Finally, we will create a simple application to simulate physics using Bullet.

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.9+).

Additionally, you will need to download the LibGDX framework for game development. You can download LibGDX from http://libgdx.badlogicgames.com/releases/. Download the version 0.1.2 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, supporting OpenGL ES 2.0, and the official Android Software Development Kit (SDK) that can be downloaded from http://developer.android.com/sdk/index.html.

To develop games for an iOS platform, you will need Mac OS X (10.9+) and an iOS device.

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, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The starter class for iOS application is RobovmLauncher.java."

A block of code is set as follows:

prefs.putInteger("sound_volume", 100); // volume @ 100%
prefs.flush();

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

package com.packtpub.libgdx.demo;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
public class Main {
public static void main(String[] args) {
   
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "demo";
cfg.width = 480;
cfg.height = 320;
new LwjglApplication(new MyDemo(), cfg);
   }
}

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: "You can quickly check this by going to the Project menu."

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 , 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.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/4775OS_ColoredImages.pdf

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.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

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 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 if you are having a problem with any aspect of the book, and we will do our best to address it.