Introduction To SmartBASIC
Lesson Six - Graphics
by Charles A. Smith

(Here is lesson six in Charles Smith's ongoing series of articles introducing readers to the art of programming in SmartBASIC.)

Perhaps the most interesting aspect of BASIC programming is GRAPHICS. We've talked a little about Low Resolution Graphics but I'm sure you'll find that the most satisfying way to add pictures to your programs is Hi-Res.

This is the method used in Arcade games and the possibilities are almost limitless. But, like anything worthwhile, there is a price. In this case the price is complexity and attention to detail. This lesson is designed only to give you a brief in-sight into the subject.

There are two ways to use Hi-Res. The easier simply draws (or plots) a shape by calling for the x,y co-ordinates:

20 HGR
30 HCOLOR = 3
40 HPLOT 0,0 TO 100,100

This program will draw a white line on a black screen diagonally from the top left (x=0,y=0) to a point on the screen representing x=100,y=100. Lines can be drawn in any direction but it's obvious that a series of horizontal lines could be plotted to produce an image exactly as a fax machine does.

The problem with using this method in your programs is the slow retrieval time. ADAM is reading your commands one line at a time in BASIC. The better method is to read the entire shape before showing it on the screen and this is done by using the Decimal equivalent of the Binary numbers which represent a series of Vector Codes to plot a shape.

Each Vector is a movement in a specific directon, with or without plotting - UP, RIGHT, DOWN and LEFT. Only eight vectors each with its own Binary and Decimal codes are needed.

ACTION                                BINARY CODE            DECIMAL CODE
Move up without plotting                   000                                    0
Move right without plotting                001                                    1
Move down without plotting              010                                    2
Move left without plotting                  011                                    3

ACTION                               BINARY CODE             DECIMAL CODE
Move up with plotting                       100                                    4
Move right with plotting                    101                                    5
Move down with plotting                  110                                    6
Move left with plotting                      111                                    7

A byte consists of eight bits. Each bit in Binary can be either a 1 or a 0. Three bits are needed to plot one vector so the maximum number of vectors in one Byte is three provided that one of them is a non-plotting vector. Makes sense, but in order to further confuse the novice the guys who design all this decided that the plotting of a Shape Byte is done from right to left!

I promise to simplify all this in the next few paragraphs so stay with me - it is valuable to understand some of the inner magic of this complex subject. So here's how a Shape Byte is plotted:

  -   -              - - -               - - -
Sect.3           Sect.2            Sect.1

Suppose we wanted to - Plot Down, Plot Right and then Move Up without plotting. The entire 8 bit Byte would look like this: (remember it goes backwards)

0 0                   1 0 1            1 1 0
- -                    - - -              - - -
Noplot              Plot              Plot
Up                  Right              Down

What does it all mean? First look at all the '1's and '0's and you'll see that they all add up to a Binary number, 101110, (we don"t count the zeroes in front) and that has a Decimal equivalent of 46. That magic number along with others similarly derived, defines a Shape.

Use them as DATA statements in a BASIC program and ADAM retrieves them almost instantaneously. The following, for example, draws a small heart Shape:

100 DATA 73,225,28,28,28,100,12,173,5,40,21,21,246,

A lot of work for one lousy little heart, you say. OK Now the simplification that I promised. Fortunately, a lot of bright people over the years have developed some wonderful SHAPEMAKER programs. Many are available in the Public Domain but my favourite is one by Ben Hinkle.

Through an ingenious bit of programming you are able to first draw your shape in Lo-Res which makes it much larger on the screen. When you've finished it is converted to Hi-Res in its true size and allows you to make corrections and then save your Shape for us in other programs.

Much more can be said on this subject. The foregoing can only be considered as a brief exposure to a complex but rewarding pursuit. BASIC books are available to those interested in a more detailed exploration.

The following, which was originally contained on a Toolkit volume from Edmonton ADAM User's Group, will get you started.


1 REM shape table maker by Ben Hinkle, based on a program by Mark Pelczarski in Softalk, July,1982
5 HIMEM :51455
7 INPUT "how many shapes in the shape table?"; e
10 w=51456:POKE w, e:POKE w+1, 0:POKE w+2, 2*e+2:
POKE w+3, 0: w=w+4:POKE 16766, 0:POKE 16767, 201
15 w=(2*e+2)+51456: il=2*e+2: sn=1: st=51460
20 p=0:POKE w, 0:POKE w+1, 0: sw=1:GR
22 x=20: y=20
23 ? "use the arrow keys to move, 'home' to plot,
and 'f'to finish the shape.";
24 ? "This is shape #"; sn:? "you are now at (x,y):";
25 il=w-51456
27 VTAB 24:HTAB 22:? " "; :VTAB 24:HTAB 22
28 ? x; ","; y;
90 COLOR =13:PLOT x, y
100 GET a$: a=ASC(a$)
110 IF a$="f" THEN 300
111 COLOR =0:PLOT x, y
113 IF p=0 THEN 120
115 COLOR =4:PLOT x, y
120 IF a=128 THEN p=4:GOTO 90
130 IF a=160 THEN m=0: y=y-1:GOTO 200
140 IF a=161 THEN m=1: x=x+1:GOTO 200
150 IF a=162 THEN m=2: y=y+1:GOTO 200
160 IF a=163 THEN m=3: x=x-1:GOTO 200
180 GOTO 25
200 v=m+p
205 p=0
210 IF sw=1 THEN sw=2: v1=v:POKE w, v:POKE w+1, 0:
220 IF v+v1=0 THEN POKE w, 88: w=w+1:POKE w, 0: v1=0:
230 IF v=0 THEN POKE w, v1+192: w=w+1:POKE w, 0:
v1=1:GOTO 25
240 v=v*8+v1:POKE w, v: w=w+1
250 sw=1:POKE w, 0
260 GOTO 25
300 IF sw=2 THEN POKE w, v1: w=w+1
305 POKE w, 0
310 GOSUB 2000
311 HOME:INPUT "are you satified with this shape
(y/n)?"; a$:IF a$="y" THEN 315
312 IF PEEK(w-1)=0 THEN GR:GOTO 22
313 w=w-1:GOTO 312
315 w=w+1: il=w-51456
317 IF sn<e THEN 350
320 HOME:INPUT "shape table name?"; a$
330 ? CHR$(4); "bsave "; a$; " ,a51456, l"; w-51455
340 TEXT:? "done"
345 END
350 sn=sn+1
360 p=INT(il/256)
370 POKE st+1, p:POKE st, il-p
400 st=st+2
410 GR
420 GOTO 22
2000 HGR:HCOLOR =12:SCALE =1:ROT =0
2010 DRAW sn AT 100, 100


Back to Top