5.0 Introduction
5.1 The redefinable and absolute colour numbers: a summary
5.2 Redefining the redefinable colour numbers
5.3 Activities
5.4 Animation
5.5 Activities
5.6 Overlapping images
5.7 Logicai operations
5.8 Activities
5.9 Discussion of activities
You can animate a graphics display, by making a series of pictures appear and disappear in quick succession. The effectiveness depends on how quickly you can make each picture change. The quickest way is to redefine the colours, so that whole areas of one colour turn into other colours virtually instantaneously. This chapter describes how to redefine colours and how to use the technique to produce animation.
0 | = black | 8 | = flashing black/white |
1 | = red | 9 | = flashing red/cyan |
2 | = green | 10 | = flashing green/magenta |
3 | = yellow | 11 | = flashing yellow/blue |
4 | = blue | 12 | = flashing blue/yellow |
5 | = magenta | 13 | = flashing magenta/green |
6 | = cyan | 14 | = flashing cyan/red |
7 | = white | 15 | = flashing white/black |
Table 5.1 The colours of the absolute colour numbers
You have already used the COLOUR statement to specify the foreground and background colours for text and the GCO! statement for graphics. You will recall that each requires a colour number which we called a redefinable colour number (see Section 2.1). We shall now summarise the essential differences between redefinable colour numbers and absolute colour numbers.
An absolute colour number is the number of the colour which is tm be reproduced on the screen. It is like a name, by which any colour can be unambiguously referred and it can never be changed, irrespective of the screen mode in operation. Table 5.1 lists the absolute colour numbers.
A redefinable colour number is the colour number which appears in the COLOUR and GCOL statements. Its effect depends on the screen mode in operation at the time and on whether or not it has been redefined. When a redefinable colour has not been redefined, the following operate by default:
For modes 0 and 4:
redefinable colour 0 = absolute colour 0 (black)
redefinable colour 1 = absolute colour 7 (white)
For modes 1 and 5:
redefinable colour 0 = absolute colour 0 (black)
redefinable colour 1 = absolute colour 1 (red)
redefinable colour 2 = absolute colour 3 (yellow)
redefinable colour 3 = absolute colour 7 (white)
For mode 2 the redefinable colour number has a default value equal to the absolute colour number given in Table 5.1.
The command to change the definition of a colour number is VDUl9. its form is as follows, where the semicolons control how BASIC interprets numbers and should not be confused with cormas:
VDU19,redefinable colour,solute colour;0;
For example, the following would redefine the redefinable colour number 0 to be the absolute colour number 4, to give blue:
VDU19,0,4;0;
As soon as you enter this line, every point on the screen which was originally written in the redefinable colour 0 becomes blue. As 0 corresponds to the background which, by default is black for all modes, this provides a blue background for all writing on the screen.
The writing on the screen in a four-colour mode can be made yellow by the following:
VDU19,3,3;0;
The redefinable colour numbers are automatically reset to the default when the mode is changed, or by the statement VDU 20, or by control/T.
For a quick and easy way to see the effects of redefining colour numbers, define the first red user-definable key to set the re definable colour 0 to 7; define the second user-definable key to set the redefinable colour 1 to 0; and set the third user-definable key to reset the redefinable colours i.e. 0 (background) to 0 (black) and I (foreground) to 7 (white). To do this, you need the following lines, entered in direct mode:
*KEY0 VDU19,0,7;0;|M
*KEY1 VDU19,l,0;0;|M
*KEY2 VDU20|M
Also change the mode to mode 4 by entering MODE4 This resets the redefinable colours and switches to a two-colour mode with redefinable colour 0=black and 1=white.
i. Now enter any message on the screen, just to get some writing there. (Ignore the error message.) Is the writing in its normal form of white writing on a black background?
ii. Next press the user-definable key f0. As the key redefines the redefinable colour 0 to be white, does the screen turn uniformly white?
iii. Next press the user-definable key f1. As the key redefines the redefinable colour 1 as black, does it cause the writing on the screen to reappear, this time as black writing on a white background?
iv. Finally press the user-definable key £2. Does it bring the system back to normal?
v. What would have happened if you had done this activity in a four colour mode? We discuss this in Section 5.9.
To produce an animated picture requires you to display, in rapid succession, independent and slightly different views, each showing a progression from the previous one. When they are presented quickly, one after the other, the result can give the illusion of movement. Although it is impossible, with present technology, for home computers to produce the equivalent of a cinema film, attractive results can be obtained. The computer has to work out a number of images and store them in memory. Then it has to arrange to switch from one to another in rapid succession to give the impression of movement. The process of redefining colours enables this succession to be sufficiently rapid, and we describe it here.
It is best to start by defining all the redefinable colours on the screen to be background, and so by default black. Then you draw a suitable number of successive images, by drawing each in a separate colour number. The number of redefinable colour numbers, and therefore the number of images, depends on the display mode, i.e. 0 to 3 for modes 1 and 5, 0 to 15 for mode 2, Each image can now be viewed, if you define its particular redefinable colour number as white, keeping ail others black. Then you can switch between one view and another by selecting which redefinable colour to define as white while keeping all others as black.
Essentially each available redefinable colour number can be used to draw a different frame of the final animated picture. To hide any one frame, its redefinable colour is defined as the background colour. Thus one colour number (usually 0) has to be reserved for the background colour and is therefore unavailable for drawing a frame. So, in a four colour mode, it is possible to draw three individual frames, each of which can be turned on or off. In the eight + eight colour mode, it is possible to draw fifteen independent frames. Clearly this produces the best animation, but you need a Model B.
This type of animation cannot be done in the two-colou graphics modes, 0 and 4. With only two colours, either nothing or all parts of the picture are visible.
i. To see the effects of animation by colour redefinition, enter Listing 5.1, which produces an animated picture of a tap dripping into a tray which slowly fills up. Screen Display 5.1 cannot do justice to the animation on the actual screen, which shows the drips fall ing from the tap. Each *FXl9 in lines 240 - 260 of the listing force the program to wait until the start of the next display frame on the television. We use it here as a method of producing a time delay and for getting better animation. This program only works on a Model B.
ii. Try to adapt the program by speeding up the drips or by altering the rate at which the tray fills up.
For the animation which we have just described, each image must be separate from the others. If any parts of the images overlap then the one which is placed on the screen last overwrites the previous one. If this is unacceptable, there is an alternative way of treating the image which allow as much overlap as necessary. This method allows only two separate images in mode I and 5 and four separate images in mode 2. We now describe it.
Within the computer ail numbers are expressed by electrical signals which are either on or off, and the redefinable colour numbers are also expressed this way. For ease of expression, computer experts speak of signals as being either I or 0 to represent the on and off states. You can therefore specify any number in terms of the l's and 0's that specify the signals inside the computer. We shall describe the process, as it works within mode 2. Consequently sixteen different colour numbers can be used. The computer specifies a colour number between 0 and 15 in terms of 1's and 0's, according to Table 5.2.
colour number | binary number |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
Table 5.2 Equivalent binary numbers and colour numbers
You will find that the following are the most important equivalents:
Screen Display 5.1
Normal colour 1 is equivalent to computer version 0001
Normal colour 2 is equivalent to computer version 0010
Normal colour 4 is equivalent to computer version 0100
Normal colour 8 is equivalent to computer version 1000
You will notice that each of these numbers uses a single 1 representing a single 'on' signal. Inside the computer there are four wires representing the colour in which you are drawing the picture. Also in the screen memory there are four separate cells in the screen memory chips which hold these four digits; they are either on or off. Each is independent of the others, which is important to remember when representing images. (Note that 1=0001 is independent of 2=0010 but that 3=0011, being a combination of I and 2, is dependent on both.) For overlapping pictures and for other pictorial effects, each of the four possible views of the picture need to be drawn, each in one of the four possible independent electrical positions, i.e. 0001, 0010, 0100 or 1000.
Provided you draw the pictures in such a way that you do not rub out anything already on the screen, an overlap between a picture drawn in redefinabie colour I (0001) with one drawn in redefinabie colour 4 (0100), produces, at that position on the screen, the electrical code 0101 which is clearly a combination of the previous two.
You could have colour number 1 (0001) for an image of a man and 4 (0100)for the image of a tree. If you define 1 (0001) as red, then anywhere on the screen where you put the: man, using the previous graphics techniques, the image will appear red. If, however, you can arrange that where the tree (0100) and the man (0001) overlap on the screen, the duality of the situation is recorded as tree (0100) + man (0001) -- (0101), then you have the foil owing possibilities: If this combined effect (0101) is defined as having the same colour as the tree (0100), then the man is not seen, be. he appears to have gone behind the tee If the combined effect (0101) is defined as having the same colour as the man (0001) then this part of the tree will not be seen, i.e. he appears to have gone in front of the tree!
These manipulations require some knowledge of the exclusive OR binary logic for combining numbers. We introduce it in the next section.
The logical operations are methods of performing what must appear at first sight to be nothing but rather strange arithmetic. Y rat this type of arithmetic is crucial for computers. As we mentioned, ail numbers inside the computer are dealt with as binary numbers and ail binary numbers are a series of l's and 0's Essentially the binary operations are a way of combining the 1's and 0's of binary arithmetic. The operation which is required for animation uses the operator called 'the exclusive OR' which is written as XOR. The binary %OR operator combines l's and 0% in the following way:
0 XOR 0 = 0
1 X0R 0 = 1
0 XOR 1 = 1
1 XOR 1 = 0
To illustrate the operation, consider two binary numbers, each represented by a combination of four l's or 0's, such as the 0100 and 0001 of the previous section. To combine these two numbers using the logical XOR operator requires that one number be written above the other like this:
0100
0001
You now operate the logical XOR between the 1's and 0's in each column to give the result 0101. Thus, if you draw one picture on the screen using colour number 4 (0100) and then manage to draw another on the screen using colour 1 (0001) - this time making sure that the second picture does not overwrite the previous but is combined with it using the logical XOR function - then at every point on the screen where you have an overlap between 4 (0100) and 1 (0001) you get the number (0101) which is 5. You now have areas from the first picture which are in colour number 4 (0100), areas from the second picture in colour number I (0001) and areas where the two overlap, which are in colour number 5 (0101). You now only need to define colour number 5 to be the same as 4 or I and one image appears to be hidden by the other, where they overlap. The information that the two images do overlap is not lost, however, as the situation does have the different colour number 5 (0101) on the screen.
To remove one of the images from a particular area of the screen as would be required if a man was to be shown walking across the screen merely requires that his image be written to the same spot, a second time still using the logical XOR function.
This happens as 1 XOR 1 = 0. This means that 0101 XOR 0001 = 0100 which, in terms of the image, means that:
'overlapping image' | XOR | 'one of images' | = | 'other image' |
0101 | XOR | 0001 | = | 0100 |
GCOL 3,C is a statement which sets up the BBC Microcomputer to do the XOR operation. Once this statement has been executed, all future graphics displays will be of colours such that they are the XOR between the colour number that you are plotting and that already on the screen.
Screen Display 5.2
Screen Display 5.2 shows a tank and a pyramid. As it uses the complete range of colour numbers, it will only work on a Model B. Enter the program of Listing 5.2. Do you see the tank move behind the pyramid? Change the program and re-run it to show the tank moving in front of the pyramid.
Activity 5.3 v: If you had done the activity in a four colour mode, you could have redefined any of the four redefinable colour numbers 0, 1, 2 and 3 to be any of the absolute colours. This redefinition of redefinable colour numbers could have included redefining all of the colour numbers to represent black. Then the screen would become black whatever was drawn on it. Alternatively ail of the redefinable colour numbers could have been defined as black, with the exception of one which could have been defined as white. Then only those parts of the picture which were drawn in the last redefinable colour would have been visible.