3. Event Driven Programs

3.1. Overview

Computer (and Video) Game Programs are event driven programs. An event driven program executes an infinite loop while it waits for messages from the "system". When it receives a message (for example, that the user clicked a mouse button), it executes a particular function, usually called the message or even handler. I put the word system in quotes because it may bethe operating system or an intermediary (usually called the window system) between the operating system and the application. In well designed systems the details of the loop and the message or event dispatching are hidden from the application writer whose main concern is the implementation of the message or event handlers. For the time being we assume that the terms event and message mean the same, although in such systems have a slightly different meaning.

To make a long story an event driven program consists of a collection of functions with names such as OnMouseMove(), OnPaint(), etc that the developer must implement and which will be invoked, respectively, when the user moves the mouse, when the screen must be redrawn, etc. This style of programming takes some used-to because the flow of the program is not controlled by the developer but by the user. If you have never written event driven programs before you should start with something simpler than a game, such as a calculator. Click here to see a piece of C-like code for a calculator. (Ignore for the moment the statements in dark red.)

This code fragment shows how to respond when the user strikes a key. Of course we also need to display the entries and the results. We cannot rely on asking for display only when there is a change in results because in a window system the display may have to be executed when the calculator window becomes visible after it has been obscured by other windows or after been restored from the iconic form. Thus we we need a drawing functions that has, roughly, the following form.

Draw()
{
	if(valid_result == TRUE) Display(result);
	else Display(n[i]);
}

This is invoked in repsonse to a message that asks to refresh the display. It is possible to generate such a message from another part of the program and that's the role of the call to the function Redraw() in the code of the OnChar() function. These what statements in dark red are for. If you find this discussion hard to follow you should try to flesh out the calculator program, even if does only additions of pairs of numbers.

3.2. Graphics

Historically we distinguish two types of graphics, vector and raster. Originally these terms reflected the hardware used for the display. Vector graphics is the oldest type and such devices were the most commonly used until the mid 1970's where the reduction in the cost of memory chips brought raster graphic devices within the reach of scientific laboratories in Universities and Industry. Eventually prices dropped low enough that for the last 20 years or all graphic displays are of the raster type. Raster graphics devices contain a frame buffer (also called refresh memory) whose contents are periodically copied on the screen.

Today the term vector graphics usually denotes a method where the graphics information is kept in terms of lines, polygons, etc that are copied in the frame buffer. For example, to draw a line we need the coordinates of its two endpoints (four integers x1, y1, x2, and y2) that are passed as arguments into a function such as DrawLine(x1, y1, x2, y2) that actually draws the line in the frame buffer. Splines is a class of curves that are specified by a discrete number of points (often called control points) and mathematical rules that generate the curves.

The above sketch shows two splines and the polygon formed by their control points. The first spline consists of pieces of quadratic arcs and the second by pieces of cubic arcs. The only information we need to store is the coordinates of the six control points. It is possible to construct spline surfaces in three dimensions and, in addition, map on each surface various colors and textures. Thus vectors graphics impose no limit on the kind of an object we can create. In this simple tutorial we will not be using splines but there are many sources to read about them. One good source on the web is http://www.ibiblio.org/e-notes/Splines/Intro.htm.

An alternative drawing method is with bitmaps that are rectangular arrays of pixels. Bitmaps are the only way to display images with variety of gray scales and/or color hues. They can also be used for vector graphics by drawing the lines and polygons in them and then copying the bitmap to the frame buffer. The latter operation (usually carried out through a call to a function called biblt()) is faster than the individual graphics operations. In a window system we may have to redraw frequently the window areas because other windows may obscure and then reveal it. Thus it make sense to first draw on a bitmap and then copy the bitmap (or parts of it) to the frame buffer whenever such an action is needed. We are dealing with a space-time tradeoff and the technological trend has been that computer memory prices drop faster compared to increases in computer speed, so it is advisable to use bitmaps whenever possible.

Here is an illustration of the two methods using pseudocode to stay away from any platform dependence. Suppose you want to have a ball "shot" from one point in the screen to another. A purely vector implementation will have the form

		// Listing A
		x = x0; y = y0;
		do {
			DrawSolidCircle(Screen, x, y, radius);
			sleep for 100 milliseconds
			EraseSolidCircle(Screen, x, y, radius);
			x = x + DX;	y = y + DY;
		} while (x,y is far from xtarget, ytarget);		

where the first argument of the drawing functions refers to the place where the drawing takes place, in this case the frame buffer. If we are going to use a bitmap the code might be like this

	// Listing B
	Create Transparent Bitmap B;
	DrawSolidCircle(B, xoffset, yoffset, radius);
	x = x0; y = y0;
	do {
		Copy B to Screen at x, y;
		sleep for 100 milliseconds
		Erase B from Screen at x, y;
		x = x + DX;	y = y + DY;
	} while (x,y is far from xtarget, ytarget);		

Copying a transparent bitmap requires some additional work that depends on the system. For modern systems with an alpha channel (see 5.1 and 5.3) it is very little.

The choice between the methods shown in Listings A and B is an engineering decision, depending on the amount of memory available and the relative speed of the drawing operations as compared to the bitmap copying operations.

The popular game platform Flash relies on vector graphics and although bitmaps have been included in later version they can be problematic (see description of a Flash bug).

Previous Section Next Section