Preface

Writing a game can be a daunting task. Even if you’re an experienced programmer, the design patterns, terminology, and thought processes can seem strange and unusual. Having spent most of my working life creating business applications, writing games has been a hobby that has seen me create many games my children have played and enjoyed over the years. With the release of the iPhone and iPod touch, it was time to unleash one of my creations on the world.

My first task was to find a good book on developing games on the iPhone. After a lot of research, I decided that the book I wanted just didn’t exist, and having had great feedback on a number of online tutorials I had created, I decided to write my own book. This was a perfect opportunity for me to create the game programming book I’ve always wanted myself.

Over the years, I’ve read many game development books and have been left wanting. Although they provide information on the individual components required to make a game and include small examples, they never go all the way to creating a complete game good enough to publish. I’ve always believed that a good book should both tell the reader what is required to make a game but also demonstrate how those components can be implemented inside a complete game project.

So, this book not only describes the components and technology needed to create a game on the iPhone, but it does so through the creation of a complete game: Sir Lamorak’s Quest: The Spell of Release. This game is currently available for free download from the App Store, and is the game you learn how to build as you work your way through this book.

Download the Game!

You can download Sir Lamorak’s Quest from the App Store:
http://itunes.apple.com/us/app/sir-lamoraks-quest-the-spell/id368507448?mt=8. The game is freely available, so go ahead and download the game, start playing around with it, and help Sir Lamorak escape from the castle!

This book describes the key components needed to create this 2D game. It covers both the technology, such as OpenGL ES and OpenAL, as well as the key game engine components required, including sprite sheets, animation, touch input, and sound.

Each chapter describes in detail a specific component within the game, along with the technology required to support it, be it a tile map editor, or some effect we’re trying to create with OpenGL ES. Once an introduction to the functionality and technology is complete, the chapter then provides details on how the component has been implemented within Sir Lamorak’s Quest. This combination of theory and real-world implementation helps to fill the void left by other game development books.

About Sir Lamorak’s Quest

My game-playing experiences started when I was given a Sinclair Spectrum 48k for Christmas in 1982. I was hooked from that moment, and I have had a close relationship with computers ever since.

While thinking about the game I wanted to develop for this book, my mind kept wandering back to the games I played in the 1980s. They may not have been visually stunning, although at the time I was impressed, but they were fun to play.

I spent some time working on the design of the game, which included not only the features I wanted in the game, but also how it should be implemented on the iPhone. One key aspect of the game is that it should be casual—that is, the concept of the game should be simple and easy to pick up, and players should be able to start and stop the game easily without losing their progress.

I also wanted the controls to be easily recognizable and therefore decided to implement an onscreen joypad to control the main character. It was important, though, to allow the player to swap the position of this joypad so that both left- and right-handed players found the game comfortable.

As for the game play itself, I decided to take a number of design ideas from games I played in the ‘80s and went with a top-down scroller, in which the player is trapped in a haunted castle and has to find a magic spell so that he can escape.

Organization of This Book

There are 16 chapters in the book, each of which deals with a specific area of creating Sir Lamorak’s Quest, as follows:

Chapter 1, “Game Design”—This chapter describes the design considerations I made while designing Sir Lamorak’s Quest. It provides an insight into the kind of thought process required when sitting down to create a game. It doesn’t cover every possible design decision needed for all genres of games, but it does cover the important ones.

Chapter 2, “The Three Ts: Terminology, Technology, and Tools”—Even experienced programmers can become confused by the three Ts used within game development. This chapter runs through the common technology, terminology, and tools used to create Sir Lamorak’s Quest and games in general. This chapter helps you understand the terms and technology covered throughout the book.

Chapter 3, “The Journey Begins”—This is where we start to get our hands on some code and get the iPhone to render something to the screen. This chapter covers the process of creating our first project using the OpenGL ES template project within Xcode. The template is described in detail and sets the scene for the chapters that follow.

Chapter 4, “The Game Loop”—The heartbeat of any game is the game loop. This loop is responsible for making sure that all the core elements of the game, such as AI and rendering, are done at the right time and in the right order. This may sound simple, but there are a number of different approaches to the game loop, and this chapter discusses them and details the approach taken for Sir Lamorak’s Quest.

Chapter 5, “Image Rendering”—Drawing images to the screen is a fundamental requirement for any game. This chapter provides an overview of OpenGL ES and runs through a number of classes created to simplify the creation and rendering of images to the screen.

Chapter 6, “Sprite Sheets”—Sprite sheets are images that contain a number of smaller images. These sheets can be used to reduce the number of individual images held in memory and the number of different textures OpenGL ES needs to bind to improving performance. They are also commonly used when creating animated sprites. This chapter covers how to create sprite sheets that contain the images used in the game, regardless of whether they have fixed or variable dimensions.

Chapter 7, “Animation”—Having created the means to store the different frames needed in an animation using sprite sheets, this chapter describes how separate images can be played in sequence to provide you with animation, such as the player character running.

Chapter 8, “Bitmap Fonts”—The most common way to interact with your game’s user is through the use of text. Being able to render instructions and information (such as the player’s score or instructions on how to use the game) is important. This chapter describes how you can use open source tools to take any font and turn it into a bitmap font. Once the bitmap font is created, you’ll see how to create a sprite sheet that contains all the images needed to render the characters in that font. It also details the Bitmap font class used in Sir Lamorak’s Quest, which provides a simple API for rendering text to the screen.

Chapter 9, “Tile Maps”—Tile maps allow large game worlds to be created from reusing a small number of tile images. This common approach has been used in the past to create large game worlds (think of the original Super Mario Brothers game for Nintendo) when memory is limited, back in the early days of home game systems. This technique is still popular today, and this chapter describes the use of an open source tile-editing tool to create tile maps, along with a class that can render these maps to the screen.

Chapter 10, “The Particle Emitter”—Many games have impressive effects, such as fire, explosions, smoke, and sparks. These are created using a particle system. The particle system is responsible for creating and controlling a number of particles; each has its own properties, such as size, shape, direction, color, and lifespan. During a particle’s life cycle, its position, speed, color, and size are changed based on the particle’s configuration. This chapter details how to create a particle system that can be used to generate any number of organic effects.

Chapter 11, “Sound”—Giving the player feedback using sound is important in today’s modern games. This chapter describes how the media player functionality of the iPhone, along with OpenAL, can be used to play a cool soundtrack in the game, as well as 3D (surround) sound effects.

Chapter 12, “User Input”—This chapter describes how to use the iPhone’s unique touch and accelerometer capabilities to control your game. It details how to capture and process multiple touches at the same time and also how data from the accelerometer can be used within your own games.

Chapter 13, “The Game Interface”—In this chapter, we start to look at how the game interface for Sir Lamorak’s Quest was implemented. This includes how to deal rotation events to make sure that the user interface is always oriented correctly. It also describes how to mix both OpenGL ES and UIKit interface controls.

Chapter 14, “Game Objects and Entities”—As the player runs around the castle in Sir Lamorak’s Quest, we want him to be able to find objects, pick them up, and fight baddies. This chapter describes how objects and entities have been implemented within Sir Lamorak’s Quest.

Chapter 15, “Collision Detection”—Having the player and baddies run through walls and doors would really spoil the game, so it’s important to be able to register collisions between either the player and the map or objects and entities within the castle. This chapter describes different types of collision detection and how this has been implemented within Sir Lamorak’s Quest.

Chapter 16, “Pulling It All Together”—At this point, a great deal of ground has been covered. There is, however, a number of things you can do to the game to add polish. This chapter covers how to save the player’s game state for when he quits or leaves the game when he has an incoming call. Chapter 16 also covers performance tuning using instruments and tips for getting your game beta tested.

Audience for This Book

This book has been written for people who are already programmers but who have never written computer games before. Although it assumes that you already have some experience with Objective-C, each chapter provides enough information on both Objective-C and other technologies so you can follow the concepts and implementations.

By the time you complete this book, you will have an in-depth understanding of the game engine that was built for Sir Lamorak’s Quest and the key capabilities and considerations are needed to create a 2D game engine. This enables you to take the same game engine developed in this book and use it in your own games, or simply use the knowledge you have gained about creating games in general and use one of the many game engines available for the iPhone, such as Cocos2D.

Who This Book Is For

If you are already developing applications for the iPhone for other platforms, but want to make a move from utility applications to games, this book is for you. It builds on the development knowledge you already have and leads you into game development by describing the terminology, technology, and tools required, as well as providing real-world implementation examples.

Who This Book Isn’t For

If you already have a grasp of the workflow required to create a game or you have a firm game idea that you know requires OpenGL ES for 3D graphics, this is not the book for you.

It is expected that before you read this book, you are already familiar with Objective-C, C, Xcode, and Interface Builder. Although the implementations described in this book have been kept as simple as possible and the use of C is limited, a firm foundation in these languages is required.

The following titles can help provide you with the grounding you need to work through this book:

Cocoa Programming for Mac OS X, Third Edition, by Aaron Hillegass (Addison-Wesley, 2008).

Learning Objective-C 2.0, by Robert Clair (Addison-Wesley, 2011).

Programming in Objective-C 2.0, by Stephen G. Kochan (Addison-Wesley, 2009).

Cocoa Design Patterns, by Erik M. Buck and Donald A. Yacktman (Addison-Wesley, 2009).

The iPhone Developer’s Cookbook, Second Edition, by Erica Sadun (Addison-Wesley, 2010).

Core Animation: Simplified Animation Techniques for Mac and iPhone Development, by Marcus Zarra and Matt Long (Addison-Wesley, 2010).

iPhone Programming: The Big Nerd Ranch Guide, by Aaron Hillegass and Joe Conway (Big Nerd Ranch, Inc., 2010).

These books, along with other resources you’ll find on the web, will help you learn more about how to program for the Mac and iPhone, giving you a deeper knowledge about the Objective-C language and the Cocoa frameworks.

Download the Source Code

Access to information is not only limited to the book. The complete, fully commented source code to Sir Lamorak’s Quest is also available for download on InformIT.com.

There is plenty of code to review throughout this book, along with exercises for you to try out, so it is assumed you have access to the Apple developer tools, such as Xcode and the iPhone SDK. Both of these can be downloaded from the Apple iPhone Dev Center.2

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.107.31