Title :True BASIC Libraries | Electronic Arcs By :Brad Grier | Volume 1, Number 2 Filename :Software2 | (c) 1987, Brad Grier & Authors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - With the advent of more sophisticated versions of BASIC, the programmers task of taking an idea and producing a working computer tool is much easier. One of the newer programming methods is the use of Program Libraries. A Program Library is a collection of sub-programs, or subroutines that can be incorporated in a larger program under development. For example, you may be developing a game that needs a routine to roll 2 dice and return the resulting number to your program. If you have written any other program that has that subroutine in it, you could just cut and paste the routine in to the new program. But if you find yourself re-writing that routine into a LOT of programs, then you could create a Program Library that has your routine in it. Just place a program call to that library program, instead of keying in the whole thing again. Over time, you will develop quite a number of routines in that Program Library, and much of your programming code will be simply linking the routines together. The Amiga comes with some Libraries on the workbench disk. Programmers who write in C can access those with commands built into the Language they are working with. Amiga Basic programmers have to create special '.bmap' files to use some of the functions provided, and that can get a bit complex. True BASIC takes a different approach to Program Libraries. Their libraries are quite easy to incorporate into a program. By using the "Library (filename)" command, you have just added a whole new set of commands or functions to your program. Currently, the True BASIC Language system supports the following Commercial Libraries: Advanced String Library - String manipulation routines Sorting and Searching Library - Search and Sort routines 3-Dimensional Graphics Library - 3-D Plotting and Function routines The following Libraries are distributed on the True BASIC Language system disk: FnhLib - Hyperbolic functions FntLib - Trigonometric functions (in radians) FntdLib - Trigonometric functions (in degrees) FnmLib - Mathematical Functions. GraphLib - Graphic subroutines MenuLib - Menu routines (Non Intuition) AmigaLib - Various Amiga system routines Next, how do we use one of these libraries in our True BASIC programs. If you wanted your Amiga to read aloud a text file, you would make a call to the AmigaLib library and use the Say(speech$) command. Your True BASIC program would then read aloud all the text in the string 'speech$'. This is illustrated in the following short program. ! ! ! ReadTo a True BASIC program to ! ! read text files aloud. ! ! ! ! By Brad Grier ! ! ! ! ! LIBRARY "True Basic:libs/AmigaLib*" ! Define Libraries we are going to use DECLARE DEF GetFile$ ! Define special function for ! Requester CLEAR ! Clear Screen LET text$="ReadTo" ! Set gadget name LET x=0 ! set requester to center of screen LET y=0 ! ! !Introductory text ! PRINT "ReadTo, a True BASIC program to read a text file aloud." PRINT PRINT "Using the requester, Highlight the file you want to have read, by" PRINT "clicking, or scrolling to it." PRINT ! ! Speak Introductory text ! CALL say("ReadTo, a True BASIC program to read a text file aloud.") CALL say("Using the requester, Highlight the file you want to have red, by") CALL say("clicking, or scrolling to it.") LET filename$=GetFile$(x,y,type$,text$) ! Get the filename IF filename$="" then ! Test for Abort CALL say("You have cancelled Read To") ! Inform user pgm aborting STOP ! Abort END IF LET b$="U have opened file " LET c$[Maxnum:0] = b$ & filename$ ! Say the name of the file gotten CALL Say(c$) ! ! Open the file ! OPEN #2: name filename$, organization text, access outin DO while more #2 ! Main Program Loop LINE INPUT #2: line$ ! Read while there are more lines PRINT line$ ! in the text file. Print as well as CALL say(line$) ! Say the line. LOOP ! Do it again CALL say("Attention!,End of File. Readto Terminated.") ! If end of file ! encountered, then END ! terminate program. In this example, we are using a call to a 'C' language routine inside the Amigalib library. This routine takes the text string, and feeds it to the speech processor, where it is spoken. If I were to develop a special routine that would ask for a name, address and phone number, and put it into a library, then anyone who had that library could use that routine. They would be spared the headache of writing or debugging it. Now that you have a bit of a feel for what libraries are used for, lets take a look at one. The True BASIC 3-D Graphics Library... This library can be thought of as a bunch of new commands that you add to True BASIC, which will allow you to easily design three dimensional graphs, charts, and objects. In short, you have a number of sub-routines that allow you to enter data in a three dimensional format. Then those routines will translate that data to a two dimensional display on your screen. Some of the commands act like their True BASIC counterparts. Others have specific functions. Here is a list of some True BASIC three dimensional commands, and what they do. Simple Graphics Command Description ------- ----------- PersWindow Set perspective window. ParaWindow Set parallel window. SetCamera3 Set camera position. AskCamera3 Ask camera position. PlotOff3 Plot point, turn beam off. PlotOn3 Plot point, turn beam on. PlotRoff3 Plot relative, turn beam off. PlotRon3 Plot relative, turn beam on. LineOn3 Plot line segment, turn beam off. LineOff3 Plot line segment, turn beam on. PlotText3 Plot Label. Project3 Convert 3D to 2D coordinates. Gaining a new Perspective... The first two commands above, open a special type of graphics window. In this window, you can create three dimensional images. There are two types of windows that you can open, Parallel and Perspective. The Perspective window, allows you to create and display images that appear to mimic three dimensional objects in real life. The objects appear to have depth, and the object shape will change according to the viewing distance and angle chosen. The Parallel window will open to allow creation of objects that do not have a vanishing point. The size and shape of the object will not appear to change in relation to the viewing distance. For most purposes, the Perspective window will be used over the Parallel, because of the added degree of realism. The three dimensional window consists of six major components, the Object(s), the Window, the Camera, and the X, Y, and Z axis. The Object(s) are a bunch of points displayed in the area seen by the window. We have already discussed the different types of windows available, so onward to the Camera. The Camera is nothing more than a point in the volume of space from which you look towards your objects. The Camera can be moved as far away as the window allows, or as close in as the middle of the object you are drawing. In some commands, the Camera will be placed at a point which allows a nice viewing angle on the Object automatically. The X, Y, and Z axis, define the volume where the object is to be plotted. These are defined in the initial calls to the window type, as you will see in the example below. ! Draw the marked cube with ! a default perspective view. ! library "3dlib" ! Define the 3-D Graphics Library call PersWindow(0,1,0,1,0,1,work$) ! Define the viewing volume as a ! Perspective window, with an X, Y, and ! Z axis from points 0 to points 1. ! work$ is an internal variable for ! command use. call MarkedCube (work$) ! This is a routine included in the ! True BASIC 3d library to draw a cube ! with marked sides. end ! End Program The call to the routine, PersWindow defined the screen width, height, and depth. The call to MarkedCube drew the cube. Boxes and cubes are nice, but what about something productive? How about a three dimensional bar graph. This next program will show just how easy programming three dimensional graphics in True BASIC can be. The image it produces is in figure 1. ! ! BARS3 ! ! A 3-D barchart using perspective, shaded boxes, ! and a grid. ! ! This program adjusts the reference point and two-d ! windows to get a more dramatic image. ! LIBRARY "3dlib*" CALL PersWindow(0,10,0,1,0,4,s$) ! Set up perspective window CALL SetRefPt3(6.5,0,1,s$) CALL SetCamera3(7.5,-5,3,s$) ! Set camera location ASK window x1,x2,y1,y2 ! Check window size SET window x1/2,x2/2,y1/3,y2/3 ! Reset window size CALL GridZ3(0,.5,11.5,-3,20,1,1,s$) ! Turn on 'ground' grid FOR x = 1 to 11 ! Draw the blocks READ z CALL Block3(x-.15,x+.15,0,1,0,z,"magenta","green","white",1,s$) NEXT x DATA 1, 1.4, 2.2, 2.4, 1.7, 2.6, 1.8, 1.1, .5, .3, 1 END ! All finished You might have noticed that this program uses some routines I have not described yet. These are some of the more advanced graphic routines in the True BASIC three dimensional graphics library. They allow direct positioning of the camera, manipulation of the view plane, view point, and perspective. The True BASIC three dimensional graphics library also includes routines for plotting three dimensional mathematical functions (z functions or functions of two variables z = f(x,y)). The routine draws the z surface of the function by following contour lines along the x or y direction. This results in images that resemble a drop of water hitting the surface of water (See figure 2). Some of these routines, take care of things like Hidden Lines, and Window clipping, and others don't. Which you use depends on your specific application. Two of the most interesting programs included on the disk, allow you to generate three dimensional rotations. These do take a lot of time, and memory, but the result is worth it. One demo shows a house rotating quite quickly, in real time! The True BASIC three dimensional graphics library includes many routines for plotting mathematical functions and formula, and for drawing objects, graphs, or what ever you want. If you need a simple method of drawing in three dimensions, then this Library is for you.