Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
## Introduction In the beginning, there was darkness. In this Age of Innocence, the unwashed masses practiced the art of entertainment with small pieces of paper bearing symbolic images of kings, queens, and jesters. Sometimes, they used minute pieces of metal and plastic, shaped like various real-world objects, moving them about on large, colored cardboard sheets in accordance with the runes inscribed upon small white cubes. To the casual observer, all was content, but in the minds of the newest generation born of this age, there was a yearning for more. Unbeknownst to the world, this yearning grew, and soon these young pioneers began working in the dark recesses of colleges and universities to bring about a change that would alter the course of history in ways no learned scholar could have ever imagined. Brandishing vacuum tube sabers and using arcane words of power such as RAM, ROM, Bit, and Byte, these acolytes of technology brought forth a new creation known as the Computer. But, their new creation was naive, and lacked intelligence or sophistication; to control it, they had to use a mystical, magical language known as Code. Thus, the Art was born, and a new era had begun. The First Age was marked by the genesis of immense, hulking beasts that required enormous resources to sustain life. These new life forms were rare, but typically stood several stories tall and required incredible life support systems to maintain an agreeable environment. These giants were moody and possessed little intelligence, but through great effort, they could be trained to perform incredible feats, for their day. The digital landscape was dotted with few of these behemoths, and their trainers were monk-like in their dedication. Unable to break free of their confines due to the immense size of their charges, these monks were forced to toil maddeningly long hours. Their torturous task of preparing the vegetarian meal of holy leaves consumed by these monsters had them working late into the night, and the Art required to create these holy leaves was arcane indeed. These monks practiced the great Art in the same dark recesses from whence it was conceived, and the Art grew. The once young pioneers had become pedantic, wizened old masters. These old wizards were still rebellious and, yearning for a new form of entertainment, they used the Art in ways that the Powers That Be felt were unproductive. Away from the watchful eyes of their overseers, they shaped the Art in ways that previously existed only in their dreams. In the dark hours of the morning, these masters used the Art to coax the new creations into providing their long-sought-after entertainment, and they played games such as Tic-Tac-Toe and Pong. Several of these wizards used the Art to create much more sophisticated forms of entertainment, and played games such as Checkers and Chess with their silicon automatons. In the wake of the First Age, walking in the shadow of the evolving Art, a new form of the Art began to grow. Like dinosaurs, the reign of these behemoths was relatively short-lived. A new catalyst known as the Corporation had used the knowledge learned in the First Age to effect an evolutionary step known as the Home PC. Thus, the Second Age began. In the Second Age, the pantheon of overlord gods began to form, controlling the evolution of this new creation from on high. Deities known as Gates and Jobs worked their mystical creation power behind the curtains of reality, but their new creations were protozoan in nature. A step back from their massive predecessors, these new single-cell machines were much less sophisticated and powerful, and teetered on the edge of extinction almost as soon as they had appeared. However, these new creatures had become visible to the unwashed masses, and began to elicit their attention. At first, few of the Untrained could afford to adopt one as a pet, but their popularity grew, and soon many of these creations made their homes on desktops in homes and businesses. The Art, still arcane in nature, had begun to emerge from its dark recesses, and several of the Untrained began to delve into its mystical powers. The Net, the once-dark netherworld populated solely by the wizards and masters of the Art, has now become a tourist attraction, replete with amusement parks, rest stops, and gift shops. Rumors of new CPUs with speeds in excess of 1,000 MHz are being whispered in the dark corners of cyber cafÈs and coffee shops. Thus, we stand at the dawn of yet another New Age. As the sun breaks over the distant horizon, its golden, shimmering rays cascading over the silicon juggernaut that stands before us, the prospects of the future are at once both exciting and frightening. Thus, our journey begins. ### The Purpose of This Book The primary goal of this book is to teach readers graphics and game programming using 8-bit color images under DirectX in the Delphi environment. | The majority of the book is dedicated to the mechanics of creating a game in Delphi, and will therefore be concerned mainly with exactly how to do something, such as fast flicker-free animation. Some parts address more theoretical issues, such as game design, and are very light in their coverage. Other parts are at a more intermediate level, such as artificial intelligence and optimization techniques. The techniques discussed in this book are very practical and can be put to use right away, as opposed to being academic theory. The goal is to give the Delphi games programmer the tools he/she needs to start competing in the shareware games market right away. This book will not cover marketing, package design, distribution, or other topics that do not deal directly with creating the game. | Due to the popularity of DirectX in the game programming industry, this book does not cover anything related to OpenGL. However, Wordware publishes an excellent OpenGL book aimed at Delphi programmers titled Delphi Developer’s Guide to OpenGL (ISBN 1-55622-657-8). Although this book is DirectX-specific, some chapters may include code examples that run directly under Windows in order to illustrate a complex topic more clearly. This book is concerned with high-performance graphics programming under DirectX, and as such will concentrate only on full-screen, exclusive mode DirectX programming techniques.
| This book will not, as others have, create a “framework” from which examples are drawn, nor will it create wrappers or attempt to hide the low-level DirectX API. This is so that readers become familiar with DirectX, not some artificial abstraction layer that might not work for their purposes. The closest to a “framework” that we’ll get in this book is the creation of a baseline application that simply gets a page flipping, full-screen DirectDraw application up and running, including mouse support. This was done in order to simplify the creation of all the examples included in the book, but it certainly doesn’t shield the developer from any of the nuances or pitfalls of DirectX programming. If you want to do game programming, you have to get in bed with the operating system, the API, and the hardware.
#### Conventions Descriptive text that details steps for implementing or using a specific technique, or narrative text that explains an algorithm or theory, uses a font style similar to the one used for this sentence. Text that contains program code uses a monospaced font style similar to the code snippet below:
    {draw the paddle graphic to the back buffer}

    SrcRect := Rect(252, 500, 339, 506);

    FBackBuffer.BltFast(XPos, YPos, FGraphics, SrcRect,

                         DDBLTFAST_NOCOLORKEY OR DDBLTFAST_WAIT);
Additionally, the text contains several callouts that point out a specific detail or area of note that the developer should be aware of. These include notes, tips, cautions, and glossaries. Notes These are general points of interest that detail specific items the developer should keep in mind when using the described techniques. Tips Tips are general suggestions or hints for improving a specific technique. Tips may also point out an alternative to some specific detail of a technique or algorithm. Cautions A caution callout alerts the developer to specific hazards with a technique or algorithm that could potentially crash the application or the system. These are specific details of which the developer should be keenly aware. Glossary Being somewhat of an introductory text for game programming, and as the vast majority of readers of this book will have little to no game programming knowledge, glossary callouts identify specific words or phrases that are commonly used in the game programming industry. Game programming, like many specialty areas, tends to have a language all its own, and these glossary items will help the novice user get up to speed on the various jargon and technical terms used when describing game programming techniques. #### What This Book Is This book is a tutorial that explains DirectX programming within the Delphi environment. Unfortunately, there are not very many professional Delphi programmers working with DirectX, and there are fewer still who have full-time jobs making games using Delphi. The vast majority of Delphi programmers who will likely pick up this book are application and database programmers professionally, but dabble in game programming at night as a hobby or just to satisfy their curiosity. Thus, this book is aimed at those programmers who are curious and interested in game programming, but have little to no experience. As such, the chapters are arranged in a “how-to” style so that you can easily learn about one aspect of DirectX or another. Additionally, those chapters that deal with DirectX-specific components have a subtle reference layout, so that you can go back and easily find a specific detail about a particular DirectX component or function. | This book could be considered an introductory text for game programmers. To this end, it concentrates on the most common DirectX core components, such as DirectDraw, DirectSound, and DirectInput. Although it covers some advanced techniques, such as force feedback, it is aimed primarily at 2D sprite-based graphics techniques, and does not cover 3D techniques or Direct3D. The techniques herein can be used in scientific or other types of visualization applications, but it is slanted toward game programming. #### What This Book is Not By necessity, this book is not a tutorial on the use of Delphi or the lexical constructions of the Object Pascal language. This book is also not a DirectX reference. The reference documentation that ships with the DirectX SDK is pretty good, and definitely expansive. Any decent DirectX reference would span many volumes. While many of the more common DirectX function calls are examined in detail, you would be wise to download the DirectX SDK so you can have the full reference documentation at your disposal. Additionally, since we are concerned only with creating high-performance game and graphics applications, this book does not cover windowed DirectX programming techniques or overlays. Plus, so we can squeeze the maximum amount of performance out of the widest range of hardware, we focus only on 8-bit color video modes, and do not discuss non-palettized video modes. #### Reader Requirements This book assumes that the reader is familiar with the Delphi IDE and with Object Pascal programming in general. It also helps to have a working knowledge of Windows API programming. While some graphics programming experience is helpful, it is not necessarily a requirement. #### Software Requirements The applications in this book were tested using Delphi 3, 4, and 5 running under Windows 95 and 98. DirectX is not fully supported under Windows NT, so while some of the examples may work under Windows NT, most will not, and none were tested. The examples in this book make use of the excellent DirectX header files written by Erik Unger for the JEDI project. You will find these files off of the root of the CD-ROM in a directory labeled DXSDK. This directory should be copied to your hard drive, and you will need to add the path to this directory to Delphi’s library path. This can be accomplished by clicking the Tools | Environment Options menu item in the Delphi IDE, selecting the Library tab, and clicking on the button next to the Library Path edit box. Adding the path to the DXSDK in this dialog box and clicking OK should allow Delphi to find the necessary files when compiling the examples. Note: You must add the path to the DXSDK directory to Delphi’s Library Path before any of the examples will compile. #### Hardware Requirements All examples were tested on a Pentium 450 with 32 MB of ram and an 8 MB video card. While this is, at the time of this writing, a respectably advanced machine, the examples should run on anything from Pentium 166 with 16 MB of RAM and a 2 MB video card or higher. Additionally, you will need a sound card and speakers for the examples using sound or music output, as well as a joystick or similar device for examples that retrieve input from game devices. Obviously, you will need a force feedback joystick in order to use any of the examples from the force feedback chapter. #### Chapter Organization Due to the “how-to” nature of this book, the chapters have been arranged in a way to facilitate an interesting read from cover to cover. However, this book could also be used as a limited reference manual, so the chapters have been arranged in a somewhat logical order to keep similar topics together. The first few chapters serve as a basic introduction to game programming. Several common concepts and terms are explained early on so as to facilitate an easier understanding of the topics to come. Basic game application architecture is also discussed, including some entry-level graphics programming techniques using regular Windows GDI function calls. DirectX is also introduced at this point, specifically covering DirectDraw. Many intermediate techniques are then covered. These include techniques for use of the system palette as well as many sprite drawing and manipulation techniques. User input is then covered in some detail, including reading input from keyboard, mouse, and game controllers, such as joysticks, using DirectInput. Force feedback is covered to some extent, although this is a more advanced and specialized topic. Sound and music programming is discussed, examining various ways to output sound and music through both DirectSound and Windows multimedia functions. Advanced topics include various ways to optimize game applications, as well as artificial intelligence techniques. Several more specific algorithms are covered that detail how to manipulate bitmaps and create graphical special effects. Most of these techniques are wrapped up into a case study application that highlights how these pieces come together to form a complete game. Installation of DirectX is also covered. ### Chapter Summaries Chapter 1: The Lure of Game Programming looks at game programming in general, and examines why the game programming industry is so special in the world of computer science. Chapter 2: The Anatomy of a Game looks at what goes into a game from a high-level perspective. It divides game applications into three constituent parts and examines each in detail. Chapter 3: Basic Graphics Programming introduces the reader to basic concepts in graphics programming. Several Windows GDI graphics programming techniques are discussed in order to prepare the reader for more advanced topics. Chapter 4: An Introduction to DirectX introduces the reader to the world of DirectX programming. DirectX is examined in a general manner, and then DirectDraw is discussed in detail. Chapter 5: Palettes looks at palette control and manipulation from a DirectX application. Chapter 6: Sprite Techniques examines how 2-D sprites work. Several different techniques are discussed, including sprite animation and collision detection. Chapter 7: Input Techniques discusses the various methods for reading input from external devices. The keyboard, joystick, and mouse peripherals are discussed in detail, examining how input can be received using both Win32 API functions as well as DirectInput. Chapter 8: Force Feedback examines the force feedback API and how it can be used to create tactile feedback on hardware devices. Chapter 9: Sound and Music looks at what it takes to output sound and music from a game application. Using both Windows multimedia functions as well as DirectSound, this chapter examines digital audio output, MIDI music output, and CD audio output. Chapter 10: Optimization Techniques discusses various methods to optimize and enhance the performance of applications. These include Delphi compiler settings as well as programmatic techniques. Chapter 11: Special Effects discusses how the Lock method of a DirectDraw surface gives developers incredible control over visual images. Several techniques are discussed for bitmap manipulation as well as dynamic image creation. Chapter 12: Artificial Intelligence Techniques looks at various methods for implementing simplistic reasoning and logic functions for game antagonists. Such topics include finite state machines and pathing techniques. Chapter 13: Putting It All Together takes many of the techniques discussed throughout the book and puts them together to form a full, working game called Delphi Blocks. Appendix: Installing DirectX contains a short but concise explanation of how the DirectSetup API simplifies the process of installing DirectX to a user’s machine. Glossary contains an alphabetical listing of all of the glossary entries introduced throughout the book.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.