Bottom     Previous     Contents

1 Graphics Commands

At first sight the BBC Microcomputer provides a bewildering range of graphics commands. In most applications it is possible to achieve the same result in a number of different ways, and while this provides great scope for individuality and expressive power, it can also appear very baffling to the novice, Superb effects can be produced with a small subset of the many facilities available. The most useful facilities are explained below.

Modes

The MODE command clears the screen and sets up the mode specified. For graphic programs the most important differences between modes are:

  1. The number of colours allowed
  2. The resolution, or size, of each pixel

Modes with high resolution and many colours require the most memory.

MODE Resolution Colours Memory
0 640x256 2 20k
1 320x256 4 20k
2 160x256 16 20k
4 120x256 3 10k
5 160x256 4 10k

MODE 0 provides very high resolution but only 2 colours. In MODE 1 the resolution is quite good and the pixels are square. MODE 2 gives rather chunky graphics but lots of colours. MODEs 4 and 5 can be regarded as watered-down versions of 1 and 2, and are the only graphics modes available on the Model A (l6k) machine. All the programs in this book use MODES I or 2, but they can easily be converted to run in MODEs 4 or 5.

Pixel sizes

All graphics modes use the same coordinate system to refer to points on the screen. The screen of the monitor or TV set is divided into a grid of hundreds of tiny squares (or, more accurately, rectangles).

There are 1280 squares from left to right on the screen (graphically speaking the x-axis) and 1024 squares from top to bottom (which by the same analogy can be thought of as the y-axis). The origin (0,0) is at the bottom left-hand corner of the screen.

To refer to these squares individually we use the same method as used to refer to points on a graph: each square is referred to by its (x,y) coordinates.

Every picture drawn on the screen is composed of clusters of these squares called 'pixels': if magnified, even rounded shapes on the screen will be seen to be made up of squares. Small pixels (composed of few squares) will give a smoother curve and a more accurate picture than large pixels, and so small pixels are said to give 'high resolutlon' images, whereas large pixels cause chunky, jagged results.

The only disadvantage of having small pixels is that more pixels are needed to fill the screen and so more memory is required. In all modes the pixels are composed of more than a single square on the coordinate grid.

MODE Pixel size in coordinate units
0 2x4
1 4x4
2 8x4
4 4x4
5 8x4

One of the advantages of the coordinate system is that programs can be run in different modes since the size and shape of an object drawn will be the same regardless of the mode.

Drawing

All drawing is done by controlling the location of the graphics cursor. The cursor is like the tip of a pen with which you would draw on a piece of paper. You can control where the cursor moves and whether it draws a line as it moves. The location of the graphics cursor is not shown on the screen.

Useful commands

MOVE X,Y

Move the cursor to the point (X,Y). Note that X and Y can be numbers, expressions, variables or functions.

DRAW X,Y

Draw a line from the current position of the cursor to the point (X,Y), It does not matter if (X,Y) is off the screen.

PLOT 69,X,Y

Put a dot at (X,Y).

PLOT 85,X,Y

Fill in the triangle formed by the last two points visited by the cursor and the point (X,Y),

PLOT 0,X,Y

Move the position of the cursor relative to the current position.

PLOT 1,X,Y

Draw a line relative

PLOT 81,X,Y

Fill the triangle formed by the last two points visited and the point obtained by moving (X,Y) relative to the current position.

Examples

(Note: Example programs are not provided on the Creative Graphics cassette,)

  1. This sequence will fill a triangle with vertices at (200,200), (800,200) and (200,800):

    l0 MODE 1
    20 MOVE 200,200:MOVE 800,200:PLOT 85,200,800
    30 END
  2. This will scatter white dots at random over the screen:

    l0 MODE 1:REPEAT
    20 PLOT 69,RND(l279),RND(l023)
    30 UNTIL FALSE
  3. A random walk:

    l0 MODE 1
    20 MOVE 640,512
    30 REPEAT
    40 PLOT 1,RND(3l)-l6,RND(3l)-l6
    50 UNTIL FALSE

Colours

To understand the way in which colours are controlled it is important to grasp the idea of 'physical' and 'logical' colours. Actually, it is best to discover how the palette works through trying it out at the keyboard, but here is an explanation of sorts.

The logical colour is just a number which is assigned to any of l6 physical colours and causes that colour to appear physically on the screen. The logical colours are numbered as follows: 0,1,2, or 3 in the case of 4-colour modes and 0-15 in the case of lti-colour modes. The physical colours are numbered from 0 to 15.

Logical colour produces Physical colour
(Code) 2-colour modes (Code)
0 0 Black
1 7 White
4-colour modes
0 0 Black
1 1 Red
2 3 Yellow
3 7 White
16-colour mode
0 0 Black
1 1 Red
2 2 Green
3 3 YelIow
4 4 B1ne
5 5 Magenta
6 6 Cyan
7 7 White
8 8 Flashing black-white
9 9 Flashing red-cyan
10 10 Flashing green-magenta
11 11 Flashing yellow-blue
12 12 Flashing blue-yellow
13 13 Flashing magenta-green
14 14 Flashing cyan-green
15 15 Flashing white-black

Note that it is only in the 16-colour mode that all the logical colours are set up to correspond to the physical colour with the same number.

The relationship between physical colours that appear on the screen and the internal logical colours is referred to as the palette. This specifies which physical colour will appear on the screen for a given internal logical colour, as shown diagrammatically below.

A MODE 1 or MODE 5 statement sets up a palette as described by the black arrows in this diagram - to start with, logical colour 0 is black, logical colour 1 is red, and so on.

To change the palette, you just 'change the arrows' as you wish using a VDU statement reserved specially for this purpose. The white arrows in the diagram show a possible set of changes.

Changing the palette

To change the palette use the VDU 19 statement as follows :

VDU 19,L,P;0;

where the logical colour number L is made to appear as the physical colour number P on the screen.

Example

Enter MODE 1 or MODE 5, and then enter

VDU 19,0,4;0;

In MODEs 1 and 5 logical colour 0 is black to begin with, so this statement causes the black on the screen to change colour to physical colour 4 (blue).

Similarly, a further statement

VDU 19,3,6;0;

causes the logical colour 3 (the white text) to become physical colour 6 (cyan). Now to change the background colour (blue at present) we must refer back to its logical colour - ie 0 (since it was black to begin with). The statement

VDU19,0,1;0;

will change the background colour to red.

Changing the palette is a powerful facility, and can be used to dramatic effect since the colour on the screen can be changed almost instantly. Here are some of the effects that can be produced:

  1. If the logical colour is set to the background colour while an object is being drawn and the palette is changed, 'instant' plotting is produced.
  2. In modes with few colours it is possible to select from any of the 8 possible steady colours which will appear on the screen at a given moment.
  3. Palette changes can produce moving images.

Selecting a colour

The GCOL command selects which logical colour to draw with and the action that drawing will have on colours already on the screen. After a GCOL command any DRAW or PLOT command will use the colour specified by GCOL. The format of GCOL is as follows:

GCOL A,C

where A specifies the mode of action and logical colour to use.

There are five possible modes of action:

  1. plot colour specified regardless of anything already there (paste on top)
  2. OR logical colour with logical colour already there
  3. AND logical colour with logical colour already there
  4. EOR logical colour with logical colour already there
  5. invert logical colour already there

The most common actions used are 0 and 3. GCOL0,C could be used to paint over all or part of an object in the background -- when drawing a cloud in the sky for example. GC0L3,C, the Exclusive-OR action, is important in animated pictures, where an object can move around over the background without affecting it. So, in the arcade game Monsters, your man can run up and down between levels without 'wiping out' the ladders as he goes. Note also that with GCOL3,C plotting the same line twice will erase that line.

Moving the origin

It is possible to move the origin to any point on the screen with the command:

VDU 29,X;Y;

which moves the origin to the absolute point (x,y). This is useful when drawing objects that are not naturally defined with the origin at the bottom left-hand corner of the screen.

Turning off the text cursor

In graphics programs the blinking text cursor can be turned off in two ways:

VDU 5

This links the text and graphics cursors, and anything that is printed will appear at the position of the graphics cursor.

VDU 23,10,32,0;0;0;

This is harder to remember but has no side effects. The text cursor is turned off by loading register l0 of the 6845 screen controller 1C with 32.

Interlace

Interlace causes the screen to judder up and down slightly. This produces the effect of rounding the characters on the screen. Some people find interlace irritating, particularly in graphics programs. It can be turned off like this:

VDU 23;8,0;0;0;

Sideways scrolling

The region of memory that the screen displays can be altered by reprogramming registers 12 and 13 of the 6845 screen controller 1C. This allows you to scroll the screen sideways. Together these registers bold the l4-bit start address (divided by 8) of the top of the screen; register 13 holds the 8 lower bits, while register 12 holds the top 6 bits.

These registers can be altered like this:

VDU 23;register number,value,0;0;0;

The FIELD program in Chapter 6 gives more details on using this technique.

Procedures in BASIC

Many of the programs in this book depend for their success on the use of procedures. However small the program, using procedures is good programming practice, and it will be well worth your while getting the hang of how they work. You will inevitably become familiar wi th their use and appreciate their val ne more as you work through this book, but to begin with, here is a brief introduction.

A procedure is like a subroutine in which certain values, called parameters, are set up automatically when the procedure is called. Here is an example that fills the screen with random squares, using the procedure PROCSQ to draw each square:

Example

10 MODE 1

20 REPEAT

30 PROCSQ(RND(1300),RND(1000),72)

40 GCOL 0,RND(3)

50 UNTIL FALSE

60 END

70 DEF PROCSQ(X%,Y%,S%)

80 VDU 29,X%;Y%;

90 MOVE 0,0:MOVE S%,0:PLOT 85,S%,S%

100 MOVE 0,S%:PLOT 85,0,0

110 ENDPROC

Description of the program

10 4-colour mode

20	Each time around this loop a square is drawn.
30	Call the procedure PROCSQ to draw a square at a random point with a side length 72.
40	Select a random colour.
50	Carry on forever.
60	This is never reached, but helps to show where the body of the program ends and the procedure declarations begin.
70	PROCSQ draws a square at the point (X%,Y%) with a side length S%.
80	Put the origin at (X%,Y%).
90-100	Fill in the square.
110	Return to the point at which PROCSQ was called.

Here, a procedure that draws a square is defined in 70-110. The procedure takes three parameters: and S%, and these specify the position and size of the square.

Each time the procedure is called at line 30 the 'formal parameters' X%, Y% and S% used in the definition take the values of the 'actual parameters' given for that particular call of the procedure; here these are RND(l300), RND(l000) and 72. The procedure is then executed and on completion control is returned to the statement after the call.

Procedures can be called from anywhere within a program, and the order in which procedures are declared does not matter.

A few of the advantages of procedures are listed below.

  1. Procedures make programs much easier to understand.
  2. Dividing a problem into small chunks makes to write and test programs.
  3. Different effects can be produced simply by altering the parameters supplied to a procedure.
  4. Modular programs are less likely to suffer from obscure bugs due to unwanted interactions between different parts of the program.

Next     Top