You are on page 1of 10

INTRODUCTION (To the First Edition)

This book is meant for computer science students and computer science teachers
alike who want to get the same graphics capabilities that certain special computer
wizards - usually teenage males - are constantly using. You, the student or teacher,
have no idea what these kids are doing. You do know, however, that they control
more colors, that their programs generally move faster, and that create all sorts of
dazzling effects, but you really have no idea what is going on.

Somehow you feel that these graphics wizards belong to a unique society. They are
like magicians, their knowledge carefully guarded and shared with those who belong
to the same secret society. Like magicians, everything they do appears effortlessly
simple, and yet, the results are amazing. Where your graphics images move with
sluggish hesitation, their graphics sprites bounce around with ease. Where you have
proudly mastered the sixteen basic VGA colors, they display a dazzling variety of
many different shades of each one of your 16 colors. Where you finally after much
effort managed to slowly save and load some graphics to and from a file, their
images leap onto the screen. You do not know what they are doing, exactly, but you
know that their way is better than yours. You can see the results. The society,
however, is closed, and membership is open only to those who speak the special
language.

Your feelings are shared by many students and teachers alike. Many people look at
their computer science textbooks and carefully follow the examples outlined in the
various chapters, but nothing seems to produce results similar to those of their more
gifted neighbors. For teenage students who do not belong to the society, there is no
problem. Many decide it is somehow genetic, and they simply do not have the
special genes.

Adults, and teachers especially, are more likely to feel uncomfortable. College
computer science courses place almost no emphasis on computer graphics
programming. The few courses that do exist on computer graphics start in outer
space on day-1, and end up in the next galaxy by day-5. An extremely brief look at
the course description discourages all but the very motivated. This leaves most
computer science teachers, perhaps like you, to the tried and true method of learning
the material on your own. Enthusiastically you sit behind your computer and
struggle through the supplied reference manual. Piece by piece, bit by bit, various
routines are making sense and start to take shape. You are happy and proudly walk
into the computer lab . . . and then you see the graphics on the screens of some of

Chapter I Introduction -1.1-


your students. Suddenly your hard earned graphics display does not look so good
anymore.

You also feel discouraged when you look at the code of these graphics programs. It
is not familiar, and it does not even resemble any normal program. The code may
look something like the code shown in the sub programs below.

─────────────────────────────────────────────────────────────────────────
void SetMode(unsigned Mode)
{
_AX = Mode;
geninterrupt(0x10);
} // void function setmode

Byte GetPixel(int X, int Y)


{
return peekb(0xA000,X+Y*320);
} // function getpixel

void SetPalette(PaletteType P)
{
outp(0x03C8,0);
for (Byte Count = 0; Count <= 255; Count++)
{
outp(0x03C9,P[Count].Red);
outp(0x03C9,P[Count].Grn);
outp(0x03C9,P[Count].Blu);
} // for count
} // void function setpalette
─────────────────────────────────────────────────────────────────────────

The code of these three routines includes some pretty bizarre looking stuff. Program
code that includes words like geninterrupt, outp, peekb, combined with strangely
positioned 0x numbers does not make much sense. What is going on, and where do
these kids get this stuff anyway?

What is going on is plenty logical. The purpose of this book is not only to give you
the power of advanced graphics routines, but also explain how such routines work.
You are about to enter the secret society, but remember, introduction into this society
is limited to those who have the power . . . and the power can only be acquired with
knowledge. This book combined with hands-on practice will give you the necessary
knowledge.

Too many graphics books plunge the poor readers into large obscure looking
routines that are very interesting, especially to the author of the graphics book, but
leave the reader bewildered. In this book, you will be looking at many small
programs that focus on one concept and guide you, with manageable increments,
from introductory basics to considerably complex concepts.

Chapter I Introduction -1.2-


Keep in mind that computer science concepts cannot be learned from the comfort of
a soft couch. You need to study the programs provided in this book and either type
in the examples or load the programs from the companion disk. After you see how
the program examples work, it is up to you to "play" with the programs. Alter the
variable values, and change things around. As you make these changes you will see
patterns emerging, and in the process, you will learn. After all, the whizkids who
use this stuff did not learn it by sticking their head inside the computer. They
looked, they listened, they read, they experimented, and then they learned. True,
many of these kids behave as if such knowledge can be soaked up from the cosmos
by breathing oxygen. They are surprised you do not understand, because "you do
breathe oxygen, don't you?"

Don't be fooled by the fact that such kids give the impression that everything is so
easy. I have seen students in my class struggle to learn new concepts and after
considerable effort and agony they learned something new and exciting ... only to
turn around to see other struggling students who had "not yet arrived." These
beginners-turned-experts act as if there is no complexity to be concerned about, and
they seem surprised that anybody would be "confused about such simplicity."

You might wonder what knowledge a person should have in order to handle the
material in this book. You do realize that "Advanced Graphics" is not meant for the
person who is at the stage of wondering how to format a diskette. The explanations
in this book assume a good understanding of programming in C++. At a minimum
you should be comfortable with the amount of computer science that is normally
taught in the "A" level A.P. Computer Science course.

People learn at their own pace. Take this book, and go as quickly or as slowly as
you like. We hope that you will enjoy the material presented here, and that it will
give you a better understanding of computer graphics.

PROGRAM STYLE CONVENTIONS


Programming styles fluctuate rather wildly, and many professional programmers and
computer science instructors feel rather strongly about their particular favorite style.
There is only moderate agreement in the computer science community on several
major features and considerable disagreement on the majority of fine points. But
most people do not really care because the one rule that everybody agrees on is that
any style selected should be consistent throughout the program.

The program examples printed in this book come directly from the companion disk.
All printed examples were compiled and tested before being loaded into the word
processing file. Whether you look at the code in this book or on the disk, it will
follow the same style. It will be helpful to understand some of the conventions that

Chapter I Introduction -1.3-


are followed. This understanding will assist you in making sense of the many
program examples.

ALL TURBO C++ KEYWORDS USE LOWER CASE

All Turbo C++ reserved words like void, struct, if, for, while, etc., must be
written in lower case, and are always shown in lower case.

All Turbo C++ predefined identifiers like cout, cin, getch, clrscr, etc. are
also shown in lower case.

This convention is helpful. For example, you may be encountering


something you have not seen before like peekb or geninterrupt. It actually
turns out that these are keywords in Turbo C++. You can get additional
information about such words in the Turbo C++ reference manual. In the
Turbo C++ environment type a word such as peekb or geninterrupt, place
the cursor on any part of the word and press <Ctrl-F1>. This will activate
the Turbo C++ on-line help feature, and a window such as the one shown
below will pop up on the edit screen.
┌───────────────────── Help ──────────────────────┐
│ ┌────────────┐ │
│ │geninterrupt│ <DOS.H> │
│ └────────────┘ │
│ Macro that generates a software interrupt │
│ │
│ Declaration: void geninterrupt(int intr_num); │
│ │
│ Remarks: │
│ The geninterrupt macro triggers a software │
│ trap for the interrupt given by intr_num. │
│ │
│ The state of all registers after the call │
│ │
└─────────────────────────────────────────────────┘

This feature is explained here because a manual on Advanced Graphics will


use Turbo C++ capabilities that often are not used by any other style
programs and may be quite foreign to the majority of C++ users.

USER-DEFINED IDENTIFIERS USE UPPER CASE LETTERS

Chapter I Introduction -1.4-


All user-defined identifiers start with upper case letters. Examples include
Color, Count, X, Y, Row, Col, etc. If the identifier is long, especially with
two or more words involved, significant parts also start with an upper case
letter. Examples are GetPixel, DrawPicture, CenterX, CenterY, etc.

ALL CONSTANTS USE UPPER CASE LETTERS

Any constants will be completely capitalized like OPEN, CLOSED,


VGA_RAM, TRUE, FALSE. The underscore character is used for
readability as shown with VGA_RAM.

One potential area of confusion comes with abbreviations. A user-defined


identifier like RedGreenBlue is normally abbreviated and becomes RGB.
This makes it look like a constant, but in reality it is a variable. The same
occurs with single letters such as X and Y. Hopefully, those few cases will
be clear enough from the context of the program and will not cause
confusion.

▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄

INTRODUCTION (To the Second Edition)


The introduction to the first edition is important, and it was reprinted intentionally.
The reasons why computer science teachers and students alike have difficulty with
learning advanced graphics are also true with the second edition, and the secret
society is still alive and well. The intention of this part of the introduction is not to
repeat what was said earlier, but to point out how the second edition is different from
the first edition.

Some readers may be aware that there never existed a first edition of this book.
Reprinting the introduction to a "first edition" that never existed may seem quite
bizarre. The first edition of Student Friendly Advanced VGA Graphics was written
in Pascal. VGA graphics is based on concept that are not necessarily language
dependent. Both the Pascal and C++ second editions are based on the groundwork

Chapter I Introduction -1.5-


that was started with the first edition. As a matter of fact, the concepts and program
examples of the Pascal and C++ editions are identical. Any statements that follow,
about differences with the first edition, specify how the VGA graphics concepts are
presented in a different manner.

USING BIOS FUNCTION IN THE BEGINNING


The first edition begins by introducing a technique for drawing a pixel that is
efficient and powerful, but it is also relatively complex. In the spirit of being more
"Student Friendly" the second edition introduces pixels in a simpler manner. The
simpler approach does draw pixels quite as fast The resulting graphics objects are
drawn in a slower manner, but it is easier to understand. Anybody who already has a
considerable amount of VGA graphics knowledge should not scoff at the routines in
the early part of the book, and anybody who finds that the graphics objects draw
slowly feel assured that it will speed up very, very considerably in a later chapter,
where more advanced routines start optimizing early, slow approaches.

DRAWING GRAPHICS IN 640 X 480 X 16 MODE


The first edition was strictly designed for creating graphics images on a screen with
320 X 200 pixels and 256 colors. The second edition also shows how to draw
objects on a screen with 640 X 480 pixels and 16 colors. This does mean sharper
looking graphics, but before you get too excited about this edition keep, in mind that
the finer pictures result in a slower execution. It also means that colors are severely
limited. The main focus of this second edition is the VGA 320 X 200 X 256 mode.
The BIOS method introduced first does allow the flexibility of other video modes,
but it is also very slow compared to the more optimized versions. Throughout the
book, mention is made about methods to accomplish certain tasks in a higher
resolution graphics mode.

MORE "CORRECT" ROUTINES ARE INCLUDED


The first edition provided graphics routines and functions that were unconcerned
with "correctness" issues such as rectangles, circles and other graphics objects
wrapping around the monitor and showing up at weird places. No protection was
provided for boundary issues. The second edition includes a chapter on the subject
of correctness in graphics routines. This chapter allows the user to select between
the conventional, faster-executing graphics routines that may cause problems and the
corrected, slower-executing routines that check for special situations and avoid
problems.

Chapter I Introduction -1.6-


A LIBRARY OF MOUSE ROUTINES
We listened after the first edition. Many people asked how to use a mouse with
graphics programs. A new chapter is added with many detailed mouse functions and
functions that allows you to write video games that have mouse interaction. With
hindsight we realize that this chapter should have been added to the first edition.
You will be pleased to know that these routines are not complex at all but unless you
know the special function calls to use a mouse, it becomes quite complex. Once
again the "secret society" is not telling, but we are. I think you will be pleased with
mouse library provided in the new chapter.

BETTER ICON EDITOR AND ICON EDITOR SOURCE CODE


The first edition provided an icon editor that let you create your own graphics
images in an efficient, convenient manner. The second edition also contains the
executable file of an improved icon editor on disk with a written explanation in the
book. In addition the second edition provides the C++ source code for two icon
editors. The first is very simple and allows you to see the logic of interaction
between the mouse and drawing pixels. The second icon editor is more
sophisticated and demonstrates a variety of more advanced features, such as drawing
a line, rectangle, circle and ellipse, as well as providing the user an opportunity to
save and load graphics images. Also, a special, full-screen editor with many bells
and whistles is provided. The code for this editor, however, is not. The intention of
the source code is to help teach you how many of the features in this book can work
together to create a very practical program. In that spirit the icon editors are
provided to teach rather than show examples of perfect graphics editors.

IMPROVED ANIMATION
Additional program examples have been provided in the area of animation. The first
edition did show many examples, but it lacked programs with moving sprites over a
colored background without destroying the background. We realized that all the
examples showed movement over a black background. The second edition
addresses that shortcoming, and provides many detailed steps from clumsy, jerky
animation to a dozen sprites simultaneously moving across the monitor.

IMPROVED TEXT DISPLAY IN GRAPHICS MODE

Chapter I Introduction -1.7-


The second edition shows how to display text characters very easily in graphics
mode using the cout function. It also demonstrates how to create "stroked fonts,"
in addition to fonts created by an icon editor. The stroked fonts are scaleable and
more efficient than earlier methods. This edition allows you more options in
creating text with your graphics pictures.

3D GRAPHICS EXPLAINED
In creating the first edition, we ran out of time when we reached the 3d graphics
chapter. Routines were provided, but explanations were not given. The second
edition starts with a variety of simpler 3d graphics routines that are admittedly slow,
but easier to understand. More advanced 3d graphics is slowly introduced, and we
hope that the explanations make sense. You must realize however that this book in
general and 3d graphics more specifically requires a good understanding of
mathematics. Anybody who thinks that the Sine of X is a circular board with a big
letter X on it might find the going tough.

SOUND APPENDIX
Sound is not an area of graphics, but many graphics programs, however, do include
sound effects and many people asked for information about sound. An appendix is
included that basically is a short chapter with a number of sound programs. It also
includes a Sound library that gives you the power to create your own music.

A WORD ABOUT OBJECT ORIENTED PROGRAMMING


We had considerable discussion on the issue of OOP. Graphics lends itself very
naturally to Object Oriented Programming. The elegance of using OOP in our
graphics routines conflicted directly with the "Student Friendly" approach. VGA
graphics concepts are not dependent on understanding OOP, and we decided that
OOP would add a level of unnecessary complexity. We are taking the attitude that
those readers who are comfortable with OOP should be able to use the graphics
information presented in this book and create appropriate classes in a manner that is
more typical of a C++ program.

IN CONCLUSION

Chapter I Introduction -1.8-


Please realize that this book was not designed to be a reference manual in the
conventional sense. This book is meant to be a tutorial in writing graphics
programs. This means that many topics go through various stages to make the
learning simpler. In other words, somebody who has not learned from this book and
uses the material as reference may well pluck out a routine that is quite inefficient
and unhappy with the result. Only after you study the chapters in sequence does it
become clear which routines are best suited for which purpose. This is especially
true for the early chapters where graphics images are drawn at an agonizingly slow
pace for the sake of simplicity.

If you have purchased this new graphics book, you should have received a diskette
containing all the sample programs listed in the text. Having the programs available
can save you the time of entering it in manually, but typing in the code yourself can
help you achieve a better understanding of the concepts described.

We have suffered, agonized, rejoiced, and learned by the creation of this book. We
sincerely hope that our efforts will make it simpler for you and other people
interested in graphics to learn this fascinating subject. Please do not think that this
book is the final authority and complete bible of graphics. The subject of graphics is
huge in scope, but we do think that a suitable amount of material is presented that
allows you to explore graphics and write some fun graphics programs on your own.
Our wish is that this book will be useful for you and that learning graphics will be
more fun than frustration.

The second C++ edition authors,

David Quarfoot, Leon Schram, Jaspreet Sidhu, Robby Slaughter, and Jason Wang

Chapter I Introduction -1.9-


Chapter I Introduction -1.10-

You might also like