Computer Game Basics
UNDER CONSTRUCTION
by
Theo Pavlidis
(Draft of October 30, 2006)
Copyright © 2006 Theo Pavlidis
Contents
- Introduction
- How Can We Reduce Platform Dependence
- Event Driven Programs
- Overview
- Graphics
- Example: Some Simple Glass Bead Games
- Elementary Animation
- Using Idle Time for Motion
- Example: A Glass Bead Game with Moving Beads
- Using Multithreading for Motion
- Moving Object Interaction
- Real Animation
- Transparent and Semi-transparent Bitmaps
- Overview
- The Hard (but General) Way
- Taking Advantage of the Alpha Channel
- Bitmaps with Transparency using Masks
- Trading Quality for Implementation Simplicity
What is the essence of game programming? We need code that responds to
user actions but also code that executes independently of the user. For
lack of a better name I will call such code background code because
it executes in the background of user actions. We also need code that
displays graphics both in the background and in response to user actions.
And last but not least we need the game logic. While some languages and
development environments make game programming easier than others one
could write game programs in any language including assembly. This is
how some of the early games were written although today you should stay
away from such low level coding unless you have a taste for pain.
You have to keep in mind the fact that games can be written in any language
if you intend to write game programs that run on small platforms such
as PDAs or cell phones or if you plan to move a game from one platform
to another.
If you want to make porting easy, you should try to keep as clean an
interface as possible between the game logic and the parts that handle
user interactions because the latter tend to be platform dependent.
Here is a quick look at the main parts.
User Interaction: When the user, say, presses a mouse
button, the computer generates an interrupt and the operating system invokes
an interrupt handler. Many systems today facilitate that task by collecting
user actions in a queue of structures that are called events or messages.
This queue also contains structures generated in response to actions of
other programs. The application program enters into an infinite loop that
examines the messages in the queue and invokes functions to handle each
such event. In modern systems you do not have to be concerned with the
loop and the handling of the events/messages but only with the functions
that handle such evens/messages. Such a program is called an Event
Driven Program and people also talk of Event Driven Programming
or Event Driven Programming Languages. I find the latter a misnomer because
with the proper support from the operating system you can write event
driven programs in any language (certainly in C). For illustration, here
is a list of names in different systems of the function that deals with
the response of the program when the user presses a mouse button.
| Java Abstract Window Toolkit |
mousePressed(MouseEvent e) |
| Flash | onClipEvent(mouseDown) |
| Microsoft Visual C++ with MFC |
OnLButtonDown(UINT nFlags, CPoint point) |
The names of the functions may change and this is not the place to document
them. We will discuss event driven programming in more detail later.
Background Computing and Animation: We need some place
for the code that deals with all other aspects of the game besides the
user actions. There are two ways to implement these parts. One is to ask
the main program to perform these actions when the event queue is empty.
This is called Idle Time Processing. Another is to use
Multi-Threading, if the platform we use supports it.
In this case we place the computation in functions that are executed independently
of the main program. We discuss both methods under the heading of animation.
Object Oriented Programming (OOP): This style of programming
is particularly appropriate for games, since character/player in the same
can be made an object. While C++ and Java are designed to support OOP,
one can write such programs in any language that supports structures,
for example C. (After all C++ started life in the early 1980's as a C
pre-processor!) Because this style is in widespread use today, I will
assume the reader is familiar with it.
Next Section |