Chapter 9 : Graphics
The original BBC Micro was relatively well endowed with graphics capabilities when it was launched in 1981, and for BASIC programmers this was reflected in specific keywords, particularly plot, and the variety of so-called vdu commands. The later 1.2 Operating System increased the scope of the plot function, and the graphics capabilities of the BBC Micro were further extended with Acorn's Graphics Extension rom, the features of which were subsequently built into the Operating System used on the Master 128 and Master Compact.
Unfortunately, no additional support has been provided explicitly within BASIC for any of these new features until the advent of BASIC V on the Archimedes. Instead, everything new had had to be handled through the use of plot and vdu instructions.
Of course, many graphics functions are significantly faster on an Archimedes, but the system as a whole incorporates only a few additional functions, plotting sprites for example. However, many more graphics functions now have an associated BASIC keyword, which both tends to draw attention to features which might otherwise be overlooked, as well as making such features easier to use for many Archimedes owners. There is one major change as far as graphics is concerned, and that is in the availability of the range of colours and modes supporting up to 256 different colours on the screen together. This important, but sometimes complex, subject was dealt with in the preceding chapter.
New Graphics Commands
The new BASIC instructions can be listed as follows:
The ORIGIN instruction duplicates the VDU29 command by allowing the origin for graphics to be moved to a new position. As with all BASIC instructions which package up VDU commands, the two arguments, the x and y co-ordinates, are separated by a simple comma instead of the semi- colons needed to terminate values in excess of 255 in VDU commands. So:
would move the origin to the centre of the screen. When specifying a new origin, the co-ordinates of the position are always given relative to an origin at the bottom left-hand corner of the screen. Similarly, the point instruction duplicates, for convenience, one of the plot commands, PLOT69, to allow a point to be plotted at a specified position. Therefore:
will plot a point at the centre of the screen, assuming the origin in its default position, and:
POINT BY 640,512
will also plot a point but by moving 640 units horizontally and 512 points vertically relative to the previous screen position. This variation duplicates the PLOT61 command. The POINT command adds no new function to BASIC V, but is an example where the use of an alternative and more meaningful BASIC keyword will help beginners to master graphics.
The POINT TO instruction may be used to move the screen pointer when this is not under mouse control. See Chapter Ten for information on using the MOUSE ON instruction. The pointer will be moved to the screen position specified, for example:
POINT TO 160, 128
The LINE instruction is a different kind of convenience in that it does not duplicate any individual PLOT or VDU command but provides a single instruction for what previously required two. Thus:
MOVE xl,yl DRAW x2,y2
can now be written as:
in order to draw a line from one point (xl,yl) to another (x2,y2). The three new shape drawing instructions, however, are again BASIC keywords duplicating existing plot instructions (on the Master 128 and Compact), and simplifying the specification of size and position into the bargain. All three instructions produce an outline shape, or a filled shape if the keyword FILL is included. The CIRCLE instruction is of the form:
where x/y is the centre of the circle, and r is the radius. The Ellipse drawing function has the format:
where x,y is the centre as before, ml is the length of the semi-major axis, and m2 is the length of the semi-minor axis. A fifth parameter may also be included which, if present, specifies the angle in radians between the x axis and the semi-major axis. For example:
ELLIPSE FILL 640,512,100,75,PI/2
would draw a filled ellipse at the centre of the screen (assuming a default origin), with major axis 200 units and minor axis 150 units rotated through 90 degrees into a vertical position. The statement is equivalent to:
ELLIPSE FILL 640,512,75,100
by reversing the lengths of the semi-major and semi-minor axes. Note that there are some limits imposed on the parameters to the ellipse instruction if an error is not to be generated. This is covered quite clearly in the User Guide (look up ELLIPSE).
The RECTANGLE statement is the last of the new BASIC keywords for drawing shapes and, as well as the FILL variation, may take either four or six parameters. In its simplest form, the first two parameters specify the co-ordinates of one corner, while the other two, which may be positive or negative, specify offsets horizontally and vertically from the first corner to the opposite corner. For example:
RECTANGLE FILL 480,384,320,256
would display a solid rectangle in the current graphics foreground colour at the centre of the screen. Note that when an outline rectangle is drawn, the graphics cursor remains at the first reference point, but that when a filled rectangle is displayed the graphics cursor is left at the opposite corner. This is exactly what would be expected if a rectangle were to be explicitly drawn as a sequence of four straight lines or two filled triangles, using MOVE and DRAW or PLOT, as with previous versions of BBC BASIC.
A second form of the RECTANGLE (or RECTANGLE FILL) statement requires two further parameters and allows the rectangular area of the screen, defined as before by the first four parameters, to be copied (no FILL) or moved (FILL) to a new position on the screen. Any graphics image contained within the rectangle defined will be copied or moved as a result, providing a 'copy and paste' or 'cut and paste' facility for graphics. When an image is to be moved, its original area is filled with the current graphics background colour.
The following program uses the same procedure, PROCrectangle, as Chapter Ten to demonstrate the use of the 'copy and paste' facility described above. The program fills the screen with randomly sized and coloured ellipses. The mouse may then be used to select any rectangular area of the screen and copy its contents elsewhere. Used in a continuous manner, it produces a 'painting' effect with attractive results.
Listing 9.1 Graphics demonstration.
10 REM >Chap9-l 100 MODE12:OFF:ON ERROR MODE12:PRINT REPORT$;" at line ";ERL:END 110 PROCellipses 120 xl=320:yl=256:w=160:h=128 130 PROCrectangle(4,xl,yl,w,h) 140 MOUSE RECTANGLE 0,0,1279-w,1023-h 150 MOUSE TO 160,128 160 REPEAT 170 MOUSE x,y,z 180 PROCcopy(xl,yl,w,h,x,y) 190 UNTIL FALSE 200 END 210 : 1000 DEF PROCrectangle(colour,RETURN xl,RETURN yl,RETURN w,RETURN h) 1010 LOCAL exit's, switch's, x, y : GCOL 3,colour 1020 *POINTER 1030 exit%=FALSE:switch%=TRUE 1040 MOUSE TO xl,yl 1060 REPEAT 1070 RECTANGLE xl,yl,w,h 1080 MOUSE x,y, z 1090 IF z THEN 1100 CASE z OF 1110 WHEN 4:switch%=NOT switch%' 1120 IF switch% THEN MOUSE TO xl,yl ELSE MOUSE TO x+w,y+h 1130 WHEN 1: exit%=TRUE 1140 ENDCASE 1150 REPEAT:MOUSE x,y,z:UNTIL z=0 1160 ENDIF 1170 WAIT 1180 RECTANGLE xl,yl,w,h 1190 IF switch% THEN xl=x:yl=y ELSE w=x-xl:h=y-yl 1200 UNTIL exit% 1210 *POINTER 0 1220 ENDPROC 1230 : 1240 DEF PROCellipses 1250 LOCAL n 1260 FOR n=l TO 100 1270 GCOL RND(8)-1 1280 ELLIPSE FILL RND (1100)+100,RND(900)+100,RND(100)+100,RND(100)+100 1290 NEXT n 1300 ENDPROC 1310 : 1320 DEF PROCcopy(RETURN xl,RETURN yl,RETURN w,RETURN h,x2,y2) 1330 RECTANGLE xl,yl,w,h TO x2,y2 1340 xl=x2:yl=y2 1350 ENDPROC
Use the mouse to move the rectangle around the screen. Pressing the SELECT button fixes the position but allows the size and proportions to be changed. You can swap between these two adjustments until you are happy with the area selected. Pressing the ADJUST button then turns that area into a paint brush for painting on the screen. Just move the mouse around to produce spectacular effects.
The program uses two further procedures, PROCellipses and PROCcopy. The former simply calls the ELLIPSE FILL instruction 100 times, specifying randomly selected values for the position and shape in each case. PROCrectangle is then called to allow the user to define a rectangular area of this display. Finally PROCcopy is called repeatedly to copy the image area defined to a new position determined by the movement of the mouse. Line 140 ensures that the image area always remains on-screen by restricting the movement of the mouse, while line 150 displays the image area in a convenient but arbitrary starting position. In the definition of PROCcopy, note the use of return with the first four parameters, so that each new position is remembered and used as the starting point of the next move or copy which is carried out by the RECTANGLE statement at line 1330. If you try this program out, as well as demonstrating the use of a good many of the new features of BASIC V, you will also end up with some highly colourful and attractive displays.
The new fill instruction allows any area of the screen to be flood-filled using the current foreground colour. The instruction requires just two parameters which specify the x,y co-ordinates of the starting point for the fill. The only other requirement is that the starting point must match the currently selected background colour, otherwise nothing happens. This problem can be easily avoided by using the following sequence. Assume the current foreground and background colours are always held in the variables fc and be respectively. A suitable routine could be written as:
MOVE x,y bc=POINT(x,y) GCOL be + 128 FILL x,y
The background is set to the colour at the point from which the fill will take place before the FILL instruction is executed. The instruction then fills in all directions until a non-background colour, the edge of the screen or the edge of the graphics window, is reached. The variation FILL BY performs exactly the same function as the FILL instruction above, except that the co-ordinates for the starting point are taken to be relative to the last position of the graphics cursor rather than being absolute co- ordinates. Whichever format is used, a flood-fill on the Archimedes is very fast indeed.
We have already seen how the incorporation of 'BY' in two keywords (POINT BY and FILL BY) allows a point to be specified relative to the last position of the graphics cursor, rather than in absolute terms. This use of by can also be applied to the two instructions MOVE and DRAW in exactly the same way.
The Archimedes provides support for sprites with a number of star commands and additions to the range of PLOT functions in BASIC, in addition to the sprite editor supplied on the Welcome disc. The star commands are, of course, not part of basic but access the SpriteUtils system module. These commands are listed here for reference:
The currently selected sprite may be plotted at any point on the screen by using a plot instruction (PLOT232 - PLOT239). The different values have the usual range of meanings for plot commands. For example:
would plot the currently selected sprite at the centre of the screen, assuming the default position for the origin. This is the most likely plotting mode to be used, often in conjunction with Exclusive OR plotting to achieve apparent movement on the screen. Of particular interest, *SGET allows any rectangular area of the screen to be saved as a sprite. *SCHOOSE selects any sprite in memory ready for plotting on the screen using PLOT&ED. These commands were used to good effect in the final program of Chapter Eight (listing 8.3).
Sprites are covered in detail in the User Guide for BASIC programmers, and in the Programmer's Reference Manual for those who require more detailed information.