4.0 Introduction
4.1 Designing a programmable character
4.2 Converting the design to code
4.3 Activities
4.4 Printing programmable characters
4.5 Activities
4.6 Composite figures
4.7 Activities
4.8 Multicoloured characters and figures
4.9 Activities
4.10 Discussion of activities
In the normal way, the BBC Microcomputer can only print those characters whose shapes are stored in memory. These are the set of numbers 0-9, the set of lower case letters a-z, the set of upper case letters A-Z, and a few others such as punctuation marks. The BBC Microcomputer does not provide characters for shapes, such as hearts, pin-men and space invaders, but it does allow you to program your own. Such characters are available in all display modes other than mode 7 and are called programmable characters. Furthermore by joining up programmable characters, you can produce larger, composite figures. Consequently you can design figures to the shape and size of your choosing! This is an exciting and powerful facility, which you will probably want to use a lot for graphics programming. Its application is timited only by your imagination. This chapter describes how to do it,
Figure 4.1a. The character M in mode 4, showing the matrix unto which it fits.
Figure 4.1b. The character g in mode 4, showing the matrix into which it fits.
Every character has to be made from spots (pixels) arranged within a matrix of eight rows and eight columns. Figures 4.la and b show these pixels in highly magnified pictures of the characters M and g, as they appear in mode 4. You will see that they do not extend through the full height and width of the matrix. There are two reasons for this. Firstly there has to be at least one column spare to prevent characters touching when several are written together; and secondly, some lower case characters, like g and y, need tails. These are called 'descenders' and they alone can occupy the bottom row.
When you come to design a programmable character, you should bear in mind that it has to fit within the eight by eight matrix. You may wish to use all the matrix. Then your programmed character will be very slightly bigger than a keyboard character.
Figure 4.2a. The first stage in designing a programmed character: designing the character.
Figure 4.2b. The second stage in designing a programmed character: drawing an eight by eight matrix.
Figure 4.2c. The third stage in designing a programmed character: integrating the character in the matrix.
However, it will not look significantly bigger on the screen. So any fine detail will be too small to show - and it is worth remembering this! (We show you how to make a larger, more complex figure in Section 4.6).
The first step in designing a programmable character is to decide on its shape. We can illustrate the process with a simple tick, as shown in Figure 4.2 a, The next step is to draw out a grid of rectangles with eight rows and eight columns, as shown in Figure 4.25. The final step is to draw the shape - in this case, the tick - onto the grid as a set of blobs, as shown in Figure 4.2c. At this stage the smooth lines of the original figure have to be translated into the step-like edges of the pixels. This is a matter of trial, error and compromise.
In order to feed the character into the computer, you have to reduce its shape to numbers. One method involves translating the blobs of each row of the figure into the l's and 0% of a binary number, taking a background blob as 0 and a foreground blob as I - and in the next paragraph we describe the process. However, if you find it tedious, you may prefer to skip to the paragraph after, because it describes a 'formula' by which you can much more simply achieve the same result. Alternatively, in Activities 4.3, we supply a program which does the whole thing for you.
The computer stores each line of a character as an eight bit binary number (a total of eight 0's and Fs). The binary number representing the top row of the tick is 00000001, which is I in numbers to the base ten. A single I in the second position gives the binary number 00000010, which is 2 in base ten numbers. If both squares are occupied, the binary representation is 00000011, which is 3 i.e. the sum of the previous two.
Figure 4.2d Turning the programmed character into codes.
Now for the 'formula'. You can take each column of the matrix as having a value: I for the right-most column, 2 for the next column, 4 for the next, 8 for the next, etc. These values are shown along the top of the matrix in Figure 4.2 d. To reduce the character to code, ail you have to do is to add up the values for each row of the figure for the positions which should be lit up. By way of example, the numbers on the right of Figure 4.2d show these sums for each row of the tick. These are the codes which have to be fed into the computer.
All that remains is to instruct the computer to accept the codes to represent a character. This is the function of the VDU23 statement. It instructs the computer to accept the coded numbers into its memory as the newly designed character. You use the statement in the following way, where 'row1' represents the code for row 1, etc., and 'character' is the ASCII code for the
VDU 23,character,row1,row2,row3,row4,row5,row6, row7,row8
Thus to reprogram ASCII character 224 as the tick would require the following statement, where the string of eight numbers after . the VDU 23,224 represents the numbers for the rows as shown in Figure 4.2d:
VDU 23,224,1,3,6,140,216,112,32,0
A word about the ASCII codes: in normal operation ASCII codes 0 - 31 inclusive are reserved for controlling the video displ ay. Therefore you cannot normally use any of these for programming characters. ASCII codes 32 - 127 are for the normal keyboard display; so you would not normally want to redefine them. Appendix 3 gives the ASCII codes. ASCII codes 224 - 255 inclusive are the most suitable for reprogramming.
You can normally only use a block of 32 at any one time. These blocks are: codes 32 - 63, 64 - 95, 96 - 127, 128 - 159, 160 - 191 and 192 - 223. With the exception of the keyboard characters, one block duplicates any other. For example, if you print CHR$(224), you get the same character as if you had printed CHR$(128), CHR$(l60) and CHR$(l92).
Listing 4.1 gives a program which allows you to design and edit your own programmable character. You merely have to use the cursor-control keys to move the cursor to that part of the grid that you want to fill, and then press I. You can edit the character by pressing 0 to change your mind. Screen Display 4.1 shows a stage during the editing of a character.
Programmable characters can only be printed on the screen in modes 0 to 6. Mode 7 has its own character set which cannot be altered.
You will be familiar with the simplest way of printing a character. Taking the character M as an example, this is:
PRINT "M"
An alternative way is to use the statement CHR$, together with the ASCII code for that character. Again taking M as an example, this would be as follows:
PRINT CHR$(77)
The shape for each character you redefine is stored in the computer's memory until the computer is either switched off, or the character is defined as something else. The definition of each of the standard characters is stored in ROM, but any characters that you reprogram must be stored in the volatile memory that holds BASIC programs. By special instructions the computer can be forced to hold a reprogrammed version of most of its characters, but this takes up even more memory space and we will not go into it here.
Figure 4.3a. A programmed figure with its VDU definition.
Figure 4.3b. A programmed figure with its VDU definition.
Figure 4.3c. A programmed figure with its VDU definition.
Figure 4.3d A programmed figure with its VDU definition.
i. We suggest that you now try making some programmed characters. You can do it in any mode except mode 7. As it takes some time to think out characters and redefine them, we help by supplying a small library of shapes for you. These are shown in Figure 4.3a,b,c,d,e,f,g, together with the corresponding VDU definitions. Our library uses the same character code, 224, such as:
Figure 4.3e. A programmed figure with its VDU definition.
Figure 4.3f. A programmed figure with its VDU definition.
for each character, but you should use a variety unless you are prepared for one character to overwrite another. Any number between 224 and 255 is suitable.
Use the VDU 23 statement to define a character code as each of the characters in our library. Then print it using a statement
PRINT CHR$(224)
ii. You may like to try programming some characters of your own. Figure 4.3g. A programmed figure with its VDU definition.
Frequently the size of a single character is just too small to give a realistic image on the screen. Then you need to make a larger figure, consisting of a number of programmed characters together. This section introduces a routine to print such composite figures.
Each character making up the composite figures has to be programmed as described above. These characters can then be printed together on any one line by including them in the same PRINT statement, separated by semicolons. Thus, printing the three characters 224, 225 and 226 side by side would require the
PRINT CHR$(224);CHR$(22 5);CHR$(226)
This can be written more compactly using the VDU statement:
VDU 224,225,226
This can be written even more compactly by making the characters 224, 225, 226 into a string, as follows:
composite$ = CHR$(224)+CHR$(225)+CHR$(226)
You can now produce the composite figure with:
PRINT composite$
To write one set of characters directly under another set requires use of the cursor control codes. The ASCII codes for moving the cursor are shown in Table 4.1:
ASCII code | result |
08 | move backwards one space |
09 | move forward one space |
10 | move down one line |
11 | move up one line |
Table 4.1 Cursor control codes
Figure 4.4a. A composite figure with its VDU definitions.
Figure 4.4b. A composite figure with its VDU definitions,
A second row of three characters could be printed directly underneath, if the cursor is first moved down one line followed by three spaces to the left. This can be achieved with the code l0 (for cursor down one line) followed by three lots of the code 8 (for cursor to move backwards one space, three times). Once again you can combine all these characters, together with the control codes, into a single string, as follows:
composite$ = CHR5$(224)+CHR5$(225)+CHR5$(226)+
CHRS$(10)+CHR5$(8)+CHRS$(8)+
CHRS$(8)+CHRS$(227)+CHR5$(228)+
CHRS$(229)
i. You may like to try programming some composite figures of your own. However, because it takes some time to think them out and program them, we have helped by supplying a small library for you to use. These are shown in Figure 4.4a, b and c, together with the corresponding VDU definitions and the procedures for the drawing.
Figure 4,4c. A composite figure with its VDU definitions and a program for drawing it.
ii. Although the result may seem a little jerky, try moving these composites around the screen using the TAB statement to locate them. We give a possible program in Section 4.10.
You can make your programmable characters and figures multicoloured. The technique relies on VDU5. Although the main function of VDU5 is to allow the character to be printed at the graphics cursor, it also has the subtle effect of only writing foreground. This means that several characters can be printed at the same place without each destroying the other. VDU5 is turned off by VDU4 which returns writing to the text cursor
Suppose you want a character to be red, green and blue. You merely define three separate characters: the first representing the red, the second the green and the third the blue. Then you write the first with the foreground set to red; the second with the foreground set to green and the third with the foreground set to blue.
For example, you can get a two-coloured T with the following lines of program:
10 M0DE5
20 VDU3
30 VDU23,224,2 55,255,0,0,0,0,0,0
40 VDU23,225,0,0,24,2 4,24,24,24,24
50 GCOL 0,1 :MOVE 500,500
60 PRINT CHR$(224)
70 GCOL 0,2 :MOVE 500,500
80 PRINT CHR$(225)
90 VDU4
100 END
Screen Display 4,1 (If your computer has a 0.1 operating system, the cursor keys cannot be used as instructed in the Screen Display. Use the following alternative keys: H to move left; J to move right; Q to move down; and A to move up.)
i Use the technique described in the previous section to make some of your own multicoloured characters and composite figures.
ii. What is the maximum number of colours that can theoretically fit into only one character? (See Section 4.10.)
Activities 4.7ii:
10 MODE 5
20 VDU23,224,0,0,0,12,31,31,17,63
30 VDU23,225,0,0,0,0,248,0,0,248
40 VDU23,226,63,127,37,42,21,15,0,0
50 VDU23,227,252,254,164,84,168,240,0,0
60 tank$=CHR$(32)+CHR$(224)+CHR$(225)+CHR$(10)+
CHR$(8)+CHR$(8)+CHR$(8)+CHR$(32)+CHR$(226)+
CHR$(227)
70 FOR X=0 TO 19
80 PRINT TAB(X,20)tank$
90 T=TIME:REPEAT UNTIL TIME=T+50
100 NEXT X
110 END
The ASCII code 32 in line 60 prints a space. This is necessary to rub out the back of the tank.
Activities 4.9ii: The number depends on the mode. For example, in a four-colour mode you can have three foreground colours. So you can have three colours in any one character.