Libgdx Cross-platform Game Development Cookbook

Book description

Over 75 practical recipes to help you master cross-platform 2D game development using the powerful Libgdx framework

In Detail

Libgdx is a very popular open source, cross-platform, Java-based game development framework that enables you to write your code once and deploy it to Windows, Mac, Linux, Android, iOS, and browsers.

Supported by code samples for each topic, this book will take you through the features of Libgdx, from the very basic aspects to the most advanced ones. Beginning with an overview of the framework and project creation, rich games. You will then explore input detection and audio and file handling, followed by details of how to make use of amazing features such as Box2D rigid body physics, lighting, and artifical intelligence techniques to name a few. You will also discover how to modify Libgdx to suit your needs and share your creation with the world.

What You Will Learn

  • Wield the power of the 2D graphics API; get to grips with textures, atlases, particles, fonts, and shaders
  • Manage input from different devices, including touch, keyboard, mouse, gamepad, and accelerometer
  • Increase player immersion with the Libgdx audio API
  • Quickly design maps with an editor and load them directly into your game
  • Exploit the 2D stage features to build great user interfaces
  • Create amazing physics simulations with Box2D
  • Master the deployment process and reach a wide audience

Table of contents

  1. Libgdx Cross-platform Game Development Cookbook
    1. Table of Contents
    2. Libgdx Cross-platform Game Development Cookbook
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Diving into Libgdx
      1. Introduction
      2. Setting up a cross-platform development environment
        1. Getting ready
        2. How to do it…
          1. Java Development Kit
            1. Windows and Mac users
            2. GNU/Linux users
          2. Eclipse
          3. Android SDK
            1. Mac users
            2. GNU/Linux users
            3. All users
          4. Eclipse plugins
          5. XCode
          6. Fixing character encoding and line endings
          7. Making sure everything is in order
        3. How it works…
        4. There's more…
        5. See also
      3. Creating a cross-platform project
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      4. Understanding the project structure and application life cycle
        1. Getting ready
        2. How to do it…
          1. Using logging to get to know the application life cycle
          2. Starter classes and configuration
            1. Desktop starter
            2. Android starter
            3. HTML starter
            4. iOS starter
        3. How it works…
        4. There's more…
          1. Living comfortably with ApplicationAdapter
          2. Managing a multiscreen application with Game
        5. See also
      5. Updating and managing project dependencies
        1. Getting ready
        2. How to do it…
          1. Gradle build file primer
          2. Updating existing dependencies
          3. Adding Libgdx extensions
            1. Bullet
              1. Core
              2. Desktop
              3. Android
              4. iOS
            2. FreeTypeFont
              1. Core
              2. Desktop
              3. Android
              4. iOS
            3. Controllers
              1. Core
              2. Desktop
              3. Android
              4. HTML5
            4. Box2D
              1. Core
              2. Desktop
              3. Android
              4. iOS
              5. HTML5
            5. Tools
              1. Core
              2. Desktop
            6. AI
              1. Core
              2. Android
              3. HTML
          4. Adding external repositories
          5. Adding additional file dependencies
          6. Managing GWT dependencies
        3. There's more…
        4. See also
      6. Using source control on a Libgdx project with Git
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Basic operations
          2. Git branches
          3. Gitignore files
        4. There's more…
        5. See also
      7. Importing and running the Libgdx official demos
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    9. 2. Working with 2D Graphics
      1. Introduction
      2. Texture rendering with SpriteBatch
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. 2D rendering using 3D techniques
          2. Coordinate system and camera setup
          3. Assets location in Libgdx
          4. Draw order
          5. Texture filters
        4. There's more…
        5. See also
      3. More effective rendering with regions and atlases
        1. Getting ready
        2. How to do it…
          1. Packing PNG files into an atlas
          2. Rendering regions of an atlas
        3. How it works…
          1. Atlas file format
          2. How SpriteBatch works
          3. A note on texture size limitations
        4. There's more…
        5. See also
      4. Taking advantage of Libgdx sprites
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Using bounds for collision detection
        5. See also
      5. Rendering sprite-sheet-based animations
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Using different play modes
          2. Checking when an animation has finished
          3. Handling a character with many animations
        5. See also
      6. Understanding orthographic cameras
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Rendering UI on top of the game world
          2. Split screen
        5. See also
      7. Using ShapeRenderer for debug graphics
        1. Getting ready
        2. How to do it…
          1. Rendering lines
          2. Rendering circles
          3. Rendering rectangles
          4. Rendering points
          5. Rendering triangles
        3. How it works…
        4. There's more…
        5. See also
      8. Handling multiple screen sizes with viewports
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Getting the most out of Viewport
          2. Working with two cameras and viewports
          3. Floating elements
          4. Build your own Viewport implementation
        5. See also
    10. 3. Advanced 2D Graphics
      1. Introduction
      2. Achieving juicy effects with particles
        1. Getting ready
        2. How to do it…
          1. The basics of Particle Editor
          2. Rendering particle effects with Libgdx
        3. How it works…
        4. There's more…
        5. See also
      3. Frame buffers and image composition
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      4. Welcome to the world of shaders
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Data types
          2. Vertex attributes
          3. Uniforms
          4. OpenGL ES precision specifiers
        4. There's more…
          1. The sepia effect
          2. Inverted colors
      5. Passing parameters into shader programs
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Detecting shader compiler errors
          2. More efficient uniform settings
      6. 2D depth of field shader
        1. Getting ready
        2. How to do it…
        3. There's more…
        4. See also
      7. Embedding a Libgdx window into a Java desktop application
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
    11. 4. Detecting User Input
      1. Introduction
      2. Detecting user input via event polling
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Checking input availability
          2. Showing the on-screen keyboard
      3. Detecting user input via event listening
        1. Getting ready
        2. How to do it…
          1. Event polling versus event listening
        3. How it works…
        4. There's more…
      4. Managing multiple listeners at once
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      5. Detecting more complex gestures
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Gesture polling
      6. Introducing the controllers API
        1. Getting ready
        2. How to do it…
          1. Including the controllers extension
          2. Enumerating controllers
          3. Controller state polling
          4. Controller event listening
        3. How it works…
        4. There's more…
          1. Controller mappings
          2. Handling disconnections
        5. See also
      7. Input mapping for cross-platform development
        1. Getting ready
        2. How to do it…
          1. Input contexts
          2. Input profiles
          3. Event notifications with InputActionListener
          4. Input mapping in action
        3. How it works…
        4. There's more…
    12. 5. Audio and File I/O
      1. Introduction
      2. Playing short sound effects
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Handling sounds individually
          2. Changing the volume
          3. Creating looping sounds
          4. Managing effect priorities
        5. See also
      3. Audio streaming for background music
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Checking the playback state
          2. Looping the background music
        5. See also
      4. Optimizing audio files to reduce download sizes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Procedural audio generators
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Engine sounds with dynamic audio
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Controlling direction through panning
          2. Music transitions
        5. See also
      7. The 2D space sound system
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      8. The first approach to file handling in Libgdx
        1. Getting ready
        2. How to do it…
          1. Retrieving basic information from files
          2. Traversing tree structures
          3. Writing to and reading from files
          4. Copying and deleting files and directories
        3. How it works…
        4. There's more…
          1. Creating temporary files
          2. Filtering directory listings
          3. File streaming
        5. See also
      9. Using preferences to store game states and options
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      10. The XML parsing primer
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      11. JSON serialization and deserialization
        1. Getting ready
        2. How to do it…
          1. Reading objects from JSON
          2. Writing objects to JSON
          3. Manual JSON parsing
        3. How it works…
        4. There's more…
          1. The Serializable interface
          2. The Serializer interface
        5. See also
    13. 6. Font Rendering
      1. Introduction
      2. Generating and rendering bitmap fonts
        1. Getting ready
        2. How to do it…
          1. Using Hiero to generate bitmap font files
          2. Rendering bitmap font files with Libgdx
        3. How it works…
        4. There's more…
        5. See also
      3. Baking effects into fonts using Hiero
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      4. Scaling friendly font rendering with distance fields
        1. Getting ready
        2. How to do it…
          1. Generating distance field fonts with Hiero
          2. Distance field fonts shader
          3. Rendering distance field fonts in Libgdx
        3. How it works…
        4. There's more…
      5. Dynamic font effects using distance fields
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    14. 7. Asset Management
      1. Introduction
      2. Getting to know AssetManager
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Asynchronous asset loading
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      4. Creating custom asset loaders
        1. Getting ready
        2. How to do it…
          1. Writing our own asset type
          2. Writing our own asset loader
          3. Summarizing...
        3. How it works…
        4. There's more…
        5. See also
      5. Managing groups of assets for bigger games
        1. Getting ready
        2. How to do it…
          1. Giving shape to the idea
          2. Empowering AssetManager
        3. How it works…
        4. There's more…
        5. See also
    15. 8. User Interfaces with Scene2D
      1. Introduction
      2. Introducing the Scene2D API
        1. Getting ready
        2. How to do it…
          1. Creating a custom actor
          2. Rendering your custom actor
          3. Manipulating an actor
            1. Simple actions
            2. Complex actions
        3. How it works…
        4. There's more…
        5. See also
      3. Widget collection overview
        1. Getting ready
        2. How to do it…
          1. Label
          2. Image
          3. Button
          4. TextButton
          5. ImageButton
          6. ImageTextButton
          7. CheckBox
          8. TextField
          9. List
          10. ScrollPane
          11. SelectBox
          12. ProgressBar
          13. Slider
          14. Touchpad
          15. Stack
          16. Tree
          17. SplitPane
          18. Window
          19. Dialog
        3. How it works…
        4. There's more…
        5. See also
      4. Creating a basic menu screen using tables
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Expand
          2. Fill
          3. Uniform
          4. Sizing
          5. Padding and spacing
          6. Alignment
          7. Columns and rows
          8. Defaults
        4. There's more…
        5. See also
      5. Skin customization
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      6. Creating new widgets
        1. Getting ready
        2. How to do it…
          1. Creating a widget
          2. Using a widget
        3. How it works…
        4. There's more…
    16. 9. The 2D Maps API
      1. Introduction
      2. Creating maps with Tiled and loading them into Libgdx
        1. Getting ready
        2. How to do it…
          1. Creating a basic map with Tiled
          2. Loading and rendering a Tiled map in Libgdx
        3. How it works…
        4. There's more…
      3. Adding and querying map metadata
        1. Getting ready
        2. How to do it…
          1. Object layers and properties in Tiled
          2. Querying map metadata from Libgdx
        3. How it works…
        4. There's more…
        5. See also
      4. Developing your own map loaders and renderers
        1. Getting ready
        2. How to do it…
        3. There's more…
    17. 10. Rigid Body Physics with Box2D
      1. Introduction
      2. Introducing Box2D
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      3. Introducing more complex shapes
        1. Getting ready
        2. How to do it…
          1. Generating JSON shape data
          2. Loading JSON shape data into Libgdx
        3. How it works…
        4. There's more…
        5. See also
      4. Introducing joints
        1. Getting ready
        2. How to do it…
          1. WeldJoint
          2. DistanceJoint
          3. RopeJoint
          4. FrictionJoint
          5. RevoluteJoint
          6. PrismaticJoint
          7. PulleyJoint
          8. GearJoint
          9. WheelJoint
        3. There's more…
        4. See also
      5. Real-life joints example – bike simulator
        1. Getting ready
        2. How to do it…
        3. There's more…
      6. Reacting to collisions
        1. Getting ready
        2. How to do it…

        3. How it works…
        4. There's more…
        5. See also
      7. Sensors and collision filtering
        1. Getting ready
        2. How to do it…
          1. Sensors
          2. Group collision filtering
          3. Flexible collision filtering
        3. How it works…
        4. There's more…
        5. See also
      8. Querying the world
        1. Getting ready
        2. How to do it…
          1. Raycasting
          2. Area querying
        3. How it works…
        4. There's more…
        5. See also
      9. Building a physics world from level data
        1. Getting ready
        2. How to do it…
          1. Defining materials
          2. Generating the physics metadata
          3. Populating your world
        3. How it works…
        4. There's more…
        5. See also
      10. Implementing a deferred raycaster
        1. Getting ready
        2. How to do it…
          1. The RayCastManager class
          2. Using RayCastManager
        3. How it works…
        4. There's more…
        5. See also
      11. The fixed timestep approach
        1. Getting ready
        2. How to do it…
          1. Variable timestep
          2. Fixed timestep
          3. Fixed timestep with interpolation
        3. There's more…
        4. See also
    18. 11. Third-party Libraries and Extras
      1. Introduction
      2. Making libraries compatible with GWT
        1. Getting ready
        2. How to do it…
        3. There's more…
        4. See also
      3. Managing platform-specific code
        1. Getting ready
        2. How to do it…
        3. There's more…
      4. Smooth animations with Universal Tween Engine
        1. Getting ready
        2. How to do it…
          1. Creating your accessor
          2. Library usage
            1. Tween – a core class
            2. Organizing tweens
        3. How it works…
      5. Dynamic 2D lighting with Box2DLights
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. RayHandler
          2. Types of lights
        4. There's more…
      6. Adding support for localization
        1. Getting ready
        2. How to do it…
          1. Creating language files
          2. Managing languages
          3. Usage example
        3. There's more…
        4. See also
      7. Finite state machine and messaging
        1. Getting ready
        2. How to do it…
          1. Creating the agents
          2. Usage example
        3. How it works…
        4. There's more…
      8. Component-based entity systems with Ashley
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      9. Skeletal animations with Spine
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    19. 12. Performance and Optimizations
      1. Introduction
      2. Profiling your application with VisualVM
        1. Getting ready
        2. How to do it…
          1. Installing VisualVM
          2. Changing the launcher
          3. Monitoring and profiling
        3. How it works…
        4. There's more…
      3. Using Libgdx features to avoid garbage collection
        1. Getting ready
        2. How to do it…
          1. Collections
          2. Caching
          3. Object pooling
        3. There's more…
      4. Avoiding unnecessary render calls with frustum culling
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    20. 13. Giving Back
      1. Introduction
      2. Releasing your Libgdx game on desktop
        1. Getting ready
        2. How to do it…
        3. There's more…
        4. See also
      3. Releasing your Libgdx game on Android
        1. Getting ready
        2. How to do it…
        3. There's more…
      4. Releasing your Libgdx game on iOS
        1. Getting ready
        2. How to do it…
        3. There's more…
      5. Releasing your Libgdx game on browsers
        1. Getting ready
        2. How to do it…
        3. There's more…
      6. Working from sources
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. See also
      7. Creating a new test for your fresh feature
        1. Getting ready
        2. How to do it…
      8. Sending a pull request on GitHub
        1. Getting ready
        2. How to do it…
        3. There's more…
        4. See also
    21. Index

Product information

  • Title: Libgdx Cross-platform Game Development Cookbook
  • Author(s): David Saltares Márquez, Alberto Cejas Sánchez
  • Release date: October 2014
  • Publisher(s): Packt Publishing
  • ISBN: 9781783287291