Developing Games in Java™

Book description

If you already have Java programming experience and are looking to program games, this book is for you. David Brackeen, along with co-authors Bret Barker and Lawrence Vanhelsuwe, show you how to make fast, full-screen action games such as side scrollers and 3D shooters. Key features covered in this book include Java 2 game programming techniques, including latest 2D graphics and sound technologies, 3D graphics and scene management, path-finding and artificial intelligence, collision detection, game scripting using BeanShell, and multi-player game engine creation.

Table of contents

  1. Copyright
    1. Dedication
  2. Preface
    1. About the Author
    2. About the Contributing Authors
    3. About the Technical Reviewers
    4. Acknowledgments
      1. Tell Us What You Think
  3. Introduction
    1. Why Java?
    2. What You Need
    3. What Is in This Book
    4. What's Not in This Book
    5. About the Code in This Book
    6. Using Ant
    7. Summary
  4. I. Java Game Fundamentals
    1. 1. Java Threads
      1. What Is a Thread?
      2. Creating and Running Threads in Java
        1. Extending the Thread Class
        2. Implementing the Runnable Interface
        3. Using Anonymous Inner Classes
        4. Waiting for a Thread to Finish
        5. Sleepy Threads
      3. Synchronization
        1. Why Synchronize?
        2. How to Synchronize
        3. What to Synchronize
        4. What Not to Synchronize
        5. Avoiding Deadlock
      4. Using wait() and notify()
      5. The Java Event Model
      6. When to Use Threads
      7. When Not to Use Threads
      8. Sum It Up: Thread Pools
      9. Summary
    2. 2. 2D Graphics and Animation
      1. Full-Screen Graphics
        1. Screen Layout
        2. Pixel Color and Bit Depth
        3. Refresh Rate
        4. Switching the Display to Full-Screen Mode
        5. Anti-Aliasing
        6. Which Display Mode to Use
      2. Images
        1. Transparency
        2. File Formats
        3. Reading Images
        4. Hardware-Accelerated Images
        5. Image-Drawing Benchmarks
        6. Animation
        7. Active Rendering
        8. The Animation Loop
      3. Getting Rid of Flicker and Tearing
        1. Double Buffering
        2. Page Flipping
        3. Monitor Refresh and Tearing
        4. The BufferStrategy Class
        5. Creating a Screen Manager
        6. Sprites
      4. Simple Effects
        1. Image Transforms
      5. Summary
    3. 3. Interactivity and User Interfaces
      1. The AWT Event Model
      2. Keyboard Input
      3. Mouse Input
      4. Mouselook-Style Mouse Movement
        1. Hiding the Cursor
      5. Creating an Input Manager
      6. Using the Input Manager
        1. Pausing the Game
        2. Adding Gravity
      7. Designing Intuitive User Interfaces
        1. User Interface Design Tips
      8. Using Swing Components
        1. Swing Basics
        2. Tricking Swing to Work in Full-Screen Mode
      9. Creating a Simple Menu
      10. Letting the Player Configure the Keyboard
      11. Summary
    4. 4. Sound Effects and Music
      1. Sound Basics
      2. The Java Sound API
        1. Opening a Sound File
        2. Using a Line
      3. Playing a Sound
      4. Creating a Real-Time Sound Filter Architecture
      5. Creating a Real-Time Echo Filter
      6. Emulating 3D Sound
        1. The Idea Behind Creating a 3D Filter
        2. Implementing a 3D Filter
        3. Trying Out the 3D Filter
      7. Creating a Sound Manager
        1. The Sound Class
        2. The SoundManager Class
        3. Thread-Local Variables
      8. Playing Music
        1. Playing CD Audio
        2. Playing MP3 and Ogg Vorbis
        3. Playing MIDI Music
        4. Creating Adaptive Music
      9. Summary
    5. 5. Creating a 2D Platform Game
      1. Creating a Tile-Based Map
        1. Implementing the Tile Map
        2. Loading Tile Maps
        3. Drawing Tile Maps
        4. Drawing Sprites
        5. Parallax Scrolling
        6. Power-Ups
        7. Simple Baddies
          1. Animations
          2. Creature Class
      2. Collision Detection
        1. Detecting a Collision
        2. Handling a Collision
        3. Sprite Collisions
      3. Finishing Things Up and Making It Fast
      4. Creating an Executable .jar File
      5. Ideas to Expand the Game
      6. Summary
    6. 6. Multi-Player Games
      1. The Revolution in Java's I/O Libraries
        1. Overview of the JDK 1.4 NIO Libraries
        2. Channels
          1. Interfaces
          2. Classes
            1. ServerSocketChannel
            2. SocketChannel
            3. DatagramChannel
        3. Buffers
          1. Classes
          2. Filling and Draining
          3. Manipulating Buffers
          4. Direct Versus Nondirect
        4. Selectors and SelectionKeys
      2. ChatterBox, A Basic Multi-Player Application
        1. The Server: ChatterServer
          1. Initial Setup
          2. The Main Loop
          3. Accepting Connections
          4. Writing Messages
          5. Reading Messages
          6. Disconnections
        2. Building and Running the Server
          1. Connecting with Telnet
        3. The Client: ChatterClient
          1. Setup
          2. Making the Connection
          3. The Main Loop
          4. Sending and Receiving Messages
        4. Building and Running the Client
      3. Multi-Player Game Server Framework
        1. Design Goals and Tactics
        2. The Design
          1. GameEvents
            1. Serialized Java Objects
            2. XML
            3. Custom Binary
            4. Binary-Encoded XML
          2. Terminology
          3. Class Diagrams
        3. Common Classes and Interfaces
          1. GameEvent Interface
          2. GameEventDefault
          3. Over-the-Wire Event Protocol
        4. Server Implementation
          1. GameServer and GameController
          2. SelectAndRead
          3. GameControllers
          4. EventWriter
        5. The Client
      4. A Sample Game: RPS (Rock, Paper, Scissors)
        1. Classes
          1. RPSController
          2. RPSGame
          3. RPSClient
          4. RPSConsoleEventReader
        2. Running the RPS Game
      5. Complete the Look: Building on the Framework
        1. Client GUI
        2. Persistence
        3. Buddy Lists, Lobbies, and Chat
      6. Server Administration
        1. Logging
        2. Startup/Shutdown
          1. Java Shutdown Hooks
        3. Server Admin Consoles
        4. Game Watching
      7. Advanced Topics
        1. Disconnects and Reconnects
          1. Ping Events
          2. The Reaper
        2. HTTP Tunneling
          1. Option 1: Combo-Server
          2. Option 2: URL-Based Load Balancing
        3. Testing with Bots
        4. Those Pesky Modems
        5. Profiling and Performance Stats
        6. Performance Tweaks
          1. The Evil Trash Collector
          2. Object Reuse
          3. Other Tweaks
      8. Summary
  5. II. 3D Graphics and Advanced Techniques
    1. 7. 3D Graphics
      1. Types of 3D Rendering
      2. Don't Forget Your Math
        1. Trigonometry and Right Triangles
        2. Vector Math
      3. 3D Basics
      4. 3D Math
      5. Polygons
      6. 3D Transforms
        1. Rotations
        2. Encapsulating Rotation and Translation Transforms
        3. Applying Transforms
        4. Rotation Order
      7. A Simple 3D Pipeline
      8. Camera Movement
      9. Solid Objects and Back-Face Removal
        1. The Dot Product
        2. The Cross Product
        3. More About the Dot and Cross Products
      10. Scan-Converting Polygons
        1. Optimizing Scan Conversion with Fixed-Point Math
      11. 3D Clipping
      12. Final Rendering Pipeline
      13. Summary
    2. 8. Texture Mapping and Lighting
      1. Perspective-Correct Texture Mapping Basics
        1. Deriving the Texture Mapping Equations
      2. A Simple Texture-Mapper
        1. Problems with the First Texture-Mapper
      3. Optimizing Texture Mapping
        1. Texture Storage
        2. Raw Optimization
        3. Method Inlining
        4. Fast Texture Mapping Demo
      4. Simple Lighting
        1. Diffuse Reflection
        2. Ambient Lighting
        3. Light Source Intensity
        4. Distance Falloff
        5. Implementing a Point Light
      5. Implementing Texture Lighting
      6. Advanced Lighting Using a Shade Map
        1. Finding the Bounding Rectangle
        2. Applying the Shade Map
        3. Building the Shade Map
        4. Building the Surface
        5. Caching Surfaces
        6. Shaded Surface Demo
      7. Additional Concepts
        1. Depth Cueing
        2. Fake Shadows
        3. MIP Mapping
        4. Bilinear Interpolation
        5. Trilinear Interpolation
        6. Normal Maps and Depth Maps
        7. Other Types of Lights
      8. Summary
    3. 9. 3D Objects
      1. Hidden Surface Removal
        1. Painter's Algorithm
        2. Reverse Painter's Algorithm
        3. Z-Buffer
        4. Z-Buffer Using 1/z
        5. Calculating the Z-Depth
      2. 3D Animation
        1. Spatial Movement
        2. Angular Movement
      3. Polygon Groups
        1. Iterating All the Polygons in a Group
      4. Loading Polygon Groups from an OBJ File
        1. The OBJ File Format
        2. The MTL File Format
      5. Game Objects
      6. Managing Game Objects
      7. Putting It All Together
      8. Future Enhancements
      9. Summary
    4. 10. 3D Scene Management Using BSP Trees
      1. BSP Tree Intro
      2. Binary Tree Basics
      3. The One-Dimensional BSP Tree
      4. The Two-Dimensional BSP Tree
        1. BSP Tree-Building Example
        2. BSP Tree-Traversing Example
      5. Implementing a 2D BSP Tree
        1. The BSP Line
        2. Determining the Side of a Point Relative to a Line
        3. The BSP Polygon
        4. Traversing a BSP Tree
        5. In-Order Traversal
        6. Front-to-Back Traversal
        7. Building a Tree
        8. Finding the Intersection of Two Lines
        9. Clipping Polygons by a Line
        10. Removing T-Junction Gaps
        11. Testing the BSP Tree
      6. Drawing Polygons Front to Back
      7. First BSP Example
      8. Drawing Objects in the Scene
      9. Loading Maps from a File
      10. Putting It All Together
      11. Enhancements
      12. Summary
    5. 11. Collision Detection
      1. Collision Basics
      2. Object-to-Object Collisions
        1. Eliminating Tests
        2. Bounding Spheres
        3. Bounding Cylinders
        4. The Discrete Time Issue
      3. Object-to-World Collisions
        1. Bounding Boxes for Testing Against Floors
        2. Finding the BSP Leaf for a Specific Location
        3. Implementing Floor and Ceiling Height Testing
        4. Bounding Boxes for Testing Against Walls
        5. Intersection of a Line Segment with a BSP Tree
        6. The Corner Issue
        7. Implementing Object-to-World Collision Detection
      4. Basic Collision-Detection Demo
      5. Collision Handling with Sliding
        1. Object-to-Object Sliding
        2. Object-to-Wall Sliding
        3. Gravity and Sliding Up Stairs (Object-to-Floor Sliding)
        4. Make It Jump
      6. Collision Detection with Sliding Demo
      7. Enhancements
      8. Summary
    6. 12. Path Finding
      1. Path-Finding Basics
      2. Some Initial Path-Finding Attempts
        1. Breadth-First Search
      3. Basics of the A* Algorithm
      4. Applying the A* Algorithm
      5. Using the A* Algorithm with a BSP Tree
        1. Portals
        2. Implementing Portals
      6. Generic Path Finding
      7. Making a PathBot
      8. Enhancing the A* Search
      9. Summary
    7. 13. Artificial Intelligence
      1. AI Basics
      2. Take Away Those Godlike Powers!
        1. Seeing
        2. Hearing
      3. State Machines and Reacting
      4. Probability Machines
        1. Some Useful Random Functions
      5. Making Decisions
      6. Patterns
        1. Dodging
        2. Attacking
        3. Running Away
        4. Aiming
        5. Firing
      7. Object Spawning
      8. Putting It All Together
        1. Brains!
        2. Health and Dying
        3. Adding a Heads-Up Display
      9. Evolution
        1. Regeneration
        2. Evolving Bots
        3. Demo Enhancements
      10. Other Game AI Ideas
        1. Team AI
      11. Summary
    8. 14. Game Scripting
      1. Scripting Cookbook: What You Need
      2. Implementing Touch and Release Notifications
        1. Triggers
      3. Game Object Listeners
      4. Scripting
        1. Designing the Script
        2. Embedding BeanShell
      5. Delayed Events
        1. Creating Delayed Events in BeanShell
      6. Putting It All Together
      7. Enhancements
      8. Summary
    9. 15. Persistence—Saving the Game
      1. Game-Saving Basics
      2. Using Java's Serialization API for Game State Persistence
        1. Introduction to Serialization
        2. Serialization: The Basics
          1. A Simple Serialization Example
        3. Serialization: The Rules
          1. Core Classes Support for Serialization
        4. Serialization: The Pitfalls
          1. No Deserialization of Unknown Objects
          2. Nonserializable Object Graphs
          3. Serializing Arrays
          4. Class Evolution and Versioning
        5. Overriding Default Serialization Engine Behavior
          1. Adding Private writeObject() or readObject() Methods
          2. Explicitly Specifying Which Fields Should be Serialized
      3. Creating Game Screen Snapshots
        1. Creating Screen Thumbnail Images
        2. Saving an Image
      4. Saving Games to the Right Destination
      5. Summary
  6. III. Tuning and Finishing Your Game
    1. 16. Optimization Techniques
      1. Optimization Rules
      2. Profiling
        1. Benchmarking
        2. Using the HotSpot Profiler
      3. HotSpot
        1. Java-Specific Optimizations
        2. Dead Code Elimination
        3. Loop Invariant Hoisting
        4. Common Subexpression Elimination
        5. Constant Propagation
        6. Loop Unrolling (Server VM Only)
        7. Method Inlining
      4. Optimization Tricks
        1. Algorithms
        2. Strength Reduction: Bit Shifting
        3. Strength Reduction: Modulus
        4. Strength Reduction: Multiplication
        5. Strength Reduction: Exponentiation
        6. More Loop Invariant Hoisting
        7. Lookup Tables
        8. Fixed-Point Math
        9. Exceptions
        10. Input/Output
        11. Memory-Mapped Files
      5. Memory Usage and the Garbage Collector
        1. The Java Heap and Garbage Collection
        2. Monitoring Garbage Collection
        3. Monitoring Memory Usage
        4. Tuning the Heap
        5. Tuning the Garbage Collector
        6. Reducing Object Creation
        7. Object Reuse
        8. Object Pools
      6. Perceived Performance
        1. Timer Resolution
      7. Summary
    2. 17. Creating Game Art and Sounds
      1. Choosing a Look and Feel
        1. Looking for Inspiration
        2. Staying Consistent
      2. Getting Royalty-Free Game Media
      3. Working with Artists and Sound Effect Engineers
      4. Tools
      5. Creating Sounds
        1. Sound File Formats
      6. Creating Textures and Sprites
        1. Graphics File Formats
        2. Creating Seamless Textures
        3. Creating “Alternate” Textures
        4. Creating Transition Textures
        5. Creating Multi-Layer Textures
      7. Creating Splash Screens and HUD Graphics
      8. Creating UI Graphics
        1. Customizing Swing
      9. Creating Your Own Fonts
      10. Summary
    3. 18. Game Design and the Last 10%
      1. The Last 10%
        1. Effects
        2. The Game State Machine
      2. Elements of Game Design
        1. Environments
        2. Story Elements
        3. Game Play
        4. Teaching Users How to Play
      3. Creating a Map Editor
      4. Debugging
        1. Debugging Java2D Issues
        2. Logging
      5. Protecting Code
      6. Game Deployment
      7. Game Deployment with Java Web Start
        1. Retrieving Resources from .jar Files
        2. Signing .jar Files
        3. Creating a JNLP File
        4. Setting Up a Web Server
      8. Game Deployment with Native Compilation
      9. Updates and Patches
      10. Bandwidth Issues
      11. Getting Feedback and Beta Testing
      12. Making Money
      13. Putting It All Together
      14. Summary
    4. 19. The Future
      1. How Java Evolves
        1. Java Community Process
        2. Bug Parade
      2. The Future: Java 1.5 “Tiger”
        1. Generics (JSR 14)
        2. Enumerations (JSR 201)
        3. Static Import (JSR 201)
        4. Enhanced for Loop (JSR 201)
        5. Compiler API (JSR 199)
        6. Network Transfer Format (JSR 200)
        7. Shared VM (JSR 121)
      3. What the Java Platform Needs
        1. Needed: More Options for Mouse and Keyboard Input
        2. Needed: Joystick Input
        3. Needed: Hardware-Accelerated Translucent Images
        4. Needed: Higher Resolution Timer
        5. Needed: Hardware-Accelerated Graphics and Full-Screen on Linux
        6. Needed: Hardware-Accelerated 3D Included with the Runtime
        7. Needed: HotSpot SIMD Optimizations
        8. Desired: More Font-Smoothing Options
        9. Other Possibilities
      4. New Devices and the Java Games Profile (JSR 134)
      5. Summary

Product information

  • Title: Developing Games in Java™
  • Author(s):
  • Release date: August 2003
  • Publisher(s): New Riders
  • ISBN: 9781592730056