ebook img

J2ME game programming PDF

794 Pages·2004·6.064 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview J2ME game programming

©2004 by Premier Press,a division ofCourse Technology.All rights reserved.No SVP,Course Professional, part ofthis book may be reproduced or transmitted in any form or by any means, Trade,Reference Group: electronic or mechanical,including photocopying,recording,or by any informa- AndyShafran tion storage or retrieval system without written permission from Course PTR, Publisher: except for the inclusion ofbriefquotations in a review. Stacy L.Hiquet The Premier Press logo and related trade dress are trademarks ofPremier Press Senior Marketing Manager: and may not be used without written permission. Sarah O’Donnell J2ME is a trademark of Sun Microsystems,Inc.in the United States and other Marketing Manager: countries.Photoshop and Acrobat are registered trademarks of Adobe Systems Heather Hurley Incorporated in the United States and other countries.Paint Shop Pro is a regis- tered trademark of Jasc Software in the United States and other countries. Manager ofEditorial Services: SourceForge is a trademark ofVA Software Corporation.RetroGuard is a trade- Heather Talbot mark ofRetrologic Systems in the United States and other countries.The MathFP package can only be used for non-commercial purposes.Permission to use the Acquisitions Editor: Nokia Developer’s Suite 2.0 for J2ME,Nokia 7210 MIDP SDK 1.0,and,Nokia Mitzi Koontz 3510i MIDP SDK 1.0,have been granted by Nokia,Inc.(c) 2004.Nokia,Inc.All Associate Marketing Manager: rights reserved. Kristin Eisenzopf All other trademarks are the propertyoftheir respective owners. Project Editor: Important:Course PTR cannot provide software support.Please contact the appro- Jenny Davidson priate software manufacturer’s technical support line or Web site for assistance. Technical Reviewer: Course PTR and the author have attempted throughout this book to distinguish Cristiano Garibaldi proprietary trademarks from descriptive terms by following the capitalization Retail Market Coordinator: style used by the manufacturer. Sarah Dubois Information contained in this book has been obtained by Course PTR from sources Copy Editor: believed to be reliable.However,because ofthe possibility ofhuman or mechani- Cathleen Snyder cal error by our sources,Course PTR,or others,the Publisher does not guarantee the accuracy,adequacy,or completeness ofany information and is not responsible Interior Layout Tech: for any errors or omissions or the results obtained from use ofsuch information. Marian Hartsough Readers should be particularly aware ofthe fact that the Internet is an ever-chang- ing entity.Some facts may have changed since this book went to press. Cover Designer: Steve Deschene Educational facilities,companies,and organizations interested in multiple copies or licensing ofthis book should contact the publisher for quantity discount infor- CD-ROM Producer: mation.Training manuals,CD-ROMs,and portions ofthis book are also avail- Brandon Penticuff able individually or can be tailored for specific needs. Indexer: ISBN:1-59200-118-1 Sharon Shock Library ofCongress Catalog Card Number:2003109341 Proofreader: Printed in the United States ofAmerica Sara Gullion Course PTR,a division ofCourse Technology 25 Thomson Place Boston,MA 02210 http://www.courseptr.com For G.I. Acknowledgments I f you’ve never written a book,I have to tell you that it’s not easy.Once upon a time I was an armchair critic of books—in hindsight,maybe that’s why I decided to write one.The thing that strikes you when you embark on writing something like this is the sheer mountain of(hard) work involved.In fact,it’s so much work that it really isn’t pos- sible for one person to do it alone.Throughout the writing ofthis book,I’ve been helped in so many ways by so many people that I can’t hope to get across how grateful I am.All I can do is say a thank you and trust you to know how much I mean it. I will,however,attempt to recognize some ofthe people who deserve a little acknowledg- ment—if I’ve forgotten you,then obviously you didn’t deserve it....:) First, thanks to André LaMothe for giving me the opportunity to contribute to such an excellent series.To Mitzi Koontz,Jenny Davidson,and Cathleen Snyder for the excellent feedback,continual support,and understanding when things didn’t quite go according to plan—we got there in the end. Thanks also to Cristiano Garibaldi for covering all the technical bases (and learning along with me). To Colin Pyle and J.Alexander von Kotze: Thanks for never giving up on the dream of making games for a living (and an extra thanks to Colin for supplying the excellent sprites used in the examples). ToBlake,for allowing me to use his laptop sometimes,and to Ryan,for showing me how a CD drive opens a thousand times. To Vandana and Pratibha Rai for buying me some time (and thanks for the math books laaa).To Scott,Rhandy,Simon,Rob,Gibbo,Mike, Jules,Kristy,Kat,Lee,and the rest of the team for keeping the day job challenging,inter- esting,and fun. vi Acknowledgments vii To Tarek,Sahar,Radfan,and Suleiman for the encouragement and support only friends can give. ToRick,my only mentor,for showing me that computing really is a science. To my mum,for showing me that being creative is a way of life and dad for throwing in regular doses of reality. And finally,to the one and only G.I.: Thanks for believing from day one we could do it, and then bearing the brunt of following through on that belief. About the Author MARTINJ.WELLS began programming his own games on a Tandy micro-computer more than 20 years ago. Throughout an extensive career in the IT industry, he has worked in many diverse fields involving a huge variety of computer languages and systems,includ- ing Java from its origins. He has extensive experience in media, communications, and entertainment industry development and has founded successful companies in all ofthese areas. Martin lives with his wife and two sons in Sydney,Australia. He loves playing soccer and inline hockey,reading,and playing with anything cool and interesting (including his sons). About the Series Editor ANDRÉLAMOTHE,CEO,Xtreme Games LLC,has been involved in the computing indus- try for more than 25 years.He wrote his first game for the TRS-80 and has been hooked ever since! His experience includes 2D/3D graphics, AI research at NASA, compiler design, robotics, virtual reality, and telecommunications. His books are top sellers in the game programming genre, and his experience is echoed in the Premier Press Game Developmentbooks. viii Letter from the Series Editor Writing games for PCs is fun,but it just doesn’t have the feel ofa console or other hand- held device.However,the thought ofcreating an embedded game for a phone was com- pletely out of the question a few years ago, unless you wanted to call up Nokia or Motorola and see if you could have the contract to create the on-board games.(I wish someone would have; they are terrible!) Anyway,luckily for us,new phones support a number of technologies that allow programmers to create fantastic applications. One such technology is Java II Micro Edition, or J2ME. And that’s what this book is all about—writing games for any phone that supports the J2ME standard. When I first thought ofdoing a book on J2ME game programming,I knew that I wanted it to push the envelope to set a new standard on what can be done on a phone. That means I had to find an author who was an expert on the platform,but was also willing to push limits and do the impossible,in a manner of speaking.I have to say that I am veryhappy with this book.The author,Martin Wells,had the same vision about want- ing to create the most amazing book on phone/J2ME game programming.For example, he knew that he had to put a chapter on 3D in the book and talk about optimization and other advanced topics.The bottom line is that this book is the best book on the market about making real games on the J2ME platform;moreover,it’s written by someone who has made numerous games on the platform.Marty knows the ins and outs and tricks of the system, which is invaluable in such a complex subject area with so many other choices to confuse you. The other amazing thing about this book is that it is completely self-contained; if you don’t know Java 2,there is a Java 2 primer contained within,so more or less all you need is your phone,the book,and some time and you are going to be creating J2ME games on your own phone! I think that this is an amazing thing to be able to do.It’s like having your own little game console in your hand.You can play your own games or give them toyour friends,or possibly even sell and market them (which is also covered within the book). Inconclusion,if you have been interested in writing games for phones under the J2ME platform, but don’t know where to start, how to integrate all the technology, or make sense of all the different APIs, then this is the book for you. Rarely can a single book empower someone to do so much,but Martin Wells has done an amazing job ofit. André LaMothe Series Editor,Premier Game DevelopmentSeries Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxv Part I What Is J2ME? . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1 J2ME History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Chapter 2 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Chapter 3 J2ME-Enabled Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 Part II Show Me the Code! . . . . . . . . . . . . . . . . .47 Chapter 4 The Development Environment . . . . . . . . . . . . . . . . . . . . . .49 Chapter 5 The J2ME API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Chapter 6 Device-Specific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Chapter 7 Game Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Part III Game On . . . . . . . . . . . . . . . . . . . . . . . .245 Chapter 8 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 Chapter 9 The Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .269 x Contents at a Glance xi Chapter 10 The Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 Chapter 11 The World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .367 Chapter 12 The Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .433 Chapter 13 The Front End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .465 Chapter 14 The Device Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .493 Chapter 15 The Optimizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .519 Chapter 16 The Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .535 Part IV Sell, Sell, Sell . . . . . . . . . . . . . . . . . .541 Chapter 17 Marketing Material . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .543 Chapter 18 Sales Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .553 Part V What Next? . . . . . . . . . . . . . . . . . . . . . .569 Chapter 19 CLDC 1.1 and MIDP 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . .571 Chapter 20 Isometric Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .621 Chapter 21 Ray Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .639 Chapter 22 Making the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . .669 Appendix A Java 2 Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .693 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .738 Contents Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Part I What Is J2ME? . . . . . . . . . . . . . . . . . . . . . .1 Chapter 1 J2ME History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Java’s Acorn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Java’s Growth in the Sun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 So What Is Java? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Multiple Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Micro Devices Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Micro Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12 Chapter 2 J2ME Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 I Shall Call It Mini-ME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 J2ME Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Configurations and Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 Two Sizes Fit All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 MIDP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 Target Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 MIDP Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 MIDP 1.0 Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 MIDP 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 xii

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.