Book Image

Libgdx Cross-platform Game Development Cookbook

Book Image

Libgdx Cross-platform Game Development Cookbook

Overview of this book

Table of Contents (20 chapters)
Libgdx Cross-platform Game Development Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Libgdx is a powerful Java-based open source cross-platform game framework. It allows developers to target Windows, Mac, Linux, Android, iOS, and HTML5 with a single codebase, sparing them the hassle of dealing with low-level, platform-specific details.

For more information, please refer to http://libgdx.badlogicgames.com/.

Through a clean and simple API, Libgdx offers a vast set of features, which are as follows:

  • Low-level OpenGL access: This includes vertex arrays, meshes, framebuffers, shaders, and immediate mode renderer

  • 2D graphics: This includes bitmaps, texture atlases, particles, scene graph, user interface, and maps

  • 3D graphics: This includes model loading, lighting, decal batching, particles, and so on

  • Audio: This includes streaming music, sound effects (MP3, WAV, and OGG), pitch control, and direct access to recording devices

  • Input: This includes keyboard, mouse, touch, accelerometer, compass, and controller events as well as gesture detection

  • Math and physics: This includes matrices, vectors, shapes, interpolators, frustum, intersection utilities, and full Box2D and Bullet physics JNI wrappers

  • File I/O: This includes cross-platform file access, preferences storage, XML, and JSON parsing

  • Utilities: This includes fast custom collections and easy multiresolution handling

  • Tools: This includes project creation, particle editor, bitmap font generator, and texture packer

Libgdx is incredibly fast, thanks to the use of JNI wrappers and heavy emphasis on avoiding garbage collection. This becomes crucial when deploying on mobile devices and browsers. Its framework nature allows you to choose which components to use as it does not enforce a single way of working like most engines do.

The community behind the framework is large, welcoming, and always willing to provide support. The GitHub repository shows how active the project is, with many commits every day, constant releases, and rich documentation. For more information, refer to https://github.com/libgdx/libgdx.

Thanks to its permissive Apache 2.0 license, Libgdx can be used for both commercial and noncommercial projects without having to necessarily disclose the source. According to AppBrain, 1.44 percent of all the Android applications (both games and nongames) are built on top of it, which reinforces its flexibility and ease of use. For more information, refer to http://appbrain.com/stats/libraries/details/libgdx/libgdx.

This book provides excellent coverage of the aforementioned systems focusing on 2D game development. Each chapter covers a set of related features through a series of recipes. A recipe explains with simple steps how to carry out a particular task with Libgdx such as rendering a texture on the screen. The practical approach will give you a very good insight on how things actually work, preparing you to use this technology on real-life projects.

After reading through the recipes in this book, you will have a solid understanding of how all the systems in Libgdx work and how to better leverage them in order to implement your games. The good practices exposed here will give you an edge to produce clean and efficient code.

What this book covers

Chapter 1, Diving into Libgdx, introduces the reader to the Libgdx development environment so as to get ready for the rest of the book. It also presents the basic architecture of the framework, how to run applications across devices, and how to put a project under source control.

Chapter 2, Working with 2D Graphics, covers the most important aspects of the Libgdx 2D API. The readers will learn how to render and animate textures, work with cameras, and deal with different resolutions.

Chapter 3, Advanced 2D Graphics, shows the reader how to achieve juicy effects with particles, shaders, and image composition.

Chapter 4, Detecting User Input, explains how to make your applications interactive across different devices. Keyboard, mouse, touch, and controller-based inputs are all found in this chapter.

Chapter 5, Audio and File I/O, shows readers how to play background music and sound effects to give that extra sparkle to the project. It also covers how to use the file's API and read popular formats such as JSON and XML.

Chapter 6, Font Rendering, details the different approaches to render text through Libgdx. Moreover, it also teaches how to overcome common pitfalls such as special characters and font scaling.

Chapter 7, Asset Management, provides information on how to control your assets' life cycle so as to accommodate low memory devices and make the most out of the hardware. It also gives insight into how to achieve asynchronous loading, so apps stay responsive while they stream content.

Chapter 8, User Interfaces with Scene2D, covers the powerful Libgdx scene hierarchy API focusing on how to build UI layouts. Moreover, it goes as far as skinning and custom widget creation. The reader will learn how to create great menus and in-game HUDs.

Chapter 9, The 2D Maps API, explains the simple Libgdx mechanisms to load and render levels created with tools such as Tiled as well as to query their metadata. The maps 2D API also allows users to provide loaders and renderers for additional formats.

Chapter 10, Rigid Body Physics with Box2D, shows the reader how to use the many features of the popular physics library that comes with Libgdx. Bodies, shapes, joints, sensors, collision handling, level geometry, raycasting, and fixed timestep are all covered in this chapter.

Chapter 11, Third-party Libraries and Extras, goes through the most popular Libgdx extensions, so the reader can make use of their enhancements. It covers a wide range of topics such as lighting, skeletal animation, localization, and entity systems.

Chapter 12, Performance and Optimizations, provides the readers with advice to achieve their performance targets. This becomes crucial when targeting mobile devices and browsers.

Chapter 13, Giving Back, lays out the process of publishing a finished application as well as contributing to the main Libgdx repository.

What you need for this book

As a minimum, you need a computer running Windows, Mac, or a Linux distribution that also supports hardware acceleration. No need to worry; currently, even the most considerably old machines support this. Additionally, to target Android devices, a phone or a tablet running Android 2.2 or higher is required. In order to target iOS, a Mac with XCode is needed, and when testing on a real device, an Apple developer subscription becomes a must.

All software dependencies are free to use even for commercial purposes. However, the Apple developer license is subject to yearly payments.

Installation instructions for additional software will be provided when needed.

Who this book is for

This book is aimed at developers who are already familiar with object-oriented programming principles, know the basics of game development, and want to use the power of Libgdx to make awesome cross-platform games without the hassle of having to deal with platform-specific nonsense.

Although Libgdx is based on Java, having experience with the language is only recommended and not a must. It can easily be learned as you go, even more so when coming from C++ or C#.

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 action takes place within the overridden processEntity() method responsible for the logic"

A block of code is set as follows:

public class DesktopResolver implements PlatformResolver {
   @Override
   public void rateGame() {
      System.out.println("Desktop");
      Gdx.net.openURI("https://facebook.com");
   }
}

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 String getCurrentLanguage() {
return currentLanguage;
}
public void setCurrentLanguage(String name) {
if(languages.containsKey(name.toLowerCase()))
currentLanguage = name;
}

Any command-line input or output is written as follows:

tar –xzvf android-sdk.r22.2.1-linux.tgz

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: "Install the Gradle IDE entry and restart Eclipse for the last time."

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

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/7291OS_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. 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 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.