Lập trình game trên android

673 734 0
Lập trình game trên android

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

tài liệu Lập trình game trên android

Beginning Android Games ■ ■ ■ Mario Zechner Beginning Android Games Copyright © 2011 by Mario Zechner All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3042-7 ISBN-13 (electronic): 978-1-4302-3043-4 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Robert Green Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Adam Heath Copy Editors: Damon Larson, Jim Compton Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. v Contents Contents at a Glance iv About the Author . xii About the Technical Reviewer . xiii Acknowledgments xiv Introduction . xv ■Chapter 1: Android, the New Kid on the Block 1 A Brief History of Android . 2 Fragmentation . 3 The Role of Google 3 The Android Open Source Project 3 The Android Market . 4 Challenges, Device Seeding, and Google I/O . 6 Android’s Features and Architecture 7 The Kernel 8 The Runtime and Dalvik . 8 System Libraries 9 The Application Framework . 10 The Software Development Kit . 11 The Developer Community 12 Devices, Devices, Devices! . 12 Hardware . 13 First Gen, Second Gen, Next Gen . 14 Mobile Gaming Is Different . 20 A Gaming Machine in Every Pocket . 20 Always Connected . 21 Casual and Hardcore 22 Big Market, Small Developers 22 Summary 23 ■Chapter 2: First Steps with the Android SDK . 25 Setting Up the Development Environment 25 Setting Up the JDK . 26 ■ CONTENTS vi Setting Up the Android SDK . 26 Installing Eclipse 28 Installing the ADT Eclipse Plug-In 28 A Quick Tour of Eclipse 30 Hello World, Android Style 32 Creating the Project . 32 Exploring the Project 33 Writing the Application Code . 35 Running and Debugging Android Applications 38 Connecting a Device 38 Creating an Android Virtual Device 38 Running an Application 39 Debugging an Application 42 LogCat and DDMS 46 Using ADB 48 Summary 49 ■Chapter 3: Game Development 101 . 51 Genres: To Each One’s Taste 51 Causal Games 52 Puzzle Games . 54 Action and Arcade Games 56 Tower-Defense Games 59 Innovation 60 Game Design: The Pen Is Mightier Than the Code 60 Core Game Mechanics . 61 A Story and an Art Style . 63 Screens and Transitions 64 Code: The Nitty-Gritty Details 70 Application and Window Managem ent 71 Input . 72 File I/O 75 Audio 76 Graphics . 80 The Game Framework 94 Summary 101 ■Chapter 4: Android for Game Developers 103 Defining an Android Application: The Manifest File 104 The <manifest> Element . 105 The <application> Element . 105 The <activity> Element . 107 The <uses-permission> Element 109 The <uses-feature> Element . 110 The <uses-sdk> Element 112 Android Game Project Setup in Ten Easy Steps . 112 Defining the Icon of Your Game . 114 Android API Basics 116 Creating a Test Project 116 ■ CONTENTS vii The Activity Life Cycle . 120 Input Device Handling . 127 File Handling 144 Audio Programming . . 150 Playing Sound Effects . 150 Streaming Music . 154 Basic Graphics Programming . 158 Best Practices . 182 Summary 183 ■Chapter 5: An Android Game Development Framework 185 Plan of Attack 185 The AndroidFileIO Class . 186 AndroidAudio, AndroidSound, and AndroidMusic: Crash, Bang, Boom! 187 AndroidInput and AccelerometerHand ler . 192 AccelerometerHandler: Which Side Is Up? . 193 The Pool Class: Because Reuse is Good fo r You! 194 KeyboardHandler: Up, Up, Down, Down, Left, Right . . . . 196 Touch Handlers 200 AndroidInput: The Great Coordinator . . 207 AndroidGraphics and AndroidPixmap: Double Rainbow 209 Handling Different Screen Sizes and Resolu tions . 209 AndroidPixmap: Pixels for the People . 215 AndroidGraphics: Serving Our Drawing Needs . 216 AndroidFastRenderView: Loop, Strech, Loop, Stretch . . 220 AndroidGame: Tying Everything Togeth er . . 223 Summary 227 ■Chapter 6: Mr. Nom Invades Android 229 Creating the Assets . 229 Setting Up the Project . . 232 MrNomGame: The Main Activi ty . . 232 Assets: A Convenient Asset Stor e . 233 Settings: Keeping Track of User Choices and High Scores . 234 LoadingScreen: Fetching the Assets from Disk 236 The Main Menu Scree n . . 237 The HelpScreen Class(es) . . 241 The High-Scores Screen . . 243 Rendering Numbers: An Excursion . 243 Implementing the Screen . . 245 Abstracting… . 247 Abstracting the World of Mr. Nom : Model, View, Controller . 248 The GameScreen Class . 259 Summary 267 ■Chapter 7: OpenGL ES: A Gentle Introduction 269 What Is OpenGL ES and Why Should I Care? . 269 The Programming Model: An Analogy . 270 Projections . 272 Normalized Device Space and the Viewpo rt . 275 ■ CONTENTS viii Matrices . 275 The Rendering Pipeline 276 Before We Begin . 277 GLSurfaceView: Making Things Easy Since 2008 . 278 GLGame: Implementing the Game Interface . 281 Look Mom, I Got a Red Triangle! . 288 Defining the Viewport 288 Defining the Projection Matrix . 289 Specifying Triangles 292 Putting It Together . 296 Specifying Per Vertex Color 300 Texture Mapping: Wallpapering Made Easy 304 Texture Coordinates . 304 Uploading Bitmaps . 306 Texture Filtering . 308 Disposing of Textures 309 A Helpful Snippet . 310 Enabling Texturing . 310 Putting It Together . 310 A Texture Class 313 Indexed Vertices: Because Reuse Is Good for You 315 Putting It Together . 316 A Vertices Class . 318 Alpha Blending: I Can See Through You 321 More Primitives: Points, Lines, Strips, and Fans 325 2D Transformations: Fun with the Model-View Matrix . 326 World and Model Space . 326 Matrices Again . 328 An First Example Using Translation . 329 More Transformations . 333 Optimizing for Performance 338 Measuring Frame Rate . 338 The Curious Case of the Hero on Android 1.5 339 What’s Making My OpenGL ES Rendering So Slow? 340 Removing Unnecessary State Changes . 341 Reducing Texture Size Means Fewer Pixels to Be Fetched . 343 Reducing Calls to OpenGL ES/JNI Methods . 344 The Concept of Binding Vertices 345 In Closing . 348 Summary 349 ■Chapter 8: 2D Game Programming Tricks . 351 Before We Begin . 351 In the Beginning There Was the Vector . 352 Working with Vectors . 353 A Little Trigonometry . 355 Implementing a Vector Class . 357 A Simple Usage Example . 360 A Little Physics in 2D 365 ■ CONTENTS ix Newton and Euler, Best Friends Forever . 365 Force and Mass . 366 Playing Around, Theoretically 367 Playing Around, Practically 368 Collision Detection and Object Representation in 2D 372 Bounding Shapes . 373 Constructing Bounding Shapes 375 Game Object Attributes 377 Broad-Phase and Narrow-Phase Collision Detection . 378 An Elaborate Example 386 A Camera in 2D . 399 The Camera2D Class 402 An Example 403 Texture Atlas: Because Sharing Is Caring . 405 An Example 407 Texture Regions, Sprites, and Batches: Hiding OpenGL ES 411 The TextureRegion Class . 411 The SpriteBatcher Class 412 Sprite Animation . 422 The Animation Class 423 An Example 424 Summary 428 ■Chapter 9: Super Jumper: A 2D OpenGL ES Game . 429 Core Game Mechanics 429 A Backstory and Art Style . 430 Screens and Transitions . 431 Defining the Game World 432 Creating the Assets . 435 The UI Elements . 435 Handling Text with Bitmap Fonts . 437 The Game Elements . 439 Texture Atlas to the Rescue . 441 Music and Sound . 442 Implementing Super Jumper 444 The Assets Class 444 The Settings Class . 447 The Main Activity . 448 The Font Class . 449 GLScreen 451 The Main Menu Screen 451 The Help Screens . 454 The High-Scores Screen 457 The Simulation Classes 459 The Game Screen . 475 The WorldRenderer Class 482 To Optimize or Not to Optimize . 486 Summary 487 ■ CONTENTS x ■Chapter 10: OpenGL ES: Going 3D 489 Before We Begin . 489 Vertices in 3D 490 Vertices3: Storing 3D Positions 490 An Example 492 Perspective Projection: The Closer, the Bigger . 495 Z-buffer: Bringing Order into Chaos 498 Fixing the Last Example . 499 Blending: There’s Nothing Behind You 500 Z-buffer Precision and Z-fighting . 503 Defining 3D Meshes 504 A Cube: Hello World in 3D 505 An Example 508 Matrices and Transformations Again 511 The Matrix Stack 512 Hierarchical Systems with the Matrix Stack 514 A Simple Camera System 520 Summary 524 ■Chapter 11: 3D Programming Tricks . 525 Before We Begin . 525 Vectors in 3D . 526 Lighting in OpenGL ES . 530 How Lighting Works . 530 Light Sources . 532 Materials 533 How OpenGL ES Calculates Lighting: Vertex Normals . 533 In Practice 534 Some Notes on Lighting in OpenGL ES 548 Mipmapping 548 Simple Cameras 553 The First-Person or Euler Camera 553 An Euler Camera Example . 556 A Look-At Camera 562 Loading Models . 564 The Wavefront OBJ Format 565 Implementing an OBJ Loader . 566 Using the OBJ Loader 570 Some Notes on Loading Models 571 A Little Physics in 3D 571 Collision Detection and Object Representation in 3D 572 Bounding Shapes in 3D 572 Bounding Sphere Overlap Testing . 573 GameObject3D and DynamicGameObject3D 574 Summary 576 ■Chapter 12: Droid Invaders: the Grand Finale . 577 Core Game Mechanics 577 A Backstory and Art Style . 579 ■ CONTENTS xi Screens and Transitions . 580 Defining the Game World 581 Creating the Assets . 582 The UI Assets . 582 The Game Assets . 584 Sound and Music . 586 Plan of Attack 587 The Assets Class . 587 The Settings Class 590 The Main Activity 591 The Main Menu Screen . 592 The Settings Screen 595 The Simulation Classes . 598 The Shield Class 598 The Shot Class . 598 The Ship Class . 599 The Invader Class . 601 The World Class . 604 The GameScreen Class . 610 The WorldRender Class . 617 Optimizations 622 Summary 623 ■Chapter 13: Publishing Your Game 625 A Word on Testing . 625 Becoming a Registered Developer 626 Sign Your Game’s APK 627 Putting Your Game on the Market . 631 Uploading Assets . 632 Listing Details 633 Publishing Options . 633 Publish! 634 Marketing . 634 The Developer Console . 634 Summary 636 ■Chapter 14: What’s Next? 637 Getting Social 637 Location Awareness 637 Multiplayer Functionality 638 OpenGL ES 2.0 and More 638 Frameworks and Engines . 638 Resources on the Web 640 Closing Words . 640 Index . 641 iv Contents at a Glance Contents v About the Author . xii About the Technical Reviewer . xiii Acknowledgments xiv Introduction . xv ■Chapter 1: Android, the New Kid on the Block 1 ■Chapter 2: First Steps with the Android SDK . 25 ■Chapter 3: Game Development 101 . 51 ■Chapter 4: Android for Game Developers 103 ■Chapter 5: An Android Game Development Framework 185 ■Chapter 6: Mr. Nom Invades Android 229 ■Chapter 7: OpenGL ES: A Gentle Introduction 269 ■Chapter 8: 2D Game Programming Tricks . 351 ■Chapter 9: Super Jumper: A 2D OpenGL ES Game . 429 ■Chapter 10: OpenGL ES: Going 3D 489 ■Chapter 11: 3D Programming Tricks . 525 ■Chapter 12: Droid Invaders: the Grand Finale . 577 ■Chapter 13: Publishing Your Game 625 ■Chapter 14: What’s Next? 637 Index . 641 . the copyright owner and the publisher. ISBN-13 (pbk): 97 8-1 -4 30 2-3 04 2-7 ISBN-13 (electronic): 97 8-1 -4 30 2-3 04 3-4 Trademarked names, logos, and images may. Beginning Android Games ■ ■ ■ Mario Zechner Beginning Android Games Copyright © 2011 by Mario Zechner All rights reserved.

Ngày đăng: 27/12/2013, 16:45

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan