Programming AmigaOS in BASIC

29. Line Graphics

It is possible to do normal line graphics on the Amiga using Basic. There are 100s of functions available from setting colour, drawing lines, rectangles, circles, patterns, area filling and so on. To use graphics you need a window open which will have an appropiate bitmap area to draw on.

30. Simple Line Graphics Commands

The following commands can do some simple line graphics on a specified rastport of a Window or Layer. For example:

COLOR foreground, background - Set foreground or background colour using preset colour pens.
PALETTE id, red, green, blue
- Change colour using RGB values (0.0 to 1.0) for a colour id.
PSET [STEP] (x, y) [, colour-id]
- Plots a point at co-ords x,y using colour (or current foreground colour). STEP means relative position.
LINE [STEP] (x1,y1) [-(x2,y2) [,[colour] [,b[f]]]]
- Draw a line from x1,y1 to x2,y2 or if only x,y given, then draw line from last postion to x,y in given colour.
CIRCLE (x,y), radius [,colour, start, end, aspect] - Draw a circle or ellipse at co-ords x,y with radius. Start and end specigy angles in degrees. Aspect is .44 by default.
POINT(x,y) - Reads point at co-ords x,y and returns colour id.

For example, the following program will draw some random sized coloured squares in a window:

RANDOMIZE TIMER
ON WINDOW GOTO
Quit
WINDOW 1, "My Window", (20,20)-(220,170), 31
WINDOW ON

' Draw random boxes on window
FOR n = 1 TO 10
   COLOR RND*32, 1 ' Random colour 0-32
   startx = 10 + RND*50
   starty = 10 +RND*50
   boxwidth = 20 + RND*50

   LINE (startx, starty)-(startx+boxwidth, starty)
   LINE (startx+boxwidth, starty)-(startx+boxwidth, starty+boxwidth)
   LINE (startx+boxwidth, starty+boxwidth)-(startx, starty+boxwidth)
   LINE (startx, starty+boxwidth)-(startx, starty)
NEXT n


WHILE -1
  SLEEP ' Wait forever or event occurs
WEND
Quit:
WINDOW CLOSE
1
END

A new function called RND produces a random number between 0 and 1. Multplying this by another number can produce a number between 0 and that mutiplier. For example, RND*50 will produce a number from 0 to 50. To prevent the same random numbers being produced every time the program is run, the RANDOMIZE TIMER command is used to 'seed' with a value from the TIMER function. You can use the INT (integer) function to round the value to nearest integer e.g. INT(50*RND+1).

I have had to use a starting position so that the Window borders are not overwritten and then used random values to create random start position of the top left of the square and a random size to draw a square. The values for LINE are not relative but absolute positions, I used the startx+value and starty+value to make it easier to draw a square. Once the draw loop is done, the program will wait until the Window close gadget is selected and program ends as normal.

21. Drawing with Areas

It is possible to produce filled polygon 'areas' using the AREA and AREAFILL statements which can plot out an outline of a shape and flood fill it with a specified colour. The syntax of the functions are as follows:

AREA [STEP] (x, y)
AREAFILL [mode]

PATTERN [line-pattern] [, area-pattern] | RESTORE
PAINT (x,y) [[, color-id] [, border-id]]

An area is initialised with a series of AREA statements which sets up a working area for areas and flood fills in the Window. The values for x,y are the absolute or relative (using STEP option) co-ordinates. The AREAFILL statement will fill the AREA defined with current pattern and foreground colour (mode 0). Otherwise it will fill with the current pattern but the inverse of foreground colour (mode 1).

The PATTERN statement defines fill pattern for lines and areas. The line pattern value is a short integer value, while the area pattern is a short array integer array. The number of elements must be a power of 2 (e.g. 2, 4, 8, 16, 32 etc). The RESTORE option (ACE Basic only), resets patterns to the default values.

The PAINT command can flood fill an enclosed region with a specified colour upto the edges specified by by the colour-id otherwise a different colour value can be specified for the border by using an option border-id. The x,y co-ordinates can be anywhere within the region.

An example below, draws a simple corridor using filled areas:

RANDOMIZE TIMER
ON WINDOW GOTO Quit
WINDOW 1, "My Window", (20,20)-(220,170), 30
WINDOW ON
' Create corridor with areas
COLOR 3,1
AREA (90,12)
AREA (90,40)
AREA (30,135)
AREA (30, 75)
AREAFILL 0


COLOR 6,1
AREA (4,75)
AREA (30, 75)
AREA (30, 135)
AREA (4, 135)
AREAFILL 0


COLOR 3,1
AREA (110, 12)
AREA (170,75)
AREA (170, 135)
AREA (110, 40)
AREAFILL 0

   
COLOR 6,1
AREA (170, 75)

AREA (195, 75)
AREA (195, 135)
AREA (170, 135)
AREAFILL 0


COLOR 1,1
LINE (90, 40)-(110, 40)
LINE (30, 135)-(170, 135)

 


WHILE -1
  SLEEP
' Wait forever or event occurs
WEND
Quit:
WINDOW CLOSE
1
END

Next Page