AMIGA 'C' " A TWO AND A HALF GENERATION LANGUAGE " A third generation language like BASIC or COBOL can be coded quickly, but the program runs slowly. (The source code is people friendly.) A second generation language like Assembler is coded slowly and painfully, but the program runs quickly. (The source code is computer friendly.) The 'C' language is somewhere in between - it is reasonably quick to write, and can be compiled into fairly efficient machine code. The 'C' langauge is becoming standardised, this makes it very portable. Source code can be written and tested on one type of computer, then compiled into object code, tested and run. The source code can then be copied to a completely different type of computer (or network), and should compile and run successfully. 'C' does not have input and output facilities of its own, these are added from a 'library' or set of related routines that can be linked with the object code. Each compiler comes with a set of standard C library high level input and output functions. In addition there are several Amiga Libraries, each containing lower level routines for use in your programs.(These functions are often called by higher level C library functions.) These programming pages will concentrate on the Amiga Library routines rather than the C syntax, unless I have a storm of protest from my readers. The 'C' language can be extended almost indefinitely, by adding routines and creating a custom library, or by including third party libraries. C LIBRARIES The main compiler header directory contains some header files (they have '.h' suffix) in addition to its subdirectories. These header files contain the ANSI compatible prototypes of the standard input and output functions, and other definitions. To use one of these functions, the appropriate header file should be 'included' with the source code. This command tells the compiler to add the header file at compile time, but not clutter the source code with it. This source code will be more portable than the source code using Amiga Library routines, but not as versatile or efficient. The program source code listing (iolib.c) is available on the support disc. When the object program (iolib) is run, the program displays a message using the built-in formatted print 'printf' command, and then another message by using the standard library 'fprintf' command to write to the current output file, or console. AMIGA LIBRARIES The main Amiga libraries used are Exec, DOS, Graphics and Intuition. Exec library routines handle the multitasking and the lowest level of interaction between the hardware and the software. DOS library routines handle input and output control. Associated with each library is a special file with extension '.fd', these files are usually found on the Extras disc in their own directory. An '.fd' file contains a list of the routines in the library, together with the input parameters that each routine takes. The more recent ANSI compatible compilers have a 'proto' directory included with the compiler headers. This directory has a header file for each library, containing the prototype for each command. If you open an Amiga library at run time, all its routines become available. The linker knows where to find the routines, and can link them to the rest of the object code. The 'OpenLibrary' routine is found in the Exec library, all the Exec library routines are always available. C DEMO The short demo program 'amilib' on the support disc uses five library routines. From the Exec library :- OpenLibrary(libName,version) opens a chosen library. ( A pointer to the library base is returned.) CloseLibrary(library) closes a library. From the Dos Library :- Delay(timeout) waits for a given timeout. Output() finds the file handle for writing text in the current window. (A pointer to the current filehandle is returned.) Write(filehandle,buffer,length) will write to a file or a window. The complete program source code listing (amilib.c) is available on the support disc. When the object program (amilib) is run, the program opens the DOS library. If the library opens successfully, the program displays a short message, delays for a few seconds, closes the library and exits. ****************************************************************************** SPRITES FOR ASSEMBLY Assembler programmers can now have sprites designed using a program like DPaintII. The height, colours and pixel data can be saved on disc and read into an Assembler program when required. COPPER LIST Each sprite has a data list of data word pairs. The first two words of each sprite's data are control words to initialise the sprite. Bits 15 to 8 of the first control word contain the vertical position of the first line of the sprite, bits 7 to 0 contain the sprite's horizontal start position. Bits 15 to 8 of the second control word contain the line after the last vertical line of the sprite. Bits 2 to 0 of the second control word contain the most significant bits of the first line, last line and horizontal position respectively. The last two data words always contain zero. The intervening data words contain the pixel data. The first two sprites use colours 16 to 19, the next two 20 to 23, the next two 24 to 27 and the last two 28 to 31. The sprite data is copied into chip memory.The Copper list can be set up to copy the address of the start of each sprite's data into the appropriate SPRPT registers. The colour instructions can also be included in the Copper list. ARTISTIC LICENCE The DPaintII utility was used to save two sprites as separate 'brushes', producing a short IFF file for each brush. I designed the sprites to be 16 pixels wide. I used four colours, the first being transparant.The file reader module 'readsprite.a' can read and interpret this file, or a similar file produced by another artist program. JUST ROUTINE The routine 'readfile' is similar to the IFF sprite file in last month's Amiga Computing, a simpilified picture file reader without a routine to unscramble compressed data. The routine can return the height of the sprite to the calling program. The routine is:- spriteheight = readfile(name,spritedata,colourlist) (d0,d1,d2) The inputs are :- name - the pointer to the name of the file containing the data spritedata - a pointer to the memory array to hold the sprite bitplane data (the address of the third word of the sprite) colourlist - a pointer to the memory storing the colours for the sprite (the colourlist address plus an offset for colour 16,20,24 or 28) The output in Register 'd0' is :- spriteheight - The height of the sprite contained in the file The routine will look for the usual chunkheaders in an IFF file. The BMAP chunk will be decoded to give the sprite height; the other data is either known (width and depth) or irrelevant (data from the screen the brush was saved on). The CMAP chunk contains the colours used when the sprite was designed, these colours will be copied into the correct place in the colourlist. The BODY chunk contains the bitplane data, the routine copies it into chosen area in memory. All other chunks are read, and their data ignored. (The routine could easily be extended to process these chunks.) The sprite height is returned so that the position of the last line of the sprite can be calulated and put in the second sprite control word. The main program will then have all the necessary sprite and colour information for the Copper list. THE DEMO The demo program on the support disc contains two source modules, rsprite.a and spriteread.a, which can be assembled and linked together in the normal way. Spriteread.a is the general purpose sprite file reader, and rsprite.a is the calling program. The object module, rsprite, displays a black, one bitplane screen. Each ILBM picture file (DPaintII brush), with the filename specified in the calling program, is opened and the sprite data read into the appropriate place for the second and third sprite. ( The first and fourth sprites will have their colours changed, the last four sprites are undisturbed.) The file is closed, and control goes back to the main program, which updates the sprite height in the sprite control words. The sprite, colour, and bitplane instructions are copied into the Copper list in chip memory. The Copper DMA is disabled, then enabled again after the new Copper list is triggered. The eight sprites are displayed on the screen until either a mouse or joystick button is pressed. The old Copper list is brought back, the memory freed, and the program exits. ****************************************************************************