i
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Programming Windows
Games with Borland
C++
ii
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
iii
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
A Division of Prentice Hall Computer Publishing
11711 North College, Carmel, Indiana, 46032 USA
PUBLISHING
S MS
Programming
Windows
Games
with Borland
C++
Nabajyoti Barkakati
iv
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
To Leha, Ivy, Emily, and Ashley
Copyright 1993 by Sams Publishing
FIRST EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying, record-
ing, or otherwise, without written permission from the publisher. No patent liability
is assumed with respect to the use of the information contained herein. Although
every precaution has been taken in the preparation of this book, the publisher and
author assume no responsibility for errors or omissions. Neither is any liability
assumed for damages resulting from the use of the information contained herein. For
information, address Sams Publishing, 11711 N. College Ave., Carmel, IN 46032.
International Standard Book Number: 0-672-30292-6
Library of Congress Catalog Card Number: 92-82084
96 95 94 93 4 3 2 1
Interpretation of the printing code: the rightmost double-digit number is the year of
the book’s printing; the rightmost single-digit, the number of the book’s printing. For
example, a printing code of 93-1 shows that the first printing of the book occurred in
1993.
Composed in Palatino and MCPdigital by Prentice Hall Computer Publishing
Printed in the United States of America
Trademarks
All terms mentioned in this book that are known to be trademarks or service marks
have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of
this information. Use of a term in this book should not be regarded as affecting the
validity of any trademark or service mark. Borland C++ is a registered trademark of
Borland International, Inc. Windows is a trademark of Microsoft Corporation.
v
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Publisher
Richard K. Swadley
Acquisitions Manager
Jordan Gold
Acquisitions Editor
Stacy Hiquet
Development Editor
Dean Miller
Senior Editor
Tad Ringo
Production Editor
Nancy Albright
Editorial Coordinators
Rebecca S. Freeman
Bill Whitmer
Editorial Assistants
Rosemarie Graham
Sharon Cox
Technical Reviewer
Tim Moore
Marketing Manager
Greg Wiegand
Cover Designers
Tim Amrhein
Kathy Hanley
Director of Production and
Manufacturing
Jeff Valler
Production Manager
Corinne Walls
Imprint Manager
Matthew Morrill
Book Designer
Michele Laseau
Production Analyst
Mary Beth Wakefield
Proofreading/Indexing
Coordinator
Joelynn Gifford
Graphics Image Specialists
Dennis Sheehan
Sue VandeWalle
Production
Christine Cook
Mitzi F. Gianakos
Howard Jones
John Kane
Sean Medlock
Roger Morgan
Juli Pavey
Angela M. Pozdol
Linda Quigley
Michelle M. Self
Suzanne Tully
Barbara Webster
Phil Worthington
Indexer
John Sleeva
vi
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Overview
Preface xiii
Introduction xvii
Part I
Basics of Game Programming with Borland C+
1 Game Programming for Windows 3
2 Windows Programming with Borland C++ 21
3 Graphics Programming with the Windows API 57
4 Understanding Image File Formats 95
5 Animating Images 149
6 Generating Sound 189
Part II
Sample Games
7 SPUZZLE—A Spelling Puzzle 205
8 3-D Graphics 275
9 BLOCKADE—A Game of Modern Naval Simulation 317
Index 495
vii
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Contents
Introduction xvii
Part I
Basics of Game Programming with Borland C++
1 Game Programming for Windows 3
An Overview of Computer Games 5
Common Themes in Computer Games 8
Common Elements of Computer Games 15
Issues in Game Programming for Windows 18
Color 18
Multimedia Games for Windows 19
Summary 20
Further Reading 20
2 Windows Programming with Borland C++ 21
Microsoft Windows Programming
with Borland C++ Classes 23
Model-View-Controller (MVC) Architecture 24
A Windows Application
Using OWL and CLASSLIB 26
Borland C++ Class Libraries 40
Breakdown of the CLASSLIB Classes 41
Template-Based Container Classes 46
OWL Classes 50
Summary 54
Further Reading 54
3 Graphics Programming with the Windows API 57
Windows Graphics Device Interface 58
Device Context 59
GDI Coordinate Systems 65
viii
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Drawing with GDI Functions 68
Drawing Points 69
Drawing Lines 69
Drawing Closed Figures 70
Manipulating Rectangles 72
Regions 74
Drawing Mode 76
Text Output 77
Handling Color 80
System Palette 81
Logical Palette 81
Bitmaps 86
Device-Dependent Bitmaps 86
Device-Independent Bitmap (DIB) Format 92
Summary 93
Further Reading 94
4 Understanding Image File Formats 95
Image File Formats 96
Common Characteristics of Image Files 97
Some Common Formats 99
C++ Classes for Handling Image Files 100
ImageData Class 101
Image Class 105
BMPImage Class 113
TGAImage Class 119
PCXImage Class 125
ImageView—A Windows Image Viewer 136
Running ImageView 136
ImageViewApp Class 136
ImageViewFrame and
ImageViewWindow Classes 139
Building ImageView 145
Summary 146
Further Reading 146
5 Animating Images 149
Animation Techniques 150
Sprite Animation 150
ix
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
C++ Classes for Sprite Animation 152
Sprite Class 152
SpriteAnimation Class 162
A Sample Animation Program 178
AnimationWindow Class 178
The ANIMATE Application 186
Building ANIMATE.EXE 187
Summary 188
Further Reading 188
6 Generating Sound 189
Sound under Windows 190
Programming for Sound 190
A Sample Program 197
Summary 201
Further Reading 201
Part II
Sample Games
7 SPUZZLE—A Spelling Puzzle 205
Playing SPUZZLE 206
Starting SPUZZLE 207
Building a Puzzle 208
Keeping Score 209
Controlling Sound Output 209
Adding a New Word 210
Designing SPUZZLE 211
Window Hierarchy 212
Assigning the Responsibilities 213
Maintaining Information about the Puzzles 213
Implementing SPUZZLE 214
SpuzzleApp Class 214
PuzzleFrame Class 216
PuzzleWindow Class 232
LetterWindow Class 245
ToolWindow Class 252
StatusWindow Class 259
Data Structures for Puzzle Information 265
x
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
High Scores Dialog Box 267
Resources for SPUZZLE 271
Help File 272
Other Files 273
Summary 273
8 3-D Graphics 275
Modeling 3-D Objects 276
3-D Cartesian Coordinates 276
Boundary Representation of 3-D Objects 277
Constructing Objects with Polygons 278
3-D Coordinate Transformations 280
Rotation 282
A Few More Vector Operations 283
Viewing a 3-D Scene 284
Transforming to View Coordinates 285
Perspective Projection 285
C++ Classes for 3-D Modeling 286
Defining the Primitive 3-D Classes 286
Defining the 3-D Scene 301
Loading a 3-D Scene from a File 311
Viewing a 3-D Scene 313
Summary 314
Further Reading 315
9 BLOCKADE—A Game of Modern Naval Simulation 317
Playing BLOCKADE 318
Overview of BLOCKADE 318
Starting BLOCKADE 319
Terminology of BLOCKADE 320
Components of the BLOCKADE Screen 321
Views in BLOCKADE 321
Controlling Simulation Speed 324
Launching Weapons 325
Designing BLOCKADE 325
Simulating the Scenario 326
Viewing the Scenario 326
Game Definition Files 327
Implementing BLOCKADE 327
xi
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Taking Stock of the Source Files 328
The Application Class 331
BlockadeFrame Class 333
LogoWindow Class 353
DisplayWindow Classes 355
ToolWindow Classes 386
StatusWindow Class 402
InfoWindow Class 411
Scenario Class 427
Platform Class 450
Sensor Class 466
Weapon Class 472
Other Files 482
Building BLOCKADE 492
Summary 493
Index 495
xii
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
xiii
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Preface
Computer games are by far the most popular type of software. The market for
game software is huge—practically anyone with a PC can use games. If you
are like me, you probably play your share of computer games and have your
own favorite categories. And because you are a programmer, you might have
thought of many ideas that could be the basis of a game—if only you knew
how to get started on the project. As you will see after reading this book, writ-
ing a computer game is not as difficult as you might have imagined. In fact,
once you see how a game is written, you have a number of well-defined steps
that you can follow to create your own game. After you have had some fun
writing your own game, it may even become a source of income for you.
The basic premise of Programming Windows Games with Borland C++ is that
whether entertaining or educational, all computer games have certain common
elements: graphics, sound, and a mouse- or keyboard-based user interface.
Thus, any programmer (with some imagination) who masters the basic tech-
niques of manipulating images, generating sound, and controlling the mouse
or keyboard can write a computer game. The best way to learn how to pro-
gram games is to see working examples that illustrate how an imaginative idea
can come alive in a computer game with graphics and sound.
Programming Windows Games with Borland C++ teaches intermediate to ad-
vanced level C++ programmers how to use object-oriented programming tech-
niques to write computer games. Borland C++ offers exceptional support for
Microsoft Windows programming, includes the ObjectWindows Library
(OWL)—a comprehensive C function library—and provides a full set of pro-
gramming tools such as MAKE, TLINK, TLIB, and the Turbo Debugger. The
games presented in this book are meant to run under Microsoft Windows 3.1
because Windows offers a rich graphical user interface for the games and, more
importantly, because there is a shortage of games that run under Windows.
xiv
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Programming Windows Games with Borland C++ features the following:
A quick overview of Windows programming with Borland C++ and
ObjectWindows Library (OWL)
C++ classes for image animation and sound generation
Full source code for SPUZZLE, a spelling puzzle for young children
Full source code for BLOCKADE, a game of strategy that simulates
a naval blockade
Disk with source and executable versions of SPUZZLE and
BLOCKADE
Programming Windows Games with Borland C++ starts with an overview of
Windows programming with Borland C++ and OWL followed by several chap-
ters on graphics, image manipulation, and sound generation under Windows.
Then the book presents SPUZZLE—an educational spelling puzzle for young
children. This game teaches spelling through a simple jigsaw puzzle that has
one or more letters associated with each piece of the puzzle. The child has to
place the pieces of the puzzle in the right order (by dragging them around with
the mouse) to complete an image, which spells the word and rewards the child
with a musical tune. SPUZZLE illustrates the basic programming techniques
for animating images and generating sound. This game makes use of a num-
ber of C++ classes that are designed to support image manipulation and sound
generation. A significant aspect of image manipulation is the ability to read,
interpret, and display image files of several common formats (PCX, BMP, and
Truevision Targa). These techniques are described in the book.
The latter part of Programming Windows Games with Borland C++ sketches the
design and development of another more elaborate game called BLOCKADE,
which is a modern naval simulation in which the player commands a combat
ship as it enforces a naval blockade. As commander of the ship, the player
relies on the ship’s sensor systems to decide how to intercept, track, and stop
“blockade runners.”
Programming Windows Games with Borland C++ includes a bound-in disk that
contains the complete source code for the games appearing in the book. This
disk makes it easy for you to try out the games and even enhance them.
xv
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Acknowledgments
I am grateful to Stacy Hiquet for suggesting the idea of this exciting book con-
cept—a book that shows the complete development of realistic computer games
that run under Microsoft Windows. Thanks to Wayne Blankenbeckler for
making the necessary arrangements to prepare the companion disk complete
with a Windows installation utility.
Thanks to Nan Borreson of Borland International for keeping me supplied
with the latest copies of the Borland C/C++ compiler and Application Frame-
work.
As usual, the production team at Sams Publishing did an excellent job of
turning my raw manuscript into this well-edited, beautifully packaged book.
Thanks to all of you at Sams for doing your part with such dedication and
perfection. In particular, thanks to Nancy Albright of Albright Communica-
tions for the thorough editing of the manuscript, and to Dean Miller at Sams
for managing the development of the book.
Finally, my greatest thanks go to my wife, Leha, for her love and support
and for taking care of everything while I went into hibernation with this book
project. My daughters, Ivy, Emily, and Ashley, did more than simply encour-
age me during the project. This being a computer game book, Ivy and Emily
were able to help me by testing the games and providing feedback while Ashley
supervised the entire operation by roaming the halls with her no-nonsense
attitude.
xvi
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
About the Author
Naba Barkakati, Ph.D., is an expert computer programmer and a successful
author. He has written some of the most popular programming titles on the
market, including X Window System Programming; Object-Oriented Programming
in C++; The Waite Group’s Microsoft C Bible, Second Edition; The Waite Group’s
Turbo C++ Bible, Second Edition; and Microsoft C/C++ 7 Developer’s Guide.
xvii
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Introduction
Programming Windows Games with Borland C++ is a book for intermediate-to-
advanced C++ programmers that covers all aspects of writing computer games
under Microsoft Windows. As the book’s title implies, I used Borland C++ and
the C++ classes in the ObjectWindows Library (OWL) to write the games that
I describe in this book. Because graphics (especially image display and anima-
tion) and sound are at the heart of all computer games, I cover image display,
animation, and sound generation in detail. In particular, I discuss how to read
and interpret image files of several formats because you are likely to use
scanned images or images drawn with a paint program as the graphics elements
in a computer game.
I also developed C++ classes that provide the graphics and sound capabili-
ties required by the games. Instead of many small games, I chose to develop
two reasonably complex computer games:
SPUZZLE, a spelling puzzle game for children
BLOCKADE, a naval simulation game for kids of all ages
You will find ready-to-run copies of these games as well as their full source
code in the companion disk.
What You Need
To make the best use of this book, you should have access to a system with
Microsoft Windows 3.1 and the Borland C++ compiler. Then, you can test the
example programs and run the games as you progress through the book. Ad-
ditionally, you will want the following:
A fast 80386/80486 system with a VGA or better display
As much memory as possible (4MB or more)
A reasonably large hard disk (100MB or more)
xviii
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
The system should be fast because the Windows environment puts a lot of
demand on the processor. The extra memory helps, because Windows can use
it with the 80386/80486 processor operating in what is known as the protected
mode. The large hard disk is necessary because all software development tools
seem to require a large amount of storage.
All examples in this book were tested with Borland C++ on an Intel 80386-
based ISA (Industry Standard Architecture) PC with 8MB of memory, a 650MB
hard disk, and a 640x480 resolution 16-color VGA display.
Conventions Used
in This Book
Programming Windows Games with Borland C++ uses a simple notational style.
All listings are typeset in a monospace font for ease of reading. All file names,
function names, variable names, and keywords appearing in text are also in
the same monospace font. The first occurrence of a new term or concept is in
italic.
How to Use This Book
If you are a newcomer to Borland C++ and Windows programming, you should
first get up to speed by using other resources such as Borland’s manuals or one
of the many books on Windows programming with Borland C++.
Once you are comfortable with Windows programming, you should browse
through Chapters 1 through 3 to see how to use Borland C++ and the OWL
classes. Chapter 1, in particular, provides an overview of the different types of
games that are currently on the market.
If you are interested in reading and interpreting image files and animating
images, you should consult Chapters 4 and 5. Chapter 6 gives a brief descrip-
tion of sound generation under Windows.
xix
Contents
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
Chapters 7 and 9 present sample games (SPUZZLE and BLOCKADE) with
full source code. If you want to learn game programming from these chapters,
start by installing and playing the games—they are on the companion disk.
Once you have seen what the game does, you can go to the appropriate chap-
ter to see how a specific feature is implemented.
How to Contact the Author
If you have any questions or suggestions, or if you want to report any errors,
please feel free to contact me either by mail or through electronic mail. Here is
how:
Write to LNB Software, Inc., 7 Welland Court, North Potomac, MD
20878-4847, USA.
If you have access to an Internet node, send e-mail to:
If you use CompuServe, specify the following as SEND TO:
>INTERNET:
From MCIMAIL, specify the following when sending mail:
EMS: INTERNET
MBX:
Please do not phone, even if you happen to come across my telephone num-
ber. Instead, drop me a letter or send an e-mail message for a prompt reply.
Introduction
xx
Programming Windows Games with Borland C++
Two/NRS6 Prog Win Games Borland C++ 30292-6 angela 4-2-93 FM LP#6
1
D2 Prog Win Games Borland C++ 30292-6 MAL 3-24-93 Sample Part LP#1
I
PART
Basics of Game
Programming
with Borland C++
2
D2 Prog Win Games Borland C++ 30292-6 MAL 3-24-93 Sample Part LP#1
3
Game Programming for Windows
Chapter
1
TWO/NS6 Prog Win Games Borland C++ 30292-6 angela 4-1-93 CH 01 LP#5
Chapter
1
Game
Programming
for Windows
4
Programming Windows Games with Borland C++
TWO/NS6 Prog Win Games Borland C++ 30292-6 angela 4-1-93 CH 01 LP#5
If you have browsed through the shelves in a software store recently, you know
the tremendous variety of computer games available on the market. The offer-
ings run the gamut from simple games such as Tic Tac Toe, pinball, and puzzles,
to sophisticated simulations of real and make-believe worlds. Some of the lat-
ter include animated graphics with digitized voices and music generated by
sound boards. Most of the games are designed to run under MS-DOS and only
a few work under Windows. But this situation is bound to change as Windows
gains popularity and game developers see the benefits of writing Windows
versions of their games. In particular, with the multimedia extensions in Win-
dows 3.1, game developers can begin using CD-ROM media and sound boards
to bring a new level of sophistication to computer games.
The device-independent manner in which Windows allows an application
to handle graphical output is another advantage of writing computer games
for Windows. With minimal effort on your part, you can ensure that the same
Windows game works properly on a 16-color EGA display as well as a 256-
color Super VGA display and even the newer XGA display. This device inde-
pendence comes at a price; animation under Windows is slower than that
designed to work in a specific mode of a video adapter. For instance, most
interactive DOS games display fast animated graphics scenes in a specific video
mode of a display adapter, usually mode 13H of the VGA (the 256-color 320x200
resolution mode). The game programmer can exploit all nuances of the adapter,
including the fact that a 256-color 320x200 image fits in exactly 64K—less than
the maximum size of a single segment of memory in 80x86 processors—which
makes image manipulation fast. You cannot use intimate knowledge of the
display when programming a game for Windows, but the speed disadvantage
is gradually disappearing as PCs and display adapters become faster.
This book looks to the future and, in anticipation of a large and profitable
market for Windows games, shows you how to develop computer game ap-
plications designed to run under Microsoft Windows. There are three features
common to all computer games:
Two-dimensional (2-D) and three-dimensional (3-D) graphics
Image manipulation
Sound generation
Of course, you also have to come up with an idea for a game and design the
game; these topics are illustrated through two complete sample games in Chap-
ters 7 and 9.
5
Game Programming for Windows
Chapter
1
TWO/NS6 Prog Win Games Borland C++ 30292-6 angela 4-1-93 CH 01 LP#5
This chapter starts with an overview of some popular computer games.
These are mostly DOS games, but they should give you an idea of the types
that are available on the market and even trigger some ideas for new games
that you might want to write. The brief overview of computer games includes
a classification of the current crop of games, the graphics and sound capabili-
ties that each type of game uses, and the major steps in developing a game.
Chapter 2 introduces the topic of Windows programming with Borland C++
and describes Windows graphics capabilities and how to use Windows Ap-
plication Programming Interface (API) functions for the graphics needed in a
game program. Chapters 3 through 6 cover the subjects of graphics, image
manipulation, and sound generation—the features that are at the heart of any
computer game.
An Overview of
Computer Games
The following is a list of the current crop of computer games divided into seven
categories:
Educational Games. Slowly the line between educational and entertain-
ment game software is getting blurred because developers realize that
an entertaining educational game is a better teacher than a dull one.
Some of the popular educational games are:
Where in the World is Carmen Sandiego? and Where in the U.S.A. is
Carmen Sandiego? from Broderbund Software, Inc., which teach
geography through a game
New Math Blaster Plus from Davidson & Associates, Inc., which
teaches mathematical skills through simple games
Learning Company’s Math Rabbit and Reader Rabbit, which include a
variety of activities to teach young children math and reading skills
Traditional Games. These are the computerized versions of traditional
games such as chess, card games (bridge, poker, solitaire), GO, mah-
jongg, and a variety of puzzles.