 _________________________________________________________________________
/#########################################################################\
|#####...####..######..######......####...####..#####..##......##......###|
|####.....###..######..########..#####.....###...####..##..######..#######|
|###..###..##..######..########..####..###..##....###..##..######..#######|
|###..###..##..######..########..####..###..##..#...#..##..######......###|
|###.......##..######..########..####.......##..##.....##..######..#######|
|###..###..##..######..########..####..###..##..###....##..######..#######|
|###..###..##...#####...#######..####..###..##..####...##..######..#######|
|###..###..##......##......##......##..###..##..#####..##......##......###|
|#########################################################################|
|####################################################################{RB}#|
|=========================================================================|
|									  |
|		           ----> PRESENTS <----				  |
|									  |
|			THE ABACUS SERIES - VOLUME 2		          |
|									  |
|            AMIGA GRAPHICS INSIDE AND OUT - THE COMPLETE BOOK            |
|									  |
| Typed / Scanned / Edited By : RAZOR BLADE.			          |
| Additional Typing by        : GLITCH ( + 8 pages by Asterix ! ).	  |
| Menu Coded by 	      : RAISTLIN				  |
| Original Supplied by	      : VIPER					  |
|									  |
|-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|
| 		CALL THE ALLIANCE WORLD HQ -> THE PLACE TO BE             |
|       UNKNOWN PLEASURES --> +44 (0) 823 322 891 --> SYSOP: BARBARIAN    |
|_________________________________________________________________________|



-----------------------------------------------------------------------------
 
4.2 THE GRAPHIC MODES OF THE AMIGA.
 
The amiga has the following nine special graphic modes :
 
        Genlock Video.
        Interlace.
        PFBA.
        Extra halfbrite.
        DUALPF
        HAM
        VP-Hide
        SPrites
        Hi-res
 
You should already be familiar with the hi-res and interlace modes supported
by AmigaBASIC. The AmigaBASIC SCREEN statement can specify screens from 
lo-res (normal 320 pixels wide), hi-res (640 pixels wide) to interlace 
(400 instead of 200 pixels high).
 
If the ViewPort will contain sprites or Vsprites, you must set the sprite 
flag (this is normally true).
 
VP-Hide is set whenver this ViewPort is covered by other ViewPorts (for
example, a screen covers another screen). The other viewport is not 
displayed.
 
Genlock Video means that instead of the background colour, an external
video signal, like a video recorder or camera, provides the background.
You need a genlock interface to use this mode.
 
DUALPF stands for "dual playfield". This mode allows you to display two
different planes in one ViewPort. The background of the top plane becomes
transparent to show the plane underneath.
 
PFBA works with the dual playfield mode. It determines the video priority 
of the two planes.
 
HAM stands for Hold and Modify. This mode allows you to display all 4096
colours of the Amiga on the screen at the same time. However, this display
mode is very difficult to program. More on this subject shortly.
 
Extra halfbrite is a new graphic mode that allows you to display up to 64
colors at the same time instead of the normal 32.
 
                                PAGE 155
 
-----------------------------------------------------------------------------
 
 
4.2.1 THE HALFBRITE MODE.
 
Extra halfbrite is one of the special graphic modes not supported by the 
BASIC SCREEN statement. It is therefore impossible to create a screen in 
this mode using SCREEN.
 
It is possible to convert an existing screen to a halfbrite screen. Before 
we show you how to do this we are going to explain the halfbrite technique.
 
Normally the Amiga can only display up to 32 colors at one time. This is 
because of two factors: 1.) The number of available colours is determined
by the number of bit-planes (5, 2^5=32)and 2). The Amiga has 32 colour 
registers to store colour values that were defined using the AmigaBASIC 
PALETTE statement.
 
When the halfbrite mode is active, you have six bit-planes available and the
number of available colours increases accordingly from 2^5=32 to 2^6=64.
However, since there are only 32 colour registers, there isnt space for the
other 32 colours. To  solve this problem we use the 32 registers twice. We
obtain colours 0 to 31 directly from registers 0 to 31. Colours 32 to 63 
are also obtained from registers 0 to 31 but the RGB values in the registers
are shifted one bit to the right.
 
However, this causes three effects: 1.) The extra 32 halfbrite colours 
cannot be freely defined because they rely on the values of the first 32
colours. 2.) the extra colours are copies of the existing colours but are
darker (therefore the name halfbrite), and 3.) if the first 32 colours are
very dark there will be little visible difference between them and the 
second 32 colors.
 
Although these limits may sound troublesome, the halfbrite mode is still 
reqarding because you can have an extra 32 slightly darker variations of 
your first 32 colours to work with.
 
Since we cannot normally active the halfbrite mode with BASIC, we are going
to create a screen with a depth of five bit-planes. From the information
in the previous chapters, we know the Amiga graphics system fairly well by
now. Therefore, it should not be difficult to set up a sixth bit-plane in
the bit-map structure of the screen. Finally, set the halfbrite flags in 
the ViewPort. (There is just one small problem that we will cover shortly).
 
To mke our screen a reality, we have to access two system libraries :
We need the following functions :
 
        RemakeDisplay()
        AllocMem()
        FreeMem()
 
 
                                PAGE 156
 
-----------------------------------------------------------------------------
 
 
Our program, the halfbrite activator, follows. In addition to the 
demonstration program there are two SUB programs, HalfBriteOn and 
HalfBriteOff. Neither SUB requires any arguments.
 
        '#####################################
        '#
        '# Section : 4.2.1
        '# Program : Halfbrite Activator.
        '# Date    : 01/17/87
        '# Author  : tob
        '# Version : 1.1
        '#
        '#######################################
        '
        ' Activates the Amiga special graphic mode "halfbrite"
        ' Not normally available in BASIC. With 6 bit-planes
        ' there are a total of 64 different colours available.
        ' We will explain the functions and the most effective
        ' programming methods for this mode in this book. NOTE :
        ' This mode only functions in LoRes mode.
 
        PRINT "Searching for .bmap file ..... "
 
        'EXEC-Library
        DECLARE FUNCTION AllocMem& LIBRARY
        'FreeMem
        'INTUITION-LIBRARY
        'RemakeDisplay()
        
        LIBRARY "intuition.library"
        LIBRARY "exec.library"
 
        main:
            loRes            = 1
            screen.nr%       = 1
            screen.x%        = 320
            screen.y%        = 200
            screen.depth%    = 5 '5 planes required'
            screen.resolution% = loRes
            SCREEN screen.nr%,screen.x%,screen.y%,screen.depth%,
screen.resolution%
 
            'Open a window in the new screen.
            windo.nr%          = 1
            windo.name$        = "Halfbrite!"
            WINDOW windo.nr%,windo.name$,,,screen.nr%
 
 
                                PAGE 157
 
-----------------------------------------------------------------------------
 
 
        demo: 'Activate Halfbrite
            HalfBriteOn
 
            PRINT TAB(10);"The HalfBrite Mode!"
             
            'The original colours.
            LOCATE 3,2:COLOR 1,0
 
            PRINT "A ";
            FOR loop%=0 TO 31
                COLOR 0,loop%
                PRINT " ";
            NEXT loop%
 
            '*  ... and the halfbrite colours.
            PRINT "B ";
            FOR loop% = 32 TO 63
                COLOR 0,loop%
                PRINT " ";
            NEXT loop%
 
            LINE (22,15)-(280,32),1,b
            LOCATE 7,2:COLOR 1,0
            PRINT "A: The 32 original colours, stored"
            PRINT "   in the hardware colour registers."
 
            LOCATE 10,2
            PRINT "B: The additional 32 HalfBrite "
            PRINT "   colours, corresponding to the "
            PRINT "   original colours at half intensity"
 
            LOCATE 14,2
            PRINT " The blinking sample shows when the"
            PRINT " Colour register of the original colour"
            PRINT " is changed, the HalfBrite colour is"
            PRINT " changed accordingly."
 
            LOCATE 19,4
            PRINT "Press the left mouse button"
 
            WHILE check% = 0
                check% = MOUSE(0)
                PALETTE 30,.7,.2,.9
                FOR t = 1 TO 500 : NEXT t
                PALETTE 30,.3,.8,.1
                FOR t = 1 TO 500 : NEXT t
            WEND
 
            FOR loop% = 0 TO 31
                COLOR loop%,loop%+32
                LOCATE 20,1
                PRINT "TEST COLOUR ";loop%
                PRINT "Text Colour            - Original colour"
                PRINT "Background Colour      - Halfbrite colour"
                FOR t = 1 TO 500: NEXT t
            NEXT loop%
            CLS
            COLOR 1,0
 
 
                                PAGE 158
 
---------------------------------------------------------------------------
 
 
        endprog:   ' Halfbrite off and close screen. '
            HalfBriteOff
            WINDOW windo.nr%,windo.name$,,,-1
            SCREEN CLOSE screen.nr%
            PRINT "End of DEMO !"
            LIBRARY CLOSE
            END
 
        SUB HalfBriteOn STATIC
            SHARED screen.mode%
            SHARED screen.viewport&
 
            ' Define variables.
            MEM.CHIP    = 2^1
            MEM.CLEAR   = 2^16
            memory.option& = MEM.CHIP + MEM.CLEAR
            window.base&   = WINDOW(7)
            screen.base&   = PEEKL(window.base&+46)
            screen.bitmap& = screen.base&+184
            screen.viewport&=screen.base& + 44
            screen.rastport&=screen.base& + 84
            screen.width%   = PEEKW(screen.bitmap&)
            screen.height%  = PEEKW(screen.bitmap&+2)
            screen.size&    = screen.width%*screen.height%
            screen.depth%   = PEEK(screen.bitmap&+5)
            screen.mode%    = PEEKW(screen.viewport&+32)
 
            '* SCREEN already has 6 bitplanes ????
            IF screen.depth%>5  THEN screen.depth%=2^8
 
            '* add missing Bitplanes.
            FOR loop1% = screen.depth%+1 TO 6
                plane&(loop1%)   = AllocMem&(screen.size&,memory.options&)
                IF plane&(loop1%) = 0 THEN
                    FOR loop2% = screen.depth%+1 TO loop1%-1
                        CALL FreeMem(plane&(loop2%),screen.size&)
                    NEXT loop2%
                    ERROR 7
                END IF
                POKEL screen.bitmap&+4+4*loop1%,plane&(loop1%)
            NEXT loop1%
            POKE screen.bitmap&+5,6
 
            'Halfbrite on
            POKEW screen.viewport&+32,(screen.mode% OR 2^7)
            CALL RemakeDisplay
        END SUB
 
        SUB HalfBriteOff STATIC
            SHARED screen.mode%
            SHARED screen.viewport&
 
            'Reset Halfbrite Flag
            POKEW screen.viewport&+32,screen.mode%
            CALL RemakeDisplay
        END SUB
 
 
                                PAGE 159
 
-----------------------------------------------------------------------------
 
 
Working with the halfbrite program :
 
After you call the SUB "HalfBriteOn" you will have 64 different colours
available. You can freely define the first 32 colours using the BASIC 
PALETTE statement:
 
        PALETTE register, reg, green, blue
 
        register : 0,31
        red,green,blue : 0.0 - 1.0
 
Colours 32 to 63 are defined at the same time except they are half as 
bright.
 
With the COLOR statement you can select any colour from 0 TO 63, draw, 
print text and fill. But you should remember that for AmigaBASIC, the 
screen still only has five bit-planes. When BASIC scrolls the screen (when
you write text in the last row) only five planes will scroll; the sixth
plane stays in place. To avoid this problem do not print to the last screen
line.
 
When you no longer need the HalfBrite mode you can deactivate it by using
the SUB "HalfBriteOff".
 
At the beginning of this section we mentioned a problem involving changes 
to the HalfBrite flag in the ViewPort. Setting this flag does not do 
anything. It doesn't matter what type of manipulation you perform in the 
ViewPort, nothing is changed in the display. 
 
This happens because the display is only changed by the hardware registers.
Since ViewPort is a data block in RAM and not a hardware register, the 
display isn't changed. In order to affect the display. the information
in ViewPort about how the display is formed must first be sent to the 
Copper. This is because the Copper controls and programs the hardware.
 
We make ViewPort changes effective when we call the Intuition function
"RemakeDisplay". This function creates a new Copper list and reflects the 
change in the ViewPort structure. Finally, this list is sent to the 
Copper.
 
 
                                PAGE 160
 
-----------------------------------------------------------------------------
 
 
4.2.2 THE HOLD AND MODIFY MODE : 4096 COLOURS.
 
The Hold and Modify mode (abbr. HAM) is also not supported by AmigaBASIC.
You cannot access it by using the SCREEN statement.
 
We can activate this mode for a screen that already exists. Before we do 
this, let's take a look at the principles for using this mode.
 
When the HAM mode is active, you can display up to 4096 colours at the 
same time. If we follow the normal rules for displaying 4096 colours, we 
would require 12 bit-planes. This would require a large amount of memory 
(1 bit-plane = 64000 bytes in lo-res, 12 bit-planes = 768000 bytes!). Also
the Amiga's DMA (Direct Memory Access) is not fast enough to retrieve and 
build a new screen every 1/60th of a second from 12 different RAM areas. 
Obviously, we need a special procedure.
 
Actually, HAM works with only 6 bit-planes just as the halfbrite mode. The 
first 16 colours are the exact colours that were defined for the first 16
colour registers. All other colours are determined by the HAM principle.
They use the colour of the pixel to the left and modify the RGB value.
 
Before we undertake the complex arrangement of a HAM graphic, we need to 
activate the mode. This is very similar to the halfbrite mode. We create
a sixth bit-plane, add it to the bit-map and then set the HAM flag in the
ViewPort. A call to RemakeDisplay switches the display to HAM. Again, there
are two SUB programs, HAMon and HAMoff.
 
        '#####################################
        '#
        '# Section : 4.2.2
        '# Program : HAM activator.
        '# Date    : 02/16/87
        '# Author  : tob
        '# Version : 1.4
        '#
        '#######################################
        '
        ' Activates the Amiga special graphic mode "HAM" (Hold 
        ' And Modify) not normally availble to BASIC. Provides 
        ' up to 4096 colours at the same time (with 6
        'bit-planes. NOTE : only functions in lores mode.
 
        PRINT "Searching for .bmap file ..... "
 
 
                                PAGE 161
 
-----------------------------------------------------------------------------
 
        'EXEC-Library
        DECLARE FUNCTION AllocMem& LIBRARY
        'FreeMem
        'INTUITION-LIBRARY
        'RemakeDisplay()
        
        LIBRARY "intuition.library"
        LIBRARY "exec.library"
 
        main:
            loRes            = 1
            screen.nr%       = 1
            screen.x%        = 320
            screen.y%        = 200
            screen.depth%    = 5 '5 planes required'
            screen.resolution% = loRes
            SCREEN screen.nr%,screen.x%,screen.y%,screen.depth%,
screen.resolution%
 
            'Open a window in the new screen.
            windo.nr%          = 1
            windo.name$        = "HAM! 4096 colours available.!"
            WINDOW windo.nr%,windo.name$,,,screen.nr%
 
        demo: 'Activate HAM
            HAMon
 
            PRINT TAB(7);"256 of 4096 colours"
             
            s = 10                ' box size
            x = 40                ' Position of upper
            y = 20                ' left corner of demo.
            PALETTE 3,0,0,0       ' FRAME COLOUR.
            PALETTE 4,.5,0,.5     ' DARK PURPLE.
            PALETTE 5,1,0,1       ' LIGHT PURPLE.
            PALETTE 6,1,0,0       ' LIGHT RED.
            PALETTE 7,0,0,1       ' LIGHT BLUE.
 
            'SET ORIENTATION MARKS.
            LINE (5,y)-(5+8,y+8),4,bf
            LINE (240,y)-(240+8,y+8),7,bf
            LINE (5,166)-(5+8,166+8),6,bf
            LINE (240,166)-(240+8,166+8),5,bf
 
            ' DRAW FRAME.
            LINE (x-1,y-1)-(x+17*s+1,y+16*s+1),3,b
 
            ' Draw first 256 HAM colours.
            FOR loop% = 0 TO 15
                LINE (x,loop%*s+y)-(s+x,loop%*s+s+y),32+loop%,bf
                FOR loop2% = 0 TO 15
                    LINE (s+loop2%*s+x,loop%*s+y)-(2*s+loop2%*s+x,
loop%*s+s+y),loop2%+16,bf
                NEXT loop2%
            NEXT loop%
 
 
                                PAGE 162
 
-----------------------------------------------------------------------------
 
 
        '* Raise Green Level.
        FOR loop2% = 0 TO 15
            PALETTE 3,0,loop2%*(1/15),0
            LOCATE 10,28
            PRINT "Green Level: "
            PRINT TAB(31) loop2%
            FOR t = 1 TO 3000: NEXT t
        NEXT loop2%
 
        LOCATE 2,7
        PRINT "Please Press a Key ! "
        WHILE INKEY$ = "": WEND
 
        endprog:   ' HAM off and close screen. '
            HAMOff
            WINDOW windo.nr%,windo.name$,,,-1
            SCREEN CLOSE screen.nr%
            PRINT "End of DEMO !"
            LIBRARY CLOSE
            END
 
        SUB HAMOn STATIC
            SHARED screen.mode%
            SHARED screen.viewport&
 
            ' Define variables.
            MEM.CHIP    = 2^1
            MEM.CLEAR   = 2^16
            memory.option& = MEM.CHIP + MEM.CLEAR
            window.base&   = WINDOW(7)
            screen.base&   = PEEKL(window.base&+46)
            screen.bitmap& = screen.base&+184
            screen.viewport&=screen.base& + 44
            screen.rastport&=screen.base& + 84
            screen.width%   = PEEKW(screen.bitmap&)
            screen.height%  = PEEKW(screen.bitmap&+2)
            screen.size&    = screen.width%*screen.height%
            screen.depth%   = PEEK(screen.bitmap&+5)
            screen.mode%    = PEEKW(screen.viewport&+32)
 
            '* SCREEN already has 6 bitplanes ????
            IF screen.depth%>5  THEN screen.depth%=2^8
 
            '* add missing Bitplanes.
            FOR loop1% = screen.depth%+1 TO 6
                plane&(loop1%)   = AllocMem&(screen.size&,memory.options&)
                IF plane&(loop1%) = 0 THEN
                    FOR loop2% = screen.depth%+1 TO loop1%-1
                        CALL FreeMem(plane&(loop2%),screen.size&)
                    NEXT loop2%
                    ERROR 7
                END IF
                POKEL screen.bitmap&+4+4*loop1%,plane&(loop1%)
 
 
                                page 163
 

-----------------------------------------------------------------------------


            NEXT loop1%
            POKE screen.bitmap&+5,6
 
            'HAM on
            POKEW screen.viewport&+32,(screen.mode% OR 2^11)
            CALL RemakeDisplay
        END SUB
 
        SUB HAMOff STATIC
            SHARED screen.mode%
            SHARED screen.viewport&
 
            'Reset HAM Flag
            POKEW screen.viewport&+32,screen.mode%
            CALL RemakeDisplay
        END SUB
 
 
After you start this program you will see a field of 256 colours that were
created by using only red and blue. In the top left hand corner is a dark
purple colour and in the lower right hand corner, a light purple colour.
Light red is in the lower left hand corner and light blue is in the upper
right hand corner.
 
Now we blend a green slowly and evenly with other colours to display all 
4096.
 
This mass of colours looks very impressive but programming them is not easy.
But, as you will see, it is not too complicated.
 
First, lets define the difference between real colours and HAM colours. Real
colors, the colours 0-15, actually display the values in colour registers
0-15. These colours are permanent and can only be changed with the 
PALETTE statement. The HAM colours, 16-63 are different because they are 
always affected by their neighbouring colour to the left. A HAM colour takes
the colour of the pixel to the left and modifies the RGB components. Which
of the three components that is changed depends on the HAM colour itself.
 
        Color 0           - 15         Real Colour.
        Color 16+0        - 16+15      HAM type 1.
        Color 32+0        - 32+15      HAM type 2.
        Color 48+0        - 48+15      HAM type 3.
 
HAM colour type 1 takes the neighbouring colour and changes the blue 
component. The blue component of the HAM colour corresponds to a value 
higher than 16. The HAM colour 16+12=28 uses the neighbouring colour
and makes a value of 12 for blue.
 
                                PAGE 164
 
-----------------------------------------------------------------------------
 
 
HAM colour type 2 takes the nieghbouring colour and modifies the red 
component. The HAM colour 32+8 = 40 takes the nighbouring colour and makes
a value of 8 in the red component.
 
HAM colour type 3 performs exactly the same function for colours 48 and up,
changing the green component.
 
In our example program we created a black frame making red, green and 
blue = 0. Directly to the right of the frame we drew with a HAM colour
of type 2. This colour checks the pixel colour to the left, the black 
frame and uses the same color. Reg,Green and Blue are still zero. The Blue
field is set by the HAM colour itself. This blue value increases in a loop
that changes every screen row.
 
Directly to the right of this HAM colour we drew 16 HAM colours of type 1.
These add in a red value of one.
 
This creates the colourpattern. The red intensity increases to the right
and the blue intensity increases downward.
 
Now we change the colour of the frme by making the previously black frame 
greener with the PALETTE command. The green value of the frame passes 
immediately to the HAM colours. The entire colour graphic effect results
from an increasing green intensity.
 
 
                                PAGE 165 


-----------------------------------------------------------------------------