Linux Game Programming Wcd Prima Techs Game Development 1st Edition Mark Collins download
Linux Game Programming Wcd Prima Techs Game Development 1st Edition Mark Collins download
https://ebookbell.com/product/linux-game-programming-wcd-prima-
techs-game-development-1st-edition-mark-collins-11359432
https://ebookbell.com/product/programming-linux-games-1st-edition-
loki-software-john-r-hall-922874
Cross Platform Game Development Make Pc Games For Windows Linux And
Mac Wordware Game Developers Library 1st Edition Alan Thorn
https://ebookbell.com/product/cross-platform-game-development-make-pc-
games-for-windows-linux-and-mac-wordware-game-developers-library-1st-
edition-alan-thorn-1320158
Minecraft The Unlikely Tale Of Markus Notch Persson And The Game That
Changed Everything Daniel Goldberg Linus Larsson Translated From The
Swedish By Jennifer Hawkins
https://ebookbell.com/product/minecraft-the-unlikely-tale-of-markus-
notch-persson-and-the-game-that-changed-everything-daniel-goldberg-
linus-larsson-translated-from-the-swedish-by-jennifer-hawkins-53017170
The State Of Play Creators And Critics On Video Game Culture Daniel
Goldberg
https://ebookbell.com/product/the-state-of-play-creators-and-critics-
on-video-game-culture-daniel-goldberg-48904176
Linux Command Line And Shell Scripting Bible 4th Edition Richard Blum
https://ebookbell.com/product/linux-command-line-and-shell-scripting-
bible-4th-edition-richard-blum-46090968
https://ebookbell.com/product/linux-the-complete-reference-sixth-
edition-6th-richard-petersen-46145398
https://ebookbell.com/product/linux-basics-for-hackers-
occupytheweb-46410142
https://ebookbell.com/product/linux-allinone-for-dummies-7th-richard-
blum-46498774
https://ebookbell.com/product/linux-user-developer-issue-141-gavin-
thomas-46964020
EIMT SERIES
CD-INCLUDED
FRDGRfllYllYllNG
5v-iT!ir-iLJM i_ inT
Andre LalVlothe
CEO Xtreme Games LLC
Tr~" J|
f
Linux" G-hiyi-e
PRDGRfllYllYllNG
PRIMA TECH'S
C-HEC-K THE UlEE fDR UfDflT€5
To check for updates or corrections relevant to this book and/or CD-ROM, visit our updates page on
the Web at http://www.prima-tech.com/updates.
Hauu to Order
For information on quantity discounts, contact the publisher: Prima Publishing, P.O. Box 1260BK,
Rocklin, CA 95677-1260; (916) 787-7000. On your letterhead, include information concerning the
intended use of the books and the number of books you want to purchase. For individual orders, turn
to the back of this book for more information.
Tr-"
Linux G-rfyi
Prdgra m nm n g
lYlflRK "Nurgle" Collins
PRIMA TECH'S
© 2001 by Prima Publishing. All rights reserved. No part of this book 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 written permission from Prima
Publishing, except for the inclusion of brief quotations in a review.
Prima Publishing and colophon are registered trademarks of Prima Communications, Inc.
PRIMA TECH and the Game Development series are trademarks of Prima
TECH Communications, Inc., Roseville, California 95661.
Important: Prima Publishing cannot provide software support. Please contact the appropriate
software manufacturer's technical support line or Web site for assistance.
Prima Publishing and the author have attempted throughout this book to distinguish proprietary
trademarks from descriptive terms by following the capitalization style used by the manufacturer.
Information contained in this book has been obtained by Prima Publishing from sources believed to
be reliable. However, because of the possibility of human or mechanical error by our sources, Prima
Publishing, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of
any information and is not responsible for any errors or omissions or the results obtained from use
of such information. Readers should be particularly aware of the fact that the Internet is an ever-
changing entity. Some facts may have changed since this book went to press.
ISBN: 0-7615-3255-2
dntentb at a cL
Glance
1 N TR DDUCT1DN XXXI
CHflFTER 1
CHAPTER 3
The Structure of a G-hme EH
CHAPTER H-
CHAPTER E
3D Grafhicb for Linux Gflmes B7
CHAPTER 7
r^ Using OfenGL in Games 117
CHAPTER B
Bound Under Linux 1H-7
CHAPTER 3
Networking 1B3
CH^^T^K ID
Artificial Intelligence 137
CH^JPTE.^ 11
DFENSDURCEB PR1END OR FOE?
APPENDIX A
Dfen5durce License Agreements S l
+3
APPENDIX E
Porting E73
Linux Gaiyie -FRaG-R-HiYiiYnNG vn
APPENDIX C
REf£RENC€5 E3 3
APPENDIX D
Globb-rry SD7
APPENDIX E
UUhatIs on the CD-RDIY1 .313
Index 315
Linux Gaiyie -PROGRAmmiNG
Jl
Jn
Linux G-hiyi-e PRciG-R-HpnnmNG
Contents Q^
Introduction xxxi
CflflFTER 1
Simulations 3
Platform Games 3
Puzzle Games 4
Adventure Games 4
Role-Play Games 4
Card Games 4
Sports Games 4
What Makes a Successful Game? 5
CHAPTER S
Linux Develop ment Tools ill
Development Tools 12
Compiler 12
Debugging Tools 15
Development Environments 18
Libraries 20
Simple DirectMedia Layer 20
OpenGL 21
libXIl 21
Other Libraries 21
CHAPTER 3
The Structure of a Gaiyie E3
The Parts of a Game 24
A Game Framework 24
Linux Gaiyie FROGRfliYimiNG
CHAPTER H-
Displaying Images 52
Tl Understanding the PCX Format 52
Displaying the Image 55
n Using Color Palettes 56
Cleaning Up 57
A Mini-Project 57
CHAPTER B
Input uht+h 5DL ..«53
Understanding Input Devices 60
Keyboard 60
Mouse 61
Joystick 61
Handling Methods 62
Event Queues 62
Polling 62
CHAPTER E
r
3D Graphics for Linux Gflmes B7
n
Some History: Mesa and OpenGL 88
What Is OpenGL? 88
Where Can Get OpenGL?
I 89
Lighting 106
Specifying the Lighting Qualities of the Object 106
Establishing the Light Source 1 07
Setting Up Surface Normals 1 08
XIV Linux Gaiyie PROGRflmmiNG
CHAPTER 7
Using DfenGL in Gaiyieb 117
Understanding Display Lists 118
Understanding Matrices 1 22
Working with Plane Equations, Line Equations,
and Distances in 3D 1 24
Using Billboards to Simulate 3D 1 26
Working with Particles 1 29
Detecting Collisions 1 29
Simplify the Shapes You Test 130
If the Objects Are Close, Test Again 131
CHAPTER B
Bound Under Linux 14-7
Open AL 148
A Simple OpenAL Application 148
Initializing and Loading Samples 148
Creating Listeners 1 50
Loading a Sound File 151
SDLAudio 159
Starting an Audio Session 1 59
Loading Samples 1 62
Using the Audio Callback Function 1 63
Playing a Sample 1 63
Pausing the Audio Stream 1 64
Cleaning Up 1 64
Mixing Different Samples 1 64
CD Audio 165
Using the i octl ( ) Function 1 65
Opening the Device 1 66
Getting a Track Listing 1 66
Playing a CD 1 67
Tracks and Indices 1 67
Some Code 1 68
Stopping a CD 1 68
3
C-H-R-PT-E-R
Networking IB 3
Introduction to Modern Networking 1 70
Peer-to-Peer Networking 1 70
Client/Server Networking 171
UDP 172
$
Linux Gaiyie -P-RaG-R-RiYiiYiiNG
Socket Programming 1 72
Opening a Socket 1 72 Ir
Networking in Games 1 87
Dead Reckoning 187
Sending Controller Inputs 1 88
XVI 11 Linux Gaiyie PRDGRfimmiNG
Lobbies 1 88
1 What Is OpenLobby? 1 88
Using the Lobby Server 1 89
w Connecting to the Lobby Server 1 89
Logging In to the Lobby Server 1 90
FindingGames 191
Creating Games 1 92
Ending Games 1 93
Chatting in the Lobby 1 93
Listing Chat Rooms 1 93
Joining and Leaving Rooms 1 94
Sending Chat Messages 1 95
Cleaning Up 1 95
Security Issues 1 96
Buffer Overflows 1 96
Denial-of-Service Attacks 1 96
CHAPTER ID
flRTificifiL Intelligence 137
Basic Artificial Intelligence 198
Route Finding 1 98
State-Trees 1 98
Generate the State-Tree 1 99
Modify the Tree to Accommodate Multiple Routes 200
Search the Tree 200
The Nurgie Method 203
Linux G-hiyie FRDGRfliYimiNG xix
Grouping 205
Problem Solving 206
Trial and Error 206
Min-Max Trees 207
The Theory Behind the Tree 207
Putting It into Practice 209
CHAPTER 11
OfenEourceS Friend or Foe? iSE5
What Is OpenSource? 226
Unlimited Redistribution 227
Ability to Charge for Expenses 227
Emulators 238
Time-Share 238 H
R-F-P-END1X fl
w
flFFENDIX B
-Porting
Porting Strategy
273
275
Try Not to Fork the Codebase 275
Getting Started 276
#ifdef Is Your Friend 277
The Harder Stuff 277
First Light 278
fIFFENDIX C
1
References 239
II
Books 302
II OpenGL Super Bible (The Red Book) 302
OpenGL Reference (The Blue Book) 302
Graphics Programming Black Book 302
Game Architecture and Design 302
TCP/IP Illustrated (Volume 2) 303
Linux Device Drivers 303
Game Development Series 303
Magazines 304
Game Developer Magazine 304
Develop 304
Edge 304
Newspapers 305
Computer Trade Weekly 305
MCV 305
Computer Weekly 305
Computing 305
Linux G-rhi-e -P«OG«-HrYirYnNG
Newsgroups 306
Mailing Lists r
306
flFFENDIX D
Glossary .3D7
flFFENDIX -E
Index 315
Linux Gaiyie -PROGRAmmiNG
Linux Gaiyie FRQGRfimmiNG
Jl
RCKNDU1LEDGIY1ENT5
First of all, I'd like to thank my co-authors, Ben Campbell, and Martin Donlon, for taking
Steve Baker,
time out of their busy lives to help complete this book on time. If it weren't for their support, this book
may not have happened at all. I'd also like to thank all the guys and gals at Prima Publishing, namely
Jody Kennen and Heather Talbot, as well as Andre LaMothe (who may one day forgive me for not
speaking at the XGDC).
A big shout goes to all the staff and regulars at up with my ever-inflating ego
GameDev.Net for putting
and for not banning me from the message boards. (Although if they did, who'd run the Linux forum?)
In the same breath, I'd like to say "hi" to all the lame software pirates in the Syndicate of London,
namely Paris, Llama, Comrade, and Cronus (I shall enjoy watching you die, Mr. Anderson), and to all
tle adventure (when we weren't arguing), and to all my friends from Bath, many of whom owe me
large quantities of alcohol.
In true hacker fashion, these acknowledgments wouldn't be complete without a list of shouts. So in no
particular order, shouts to: ShinyEyedMermaid, JerryLee, TheHermit, Nurse, Mistress Demonica,
nes8bit, pouya, Myopic Rhino, jhky, TANSTAAFL, JadeFalcon, Dulie, Paris, Comrade, Llama, A cronus A ,
Silicon Dreams (especially Lara, she's hot), Gumby, Trev, Ruth, Puppy, Mike, Lil' Chris, Roper, Amy,
Jonsey—and last, but not least, Invisible Bob (the unofficial Indrema mascot).
Author -Bio
The Infamous Mark "Nurgle" Collins, the teenager with an ego that dwarfs Texas, has worked in just
about every area of the IT industry, from Web design to network consultancy, but one field has repeat-
edly attracted his expertise and outside-the-box thinking. That area is game development.
In addition to all his hobbyist involvement, Mark has worked for one of the largest developers in
Europe, and he acts as a freelance consultant to various commercial developers all over the globe.
Having skills in networking, AI, and graphics, he believes that games should be fun, rather than yet
another Quake clone.
Linux G-nm-E FRDGRfimiYnNG
am t-h
1 Editor
Dear Reader,
A long time ago, in a cubicle far, far away, UNIX was created. UNIX has shown itself
to be a reliable and robust platform for workstations, mainframes, and even PCs.
But, UNIX had never really gained much momentum on the PC platform for anv-
one other than the power user or technically minded. However, the time has come
when the PC version of UNIX, named Linux, has begun to make major in-roads
into the average user's home, and game programmers are taking notice! Now is the
time that gamers and developers alike want to play and develop games for Linux.
However, Linux, like UNIX, wasn't a simple platform to learn to program games
for — until now...
Linux Game Programming is without a doubt one of the most ambitious game pro-
gramming books ever written. There are so manv aspects to basic Linux program-
ming, it boggles the mind to even begin to think about writing 2D 3D games for
Linux. But, I can sav that Linux Game Programming does it all. The author. Mark
"Nurgle" Collins, starts from square one setting up the compiler, explaining the
tools and libraries of Linux, and covering general game programming theory.
This book is not just a bridge for Windows game programmers who want to learn
how to port to Linux, but an entire treatise on game programming on the Linux
platform. This book covers it all! .And I mean evervthing-from 2D and the SDL
(Simple DirectMedia Laver) to open GL and full 3D game programming.
In conclusion, this book sets the standard for Linux game programming and will
.^Q%&^
Andre LaxMothe
March 2001
Linux Gflme FROGRflmnmNG
Linux Gflm-E FROGRfliYiiniNG
lNTRDDUCTlDN
j
I ell, it's been a long time coming, but now there is finally a book on Linux game program-
^^^J ming. With the growing interest in both the commercial and desktop uses of the free Linux
operating system, it was only a matter of time until people started thinking about writing games for it.
Sure, many people have written little games in the past, such as x^7/and xboing (and a plethora of titles
starting with the letter x), but only in recent years have games that are almost commercial quality been
available for Linux.
With companies such as Loki porting successful titles such as Descent 3 and SimCity 3000, it's only a mat-
ter of time until software houses start developing original titles for the Linux platform. With the immi-
nent launch of the Indrema console, a Linux-based system, that future is set to become a reality.
But what is Linux? In 1991, a student in some country in Scandinavia made an announcement on a
message board with this immortal line: "I'm working on a minix-based operating system; it won't be as
big as the GNU project, though." (I'm paraphrasing here.) Since then, the "small" operating system
that was never going to be as big as the GNU project has grown to several million users worldwide, with
Linux is a semi-POSIX-compatible, UNIX-like operating system that runs on almost every platform
available today, ranging from i386s to PalmPilots. Until the past few years, however, few people consid-
ered using Linux as a desktop environment, but with projects such as KDE and Gnome paving the way,
more and more people are wiping out Windows and switching to one of the many Linux distributions
available.
while moving with the keyboard. I was impressed with this feature but knew it wasn't going to last.
Linux Gaiyie FRDGRfliYiiYiiNG
After a few weeks, I tried to install a few things from the CD, but horror struck! I was running on a
UMSDOS partition, co-existing with Chicago (later renamed Windows 95). A broken installation pro-
ll
gram wiped out all the partitions on my hard disk completely, without even having the courtesy to ask
first.
n
A few years later (and after the phenomenal success of Spong, a DOS, which really bad Pong clone for
everyone in my school became addicted to), I decided to try Linux again. So I popped down to the
shop and bought a RedHat boxed set, and I was away. After a few problems getting XWindows running
in anything higher than standard VGA, everything ran perfectly. I was happily playing around with
stuff, reconfiguring files here and there, setting up security, and so on. But I never actually used it.
Why? Because there was nothing to do. Every time I wanted to play Dungeon Keeper, I would reboot into
Windows and load it from there.
After some time, I did start playing some of the Linux games that were in existence, but none of them
really impressed me. So I started looking for ways to write my own games. I downloaded SDL and start-
ed playing around with it. Then the worst thing in the world happened: I got a job. Working on server-
side Java for Web sites, I had no time for personal projects. Soon I moved up to network consultancy
and still had no time for games. About a year after that, however, things changed.
I was looking for a career change, and boy did I find one. Within a week after sending my curriculum
vitae to an agency, I had an interview at Silicon Dreams. And I got the job. I stayed in that job until
December 2000, at which point I decided to write this book full time. (I was fired.)
While I was at Silicon Dreams, I learned many things (one of which was to not annoy Microsoft;
they'll complain to your boss if you do). One of the most important things I learned is this: No one
in the "professional" industry cares about Linux development — at least officially, that is. Many of
my co-workers at Dreams were closet die-hard Linux fans; as soon as they found out that I was writing
this book, I had the sudden urge to kill many of them.
As unlikely as it may seem, the small project that was never destined for big things may actually become
a hardcore gaming platform. I, for one, would like to see that happen.
1j-" \i
CHAPTER I
Introduction
TO GflFYl
D-EV-ELO-FIY1-ENT
Linux G-nm-E FRDGRflinmiNG
f
Listen my children, for I will tell a tale of a quest for glory, an adventure most heroic. A long time
ago, in a place far away, a young man sat in front of a magic box and decided to give something
to the world. He spent many moons working on his creation. And when it was complete, he looked
upon his creation and saw that it was good. This creation was a computer game. (After this he went
down to the pub for a few drinks because he really needed a good night out.)
i
and other software are recognized by large software developers such as Microsoft and have forced
these companies to create additional tools to allow developers to access the hardware directly.
Let's compare a game to a word What do you do in a word processor? You type stuff in, add
processor.
a bit of formatting, stick in some random comments for your editor to have a chuckle over, and save
the file. But in a game, a lot more is involved. You have to update the screen as many times as possible
during a single second. You need enemies reacting to the player's actions, you need a goal to achieve,
and most important, the game has to be fun.
bHDDT"€fn-U
The shoot-em-up type of game is probably one of the most popular game genres at the moment. This
category includes games such as Quake and Unreal Tournament. There are several types of shoot-em-up,
ranging from first-person perspective to scrolling games such as Defender. Most games in this genre have
but one goal: to kill anything that moves, from a shotgun to space ship.
Many newer shoot-em-ups have multiplayer capabilities, while others are written only for use as
multiplayer games (for example, Quake HI: Arena). These games have come under a lot of fire from the
media in recent years and have been blamed for the Columbine shootings.
Introduction to Gflme Dcvelopiyient
Games of this genre generally have very little in the way of a storyline —who needs an excuse to kill the
alien invaders? Although some games make an effort to craft a story, original stories are harder to
come up with these days.
Many games based on films fall into this category, such as Die Hard Tribgy and Alien versus Predator. [
5T-R-RT-EGY GflfYlES
d_
There are two types of strategy games: real-time and turn-based. Games such as Command and Conquer,
StarCraft, and Dune are real-time, whereas the hugely successful Civilization series is turn-based.
Real-time strategy games generally consist of two or more sides battling it out for an area of land. These
games usually require the mining of raw materials to build new structures and units. These games are
nearly as popular as shoot-em-ups but require a lot more time to play (a single level of Command and
Conquer can take several hours of playing before it's completed) Consequendy,
. it takes a lot of patience
to play these games, and they can become very frustrating.
Games that require the building of a civilization, such as Sid Miers Civilization, tend to be turn based.
Turn-based games can be played for days on end with only regular cups of coffee to keep you going.
Turn-based games allow a player to move all of their units before allowing the other players to have
their turns.
5llYlULflTlDN5
Any game that attempts to simulate a real event is a simulator, be it a flight simulator or a fishing game.
These games are designed to emulate real life as much as possible, allowing the users to gain
Some simulators are used for training, such as the Concorde simulator at British Aerospace Filton in
the United Kingdom. This multimillion-dollar computer game emulates the real behavior of the
aircraft exacdy (except that they won't let you barrel roll it). A few years ago, I had the opportunity to
browse through the source code for the simulator (written in Assembler) — all several million lines
worth of code! Accurate simulators are big projects and are not suited for the weekend programmer.
PLflTfORm GfllYlEB
Some of the most successful games ever have been platform games, such as the Mario series. This genre
consists of a character moving around on a level that usually scrolls from side to side, but that in more
recent years has become a full three-dimensional environment, with the character jumping around like
a hyperactive frog on anti-depressants.
Many people new to games development choose a platform game as one of their earlier projects
(usually after a few puzzle games). This is because platform games are generally uncomplicated from
the developer's point of view and can result in hours of entertainment for the player.
Linux GfimE P-ROG«flrYimiNG
FUZZLE GfllYl€5
There are probably more puzzle games in the world than any other type of game. Puzzle games are
very easy to code and can be very addictive for the player. This type of game requires very little in the
way of artificial intelligence and tends to be relatively small compared to games from other genres that
Examples of puzzle games include Tetris and Minesweeper. These games require patience and planning
to win. Like strategy games, puzzle games can get very frustrating over time.
RDV-ENTU-R-E G-HIY1-E5
Adventure games run the gamut of complexity, from the text-based Zork to the full 3D Tomb Raider.
This genre usually consists of the main character interacting with various NPCs (Non-Player
Characters) to achieve a goal, whether that goal is up with a sexy
rescuing a lost artifact or hooking
babe (as in Leisure Suit Larry, by Sierra). These games tend to be highly immersive and can suck away
many days of life from a dedicated adventurer.
Rdle-Flay GfllYl€5
Similar to adventure games, role-play games often involve going on a quest of some description, but
tend to be a lot more open ended. Players can spend years building up a character, giving it a
personality, and collecting a large stash of cash.
Some RPGs (Role-Play Games), such as Ultima, used very simple graphics but proved
of the earlier
highly successful, and some are still played today. Multiplayer RPGs have traditionally been text-based
MUDs (Multi-User Dungeons), but some of the more recent games have been graphical, such as
Ultima: Online and EverQuest.
Card Gflm-ES
Everyone has played Solitaire or FreeCell. These card games tend to be played when the user is waiting
for something to be done or is on the phone with an annoying customer. Card games can become very
addictive; I know some people who wouldn't get out of bed if it weren't for FreeCell
5FDRT5 GfllYlE5
Another highly successful game genre is the sports genre. Here in the United Kingdom, every gamer
out there has played at least one soccer game, one racing simulation, and at least one beat-em-up.
Some of the first games ever created fall into this genre (Pong being very similar to a game of tennis
without any nets).
.
Games such as the FIFA series are always bestsellers, and with good reason. They bring out the
competitive instinct in the player and allow the user to kick ass without actually hurting anyone. Many
H
a time my brother challenged me to a quick game of Actua Soccer, and many a time I won (and many a
time he beat me up for winning)
Sports games are generally highly addictive and can lead to large tournaments among groups of
friends. People like sports, and they like them even more if they don't have to get injured in the
process.
Nobody likes to play a game where you have to type for ten minutes just to get a square to move one
pixel to the left.
The game also needs to have a user-friendly interface, with easy-to-navigate menus that get the player
into the game as soon as it loads. Continuing from this, a good game should be very easy to learn but
should take a lifetime to master. A good example of this criterion is Street Fighter. Anybody can play the
game well, but very few are really good at it. This is because the game controls are intuitive; randomly
hitting them allows even a new player to win a fight against a master. However, a master can learn the
Some games have overly complex control systems, such as Alien versus Predator. This game had too
many controls to master before you could get into the game. This meant only a dedicated gamer could
play it for more than five minutes without deleting it from the hard disk out of frustration. Having to
constantly jump around the keyboard just to select a gun and fire it puts most players off for life.
Another key requirement to the success of some games (although not all) is a good plot. It's all very
well to run around killing ever)' goblin in sight, but why are you killing them? Did they murder your
second-cousin's pet cat? Or are you just killingthem because they are there to kill? Even a simple
background story can contribute a huge amount to the game's success, even if the actual story has no
relevance to the game itself.
Good story lines don't have to be predictable to be interesting. Take, for example, the film From Dusk
Till Dawn. Half the film is about two criminals trying to get to Mexico, when suddenly, out of nowhere,
they are attacked by a hoard of vampires. This surprising twist made the film much more interesting.
Games with sudden plot twists have done very well. A good example of this is Wing Commander III, in
which the Kil'rathi pilot who defected changes sides halfway through the story, even though he showed
complete loyalty to the Terran struggle.
Sometimes, being a well-known game developer means that all your games will be a huge success, even
if there is nothing special about your project. A good example of this condition is Doom 2, the sequel to
Linux G-riyi-e F-ROG-R-RiYinmNG
the highly successful Doom. There weren't any brand-spanking new features or a supersmart AI system
in this sequel to the successful Doom game, but people like id software, and they will buy
their games.
However, fame has a price. People are more than willing to jump on any bugs in a game from a
successful developer, while a lesser-known developer might manage to sneak a few "inconsistencies"
past the critics.
What goes into a design document? Just about everything from the story line to the 3D engine you're
going to use. Some design documents are several hundred pages long and cover every single detail in
the game; simpler games can use shorter design documents. Even the smallest documents should
contain the following:
• General Overview. This section of the document covers the game story line, the main characters, a basic
• Screen and User Interface. This section of the document covers the in-game menus, the in-game HUD
(Heads-up Display) you might use, as well as any icons used in the game.
• Art Specification. This section of the document lists any in-game artwork, cut-scenes, and sprites. Include
any appropriate storyboards and sketches.
• Sound and Music. All sound effects and music must be documented in this section, as well as any voice
talents the game will need.
• Technical Specification. This section covers how the game is going to run, including flowcharts and any
benchmarking you plan to perform.
After you have written your design document, you should share it with everyone on the development
team so that they can give you feedback on your ideas. You will probably get through several versions of
the design document before you all agree on what it contains, but that isn't a bad thing. The revision
process will help form new ideas from old ones and might point out some serious problems that might
otherwise occur only later in the development process.
Introduction to G«iyi-e Develdfiyient
DeveLa-pmeNT
IT
After your team has agreed on a design, you can actually get to work on writing the code. But where do
you begin? Do you write the game engine or the menus first? And which part of the game engine?
have to make when new project, but n
Where to start is one of the toughest choices you'll starting a
One of the best places to start when writing your game is at the beginning.mean that you
I don't
should write the whole thing in order, from intro sequence to closing mean that you should
credits, I
start where the program starts: the initialization routine where you prepare any variables you may need
or fire up any graphics APIs you use. Without this vital part of the program, you can't do anything.
WTiere you go from there is up to you, but I like to continue by developing the graphics engine. This
allows me to code the core of the game next, but does make it difficult to add or modify features later
If you are on a team that is developing a large project, you will need a way of sharing files. E-mail is a
fine exchange medium if everyone is working on completely separate parts of the program and you
have a clearly defined interface to work with. How ever,
T
if you have problems with version concurrency
you might want to look into a CVS (Concurrency Versioning System) repository. A CVS repository
allows multiple developers to work on the same problem by checking files in and out of a "repository."
I'll go into further detail about CVS options in Chapter 2, "Linux Development Tools."
Testing
One of the most important steps in the development process is testing your funky new game. If anv
serious bugs show up after you release the game, your reputation will be ruined. Frontier II: First
Encounters is a good example of incomplete testing resulting in a buggy release (although I can't reallv
place the whole blame on the developers). Because of a tight deadline, the program wasn't fullv tested
and several major bugs caused serious problems in the game. These bugs seriously damaged the
reputation of the developer, even though it had created Elite, one of the most popular games ever.
There are several stages to testing your game, commonly referred to as Alpha, Beta, and Gamma
testing phases. The Alpha testing stage done by the developers, in which they fix any obvious bugs
is
and glitches they find wiiile writing the code. Beta testing is performed by a third partv (usuallv a
dedicated gamer) who will try to find any of the hidden bugs; finally, Gamma testing is performed by
the general game playing community. When a bug is found in the game, chances are you will hear
about it. WTien you do, you try to reproduce the problem and then generate a patch to fix the
program.
When it comes to testing, one of the most useful tools you can create is a debug cheat. A cheat allows
you to jump to any part of the game, gives you any extras you need (such as cash, weapons, or bonus
points) , and can save you hours of time. Because all cheats are removed before the game is released
Linux G-riyi-e FROGRfHYinniNG
(honest), there are some funky tricks you can use to enable or disable them at compile time using
preprocessor directives.
If your compiler allows a flag to add a symbol at compile time, you can use that flag to specify a release
type, depending on whether the code you are compiling is a debug or release version of the game. For
example, you can include the following code in your game program.
// test.c
//include <stdio.h>
main ( ) {
#ifdef DEBUG
puts("This is a debugging version!");
#el se
(
puts " Th i s is a release version");
#endi f
If you then compile the program with the following command and run the program, you should get
This is the release version.
However, if you include the -DDE BUG option on the command line, as shown in the following
command, the program will output the line This is a debugging version!
This trick allows you to add cheats using #i fdef blocks of code. You can then turn off the cheats when
you are ready to release the game, if you wish. You can also add simple debugging statements in #i fdef
blocks to track the program flow without the use of an external debugger (as discussed in Chapter 2).
choices about what to do at this stage, and I will discuss three of your options here: OpenSource,
commercial, and shareware.
DP€N5DURC€ GfllTlEB
OpenSource is probably the simplest and quickest route to getting your games on the market. If you
want people to play your game as soon as possible, you can release the game while you're still writing it.
Not only does this approach let people know your game exists long before it's finished, it also lets
other people work with you to help fix any problems they may find or to make suggestions for
improvement.
lNT-RODUCITION TO G-RIYie D€V€LDPIYI£NT
The OpenSource route also means that you'll have a larger number of testers working for free. This
approach can speed up the development process dramatically and might even mean that your game
will be ported to other operating systems by third parties.
Although many people claim that it is impossible to make any money out of OpenSource software, this
has been proved false too many times to count. Companies such as RedHat and SuSE have made a
huge success out of distributing OpenSource
software for years by offering support and printed
manuals. NOTE
Make sure that you don't choose a
The first step to releasing an OpenSource project
license just because everyone else is
is to make it publicly available, including the
using Read through several licenses
it.
source code. After you have made it possible for
(printed versions of which can be
everyone to get your program, make an found in the back of this book) and
announcement on a few mailing lists and on sites choose the one you agree with the
such as Freshmeat (http://freshmeat.net) .
most.
FRE5HrYl£flT: HTTf='!//FRe5HrYieflT.NeT
made your announcement, expect input from users within a very short period of time.
After you have
Some may have patches, others may have criticisms, and others may need help using your game. It is
good practice in the OpenSource community to reply to all e-mail messages you receive regarding the
product; doing so will help you gain a name for yourself in the OpenSource community.
CammERciflL G-riyi-es
Many developers have realized that Linux is a viable option for commercial software, including games.
Companies such as Loki have made a name for themselves by porting successful games; other
companies such as Epic (Unreal) are also starting to develop games specifically for Linux machines.
Getting a game published commercially is not the easiest thing to do, especially for die Linux platform.
The process is long and complicated, involving several contracts, XDAs (Non-Disclosure Agreements),
and tough milestones. However, the rewards far outweigh the costs. Cash advances, high rovalties, and
fame encourage many independent developers to strive for bigger, better, and faster games.
To get published, you must first approach a publisher. Look around at your local Electronics Boutique to
see what publishers are out there. Names and sometimes addresses of games publishers appear on the
packaging of the products you'll see.Be aware that many publishers such as Electronic Arts have in-
house development teams and refuse unsolicited submissions. Look for companies that publish games
from a wide range of developers, such as GT Interactive and ActiVision. When you have determined
who you want to publish your game, request a submission pack from the publisher. This packet will
contain an NDA, which protects all parties involved from any legal problems that may arise (for
example, plagiarism).
Linux CE-fhyi-e P-RDG-R-RmmiNG
When you have returned the NDA with your completed design document —and hopefully a working
—
demo of your game the publisher will tell you whether it is interested in your submission. If it turns
you down, don't give up. Keep trying until you get a new publisher.
If the publisher approves your game, negotiations will begin. It might be a good idea to contact a
lawyer because legal documents will be involved; unless you are fluent in legalese, the contracts will
probably be a bit confusing.
A good guide to getting your games published has been written by Geoff Howland of Lupine Games.
You can find the guide at http://www.lupinegames.com/articles/getstart.html.
5+Hfl-R-Eui-H-R-E G-nmes
Another option when it comes to releasing your game is the shareware route. Some of the most
successful games ever, such as Doom and Quake, were released initially as shareware. Releasing a game as
shareware is very similar to releasing a game in the OpenSource route, except that you generally don't
distribute the source code. (Note, however, that id software has now made the source code to Quake
available, although it still requires a license for the in-game data.)
You have several options to choose from when making your game shareware. You can use an honor
system and ask the user to pay you after a certain amount of time has elapsed, or you can restrict the
game in some way (such as only including the first few levels) until the user has paid you for the
complete version.
usually a good start) and add a message to the game letting the users know where they have to send
the money. In games such as Doom and Quake, a message was displayed when you quit the game, giving
the user registration details. Some games use pop-up messages during the game to get the shareware
message across.
When it comes to distributing your game, you have several options. You can try and have it stuck on
the cover CD of your favorite magazine, or you can upload it to the Internet and have some download
sites such as Tucows (http://www.tucows.com), sending random surfers to get your game. The
possibilities are endless.
Linux
Develdfiyient
Tools
-ol
!:.^S/fts3tlw
-Jlj—l 3
'5»»*Z!5
Linux Gaiyie FRDGRflmiYnNG
nl
Just as every plumber needs a plunger and every surgeon needs a scalpel, game developers have
certain tools they need to create the next hit games. These tools range from development
environments to debuggers and profilers. In this chapter, you'll learn what types of tools make up the
successful game developer's toolkit.
Develofiyient Tools
You will use several key tools when you write any software, including compilers, interpreters, debuggers,
and version management tools such as make. Some tools, such as KDevelop, integrate all of these
utilities into one environment.
Cdiyifiler
The most important tool you will need is a compiler. Without one, your code is about as useful as a
bikini in the Arctic Circle. Although some languages such as bash are not strictly compiled, you will still
need an interpreter of some kind. Because the code in this book is written in C, I will focus on the
GNU C compiler, GCC, available from http://www.gnu.org/software/gcc/gcc.html.
Compiling with the GCC can be a problem and isn't covered by this book; if you download the source
code that's available for distribution, you should read all the documentation before you begin to use
the compiler. Note, however, that almost all Linux distributions include the GCC as part of the default
installation, so you probably already have the compiler.
The GCC incorporates most of the tools you will ever need into one easy-to-use program. The GCC
includes preprocessors, compilers, and linkers for various platforms (you can cross-compile to a Win32
executable, for example). To demonstrate GCC, copy the following example:
/* gcc-test.c - GCC test application */
pri ntf (
" Li Number of arguments: %d\n",
ne: %d -
LI NE argc);
printf "Li ne: %d
( First argument: %s\n
-
LI NE argv[l]); , .
return 0;
(
The gcc -o gcc-test gcc-test.c command is fine for simple, single-file projects, but what if your
program spans multiple files? If you include the -c option on the command line, the GCC does not
generate an executable file but instead generates a linkable object file. Try the following sample
program. In one file (gcc-funcl .c), type the following code:
void f unction( ) {
printfC'Hello world\n");
Before you can call this function (cleverly named f uncti on )), you must have a mai n( ) somewhere. So
let's create a second file (gcc-func2.c) that contains the ma i n ( ) function:
main( ) {
f uncti on( ) ;
return 0;
In normal situations, you'd use a makefile for multiple source files, but I'll cover that technique later in
this chapter. For this example, we'll use the command line to compile this two-file program. For each
file you want to compile, you use the -
c parameter. When you want to link the program, you tell the
compiler to use the object files for input instead of the source code, like so:
Like any standard compiler, GCC supports multiple warning levels. You set the level with the -
W
command-line switch. It is generally considered good practice to enable all warnings by using the -Wal 1
switch. In addition to warning levels, the GCC also has several levels of optimization. By default, GCC
disables optimization for debugging purposes (0); for release versions of your programs, you will
« probably want to enable A word of warning, however: Full
full optimization with the 03 switch.
optimization may introduce stability problems to your program, so people generally use the 02 switch
instead.
If you're developing for multiple platforms, you may want to make use of the - D switch to define
symbols such as architecture. Chapter contained a simple example of the use of the - D switch to
i enable or disable debugging commands
1
#ifdef LINUX
# ifdef 1386
char *platform = "Linux x86\n";
# else ifdef SPARC
char *platform = "Linux SPARCXn";
# end i f
//else
char *platform = "Unknown\n";
#endi f
main( ) {
return 0;
There are three possible options when compiling this program. The first is to add the -
DLI NUX - DI 386
If the library you want to use is not on the library search path, use the - Kpa th> switch, or set the
LI BRARY_DI R environment variable to point to the directory in which the library is located. You use a
similar method for includes, except that you use the -Kpdth> switch or the INCLUDE_DIR environment
variable.
Debugging Tools
How many times have you compiled a program with no compilation errors, yet found a strange bug
that was nearly impossible to trace? Personally, I have lost weeks of coding rime tracking down
annoying bugs like this, and only found out what was going on by using my trusty debugger.
There are several debuggers available, but my personal favorite is the DDD/GDB combination. DDD is
that DDD can render runtime variables into easy-to-read graphs that allow at-a-glance checking of your
variables.
To demonstrate the power of DDD and how to use it, let's create a short test program. The following
example should display a simple table of names, except that there is a small and stupid bug that causes
unexpected results.
i nt I = 0, row = 1
printf("%s\t". string[i++]
printf ("\n" )
Linux G-riyi-e -F-ROGf^-HnnrYnNG
(
pr i ntf " \ n " ) ;
row++;
i = 0;
[ NOTE
This sample program is based on a bug
When you compile and run this program, you'd ized what I did wrong, I hated myself.
there is something very wrong with this simple program. Time to fire up the debugger.
Before we load DDD, we must compile the program with debugging symbols. This can be done quite
easily by adding the -g option to the gcc command line, as shown here:
After the program has finished compiling, load DDD and open the new executable file we just created.
The main window should look something like the one shown in Figure 2.1.
Now that we can see our misbehaving program in DDD, we need to find out where the fault is. Let's try
to narrow down the potential areas where the problem could occur. The program displays the table
headers okay, but everything after that is just plain screwed up. If we set a breakpoint on line 20, we
can interrupt the program when things start to go wrong. There are two ways of doing this. The first
one is to type chapter2- 1 . c : 22 in the textboxjust above the code window, the other is to select line 22
(printf "\n" (
) ;) in the code display.
Next, we also want toknow what the values of our two variables (i and row) are. We can see this by
right-clicking the variable names and selecting Display <variable> from the context menu. If we then
click the Run button in the floating toolbox, the program will execute up to line 20. After all these
manipulations, the DDD window should look like the one in Figure 2.2.
The floating toolbox contains several buttons that will be useful in our debugging efforts. The buttons
we will use here are Next and Step. The differences between the two buttons are subde, but important.
When you click Step, the program runs to the next line of the program; if you click Next, the program
continues to the next instruction. If you want to debug a bit of Assembler, for example, you will
CHAPTER I
PRIMITIVE MAN
ebookbell.com