Tải bản đầy đủ (.pdf) (468 trang)

410 windows mobile game development

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.6 MB, 468 trang )

www.it-ebooks.info
BOOKS FOR PROFESSIONALS BY PROFESSIONALS ®

THE EXPERT’S VOICE ® IN .NET
Companion
eBook
Available

Building Games for the Windows Phone
and other Mobile Devices
Dear Reader,
In Windows Mobile Game Development I give you everything you need to allow
yourself to maximize your own creativity and bring both yourself and the world
some fantastic mobile gaming opportunities. Just think about how a gaming
device is always in your pocket, as a phone always is... it’s too good an opportunity to miss, so I’ll show you how to create the games you want to make.
I’ll guide you from your first Windows Mobile development steps, right
through to you working with advanced graphics techniques involving the
OpenGL ES graphics library. Along the way we’ll cover everything you’ll need to
get the best from your games, including:

Adam Dawes

• input and game control mechanisms
• flexible methods for controlling on-screen objects within your game
• consistent timing to ensure that your game runs at the speed you want
• music and sound effects
There are some key differences between the Windows Mobile devices your
gaming audience are using, so in Windows Mobile Game Development I’ll show
you how to overcome compatibility issues so your games can be available to as
many players as possible on their devices.
Along the way I’ll share with you my passion for gaming on mobile devices,


and I’ll show you how huge the possibilities are for you to create games on the
move.
Adam Dawes

RELATED TITLES

Companion eBook

Windows Mobile Game Development

Windows Mobile Game Development:

SOURCE CODE ONLINE

US $49.99
Shelve in:
.NET
User level:
Beginning – Advanced

Dawes

www.apress.com

Windows Mobile
Game Development
Building Games for the Windows Phone
and other Mobile Devices
Discover the world of 2D and 3D game programming
for Windows Mobile devices using C# or VB.NET


Adam Dawes


www.it-ebooks.info


www.it-ebooks.info

Windows Mobile Game
Development
Building Games for the Windows Phone and
Other Mobile Devices

■■■
Adam Dawes

i


www.it-ebooks.info

Windows Mobile Game Development: Building Games for the Windows Phone and Other Mobile
Devices
Copyright © 2010 by Adam Dawes
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-2928-5
ISBN-13 (electronic): 978-1-4302-2929-2

Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every
occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of
the trademark owner, with no intention of infringement of the trademark.
Publisher and President: Paul Manning
Lead Editor: Jonathan Hassell
Technical Reviewer: Don Sorcinelli
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,
Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes,
Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Coordinating Editor: Debra Kelly
Copy Editor: Heather Lang
Compositor: MacPS, LLC.
Indexer: BIM Indexing & Proofreading Services
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit www.springeronline.com.
For information on translations, please e-mail , 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.


ii


www.it-ebooks.info

For Ritu and Kieran

iii


www.it-ebooks.info

Contents at a Glance

■Contents at a Glance ............................................................................................iv
■Contents ................................................................................................................v
■About the Author ................................................................................................. xv
■About the Technical Reviewer............................................................................ xvi
■Acknowledgments............................................................................................. xvii
■Introduction ..................................................................................................... xviii
Part 1: Windows Mobile Development......................................................................1
■Chapter 1: Windows Mobile and .NET ...................................................................3
■Chapter 2: Mastering the User Interface .............................................................29
Part 2: Creating Games ..........................................................................................49
■Chapter 3: GDI Graphics ......................................................................................51
■Chapter 4: Taming the Device with the Game Engine .........................................77
■Chapter 5: Timing to Perfection ........................................................................111
■Chapter 6: Exploring User Input ........................................................................129
■Chapter 7: Sounding Out with Game Audio .......................................................157
■Chapter 8: Game in Focus: GemDrops...............................................................179

■Chapter 9: Common Game Components............................................................225
Part 3: OpenGL ES Graphics..................................................................................243
■Chapter 10: A New Window on the World with OpenGL ES ...............................245
■Chapter 11: Creating 2D Games with OpenGL ...................................................289
■Chapter 12: The Ins and Outs of the Third Dimension.......................................311
■Chapter 13: Further OpenGL Features and Techniques .....................................353
Part 4: Distribution ...............................................................................................383
■Chapter 14: Distributing Your Game .................................................................385
■Index .................................................................................................................411

iv


www.it-ebooks.info

Contents
■Contents at a Glance ............................................................................................iv
■Contents ................................................................................................................v
■About the Author ................................................................................................. xv
■About the Technical Reviewer............................................................................ xvi
■Acknowledgments............................................................................................. xvii
■Introduction...................................................................................................... xviii
Part 1: Windows Mobile Development......................................................................1
■Chapter 1: Windows Mobile and .NET ...................................................................3
Looking Closely at .NET for Windows Mobile.................................................................. 4
Language Choices..................................................................................................................................4
The .NET Runtime Libraries....................................................................................................................5
IDE Features...........................................................................................................................................5
Preparing for Windows Mobile Development Challenges ............................................... 6
Numerous Windows Mobile Versions and Editions ................................................................................6

Hardware Considerations.......................................................................................................................8
Using Visual Studio for Windows Mobile Development................................................. 10
Installing Visual Studio.........................................................................................................................10
Creating a Windows Mobile Project .....................................................................................................11
Designing a Form .................................................................................................................................14
Running the Application.......................................................................................................................16
Working with the Emulators.................................................................................................................18
Targeting Different Platforms...............................................................................................................19
Running on a Real Device ....................................................................................................................20
Debugging............................................................................................................................................22
Getting Help .........................................................................................................................................24
v


www.it-ebooks.info
■ CONTENTS

Windows Mobile Game Development ........................................................................... 25
Suitable Games ....................................................................................................................................25
Graphics APIs .......................................................................................................................................25
Technologies Used in This book...........................................................................................................27
Welcome to the World of Windows Mobile Development ............................................. 27
■Chapter 2: Mastering the User Interface .............................................................29
Developing for Touch Screen and Smart Phone Devices.............................................. 29
The User Interface Controls .......................................................................................... 31
Forms ...................................................................................................................................................31
Labels...................................................................................................................................................35
Buttons.................................................................................................................................................35
Menu Bars............................................................................................................................................36
Context Menus .....................................................................................................................................39

Timers ..................................................................................................................................................40
File Dialog Controls ..............................................................................................................................42
Input Panels .........................................................................................................................................43
Capturing Camera Images....................................................................................................................45
The “Busy” Cursor ........................................................................................................ 46
On with the Game ......................................................................................................... 47
Part 2: Creating Games...........................................................................................49
■Chapter 3: GDI Graphics ......................................................................................51
All About GDI ................................................................................................................. 51
Let’s Paint ..................................................................................................................... 51
Invalidating a Form ..............................................................................................................................53
The Drawing Coordinate System..........................................................................................................53
Colors ...................................................................................................................................................53
Pens and Brushes ................................................................................................................................55
Drawing Lines ......................................................................................................................................56
Drawing Polygons ................................................................................................................................57
Drawing Rectangles.............................................................................................................................59
Drawing Ellipses ..................................................................................................................................59
Working with Pixels .............................................................................................................................60
vi


www.it-ebooks.info
■ CONTENTS

Displaying Text.....................................................................................................................................60
Clearing the Background......................................................................................................................63
Painting in Action .................................................................................................................................63

Bitmaps......................................................................................................................... 64

Creating Bitmaps Using Graphics Primitives........................................................................................64
Creating Bitmaps from Predrawn Graphics..........................................................................................65
Painting Bitmaps to the Screen ...........................................................................................................69
Bitmaps in Action .................................................................................................................................72
Smooth Animation......................................................................................................... 72
Getting the Most From GDI............................................................................................ 76
■Chapter 4: Taming the Device with the Game Engine .........................................77
Designing the Game Engine.......................................................................................... 77
Implementing the Engine .............................................................................................. 79
CGameEngineBase ...............................................................................................................................79
CGameObjectBase................................................................................................................................85
CGameObjectGDIBase ..........................................................................................................................87
CGameEngineGDIBase .........................................................................................................................88
CGameFunctions ..................................................................................................................................90
Using the Game Engine................................................................................................. 91
Creating the Bounce Example Game....................................................................................................91
Optimizing Rendering.................................................................................................... 98
Adding, Updating, and Deleting Objects.............................................................................................102
Forcing a Repaint ...............................................................................................................................102
Performance Impact...........................................................................................................................103
Other Engine Features ................................................................................................ 103
Interacting with the Device ................................................................................................................103
Checking Device Capabilities.............................................................................................................106
Future Enhancements ........................................................................................................................109
Next Steps................................................................................................................... 109
■Chapter 5: Timing to Perfection ........................................................................111
Affirming the Need for Consistent Timing................................................................... 111
Processor Speed ................................................................................................................................111
vii



www.it-ebooks.info
■ CONTENTS

Graphics Performance .......................................................................................................................112
Multitasking .......................................................................................................................................112
Processing and Graphical Complexity................................................................................................112
Development Mode vs. Released Code..............................................................................................112

Overcoming Performance Inconsistencies.................................................................. 112
Fixed Interval Updates .......................................................................................................................113
Dynamic Update Intervals ..................................................................................................................113
Interpolated Updates..........................................................................................................................115
Using an External Timer.............................................................................................. 120
DateTime.Now....................................................................................................................................120
Environment.TickCount ......................................................................................................................120
The High-Performance Timer.............................................................................................................120
Timing in the Game Engine ......................................................................................... 121
Initializing and Interrogating the Timer..............................................................................................121
Changes to the Interpolation-Based Functions ..................................................................................124
Changes to the Noninterpolation Functions .......................................................................................128
Using the Game Engine......................................................................................................................128
Let’s Bounce Again ..................................................................................................... 128
■Chapter 6: Exploring User Input ........................................................................129
Touch Screen Input..................................................................................................... 129
Touch Screen Events .........................................................................................................................129
Selecting, Dragging, and Swiping......................................................................................................132
Adding Context Menus .......................................................................................................................144
Using Finger-Friendly Input................................................................................................................146
Using Multitouch Input? .....................................................................................................................147

Using Button and Keyboard Input ............................................................................... 147
Button and Keyboard Events..............................................................................................................147
Reading the Keyboard State ..............................................................................................................149
Input from the SIP ..............................................................................................................................150
Choosing the Keyboard Input Method ................................................................................................150
Reading From an Accelerometer ................................................................................ 151
Initializing the Accelerometer ............................................................................................................151
viii


www.it-ebooks.info
■ CONTENTS

Reading Data from the Accelerometer...............................................................................................152
Detecting the Presence of a Supported Accelerometer .....................................................................154
Supporting Devices With No Accelerometer ......................................................................................156

Considering Input Design............................................................................................ 156
■Chapter 7: Sounding Out with Game Audio .......................................................157
Understanding Sound File Types ................................................................................ 157
Exploring the Available Sound APIs ............................................................................ 158
Using the Sound APIs.................................................................................................. 160
PlaySound ..........................................................................................................................................160
System.Media.SoundPlayer ...............................................................................................................164
AygShell Sound Functions .................................................................................................................167
BASS.dll .............................................................................................................................................169
Adding Support for Sounds to the Game Engine......................................................... 176
Choosing a Sound API ................................................................................................. 177
Make Some Noise ....................................................................................................... 177
■Chapter 8: Game in Focus: GemDrops ...............................................................179

Designing the Game.................................................................................................... 179
Creating the GemDrops Design Brief .................................................................................................179
Conceptualizing the Game Controls ...................................................................................................182
Choosing the Sound Effects ...............................................................................................................183
Outlining the Minimum Requirements ...............................................................................................183
Writing the Game ........................................................................................................ 184
Creating the Project ...........................................................................................................................184
Creating the Game Form ....................................................................................................................185
Preparing the Game ...........................................................................................................................186
Creating the Gem Game Object..........................................................................................................188
Resetting the Game............................................................................................................................191
Pausing the Game ..............................................................................................................................194
Displaying the Player Gems ...............................................................................................................194
Updating the Player’s Gems...............................................................................................................200
Adding Player Control.........................................................................................................................208
Removing Gems from the Board ........................................................................................................215
ix


www.it-ebooks.info
■ CONTENTS

Creating Score Objects ......................................................................................................................221

Finishing Up ................................................................................................................ 224
■Chapter 9: Common Game Components ............................................................225
Managing Game Settings............................................................................................ 225
Using the Settings Class ....................................................................................................................226
Understanding How the CSettings Class Works.................................................................................228
Replacing the MessageBox......................................................................................... 230

Using the MessageBox Class .............................................................................................................230
Understanding How the CMessageBox Class Works .........................................................................231
Creating a High Score Table ....................................................................................... 232
Using the High Score Class ................................................................................................................232
Understanding How the CHighScores Class Works............................................................................235
Creating an About Box ................................................................................................ 238
Using the About Box Class .................................................................................................................239
Understanding How the CAboutBox Class Works...............................................................................241
Using Common Game Components............................................................................. 241
Part 3: OpenGL ES Graphics..................................................................................243
■Chapter 10: A New Window on the World with OpenGL ES ...............................245
Preparing to Use OpenGL ............................................................................................ 245
Hardware Support..............................................................................................................................245
Language Support..............................................................................................................................246
Understanding the OpenGL Features .................................................................................................246
Rendering in 3D .................................................................................................................................247
Using OpenGL in Visual Studio.NET............................................................................. 248
Calling OpenGL from Managed Languages ........................................................................................248
Understanding OpenGL’s Rendering Approach ..................................................................................249
Considering the Hardware Capabilities and Limitations ....................................................................249
Closing OpenGL Applications .............................................................................................................250
Creating an OpenGL Program...................................................................................... 250
Configuring the Project ......................................................................................................................250
Creating the OpenGL Environment .....................................................................................................252
Initializing OpenGL .............................................................................................................................254
x


www.it-ebooks.info
■ CONTENTS


Rendering Graphics in OpenGL ..........................................................................................................256
Adding Form Functions ......................................................................................................................260
Terminating OpenGL ..........................................................................................................................261
Running the Program .........................................................................................................................262
Adding Some Sparkle.........................................................................................................................263

Using Matrix Transformations..................................................................................... 264
Setting the Identity Matrix..................................................................................................................264
Applying Translation Transformations ...............................................................................................265
Applying Rotation Transformations....................................................................................................265
Applying Scaling Transformations .....................................................................................................266
Applying Multiple Transformations ....................................................................................................267
Specifying Vertex Positions................................................................................................................269
Pushing and Popping the Matrix ........................................................................................................269
Practicing Matrix Transformations with Example Projects ................................................................269
Drawing Functions ...................................................................................................... 274
Drawing Points...................................................................................................................................274
Drawing Lines ....................................................................................................................................274
Drawing Triangles ..............................................................................................................................275
Using Texture Mapping ............................................................................................... 277
Loading Graphics ...............................................................................................................................277
Rendering with Textures....................................................................................................................278
Specifying Texture Coordinates .........................................................................................................280
Cleaning Up........................................................................................................................................282
Using Transparency and Alpha Blending .................................................................... 282
Applying Transparency ......................................................................................................................282
Alpha Blending ...................................................................................................................................283
Alpha Blending with Textures ............................................................................................................284
Knowing the Available Blending Factors............................................................................................285

Understanding Orthographic Coordinate Systems ...................................................... 286
Taking Control of OpenGL ........................................................................................... 288
■Chapter 11: Creating 2D Games with OpenGL ...................................................289
Adding OpenGL to the Game Engine ........................................................................... 289
xi


www.it-ebooks.info
■ CONTENTS

Understanding the CGameEngineOpenGLBase Class.........................................................................290
Understanding the CGameObjectOpenGLBase Class .........................................................................292
Performing the Capabilities Check.....................................................................................................294
Creating the Game Form ....................................................................................................................296

Using the OpenGL Game Engine ................................................................................. 298
Preparing the Balloons Game.............................................................................................................298
Setting up the Projection Matrix ........................................................................................................299
Rendering the Balloons......................................................................................................................301
Sorting the Balloons...........................................................................................................................303
Playing the Game ...............................................................................................................................306
2D Possibilities with OpenGL ...................................................................................... 309
■Chapter 12: The Ins and Outs of the Third Dimension.......................................311
Understanding Perspective Projection........................................................................ 311
Understanding the Viewing Frustum..................................................................................................311
Defining the Viewing Frustum in OpenGL ..........................................................................................315
Understanding the Depth Buffer ................................................................................. 316
Enabling the Depth Buffer..................................................................................................................316
Rendering Transparent Objects with the Depth Buffer ......................................................................318
Rendering 3D Objects ................................................................................................. 318

Defining a 3D Object ..........................................................................................................................318
Removing Hidden Surfaces................................................................................................................324
Using Indexed Triangles.....................................................................................................................326
Lighting Your Projects................................................................................................. 329
Introducing the Lights and Materials .................................................................................................329
Exploring the Types of Illumination....................................................................................................329
Using Material Properties...................................................................................................................332
Exploring Light and Material Interaction ............................................................................................333
Using Multiple Lights .........................................................................................................................333
Reusing Lights ...................................................................................................................................334
Exploring the Types of Light Source ..................................................................................................334
Calculating Light Reflections in OpenGL ............................................................................................336
Adding Light to the Game Engine.......................................................................................................340
xii


www.it-ebooks.info
■ CONTENTS

Calculating Normals Programmatically..............................................................................................347
Using Normals with Scaled Objects ...................................................................................................351

Mastering the 3D World .............................................................................................. 352
■Chapter 13: Further OpenGL Features and Techniques .....................................353
Importing Geometry .................................................................................................... 353
Introducing SketchUp.........................................................................................................................353
Using the .0bj File Format ..................................................................................................................357
Importing Geometry into the Game Engine ........................................................................................359
Moving the Camera..................................................................................................... 364
Positioning the Camera......................................................................................................................364

Adding Camera Objects to the Game Engine .....................................................................................365
Lights, Camera, Action!......................................................................................................................370
Optimizing the Camera Calculation....................................................................................................371
Cameras and the Projection Matrix....................................................................................................374
Rendering Fog............................................................................................................. 374
Adding Fog Support to the Game Engine ...........................................................................................375
Using Fog ...........................................................................................................................................375
Working with Billboards.............................................................................................. 377
Rendering Billboards..........................................................................................................................377
Adding Billboard Support to the Game Engine...................................................................................379
Learning More about OpenGL ES ................................................................................ 381
Part 4: Distribution ...............................................................................................383
■Chapter 14: Distributing Your Game..................................................................385
Preparing a Game for Distribution .............................................................................. 385
Settings the Assembly Properties ......................................................................................................385
Project Versioning ..............................................................................................................................387
Creating an Icon .................................................................................................................................388
Building Distribution Packages ................................................................................... 391
Switching into Release Mode.............................................................................................................391
Creating the Setup Project .................................................................................................................392
Adding the Setup Project’s Files ........................................................................................................393
Creating a Programs Menu Shortcut..................................................................................................395
xiii


www.it-ebooks.info
■ CONTENTS

Building the CAB File..........................................................................................................................397


Selling Games ............................................................................................................. 397
Creating Evaluation Applications .......................................................................................................397
Upgrading to Full Versions .................................................................................................................398
Using Windows Marketplace for Mobile ............................................................................................400
Minimizing Piracy...............................................................................................................................401
Implementing Reverse Engineering ............................................................................ 402
Obfuscating with Dotfuscator Community Edition .............................................................................403
Using Advanced Obfuscation .............................................................................................................405
Adding Obfuscated Files to CAB Setup Projects ................................................................................407
Releasing New Versions of Your Game....................................................................... 407
Promoting Your Game ................................................................................................. 409
Go Create! ................................................................................................................... 410
■Index ........................................................................................................................ 411

xiv


www.it-ebooks.info
■ CONTENTS

About the Author

■ Adam Dawes is a software developer and systems architect working at a cuttingedge online service development company.
He has been a compulsive programmer since the age of four, when he was first
introduced to a monochrome Commodore PET. The love affair has continued
through three subsequent decades, flourishing through the days of the 8-bit
dinosaurs to today’s era of multicore processors and pocket supercomputers.
A constant for all of this time has been Adam’s fondness for computer games.
From the very first time Nightmare Park displayed its devious maze of pathways in
green symbols back in 1980, he has been a game player across a variety of genres

and styles. These days, he spends his spare time playing the latest 3D titles on his
PC or enjoying some of the classics in his stand-up arcade machine or sit-in cockpit driving cabinet.
Creating his own games has always been a hobby, and while he has no intention of becoming part of the
professional games industry, he has a lot of fun developing his own titles nonetheless.
Photograph copyright ©
Dave Parker, 2009

Adam lives with his wife, Ritu, and son, Kieran, in the southeast of England. His web site is at
www.adamdawes.com (and all of his finished projects can be downloaded there), and he can be
e-mailed at He would particularly like to see the results of your own game
development projects.

xv


www.it-ebooks.info
■ CONTENTS

About the Technical Reviewer

■ Don Sorcinelli has been involved with planning, developing, and deploying enterprise applications
for more than 15 years. His involvement in these processes expanded to include the PDA platforms
starting in the late 1990s. He is currently a Mobility Consultant for Enterprise Mobile in Watertown, MA,
where he works regularly with large enterprises on all aspects of mobility, including the design and
development of Windows Mobile line of business applications.
Don frequently presents on Windows Mobile topics for users, developers, and IT professionals. As a
result, he was awarded Most Valuable Professional status for Windows Mobile Devices by Microsoft
Corporation in January 2004 for his work with the Windows Mobile community.
Don currently is co-manager of the Boston/New England Windows Mobile User and Developer Group,
and webmaster of BostonPocketPC.com (). He can be contacted at



xvi


www.it-ebooks.info
■ CONTENTS

Acknowledgments
I must start by thanking my parents for all of the opportunities they gave me when I was growing up and
for encouraging my computer habit from a very young age.
Thank you to everyone at Apress for your assistance in getting this book written and delivered, in
particular to Mark Beckner for allowing me the opportunity in the first place and to Debra Kelly for her
tireless assistance and encouragement.
And finally, of course, huge thanks to my wife, Ritu, and my son, Kieran, for tolerating me shutting
myself in my study and writing every evening and weekend—I'll be spending much more time with you
both now; I promise!

xvii


www.it-ebooks.info
■ CONTENTS

Introduction
Goal of This Book
Gaming on the move has become very popular over recent years. With the arrival of the Nintendo
Gameboy, people realized that they could take their games with them, and as technology has become more
sophisticated these games have grown too, encompassing complex game mechanics, advanced 2D and 3D
graphics, and engrossing stories and game worlds that the player can literally become lost within.

Alongside these game improvements is the explosion in popularity of mobile communication
devices. Nearly everyone carries a phone with every time they leave the house. These devices have
become much more than just phones however; they provide contact management, e-mail, Web
browsing, satellite navigation, and entertainment.
Writing games for mobile devices allows both of these trends to be brought together into the same
place. It is very easy for people to pick up and play games on mobile devices, as they always have the
devices in their pockets. Whether they are progressing through a sprawling role-playing game on a train
or simply want a few minutes casual diversion waiting for an appointment, mobile gaming can provide.
This book aims to bring you the knowledge and techniques that you will need to create your own
games for Windows Mobile and Windows Phone classic devices. Starting with the basics of the platform
and its development environment and progressing through to advanced topics such as 3D graphics, this
book will guide you step by step toward creating a simple and manageable environment into which you
can write your own mobile games and distribute them to the world for fun or profit. Example projects
are provided to demonstrate all of the techniques discussed, and are ideal as a basis for experimentation.
It can be difficult to cater for the diversity of hardware available running Windows Mobile. This
book will show you how to create games that work on the largest possible array of devices, catering for
different screen resolutions, devices with and without touch screens, and accommodating all sorts of
other hardware capabilities that your games may need to work with.

Who This Book Is For
This book is written for those who are already familiar with programming one of the two main managed
Visual Studio languages, C# or Visual Basic.NET. It is assumed that you already have a grasp of the
fundamentals of programming and are familiar with using the environment for PC-based application
development. This book is not an introduction to programming or to Visual Studio itself.
You will, however, be given a complete guide to setting up the development environment for
Windows Mobile programming, getting your first programs to compile, and interactively debugging your
games as they run either on the Windows Mobile emulators included with Visual Studio or on a real
device.
To develop software for your device, you will need access to either Visual Studio 2005 Standard or
Visual Studio 2008 Professional. While many of the projects in this book can be developed using the

Windows Mobile emulators, I strongly recommended that you do have access to a real device to test
your games.

xviii


www.it-ebooks.info
■ CONTENTS

For developing games using OpenGL, as discussed toward the end of the book, you will need a
device that has OpenGL hardware acceleration available, as no emulators currently offer this. Most
recent devices do have OpenGL support—check the Internet if you are unsure.
The examples in this book are all written using C#, but the vast majority are capable of being
converted to VB.NET without any problems. Tips and advice for VB.NET programmers are offered within
the text, and workarounds are provided for the few cases where a direct VB.NET conversion is not
available.

Chapter Overview
The following is a brief description of each chapter. The chapters tend to build on one another, so I
recommend that you read them in sequence to avoid knowledge gaps in later chapters.
Chapter 1 introduces Windows Mobile and using the Visual Studio development environment to
create Windows Mobile games applications. It covers some of the different hardware configurations that
you may need to work with and explains how to set up simple .NET Compact Framework projects
running against the emulators and hardware devices.
Chapter 2 explores the user interface, explaining how to use forms and controls, menus, and timers
as well as more specialized subjects such as capturing pictures using the camera.
Chapter 3 introduces the first game development concepts, exploring the Graphics Device Interface
(GDI) graphics system. While the GDI is fairly primitive in its abilities, it is still capable of producing
interesting and playable games and works across all Windows Mobile devices, and the mechanisms for
creating these are investigated.

Chapter 4 starts to build a reusable game engine that will provide simplification for lots of the
features that we need to use to make complex and flexible games. It provides a simple mechanism for
creating lots of independent and interdependent objects within a game environment and optimizes the
GDI rendering process to get games running as fast as possible.
Chapter 5 shows how the timing of games can be made consistent across all devices, regardless of
their speed, graphical capabilities, or processor load from other parts of the system. The speed of
animation is made entirely predictable without any loss of flexibility or fluidity.
Chapter 6 covers the subject of user input. All sorts of input devices are available on Windows
Mobile devices, from touch screens and keyboards through to accelerometers, and all of these are
explored in detail to show how they can be used to allow your games to be controlled.
Chapter 7 turns up the volume and reveals the options for game audio, covering simple sound
effects to MP3 and music playback. Everything you need to know about sound for your games can be
found here.
Chapter 8 combines everything that has been covered so far into a full working game called
GemDrops. Featuring colorful graphics, a variety of control mechanisms for different device capabilities,
screen resolution independence, sound effects and music, the whole game is built step by step to show
how an actual game can be developed.
Chapter 9 provides a series of reusable components that may be used in any game. A simple
mechanism for loading and saving user settings, a message presentation window, a flexible high score
table, and an application information page are all provided to allow you to focus on writing your game
rather than having to reinvent these features yourself.
Chapter 10 opens the door to the world of OpenGL for Embedded Systems (OpenGL ES) graphics
programming. Beginning by exploring the concepts and mechanisms behind OpenGL ES and comparing
and contrasting these to GDI, everything you need to initialize an OpenGL ES environment and present
colorful texture-mapped graphics can be found here.
Chapter 11 integrates the OpenGL ES features from Chapter 10 into the game engine, providing a
series of reusable functions to simplify OpenGL ES game development. The focus of this chapter is using
the game engine for 2D graphics, exploring the features that are opened up in this area by OpenGL ES
beyond those provided by GDI.


xix


www.it-ebooks.info
■ CONTENTS

Chapter 12 lifts up the OpenGL ES feature set into the third dimension, explaining how to create 3D
game worlds. Subjects covered include perspective, the depth buffer, and lighting so that your scenes
really come to life.
Chapter 13 continues the exploration of OpenGL ES in the third dimension and introduces a
number of useful new features to the game engine. These include importing 3D objects and third-party
modeling packages, moving and manipulating the cameras within a game world, and applying fog to a
3D scene.
Chapter 14 wraps up everything with tips and techniques for distributing your game to the world,
covering subjects such as version control, creating installation packages, registration code systems,
reverse engineering, and promotion of your game.

xx


www.it-ebooks.info

PART 1
■■■

Windows Mobile
Development


www.it-ebooks.info


2


www.it-ebooks.info

CHAPTER 1
■■■

Windows Mobile and .NET

It is a genuine pleasure to develop software for Windows Mobile devices using Visual Studio .NET.
For a substantial part of its lifetime, programming for Microsoft’s mobile operating system involved
using the suite of eMbedded Visual Tools. These came supporting two different languages: eMbedded
Visual Basic and eMbedded Visual C++.
eMbedded Visual Basic was based on the same technologies as Visual Basic for Applications (VBA).
This was similar in a number of ways to Visual Basic 6 (VB6), the desktop version of VB that was current
at the time, but with many shortcomings, such as the lack of strongly typed variables and poor object
orientation features. Programs were written using a stand-alone IDE, which had its own peculiarities
and different ways of working to VB6 itself.
eMbedded Visual C++ presented more of a challenge because of differences not only in the IDE but
in the code too. While established C++ programmers would no doubt have managed to pick up this
language without too many problems, those less-well-versed in the intricacies of C++ would have found
that the amount of new information they needed to learn proved a significant barrier to entry.
All of this changed with the release of Visual Studio .NET and the .NET Compact Framework (.NET CF).
.NET CF provides a set of class libraries that are parallel to the desktop .NET Framework. The
libraries are not identical, as large parts of the full .NET Framework functionality are missing from .NET
CF. However, a substantial set of identical functionality does exist, and any programmer who is
comfortable developing C# or Visual Basic .NET applications for Windows will be instantly at home
developing for Windows Mobile too.

The .NET Framework has met resistance from some quarters in the Windows desktop world. While
various versions of the framework come preinstalled with recent versions of Windows, getting users to
accept .NET as a requirement of an application can still be difficult. Fortunately, there seems to be no
such reluctance to install .NET CF on Windows Mobile devices, perhaps in part due to the huge amount
of software that requires it in order to run.
A major advantage of developing for Windows Mobile using Visual Studio .NET is that the exact
same IDE is used as for Windows desktop development. There no need to learn the details or keyboard
shortcuts of a new IDE: instead, you will be working within the environment you are already used to,
which includes all of your user interface tweaks and preferences changes. Developing an application for
Windows Mobile is simply a question of creating a different project type.
Programming within Visual Studio .NET also means that the Windows Mobile developer is able to
take advantage of the maturity of the Visual Studio.NET development environment. Microsoft has
spent many years improving the user interfaces and functionality of Visual Studio, and countless
versions and releases have cumulated in an extremely powerful and user-friendly studio for
application design, development, and debugging. All of this is at your disposal when developing
Windows Mobile applications.

3


×