0% found this document useful (0 votes)
32 views38 pages

Linux Game Programming W CD Prima Tech S Game Development 1st Edition Mark Collins Instant Download

The document provides information on various ebooks available for download, including titles related to Linux game programming, game development, and other subjects like health and e-commerce. It lists specific books along with their authors and links for instant digital downloads. Additionally, it includes a table of contents for a book on programming Linux games, detailing various game genres, development tools, and gaming APIs.

Uploaded by

gailsjanssge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views38 pages

Linux Game Programming W CD Prima Tech S Game Development 1st Edition Mark Collins Instant Download

The document provides information on various ebooks available for download, including titles related to Linux game programming, game development, and other subjects like health and e-commerce. It lists specific books along with their authors and links for instant digital downloads. Additionally, it includes a table of contents for a book on programming Linux games, detailing various game genres, development tools, and gaming APIs.

Uploaded by

gailsjanssge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Linux Game Programming w CD Prima Tech s Game

Development 1st Edition Mark Collins download

https://ebookname.com/product/linux-game-programming-w-cd-prima-
tech-s-game-development-1st-edition-mark-collins/

Get Instant Ebook Downloads – Browse at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

The Sims 3 Prima Official Game Guide Prima Official


Game Guides Catherine Browne

https://ebookname.com/product/the-sims-3-prima-official-game-
guide-prima-official-game-guides-catherine-browne/

Game Programming With Python Game Development Series


1st Edition Riley

https://ebookname.com/product/game-programming-with-python-game-
development-series-1st-edition-riley/

Mac game programming 1st Edition Mark Szymczyk

https://ebookname.com/product/mac-game-programming-1st-edition-
mark-szymczyk/

Processing of High Temperature Superconductors


Proceedings of the symposium held at the 104th Annual
Meeting of The American Ceramic Society April 28 May 1
Transactions Ceramic Transactions Series 1st Edition
Amit Goyal
https://ebookname.com/product/processing-of-high-temperature-
superconductors-proceedings-of-the-symposium-held-at-the-104th-
annual-meeting-of-the-american-ceramic-society-
E commerce the cutting edge of business 2nd Edition
Kamlesh K. Bajaj

https://ebookname.com/product/e-commerce-the-cutting-edge-of-
business-2nd-edition-kamlesh-k-bajaj/

Health The Basics 10th Edition Rebecca J. Donatelle

https://ebookname.com/product/health-the-basics-10th-edition-
rebecca-j-donatelle/

The Brain in Context A Pragmatic Guide to Neuroscience


1st Edition Jonathan D. Moreno

https://ebookname.com/product/the-brain-in-context-a-pragmatic-
guide-to-neuroscience-1st-edition-jonathan-d-moreno/

The Second Decade of Transition in Emerging Europe 1st


Edition Ognjen Radonji■ And Sr■an Kokotovi■

https://ebookname.com/product/the-second-decade-of-transition-in-
emerging-europe-1st-edition-ognjen-radonjic-and-srdan-kokotovic/

Functional Thinking Paradigm Over Syntax 1st Edition


Neal Ford

https://ebookname.com/product/functional-thinking-paradigm-over-
syntax-1st-edition-neal-ford/
The Digital Filmmaking Handbook Fourth Edition Sonja
Schenk

https://ebookname.com/product/the-digital-filmmaking-handbook-
fourth-edition-sonja-schenk/
Programming Linux Games

John R. Hall
Loki Software, Inc.

January 15, 2001


2
Contents

Work In Progress i

1 The Anatomy of a Game 1


A Quick Survey of Game Genres . . . . . . . . . . . . . . . . . . . . . . 2
Simulation Games . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
First Person Shooters . . . . . . . . . . . . . . . . . . . . . . . . . 4
Real-time Strategy Games . . . . . . . . . . . . . . . . . . . . . . . 5
Turn-based Strategy Games . . . . . . . . . . . . . . . . . . . . . . 7
Role-playing Games . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Puzzle Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Multi User Dungeons . . . . . . . . . . . . . . . . . . . . . . . . . . 9
A Quick Look Under The Hood . . . . . . . . . . . . . . . . . . . . . . . 11
The Input Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Display Subsystem . . . . . . . . . . . . . . . . . . . . . . . . 12
The Audio Subsystem . . . . . . . . . . . . . . . . . . . . . . . . . 13
The Network Subsystem . . . . . . . . . . . . . . . . . . . . . . . . 13
The Update Subsystem . . . . . . . . . . . . . . . . . . . . . . . . 14
The Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

3
4 CONTENTS

2 Linux Development Tools 17


Programming Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
vi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
NEdit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Compiling Programs Under Linux . . . . . . . . . . . . . . . . . . . . . 21
Using The Make Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Creating Makefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Working With Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Static Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Shared Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Linux Linker Quirks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Debugging Linux Applications . . . . . . . . . . . . . . . . . . . . . . . 33
Compiling For Debugging . . . . . . . . . . . . . . . . . . . . . . . 33
The GNU Debugger, gdb . . . . . . . . . . . . . . . . . . . . . . . 34
The Data Display Debugger . . . . . . . . . . . . . . . . . . . . . . 40
Bug Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Project Management With CVS . . . . . . . . . . . . . . . . . . . . . . 42
A Brief CVS Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . 43
Other Useful Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Rapid Text Searching with grep . . . . . . . . . . . . . . . . . . . . 49
Updating Source with diff and patch . . . . . . . . . . . . . . . . . 50

3 Linux Gaming APIs 53


Times Have Changed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
CONTENTS 5

Graphics APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
SVGALib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
General Graphics Interface, LibGGI . . . . . . . . . . . . . . . . . 56
Simple DirectMedia Layer, SDL . . . . . . . . . . . . . . . . . . . . 56
ClanLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Plib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Glide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Xlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Graphical User Interface Toolkits . . . . . . . . . . . . . . . . . . . . . . 59
GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Tk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Fltk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Qt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
SDL GUI support . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Audio APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The Open Sound System, OSS . . . . . . . . . . . . . . . . . . . . 61
Advanced Linux Sound Architecture, ALSA . . . . . . . . . . . . . 61
The Enlightened Sound Daemon, EsounD . . . . . . . . . . . . . . 62
The Open Audio Library, OpenAL . . . . . . . . . . . . . . . . . . 62
Scripting Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Guile and MzScheme . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Python and Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Networking APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6 CONTENTS

BSD Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
OpenPlay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
IPX and SPX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
libpng and libjpeg . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
libaudiofile and libsndfile . . . . . . . . . . . . . . . . . . . . . . . . 66
Ogg Vorbis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
The SDL MPEG Library, SMPEG . . . . . . . . . . . . . . . . . . 67
zlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

4 Mastering SDL 69
Computer Graphics Hardware . . . . . . . . . . . . . . . . . . . . . . . . 70
The Framebuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
The SDL Video API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Setting Up The Display . . . . . . . . . . . . . . . . . . . . . . . . 73
Direct Surface Drawing . . . . . . . . . . . . . . . . . . . . . . . . 77
Drawing With Blits . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Colorkeys And Transparency . . . . . . . . . . . . . . . . . . . . . 85
Loading Other Image Formats . . . . . . . . . . . . . . . . . . . . . 89
Alpha Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Achieving Smooth Animation With SDL . . . . . . . . . . . . . . . 95
Input and Event Processing . . . . . . . . . . . . . . . . . . . . . . . . . 105
Processing Mouse Events . . . . . . . . . . . . . . . . . . . . . . . 105
Processing Keyboard Events . . . . . . . . . . . . . . . . . . . . . . 110
Joystick Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Portable Threading Support . . . . . . . . . . . . . . . . . . . . . . . . . 117
CONTENTS 7

SDL Audio Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 121


Representing Sound with PCM . . . . . . . . . . . . . . . . . . . . 121
Feeding a Sound Card . . . . . . . . . . . . . . . . . . . . . . . . . 124
An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Integrating OpenGL with SDL . . . . . . . . . . . . . . . . . . . . . . . 135
Penguin Warrior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Creating Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Implementing a Parallaxing Scroller in SDL . . . . . . . . . . . . . 142
A Simple Particle System . . . . . . . . . . . . . . . . . . . . . . . 147
Game Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
To Be Continued... . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

5 Linux Audio Programming 155


Competing APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Introducing Multi-Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Loading Sound Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Using libsndfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Other Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Using the Open Sound System . . . . . . . . . . . . . . . . . . . . . . . 163
Reality Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Squeezing Sound Performance with Direct DMA Access . . . . . . 171
Playing Sound with ALSA . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Sharing the Sound Card with ESD . . . . . . . . . . . . . . . . . . . . . 187
Building Multi-Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Environmental Audio with OpenAL . . . . . . . . . . . . . . . . . . . . 198
OpenAL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
8 CONTENTS

Adding Environmental Audio to Penguin Warrior . . . . . . . . . . 204


Implementing Game Music with Ogg Vorbis . . . . . . . . . . . . . . . . 212
Working with Vorbis Files . . . . . . . . . . . . . . . . . . . . . . . 213
Adding Music to Penguin Warrior . . . . . . . . . . . . . . . . . . 216

6 Game Scripting Under Linux 227


A Crash Course in Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Built-in Tcl Commands . . . . . . . . . . . . . . . . . . . . . . . . 230
Interfacing Tcl With C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Linking Against Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Executing Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Commands And Objects . . . . . . . . . . . . . . . . . . . . . . . . 240
A Simple Scripting Engine . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Designing A Game Script . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Applying Scripting To The Real World . . . . . . . . . . . . . . . . . . . 254
Single Versus Multiple Contexts . . . . . . . . . . . . . . . . . . . . 255
Can We Trust The Script? . . . . . . . . . . . . . . . . . . . . . . . 256
Script Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Who’s Writing The Script? . . . . . . . . . . . . . . . . . . . . . . 257

7 Networked Gaming with Linux 259


’Tis A Big Net, Quoth The Raven . . . . . . . . . . . . . . . . . . . . . 260
Internet Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Addresses and Ports . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Name Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Socket Programming 101 . . . . . . . . . . . . . . . . . . . . . . . . . . 263
CONTENTS 9

Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Connecting TCP Sockets . . . . . . . . . . . . . . . . . . . . . . . . 264
Receiving TCP Connections . . . . . . . . . . . . . . . . . . . . . . 273
Working With UDP Sockets . . . . . . . . . . . . . . . . . . . . . . . . . 279
Multiplayer Penguin Warrior . . . . . . . . . . . . . . . . . . . . . . . . 287
Network Gaming Models . . . . . . . . . . . . . . . . . . . . . . . . 287
Penguin Warrior’s Networking System . . . . . . . . . . . . . . . . 288
Network Game Performance . . . . . . . . . . . . . . . . . . . . . . . . . 297
Security Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

10 To Every Man A Linux Distribution 301


Source Or Binary? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Local Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Linux Isn’t Alone: Supporting FreeBSD . . . . . . . . . . . . . . . . . . 304
Packaging Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Graphical Installation Goodness: Loki’s Setup Program . . . . . . . . . 308
Grokking The Linux Filesystem Standard . . . . . . . . . . . . . . . . . 313
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

Glossary of Terms 319


10 CONTENTS
Work In Progress

This is a recent draft of the partially-completed book Programming Linux Games,


to be published by No Starch Press (http://www.nostarch.com) early in 2001.
This book was previously being written in DocBook, but it has recently been
converted to LATEX. For this reason the HTML version of this document is no
longer available.
This document is copyright c 2000 by Loki Software, Inc.
Neither Loki Software, No Starch Press, nor John Hall make any claim as to the
correctness of the information in this document. If you find any errors, please let
us know. You may contact the author at [email protected].
Thanks, and enjoy!
-John Hall, author

i
ii WORK IN PROGRESS
Chapter 1

The Anatomy of a Game

In 1991 a Finnish university student named Linus Torvalds began working on a


new operating system in his spare time. He didn’t work in isolation, nor did he
make a big deal about what he was doing; rather, he modestly invited
programmers from all over the world to join his project, which he dubbed
“Linux.” This loosely knit team of students, professionals, and hobbyists
collaborated through the Internet, with the expectation of learning a bit about
programming and having a good time. Linus never thought that his project
would spawn an entire industry.
Since then, Linux has grown into a general-purpose operating system for a wide
variety of hardware platforms. With over 10 million users (a number which is
constantly growing), there is a sizeable audience for Linux-based computer
games. Linux is now capable of accelerated 3D graphics, environmental audio,
and seamless game controller handling, in addition to the server tasks that
UNIX-like operating systems generally carry out. Although Linux is still
evolving, it is already a solid environment for serious game development.
This book is about the toolkits and the environments that allow programmers to
write 2D and 3D games for Linux. We will learn how to draw animated graphics
on the screen, how to play high-quality digital sound through several different
software libraries, and how to set up OpenGL to create fluid 3D graphics. By
the end of this book, you will know what makes Linux games tick, and how to
create your own games for this platform.
This book is not about game design, the mathematics of 3D graphics, or

1
2 CHAPTER 1. THE ANATOMY OF A GAME

advanced OpenGL programming. These topics are best left to books of their
own; I could not hope to do them service here. However, with the knowledge you
will gain from this book, you will be prepared to tackle these topics later on.
Before we begin our discussion of Linux game programming, however, let’s take
a quick glance at our surroundings in the gaming industry so that we can better
understand what goes into this type of project.

A Quick Survey of Game Genres

Computer games tend to fall into a number of distinct genres. Many players
have strong preferences for certain genres, which makes this an important issue
for game designers to consider. And, the presentation of a game concept can
make an enormous difference in its success.

Simulation Games

The simulation genre encompasses a wide variety of games, from flight


simulators to Mech combat scenarios. An ideal simulator provides a high level of
realism in graphics, sound, and game physics. Some popular simulation games
are Heavy Gear II, Mech Warrior, and Microsoft Flight Simulator. The basic
goal of any simulation game is to put the player behind the controls of
something exciting, something that he probably would not have access to in real
life. Simulations strive for immersion.
Simulation games (sims) are basically at two extremes. Some aim for absolute
realism, seeking to entertain the player with an almost completely accurate
portrayal of real life. These “games” are sometimes even used for real-life
training purposes. Other sims, like the Heavy Gear and Mech Warrior series,
trade realism for pure entertainment value. These games are based only loosely
on reality; they simulate imaginary vehicles with extraordinary but rather
impossible capabilities. (Interestingly, the Mech Warrior and Heavy Gear
computer games are based on pencil-and-paper role-playing games.)
Simulations pose a serious development challenge. Since a good modern
simulation requires high-quality 3D graphics, detailed vehicle models, a game
physics system for simulating the physics of the real world, realistic input
response, network capabilities, and possibly a certain amount of artificial
A QUICK SURVEY OF GAME GENRES 3

intelligence for the computer-controlled players, a contemporay sim is not trivial


to construct.
What makes a simulation game successful? Let’s look at an example of a
“realistic” simulator and an “action” simulator. Microsoft Flight Simulator is a
popular flight simulator for the PC (and in fact the current iteration of a long
line of flight simulators by the same developers, dating back to the Commodore
64) that combines realistic control with excellent 3D graphics and interesting
airplanes. Its simulated instrumentation is extremely true to life (though I’m not
a pilot, so this is mainly hearsay), and the terrain looks reasonably close to the
real world’s1 .
Microsoft Flight Simulator tries to make the player feel like he’s in the cockpit,
not just collecting cellulite behind the keyboard of a fast computer. Although
this game will not run under Linux (except possibly under WINE2 ), it’s certainly
worth a look if you’re thinking of writing a flight simulator.
On another front, the Flight Gear project is presently developing a free flight
simulator for Linux. The simulator already sports a realistic physics model and
an excellent terrain engine, and it is slated to eventually become one of the best
flight simulators ever. Flight Gear is portable to many platforms, as it is based
almost entirely on open technology.
Heavy Gear II from Activision is a good example of an action simulator. It puts
the player behind the controls of a multi-ton Gear (a two-legged walking vehicle
with big guns), and succeeds because of its realistic graphics, simple but capable
control system, damage simulation, and interesting gameplay. The player is in
complete control of his Gear, and is free to do anything during the game
(although accomplishing the mission without getting killed is usually the best
plan). Heavy Gear II creates a sense of power and euphoria in the player, and
this makes it a pleasant experience. Activision has also published several Mech
Warrior titles that are very similar to the Heavy Gear series.
Finally, one of my personal favorite simulation games (from many years ago) is
Corncob 3D, a completely unrealistic shareware DOS-based flight simulator.
Guised as a flight simulator, this is a classic “Defend Earth from Space Invasion”
1
One of the first rules of game design (and, to some extent, computer graphics in general) is
that it doesn’t matter if something is realistic, as long as it looks realistic. Unfortunately, most
people don’t have 5 terahertz machines, so game creators have to take a few shortcuts. Most
flight simulators really aren’t that realistic when it comes down to it, but they sure seem realistic.
2
http://www.winehq.com
4 CHAPTER 1. THE ANATOMY OF A GAME

Figure 1–1: Screenshot of Heavy Gear II

game with lots of missions, missiles, and mayhem. By today’s standards, of


course, this game is laughable. But it ran well on the low-end hardware of the
day, and it was a lot of fun to play. Corncob 3D is a good example of a simulator
that trades realism for entertainment value.

First Person Shooters

First person shooters (FPS) are some of the most popular games today.
They typically involve a weak storyline (with exceptions, of course), hordes of
enemies, big explosions, and lots of blood. The basic premise of most first person
shooters is to give the player an adrenaline rush by putting him in the middle of
a hostile environment with insidious monsters and powerful weapons. These
games have improved in quality over the years, and are beginning to reach a very
high level of realism. Some popular ones are Quake 3, Half-Life, and Soldier of
Fortune, all of which are available for Linux (though Half-Life is not native to
Linux, and requires the WINE (http://www.winehq.com) library to run).
A QUICK SURVEY OF GAME GENRES 5

High-quality first person shooters are difficult to produce, not just because
they’re hard to program (facilitated by standard 3D libraries such as OpenGL),
but also because they require detailed 3D character models and levels. 3D game
engine programming requires a solid knowledge of linear algebra and a firm
grasp of certain types of data structures. However, mathematically-inclined
people are likely to find 3D game programming both challenging and rewarding.
Valve’s Half-Life is one of the most successful first person shooters, combining
the thrill of a typical FPS with a compelling storyline, amazingly realistic
enemies, and an excellent multiplayer mode. Half-Life is based on Quake II’s
rendering technology, but that is where the similarities end. Unlike the Quake
series, Half-Life has a plot, an excellent single player mode as well as network
game support, and a more complex virtual environment (complete with
moveable objects and vehicles).
Another interesting first person shooter (also based on the Quake II engine) is
Activision’s Soldier of Fortune. Although decried by critics as gratuitously
violent (and hence “indexed” in Germany and classified as an adult title
elsewhere), Soldier of Fortune combines traditional first person shooter action
with frightening realism, even going so far as to correctly simulate bodily
damage due to gunshot wounds. It also has a solid plot that develops throughout
the game. Overall, a very enjoyable title, if you’re not disturbed by the violence
(I won’t go into the highly emotional politics surrounding this subject).
A current trend is to mix first person 3D technology with the role-playing game.
Deus Ex is one such example, an RPG based on the Unreal engine. Deus Ex has
been ported to Linux, and I strongly recommend giving it a try.

Real-time Strategy Games

This genre includes such popular titles as StarCraft, Command and Conquer,
and Total Annihilation – games that allow the player to command individual
parts of an army from an overhead view, with success in battle usually leading to
better equipment and soldiers. Because success is usually determined by a
player’s tactics, these are considered strategy games. Real-time strategy
(RTS) games often have a high replay value; they’re fun to play again and again.
Real-time strategy games are comparatively easy to program, because, with
some exceptions, they do not involve 3D graphics or complicated mathematics;
6 CHAPTER 1. THE ANATOMY OF A GAME

Figure 1–2: Screenshot of StarCraft

however, good real-time strategy games are hard to produce, and they tend to be
few and far between. RTS games also often involve a certain amount of artificial
intelligence (AI) programming for controlling the simulated opponents in
single-player games — a fascinating field and an area that we won’t touch in this
book.
StarCraft is by far the most successful RTS game, combining pleasing graphics, a
large selection of well-balanced units, and interesting battlefields in a very
well-rounded game and exciting game. Solid game design is by far the most
important issue in creating a real-time strategy game, and StarCraft is an
excellent example. StarCraft (which will run under Linux with WINE3 ) is not
the first notable game from Blizzard Entertainment, and it will be interesting to
see what Blizzard comes up with in the future.

3
WINE is an open source implementation of the Win32 API, as well as a layer for loading
Windows .EXE files. This allows Linux to run a number of Windows applications out of the
box. It is not perfect, but it is improving rapidly.
A QUICK SURVEY OF GAME GENRES 7

Turn-based Strategy Games

Turn-based strategy (or TBS) games are like real-time strategy games, but
the gameplay is divided into turns, usually with no time limit, thus giving the
player time to think and relax, while giving the game an entirely different feel
from the faster-paced strategy games. TBS games are not decided by reflexes,
but rather by careful planning, which often makes them more difficult, and more
attractive to many players. Sid Meier’s Civilization II is widely regarded as the
best turn-based strategy game, because of its balance and replay value.

Deceptively Complex
I once thought that TBS games were easy to write, but then I saw the
source code to Sid Meier’s Alpha Centauri. Most players don’t realize
it, but SMAC actually uses a 3D technique called voxels to render its
units on the fly, and to draw a height-sensitive landscape with
perspective texture mapping and dynamic palette mapping (made
possible by self-modifying assembly code). Sid Meier’s Alpha Centauri
was obviously not easy to port to Linux. While it’s possible to write a
good TBS game without such sophistication, don’t think of the TBS
genre as an easy way out — its complexity can be deceiving.

Role-playing Games

The role-playing game (or RPG) stems from the Dungeons and Dragons
role-playing system4 . In RPGs, the player assumes the role of one or more
characters on some sort of quest. role-playing games put the player in a world
with many possibilities; a good one gives the player a sense of immersion and
true interaction, and allows him to effectively become someone else.
The quality of a role-playing game depends much more on its storyline,
interaction, and depth than on its graphics. Ultima Online is an example of a
good online RPG. While its graphics are not spectacular, the depth of its
gameplay is incredible, because it allows for complex interactions between
4
There are lots of similar role-playing systems; I just give DND as an example.
8 CHAPTER 1. THE ANATOMY OF A GAME

Figure 1–3: Screenshot of Civilization: Call To Power

players in a virtual universe. Ultima is not exactly a “hardcore” RPG though;


true die-hard role-playing fans often prefer other types of RPGs, like the
Wizardry series, for example.

Puzzle Games

Puzzle games receive less attention than the other game genres because they are
less spectacular, but they certainly deserve mention. Puzzle games challenge the
player with problems that require thought and patience. This genre includes
everything from simple box-pushing games (Boxxel and the dangerously
addictive Sokoban) to the animated and ubiquitous Tetris.
A successful puzzle game is usually challenging (but not impossible), pleasant to
look at (graphics should not be ignored), and replayable (one-shot puzzle games
are usually not very enjoyable the second time around, and players don’t
appreciate that). The difficulty in creating a puzzle game depends on the
A QUICK SURVEY OF GAME GENRES 9

Figure 1–4: Screenshot of Ultima Online

particular game; some are extremely complex, involving massive amounts of


artwork and graphics processing, while others are simple to implement.

Multi User Dungeons

Multi User Dungeons (commonly known as MUDs) are massively multiplayer


games, typically hosted on Internet servers and accessed with special MUD client
programs. MUDs are extremely popular because one’s opponents are real people,
not computer-controlled robots. MUDs are essentially text-based role-playing
games, immersing their players in worlds with magical objects, wizardry, and
battle. MUD fans wishing to host a game of their own often obtain a pre-written
MUD server program and develop their own ”world” through configuration files
and scripting languages. If they do a good job, they may attract lots of players,
which is very satisfying. Two popular MUD server programs are ROM and
DikuMud, which may be downloaded from the Internet. There are untold
10 CHAPTER 1. THE ANATOMY OF A GAME

Figure 1–5: Screenshot of KSokoban, a version of Sokoban for KDE

thousands of private ROM-based MUDs on the Internet.


MUDs are relatively easy to create, though writing a MUD server is not trivial,
requiring a solid background in C or similar and a knowledge of network
programming. Creating MUD data files requires little programming knowledge,
but a lot of creativity. A good MUD has an interesting game world to explore
and a good balance of races and abilities. Also, some MUDs are prone to “god
moding”, or abuse by the person running the server; while this obviously
depends on the players, good design can lessen this undesirable effect.
If you’ve never been “mudding”, give it a try. A good MUD can provide a truly
interesting experience. You can find MUDs all over the Internet; just search the
Web for the word “mud”.
A QUICK LOOK UNDER THE HOOD 11

A Quick Look Under The Hood

Most games have a lot in common behind the scenes. The engine, or main code,
of a “typical” game (if there is such a thing) can be logically divided into several
subsystems: the Input Subsystem, the Display Subsystem, the Audio Subsystem,
the Networking Subsystem, the Update Subsystem, and the main loop. These
subsystems are rarely labelled as such, but you are likely to find all of these
components in any given game engine. Each subsystem is most often
implemented with several separate source files; two or three in small games, but
easily hundred or more in a large production. We’ll look briefly at each of these
subsystems now, and explore the implementation of each through the rest of the
book.

This Code Is Awful!

If you ever get a peek at the code behind a major commercial game,
please do not take it as a treatise on proper software design or coding!
Games often start out as well-designed software, and they sometimes
even make it to the shelves in a tolerable state of internal organization,
but more often than not a game’s code falls into disarray during the last
few months of development. Why, you might ask? The gaming industry
is volatile, dangerous, and extremely competitive. Game studios seem
to find themselves in a perpetual struggle to meet release deadlines, get
their games out ahead of their competitors, and implement the features
that players demand, lest they be left in the dust with a stack of unsold
games. This often results in extremely hurried and sloppy code.
Unfortunately, this often causes serious problems if someone later tries
to add an expansion pack to the game or port the game to another
operating system.

The Input Subsystem

The input subsystem receives the user’s commands through an input device (like
the keyboard or a joystick) and records these commands for further processing.
While input device programming is not difficult, it should be done carefully,
because flawed input processing can easily ruin an otherwise excellent game. The
12 CHAPTER 1. THE ANATOMY OF A GAME

first version of Apogee’s Rise of the Triad (a first person shooter from several
years ago) suffered from particularly bad input handling, and the game was
aggravating to play until this problem was fixed.
One of the input subsystem’s most important jobs is to simultaneously support a
variety of input devices. A well-written input subsystem should be able to
integrate just about type of oddball game controller with minimal effort (this is
made a bit easier by libraries like SDL, but it’s still something to keep in mind
as you code). Some players prefer to use joysticks rather than mice, and an input
subsystem should be able to accomodate this preference without modification to
the main game code. As far as the game is concerned, the joystick should appear
as a generic device, capable of producing “left”, “right”, “up”, and “down”
commands. We will discuss SDL’s input handling and abstraction in 4, and we’ll
touch on the lower levels of input handling in Linux later on.
Nearly every game on the market allows you to remap the keyboard and other
input devices to your liking, and this is a feature that players demand. Many
people have non-US keyboards with different key locations, and you’ll end up
cutting off a lot of would-be players unless you allow them to configure the game
to work with their keyboards. Fortunately, this is not difficult; it can be
accomplished with a simple lookup table. It is also a good idea to allow the
player to store and retrieve multiple key mappings, in case a friend prefers a
different configuration.

The Display Subsystem

The display subsystem conveys the game’s status to the player in a visually
impressive way, whether through simple 2D graphics, or advanced 3D rendering
(the method doesn’t matter, as long as it is appropriate for the game).
Regardless of the type of graphics produced by the display subsystem, the
structure of the code is substantially the same.
The display subsystem is responsible for taking advantage of the available
display hardware. Serious gamers often equip their machines with snazzy 3D
graphics cards, which can bring enormous performance and quality improvement
to 3D games. However, utilizing these devices is not automatic, and requires
special effort by the programmer, which is usually accomplished through a game
programming API like OpenGL. 3D acceleration is beyond the scope of this
book, but we’ll demonstrate how to get OpenGL up and running in 4.
A QUICK LOOK UNDER THE HOOD 13

Before you can show off your graphics code, you’ll need something to display.
Although it is common for a programmer to develop temporary artwork for
testing purposes, few are skilled artists, and it is usually necessary to enlist the
help of a skilled digital artist to produce acceptable game artwork. Players are a
finicky bunch, and they are most intolerant of sub-par graphics. Game
programmers should spend a great deal of time developing a good graphics
engine, and a designer should place a high priority on obtaining quality artwork
for a game.

The Audio Subsystem

Although computer audio technology has not been hyped as much as computer
rendering technology during the past few years as, a game’s audio subsystem is
every bit as important as its graphics subsystem. Fortunately, producing
high-quality sound on a computer is not as difficult as producing high-quality
graphics.
Sound is easy to play back (usually a simple matter of a few function calls with a
multimedia toolkit), but creating production-quality sound effects for a game is
as much an art as creating graphics, and should be left to a specialist. Stellar
sound effects can boost a game’s atmosphere, and lousy sound effects can
seriously damage a game’s potential.
3D enhanced audio is one of the latest trends in computer sound technology with
modern sound cards (like Creative’s SB Live! series) supporting four-speaker
surround sound, and 3D-aware sound processing to simulate the Doppler effect
and other complex sound wave interactions. (Simple two-channel stereo sound
just falls short of the immersive environments of today’s 3D games.) In fact,
some sound cards can even accelerate these effects in hardware. Several
competing 3D sound API’s have emerged, and we will discuss one of these
(OpenAL) in Chapter 5.

The Network Subsystem

Multiplayer gaming is very popular these days, and it is reasonable to assume


that this trend will continue. The network subsystem connects a game to other
computers over a network so that multiple players can participate in the action.
Network programming is not as difficult as it used to be, especially with the
Discovering Diverse Content Through
Random Scribd Documents
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like