Suite101

Programming Graphics in BBC BASIC

An introduction to using colors and line drawing commands

© Guy Lecky-Thompson

In which we learn how to set colors, using GCOL and COLOR, create Teletext style block graphics, and draw simple line drawings with other graphics commands in BBC BASIC.

Introduction

Originally, the BBC Micro, on which BBC BASIC was first implemented, did not provide enough speed to make drawing with graphics in BASIC a viable proposition; unless one had time on their hands. Simple games graphics were possible, but advanced game graphics support (think Ravenskull and Repton) required assembly language and machine code programming.

However, to a certain extend, the PC has given BBC BASIC graphics programming something of a new lease of life. Arguably, capabilities provided under BBC BASIC for graphics programming were always far in advance of the underlying architecture, but now we have bigger processors, graphics cards, and optimized compiler/interpreters, we can really take advantage of all that power.

About MODEs

There are three classes of MODE in BBC BASIC:

  • Text only (MODE 7, for example)
  • Low Resolution Text & Graphics (MODEs 2 and 5)
  • Higher Resolution Text & Grapics (MODE 4)
  • High Resolution Text & Graphics (MODE 0)

However, due to the restrictions of the underlying architecture, there is always a trade-off between the graphics resolution, number of colors available, and the text output capabilities. For each of the standard MODEs 0 to 7, this plays out as:

MODE 0 640x512x2 0x32
MODE 1 640x512x4 40x32
MODE 2 640x512x16 20x32
MODE 3 640x500x16 80x32
MODE 4 640x512x2 40x32
MODE 5 640x512x4 20x32
MODE 6 640x500x16 40x32
MODE 7 n/a 40x32

Of particular interest is the pseudo-graphical 'teletext' mode, which was the basis for the BBC television Ceefax service. It allows graphics to be built up from block characters. It is, in fact, compatible with Videotex (Viewdata) and standard Teletext, control codes.

The characters 160 to 191 and 224 to 255 are used in MODE 7 to display one of 64 different block graphics. For those wanting a useful reference chart, the following program will display one.

10 MODE 7
20 nChar% = 160 : REM 160 - 191, 224 - 255
30 X% = 1
40 Y% = 1
50 FOR nCount% = 0 TO 63
60 PRINT TAB(X%, Y%);STR$(nChar%);CHR$151;CHR$(nChar%);CHR$135;
70 nChar% = nChar% + 1
80 IF nChar% = 192 THEN
90 nChar% = 224
100 ENDIF
110 X% = X% + 6
120 IF X% > 30 THEN
130 Y% = Y% + 1
140 X% = 1
150 ENDIF
160 NEXT nCount%

Deciphering each stage is left as an exercise for the reader, remembering that control codes (PRINT CHR$nnn) 145 to 151 select the block graphics mode of display, and the color at the same time (red, green, yellow, blue, magenta, cyan, white).

The MOVE, DRAW, and LINE Commands

In modes other than MODE 7, we can draw line graphics as well as output text. One point that needs to be remembered is that the co-ordinate reference points for the text and graphics output modes are different. When printing text on the screen the co-ordinate 0,0 is in the top-left corner.

When drawing graphics, the origin is in the bottom left corner. So, the following code will draw a line from the origin to a point on the screen:

MOVE 0,0 : DRAW 100, 100

If the reader tries this in a BBC BASIC interpreter, they will note that a line from the bottom left of the screen to a point above and to the right of the origin is drawn. This is different to some other graphics co-ordinates systems.

Rather than MOVEing and DRAWing, we can accomplish the above with a single command:

LINE 0, 0, 100, 100

The line is drawn in the currently selected foreground graphics color. The default is white.

Using Color and GCOL

The text color can be changed with the COLOUR command. Note the British English spelling of the word 'colour'. This command, however, does not affect the graphics color at all. To change the current graphics color, we use the GCOL keyword, thus:

GCOL 4

This will set the current foreground color to the palette entry 1. In MODE 2, this is red, but the number of colors available, as well as the indexes of each palette entry will change depending on the mode. Experimentation is often the best way to find the perfect color index.

If the number is larger than 128, then the background color is set to the palette index referenced by 128-index. So, to turn the background green in MODE 2 (palette index 2), we use the command:

MODE 2 : GCOL 130 : CLG

The final command, CLG, clears the graphics screen, thus setting the color to green.

More accurate colors are possible, but they require 'stealing' an index in the palette. For example, we could set logical entry 1 to bright white by using the following command:

VDU 19, 1, -1, 63, 63, 63

The standard form for the VDU 19 command is as follows:

VDU 19, palette, -1, red, green, blue

Note that the RGB is given as a 6-bit value, with 0 the lowest intensity (0,0,0 = Black), and 63 the highest (63, 63, 63 = Bright White). BBC BASIC for Windows users will note that 8-bit color scan be set using the following:

VDU 19, palette, 16, red, green, blue

One last note on the GCOL statement is that there is an alternative form, where a second digit can be added:

GCOL color, operation

Those familiar with Windows programming will be glad to hear that BBC BASIC offers the same possibility to combine the graphics to be plotted with that already there, by specifying a drawing raster operation:

  • 0 : Plot the color
  • 1 : Plot the color OR the existing color
  • 2 : Plot the color AND the existing color
  • 3 : Plot the color XOR the existing color
  • 4 : Invert the existing color

Trial and error, for those not already familiar with these, will be the best way to see their effects.

Drawing Filled Shapes with The CIRCLE, ELLIPSE, RECTANGLE Keywords

Finally, we have some commands which enable us to draw both filled and non-filled shapes. The RECTANGLE keyword is followed by four numbers, giving the origin and size of each of the sides:

RECTANGLE 100, 100, 150, 175

This will draw a RECTANGLE in the foreground graphics color. If we include the FILL keyword, then it will be filled, again in the foreground graphics color. To fill in an alternative color, it would be necessary to draw two rectangles - one in outline, and one filled, just inside it.

The CIRCLE and ELLIPSE keywords work in the same way:

CIRCLE [FILL] x_origin, y_origin, radius
ELLIPSE [FILL] x_origin, y_origin, x_side, y_side

The key difference between the two, is that a CIRCLE needs only a radius to be specified, whereas an ELLIPSE is drawn within a bounding rectangle, specified by the x_side and y_side values. There are many other graphics keywords to explore, but this should whet the appetite, and encourage some further experimentation.

Links


The copyright of the article Programming Graphics in BBC BASIC in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Programming Graphics in BBC BASIC in print or online must be granted by the author in writing.





Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo