1.0 Introduction
1.1 The display modes of a Model B BBC Microcomputer
1,2 Graphics on a Model A BBC Microcomputer
1.3 Getting into a mode
1.4 Addressing pixels
1.5 Activities
1.6 Drawing lines
1.7 Activities
1.8 Drawing simple curves
1.9 Activities
1.10 Discussion of activities
This chapter is about plotting points and drawing lines and curves. It teaches you how to use the graphics instructions which are available within BBC BASIC, and we have called it 'fundamentals of graphics'. This is to distinguish it from the more sophisticated graphics of later chapters, which rely, not only on the instructions available within BBC BASIC, but also on procedures which we provide for you.
The BBC Microcomputer allows you to make up a display using the following:
- Teletext characters which you will have seen on television's Ceefax and Oracle;
- alpha numeric characters, i.e. letters and numbers of the sort used in ordinary text; and
- points or spots, making up lines and areas.
Model B of the BBC Microcomputer offers eight display modes, allowing a choice of character size and spot size. These modes are numbered from 0 to 7.
Mode 7 is the mode in which the computer first turns on. it can display both text characters and graphics in eight colours. 40 characters can be fitted on a line with 25 lines per screen. The mode is sometimes called the Teletext mode as it allows the colours and symbols of television's Teletext. Teletext colours and graphics have to be turned on and off in a completely different way from that for all the other seven modes. Mode 7 requires 1K of memory, which is far less than for any other mode, and this offers advantages that make it worthwhile to program Teletext graphics. Nevertheless mode 7 is not really a true graphics mode - and we consider it separately later (see Chapter 15). Modes 3 and 6 are not graphics modes either because they allow only text characters.
We shall be concentrating on the other five modes, the 'graphics modes', namely modes 0,1,2,4 and 5. Briefly their characteristics are as follows:
Mode 0 allows the display of 640 by 256 graphics spots in two colours. In addition any text comes out with 80 characters to a fine with 32 lines on the screen. Mode 0 requires 2DK of memory, which is the maximum for any display.
Mode 1 allows the display of 320 by 256 graphics spots each of which may be in any of four colours. Text comes out with 40 characters to the line with 32 lines on the screen. Mode I requires 20K of memory.
Mode 2 allows the display of 160 by 256 graphics spots each of which may be in any of the 8 available colours, which may be steady or flashing. Text comes out with 20 characters to the line with 32 lines on the screen. Mode 2 requires 20K of memory.
Mode 4 allows the display of 320 by 256 graphics spots, each in one of two colours. Text comes out with 40 characters to the line with 32 lines on the screen. Mode 4 requires l0K of memory.
Mode 5 allows the display of 160 by 256 graphics spots each of which may be in any of four colours. Text comes out with 20 characters to the line with 32 lines on the screen. Mode 5 requires l0K of memory.
In those modes which allow a greater number of spots, the spots are accordingly smaller. This means that these graphics displays can show more detail, which is described by saying that there is a greater 'resolution'.
Table 1.1 gives a summary of the facilities in the various modes.
mode | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
characters per line | 40 | 40 | 20 | 40 | 80 | 20 | 40 | 80 |
lines per screen | 25 | 25 | 32 | 32 | 25 | 32 | 32 | 32 |
spots horizontally | - | - | 160 | 320 | - | 160 | 320 | 640 |
spots vertically | - | - | 256 | 256 | - | 256 | 256 | 256 |
memory required | 1 | 8 | 10 | 10 | 16 | 20 | 20 | 20 |
colours available | Teletext features | 2 | 4 | 2 | 2 | 8+8 | 4 | 2 |
Table 1.1 Features available in various modes
Model A of the BBC Microcomputer is limited in the amount of memory available and so you will need to add to it if you want sophisticated graphics. This is particularly so because, if memory is being used for the display, it is not available for program storage. So there may be times when there is a clash between the memory requirements of a long program and a complex graphics display. For this reason many people add extra chips to make the Model A equivalent to the Model B in terms of memory. The Model A Microcomputer can only produce displays in modes 4, 5, 6 and 7. This means that the graphics is limited to four colours with 160 by 256 spots or two colours with 320 by 256 spots.
Many of the programs in this book will run on the Model A, but a few are too long to fit into the limited memory and have to be run on either an expanded Model A or the full Model B. These are equivalent, as far as length of program is concerned.
When you turn on the BBC Microcomputer, you are in mode 7. You can select another mode, say mode 4, by entering one of the following. Either is acceptable because although BBC BASIC does not require a space in front of a number, it normally allows one to make the reading easier.
MODE 4
or MODE4
This statement causes the screen to clear, and anything that you now write is in the new mode.
In the graphics modes (0, 1, 2, 4 and 5) you may draw pictures, shapes and graphs by fighting up small rectangular spots on the screen. Each spot is very small and is called a 'pixel'. Figures 1.1a,b,c show the prompt and the letter A in modes 0, I and 2, as made up from pixels. The size and shape of a pixel varies according to the graphics mode. The smallest is in made 0.
Figure 1a. The prompt and the letter A in mode 0, as made up from pixels.
Figure 1b. The prompt and the letter A in mode 1, as made up from pixels.
Figure 1.1c. The prompt and the letter A in mode 2, as made up from pixels.
BBC BASIC allows for a maximum of 1280 pixels horizontally and 1024 vertically. Unfortunately, with present models of the BBC Microcomputer, the display gives a maximum of only half this number in the horizontal direction and a quarter this number vertically. BBC BASIC allows for the difference between the theoretically possible high resolution display and that which is actually available by an automatic scaling. Irrespective of mode, you always have to address the screen as if it had the theoretical resolution, i.e. as if it had 1280 pixels in the horizontal direction and 1024 vertically. These theoretical pixels are called 'addressable points'.
The position of an addressable point has to be specified by how far across from the left it is - which is called its 'X co-ordinate' - and how far up it is - which is called its 'Y co-ordinate'. Thus the co-ordinate of a position at the bottom left-hand comer of the screen is 0,0. This is illustrated in Figure 1,2 which shows the co-ordinates of various points on the screen. F or example, the co-ordinates of a point half way across the bottom of the screen are 640,0 and the co-ordinates of the centre of the screen are 640,512.
Figure 1.2. The co-ordinates of various points on the screen.
Figure 1.3a shows the pixels in the prompt and the letter A in mode 0. There are several addressable points per pixel. Figure 1.3b shows an enlargement of the four pixels in the lower left-hand corner of Figure L3a with the addressable points in the first pixel marked off and labelled, To simplify things, BBC BASIC requires that, to address a pixel, you merely have to address one of the addressable points within it. Any one will do, It is rather as if you were writing to a firm which had taken over and spread into a number of neighbouring premises. It would not matter which one you addressed, as the firm would be reached via any one of them. To carry the analogy further, it would be pointless to go to more than one. It is equally pointless to address more than one addressable point within a pixel, One is enough and any one will do. We shall illustrate by looking at each mode separately.
Figure 1.3a. The pixels within the image of >A in mode 0.
Figure 1.3b. An enlargement of the six pixels in the lower left-hand corner of Figure 1.3a. Addressable points in the first pixel are marked off and labelled.
In mode 0 there are 640 by 256 pixels. As there are always 1280 by 1024 addressable points, a pixel consists of an area two addressable points wide and four addressable points high. As you see in Figure l.3b, the first pixel at the extreme left-hand corner of the screen contains the following addressable points:
0,3 | 1,3 |
0,2 | 1,2 |
0,1 | 1,1 |
0,0 | 1,0 |
You can of course address the whole pixel via any one of these points. In any mode the pixel corresponds to the smallest point of light on the screen.
In modes 1 and 4 there are 320 by 256 pixels, each consisting of sixteen addressable points, four horizontally and four vertically. In modes 2 and 5 there are 160 by 256 pixels, each consisting of thirty-two addressable points, eight horizontally and four vertically.
i. Enter the program given in Listing 1.1 (You will find it over the page, with the corresponding screen displays: Screen Display 1.1a, 1.1b and 1.1c.) This program writes 1234 on the bottom left-hand corner of the screen and then enlarges this by sixteen times. The size of the original characters and of the enlargement depends on the display mode chosen. It does not matter if you do not understand ail the lines in the procedure definition of the program, because we merely want to demonstrate the different sizes of the pixels in various modes.
ii. Run the program several times and, each time, when the program asks you to choose a mode, choose a different one from the graphics modes 0, 1, 2, 4 or 5, (You can see which ones we chose in order to get our displays, because we show the dialogues between a user and the computer, and we underline the data that the user feeds in.) Can you see why the same program gives different sizes of enlarged characters in modes 0, I and 5 even though the screen is addressed identically in each? We discuss the reason in Section 1.10 at the end of the chapter.
Screen Display 1.1a
Screen Display 1.1b
Screen Display 1.1c
The MOVE and DRAW statements are elementary statements for computer graphics. DRAW draws a straight line from the last point addressed on the screen to a point whose co-ordinates have to be supplied with the statement. Thus a program line such as the following draws a straight line to the centre of the screen, point 640,512, from wherever the last graphics statement finished off:
Screen Display 1.2
120 DRAW 640,512
MOVE is a statement which sets the starting position for a DRAW and other graphics statements. By itself, it produces no visible effect on the screen. By way of example, the following is a simple program in graphics mode 4 to draw a line between the point 10,0 and the point 640,1024 which is half way across the top of the screen.
10 MODE 4
20 MOVE 10,0
30 DRAW 640,1024
40 END
Line 20 produces no visible action but specifies the starting point for the DRAW in line 30.
The following is an additional program line which draws a second line from where the previous line stopped at the top of the screen, down to the bottom right-hand corner 1280,0:
35 DRAW 1280,0
Screen Display 1.3
Consequently, the following program would draw a rectangle, the four lines containing the DRAW statements produce the four sides:
10 MODE 4
20 MOVE 50,50
30 DRAW 1000,50
40 DRAW 1000,1000
50 DRAW 50,1000
60 DRAW 50,50
70 END
We will now show you a general purpose program to draw any shape that can be made from a series of rectangles. In this program we have a need to draw many rectangles and so it is convenient to enclose a set of lines, similar to the above, in a procedure. Then each rectangle is drawn by a call to this procedure, as for example in the following program line, where X and Y are the co-ordinates of the bottom left-hand corner and W and H are respectively the width and height of the box:
30 PROCbox(X,Y ,W,H)
The procedure to draw the box is defined by the following lines:
220 DEF PROCbox(X, Y,W,H)
230 MOVE X, Y :DRAW X+W,Y :DRAW X+W,Y+H
240 DRAW X,Y+H :DRAW X,Y
250 ENDPROC
In our program the values of X, Y, W and H are stored in DA T A statements for each of the rectangles which make up the major part of the display.
Such a program can be written to draw a variety of shapes. Ours draws a house, which is shown in Screen Display 1.2. A few extra DRA W statements have been included to draw in the few sect ions of the house, such as the roof, which are not rectangular. The complete program is given in Listing 1.2.
i. Run the picture-drawing program of Listing 1.2.
ii. Construct your own set of data for drawing same other shape, and run the program to test it.
You can use the MOVE and DRAW statements to draw simple curves, We illustrate this with a program to draw a wave. For obtaining alternating values, we rely on the mathematical function SIN(X). As X increases in magnitude, the function gives values which alternate between -1 and +1. To get a screen display following the same shape, we make the position across the screen correspond to X and the height Up the screen correspond to 1+SIN(X). Multiplying by 512 makes sure that the values representing co-ordinates up the screen run from 0 to 1024 instead of from 0 to +2. Dividing X by 40 reduces the number of oscillations to make the display fit better onto the screen. 512 *(I +5IN(X/ 40)) gives values varying from 0 to 1024, suitable for addressing the height of the screen in screen co-ordinates. The complete program for drawing the wave is in Listing 1.3, opposite the corresponding screen display, Screen Display 1.3.
i. Run the curve-drawing program of Listing 1.3.
ii. Can you work out what would happen if you had left out the MOVE statement in line 20? Try it to see.
iii. Try the effect of adding STEP 4 to line 30. We comment in Section 1.10.
iv. Try to adapt the program to draw some other curve.
Activity 1.5: The characters are printed in different sizes in modes 0, I and 5, because the pixels are of different sizes in these modes (see Figure 1.1 a,b,c).
Activity 1.9 iii: With drawing curves, there is always a compromise between speed and smoothness. The STEP 4 speeds up the display, but the appearance is coarser.