Linux Game Programming W CD Prima Tech S Game Development 1st Edition Mark Collins Instant Download
Linux Game Programming W CD Prima Tech S Game Development 1st Edition Mark Collins Instant Download
https://ebookname.com/product/linux-game-programming-w-cd-prima-
tech-s-game-development-1st-edition-mark-collins/
https://ebookname.com/product/the-sims-3-prima-official-game-
guide-prima-official-game-guides-catherine-browne/
https://ebookname.com/product/game-programming-with-python-game-
development-series-1st-edition-riley/
https://ebookname.com/product/mac-game-programming-1st-edition-
mark-szymczyk/
https://ebookname.com/product/e-commerce-the-cutting-edge-of-
business-2nd-edition-kamlesh-k-bajaj/
https://ebookname.com/product/health-the-basics-10th-edition-
rebecca-j-donatelle/
https://ebookname.com/product/the-brain-in-context-a-pragmatic-
guide-to-neuroscience-1st-edition-jonathan-d-moreno/
https://ebookname.com/product/the-second-decade-of-transition-in-
emerging-europe-1st-edition-ognjen-radonjic-and-srdan-kokotovic/
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.
Work In Progress i
3
4 CONTENTS
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
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
i
ii WORK IN PROGRESS
Chapter 1
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.
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
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.
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
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 (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
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
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.
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 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 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.
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.
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.
• 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 comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
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.
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.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
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.
ebookname.com