home *** CD-ROM | disk | FTP | other *** search
/ Chestnut's Multimedia Mania / MM_MANIA.ISO / graphics / povsrc20 / amiga.c < prev    next >
C/C++ Source or Header  |  1993-09-21  |  24KB  |  910 lines

  1. /****************************************************************************
  2. *                   amiga.c
  3. *
  4. *  This module handles all of the Amiga-specific code for the raytracer.
  5. *
  6. *  from Persistence of Vision Raytracer
  7. *  Copyright 1993 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. *  NOTICE: This source code file is provided so that users may experiment
  10. *  with enhancements to POV-Ray and to port the software to platforms other 
  11. *  than those supported by the POV-Ray Team.  There are strict rules under
  12. *  which you are permitted to use this file.  The rules are in the file
  13. *  named POVLEGAL.DOC which should be distributed with this file. If 
  14. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  16. *  Forum.  The latest version of POV-Ray may be found there as well.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23.  
  24.  
  25. #include "frame.h"
  26. #include "povproto.h"
  27.  
  28. #include <proto/exec.h>
  29. #include <proto/intuition.h>
  30. #include <proto/graphics.h>
  31. #include <proto/dos.h>
  32. #include <proto/expansion.h>
  33. #include <exec/types.h>
  34. #include <intuition/intuition.h>
  35. #include <graphics/display.h>
  36. #include <graphics/gfxbase.h>
  37. #include <graphics/gfx.h>
  38. #include <libraries/expansionbase.h>
  39.  
  40. void geta4(void);
  41. void Requestor_Handler(void);
  42. void Amiga_open(void);
  43. void Amiga_close(void);
  44. void open_requestor(void);
  45. void close_requestor(void);
  46. void write_byte(int x, int y, unsigned char n);
  47. void write_hame_pixel(int x, int y, char Red, char Green, char Blue);
  48.  
  49. void write_cookie(unsigned char *brand, int line);
  50. void make_hame_palette(struct ViewPort *vp);
  51. void SetRGB8 (short reg, unsigned char rr, unsigned char gg,
  52.               unsigned char bb, short base);
  53.  
  54. int  OpenHam8 (void);
  55. void    write_ham8_pixel(int x, int y, char r, char g, char b);
  56. int  OpenFirecracker (void);
  57. void    write_firecracker_pixel(UWORD x, UWORD y, UBYTE r, UBYTE g, UBYTE b);
  58.  
  59. extern unsigned int Options;
  60. extern char DisplayFormat;
  61. extern FRAME Frame;
  62.  
  63. #define INT_REV 29L
  64. #define GR_REV 29L
  65.  
  66. struct IntuitionBase *IntuitionBase;
  67. struct GfxBase *GfxBase;
  68. struct Library *ExpansionBase;
  69.  
  70. struct Screen *s = NULL;
  71. volatile struct Window *w;
  72. struct Task *Requestor_Task;
  73.  
  74. volatile int Requestor_Running;
  75. volatile extern int Stop_Flag;
  76.  
  77. int width, height, depth;
  78. int bytesperrow;
  79. int leftedge, rightedge;
  80. int topedge, bottomedge;
  81.  
  82. int IsAGA;
  83.  
  84. struct Rectangle Rect1 =
  85.    {
  86.    0, 0, 0, 0
  87.    };
  88.  
  89. struct TagItem Tag1 =
  90.    {
  91.    SA_DClip,
  92.    &Rect1
  93.    };
  94.  
  95. struct ExtNewScreen Ham_Screen =
  96.    {
  97.    0, 0,
  98.    0, 0,
  99.    6,
  100.    0, 1,
  101.    HAM,
  102.    SCREENQUIET | NS_EXTENDED,
  103.    NULL,
  104.    (UBYTE *) "POV-Ray",
  105.    NULL,
  106.    NULL,
  107.    &Tag1
  108.    };
  109.  
  110.  
  111. struct NewScreen Ham_E_Screen =
  112.    {
  113.    0, 0,
  114.    0, 0,
  115.    4,
  116.    0, 1,
  117.    INTERLACE | HIRES,
  118.    SCREENQUIET,
  119.    NULL,
  120.    (UBYTE *) "POV-Ray",
  121.    NULL,
  122.    NULL
  123.    };
  124.  
  125. int lacer; /* if non-zero, screen is an interlace screen. Set this... */
  126.            /* ...as soon as you open your HAM-E screen.               */
  127.  
  128. unsigned char *fp0,*fp1,*fp2,*fp3; /* These are pointers which have been... */
  129.                                    /* ...cached from the screens BitMap[]   */
  130.                                    /* ...array. This allows us to get at    */
  131.                                    /* ...them much faster. Set them as soon */
  132.                                    /* ...as you open your HAM-E screen.     */
  133.  
  134. unsigned char bitpat[] =    /* This table is used as a table of masks... */
  135.   {                         /* ...to isolate bits in the HAM-E pixels    */
  136.     128,64,32,16,8,4,2,1,
  137.   };
  138.  
  139. unsigned char ham_cookie[] =  /* ham mode cookie... preceeds any HAM...    */
  140.   {                           /* ...color registers, and triggers hardware */
  141.     0xA2,0xF5,0x84,0xDC,      /* ...into ham mode.                         */
  142.     0x6D,0xB0,0x7F,0x18
  143.   };
  144.  
  145. struct Window *Requestor_Window;
  146. volatile struct MsgPort *Requestor_Port;
  147.  
  148. struct IntuiText chip Body_Text =
  149.    {0, 1, JAM1, 5, 10, NULL, (UBYTE *) "Click to abort the picture", NULL};
  150.  
  151. struct IntuiText chip Abort_Text =
  152.    {0, 1, JAM1, 5, 3, NULL, (UBYTE *) "Abort", NULL};
  153.  
  154. UWORD chip ColorTbl[16] = { 0x000, 0x111, 0x222, 0x333, 0x444, 0x555, 0x666,
  155.                        0x777, 0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd,
  156.                        0xeee, 0xfff };
  157.  
  158. LONG last_red = 0, last_green = 0, last_blue = 0, last_x = -1, last_y = -1;
  159.  
  160. /* Firecracker routines */
  161. typedef struct BOARD {
  162.     ULONG    orgb;
  163.     ULONG    orgb2;        /* autoincrementing */
  164.     UWORD    pad;
  165.     UBYTE    control0;
  166.     UBYTE    control1;
  167.     UWORD    y, x;
  168. } BOARD;
  169.  
  170. BOARD        *board;
  171.  
  172. void amiga_init_POVRAY(void )
  173.    {
  174.    (void) onbreak(amiga_close_all);
  175.    }
  176.  
  177. int matherr (x)
  178.    struct exception *x;
  179.    {
  180.    fprintf (stderr, "Math error type: %d from function %s values: %g %g\n",
  181.            x->type, x->name, x->arg1, x->arg2);
  182.  
  183.    switch(x->type) 
  184.      {
  185.      case DOMAIN:
  186.      case OVERFLOW:
  187.         x->retval = 1.0e17;
  188.         break;
  189.  
  190.      case SING:
  191.      case UNDERFLOW:
  192.         x->retval = 0.0;
  193.         break;
  194.  
  195.      case TLOSS:
  196.      case PLOSS:
  197.         return (0);
  198.  
  199.      default:
  200.         break;
  201.      }
  202.    return(1);
  203.    }
  204.  
  205. void Requestor_Handler ()
  206.    {
  207.    Requestor_Port = CreatePort ("ray trace port", 0L);
  208.    Requestor_Window = BuildSysRequest
  209.              (NULL, &Body_Text, NULL, &Abort_Text, GADGETUP, 280L, 60L);
  210.    Wait ((1 << Requestor_Port -> mp_SigBit)
  211.           | (1 << Requestor_Window -> UserPort -> mp_SigBit));
  212.  
  213.    Requestor_Running = FALSE;
  214.    Stop_Flag = TRUE;
  215.    }
  216.  
  217. void Amiga_open()
  218.    {
  219.    IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",INT_REV);
  220.    if (IntuitionBase == NULL)
  221.      exit(FALSE);
  222.  
  223.    GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", GR_REV);
  224.    if (GfxBase == NULL)
  225.      exit(FALSE);
  226.  
  227.    IsAGA = GfxBase->ChipRevBits0;
  228. //   printf ("IsAGA:%x\n", IsAGA);
  229.  
  230.    Requestor_Running = FALSE;
  231.    }
  232.  
  233. void Amiga_close()
  234.    {
  235.    if (Requestor_Running) {
  236.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  237.       Delay (2L);
  238.       }
  239.  
  240.    if (Requestor_Window)
  241.       FreeSysRequest (Requestor_Window);
  242.  
  243.    Requestor_Window = NULL;
  244.  
  245.    CloseLibrary ((struct Library *) GfxBase) ;
  246.    CloseLibrary (IntuitionBase) ;
  247.    }
  248.  
  249. void open_requestor()
  250.    {
  251.    Requestor_Window = NULL;
  252.    Stop_Flag = FALSE;
  253.    Requestor_Running = TRUE;
  254.    Requestor_Task = CreateTask ("Raytrace Requestor", 2L,
  255.                                 (APTR) Requestor_Handler, 20000L);
  256.    }
  257.  
  258. void display_finished ()
  259.    {
  260.    if (Requestor_Running) {
  261.      Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  262.      Delay (2L);
  263.      }
  264.  
  265.    if (Requestor_Window)
  266.       FreeSysRequest (Requestor_Window);
  267.  
  268.    Requestor_Window = NULL;
  269.    if (Options & PROMPTEXIT)
  270.       {
  271.       printf ("Finished.\nPress CR to quit.\n");
  272.       getchar();
  273.       }
  274.    }
  275.  
  276. int    OpenHam8()
  277. {
  278.     return FALSE;
  279. }
  280.  
  281. int    OpenFirecracker() {
  282.     struct ConfigDev    *dev;
  283.     ULONG            i;
  284.  
  285.     ExpansionBase = OpenLibrary("expansion.library", 0L);
  286.     if (!ExpansionBase) {
  287.         printf("No expansion library\n");
  288.         return TRUE;
  289.     }
  290.  
  291.     dev = FindConfigDev(NULL, 0x838, 0);
  292.     if (dev) {
  293.         board = (BOARD *)dev->cd_BoardAddr;
  294.     }
  295.     else {
  296.         printf("Can't find FireCracker\n");
  297.         return TRUE;
  298.     }
  299.  
  300.     CloseLibrary((struct Library *) ExpansionBase);
  301.         height = 482;
  302.  
  303.         if (Frame.Screen_Width > 768) {
  304.        board->control0 = 0xf8;
  305.            width = 1024;
  306.            }
  307.         else if (Frame.Screen_Width > 512) {
  308.        board->control0 = 0xb8;           
  309.            width = 768;
  310.            }
  311.         else if (Frame.Screen_Width > 384) {
  312.        board->control0 = 0x78;
  313.            width = 512;
  314.            }
  315.         else {
  316.        board->control0 = 0x38;
  317.            width = 384;
  318.            }
  319.  
  320.     board->control1 = 0x7f;
  321.     board->y = 0; board->x = 0;
  322.     for (i=0; i<width*482; i++) {
  323.            /* I write to the auto-increment and non-autoincrement registers
  324.               to make this work on 68000's as well as 68020's. */
  325.            board->orgb  = 0x40404040;
  326.            board->orgb2 = 0x40404040;
  327.            }
  328.  
  329.     return FALSE;
  330. }
  331.  
  332.  
  333. void    write_firecracker_pixel(x,y, r,g,b)
  334. UWORD    x,y;
  335. UBYTE    r,g,b;
  336. {
  337.     ULONG        v1,v2,v3,v;
  338.         int             new_x, new_y;
  339.  
  340.     v1 = r; v2 = g; v3 = b;
  341.     v = ((v1<<16)&0xff0000) + ((v2<<8)&0xff00) + (v3&0xff);
  342.  
  343.     new_y = y+(482-Frame.Screen_Height)/2; new_x = x+(width-Frame.Screen_Width)/2;
  344.         if ((new_x >= 0) && (new_y >= 0)
  345.             && (new_x < width) && (new_y < 482)) {
  346.            board->y = new_y;
  347.            board->x = new_x;
  348.        board->orgb2 = v;
  349.            }
  350. }
  351.  
  352. /*:
  353. .n write_byte()
  354. .k low_level write_byte byte_write write_pixel
  355. .b
  356.  
  357. SYNOPSIS:   void write_byte(x,y,n);
  358.                 short x;
  359.                 int y;
  360.                 unsigned char n;
  361.  
  362. FUNCTION:   This function is similar to the Amiga's WritePixel routine.
  363.             is simply takes the incoming position and value and writes
  364.             them to the HAM-E screen appropriately.
  365.  
  366. INPUTS:     x - horizontal position on screen.
  367.             y - vertical position on screen.
  368.             n - value 0-255 to write in the pixel.
  369.  
  370. RESULTS:    None
  371.  
  372. BUGS:       None Known.
  373.  
  374. LIMITATIONS:Hard coded for screen width of 320 pixels (640 hi-res pixels)
  375.  
  376. SEE ALSO:   Global variables "fp3", "fp2", "fp1", "fp0", "bitpat[]"
  377.  
  378. :*/
  379.  
  380. void write_byte(x,y,n)
  381.   int x;
  382.   int y;
  383.   unsigned char n;
  384.   {
  385.   register int ypos,byte_offset;
  386.   register short bit_offset;
  387.     ypos = y * 80; /* index to correct scan line - note hard coded width! */
  388.     bit_offset = (x << 1) & 7; /* find base bit position */
  389.     byte_offset = (x >> 2);   /* find base byte offset */
  390.     if (n & 128) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  391.             else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  392.     if (n &  64) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  393.             else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  394.     if (n &  32) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  395.             else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  396.     if (n &  16) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  397.             else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  398.     bit_offset++; /* to next nybble */
  399.     if (bit_offset == 8) /* carry into next byte? */
  400.       {
  401.         bit_offset=0;
  402.         byte_offset++;
  403.       }
  404.     if (n & 8) *(fp3 + ypos + byte_offset) |= bitpat[bit_offset];
  405.           else *(fp3 + ypos + byte_offset) &= ~bitpat[bit_offset];
  406.     if (n & 4) *(fp2 + ypos + byte_offset) |= bitpat[bit_offset];
  407.           else *(fp2 + ypos + byte_offset) &= ~bitpat[bit_offset];
  408.     if (n & 2) *(fp1 + ypos + byte_offset) |= bitpat[bit_offset];
  409.           else *(fp1 + ypos + byte_offset) &= ~bitpat[bit_offset];
  410.     if (n & 1) *(fp0 + ypos + byte_offset) |= bitpat[bit_offset];
  411.           else *(fp0 + ypos + byte_offset) &= ~bitpat[bit_offset];
  412.   }
  413.  
  414. /*:
  415. .n write_cookie()
  416. .k cookie low_level setup configure
  417. .b
  418.  
  419. SYNOPSIS:   void write_cookie(brand,line);
  420.                 unsigned char *brand;
  421.                 int line;
  422.  
  423. FUNCTION:    This function writes the cookie on a particular line.
  424.              The variable "brand" is a pointer to an arrary of data
  425.              that contains the particular cookie for the mode you want.
  426.              These arrays are the global ones "ham_cookie[]" and
  427.              "reg_cookie[]".
  428.              
  429.              Call as:
  430.              
  431.                   write_cookie(ham_cookie,line);
  432.                               -or-
  433.                   write_cookie(reg_cookie,line);
  434.              
  435.              Note: If you have a four line palette, you need to call
  436.              this function for each successive line the palette exists
  437.              upon, for example:
  438.              
  439.                   write_cookie(reg_cookie,0);
  440.                   write_cookie(reg_cookie,1);
  441.                   write_cookie(reg_cookie,2);
  442.                   write_cookie(reg_cookie,3);
  443.                   
  444.                         -or-
  445.                   
  446.                   for (i=0; i<4; i++)
  447.                     {
  448.                       write_cookie(reg_cookie,i);
  449.                     }
  450.              
  451.              If the global variable "lacer" is set, this function will
  452.              write the cookie data on the appropriate lines in both
  453.              fields... sending four lines of cookie to the function
  454.              with the lines 0,1,2,3 will write cookies on line pairs
  455.              0-1, 2-3, 4-5, and 6-7.
  456.  
  457. INPUTS:     A pointer to the apropriate cookie and the line to put it on.
  458.  
  459. RESULTS:    None
  460.  
  461. BUGS:       None Known.
  462.  
  463. LIMITATIONS:None Known.
  464.  
  465. SEE ALSO:   The global variable "lacer" and the "ham_cookie[]"
  466.             and reg_cookie[] global arrays.
  467.  
  468. :*/
  469. void write_cookie(brand,line)
  470.   unsigned char *brand;
  471.   int line;
  472.   {
  473.   int i;
  474.     if (lacer) /* we need double the cookie data! */
  475.       {
  476.         for (i=0; i<8; i++)
  477.           {
  478.             write_byte(i,line*2,brand[i]);
  479.             write_byte(i,(line*2)+1,brand[i]);
  480.           }
  481.       }
  482.     else
  483.       {
  484.         for (i=0; i<8; i++)
  485.           {
  486.             write_byte(i,line,brand[i]);
  487.           }
  488.       }
  489.   }
  490.  
  491. /*:
  492. .n make_hame_palette()
  493. .k amiga_palette palette_amiga setup configure
  494. .b
  495.  
  496. SYNOPSIS:   void make_hame_palette(vp);
  497.                 struct ViewPort *vp;
  498.  
  499. FUNCTION:   Sets palette for Amiga side of hardware: The palette here is
  500.             designed to achieve two independant goals. First, and most
  501.             importantly, it creates a situation where the IRGB lines at
  502.             the Amiga's data port will exactly mirror the data in the
  503.             bitplanes of the screen as each pixel is emitted. 
  504.             Secondly, this palette makes the images visible, if not
  505.             sensible, on a non HAM-e equipped Amiga... Hopefully this
  506.             distinctive color palette will quickly cue the user that
  507.             they are missing something good. :^)
  508.             
  509.             IRGB to 12 bit correspondence:
  510.             
  511.             bit 8 - b3 of red
  512.             bit 4 - b3 of green
  513.             bit 2 - b3 of blue
  514.             bit 1 - b0 of blue
  515.  
  516. INPUTS:     A pointer to the ViewPort that "belongs" to this screen.
  517.  
  518. RESULTS:    None
  519.  
  520. BUGS:       None Known.
  521.  
  522. LIMITATIONS:None Known.
  523.  
  524. SEE ALSO:   
  525.  
  526. :*/
  527. void make_hame_palette(vp)
  528.   struct ViewPort *vp;
  529.   {
  530.   int rr,gg,bb,i;
  531.   int col;
  532.     col=0;
  533.     for (i=0; i<16; i++)
  534.       {
  535.         rr=0; gg=0; bb=0;
  536.         if (i & 8) rr  = 8; /* this builds the IRGB bit outputs...   */
  537.         if (i & 4) gg  = 8; /* ...these four bits are all that are   */
  538.         if (i & 2) bb  = 8; /* ...required to make the HAM-E run,    */
  539.         if (i & 1) bb |= 1; /* ...they xfer b0->b3 to the IRGB lines */
  540.         if (i != 0)     /* we don't mess with c0 - we leave it black */
  541.           {
  542.             rr += (col & 7); /* build strange colors in cregs 1-15...  */
  543.             col += 2;        /* ...these extra bits sent to the color  */
  544.             gg += (col & 7); /* ...registers make amiga palette very   */
  545.             col += 2;        /* ...interesting to look at if the HAM-E */
  546.             bb += (col & 6); /* ...is NOT attached. Otherwise useless. */
  547.             col += 2;        /* ...The code in this "if" is optional.  */
  548.           }
  549.         SetRGB4(vp,i,rr,gg,bb); /* this actually sets the Amiga color regs */
  550.       }
  551.   }
  552.  
  553. /*:
  554. .n SetRGB8()
  555. .k palette_hame hame_palette setup configure
  556. .b
  557.  
  558. SYNOPSIS:   void SetRGB8(reg,rr,gg,bb,base);
  559.                 int reg,rr,gg,bb,base;
  560.  
  561. FUNCTION:   This routine sets the color registers in the HAM-E hardware.
  562.             It can handle color registers located at any point on screen,
  563.             by setting the "base" variable to the starting scan line where
  564.             the color registers exist. Normally, the cookie and it's
  565.             associated color registers are located beginning at scan line 0.
  566.             If the screen is an interlace screen, this routine will set both
  567.             set of color registers identically; when in interlace, the
  568.             HAM-E maintains separate sets of color registers for the
  569.             odd and even interlace fields. Since this routine sets both
  570.             sets of color registers identically, you don't have to
  571.             worry about dealing with this feature.
  572.             
  573. INPUTS:     reg  - the color register number from 0 to 255 to be set
  574.             rr   - the red value from 0-255
  575.             gg   - the green value from 0-255
  576.             bb   - the blue value from 0-255
  577.             base - the starting scan line of the first coookie position
  578.                    (usually zero)
  579.  
  580. RESULTS:    None
  581.  
  582. BUGS:       None Known.
  583.  
  584. LIMITATIONS:None Known.
  585.  
  586. SEE ALSO:   The global variable "lacer"
  587.  
  588. :*/
  589. void SetRGB8(reg,rr,gg,bb,base)
  590.   short reg;
  591.   unsigned char rr,gg,bb;
  592.   short base;
  593.   {
  594.   short p_row,p_index;
  595.     p_row = (reg >> 6) + base;           /* palette row 0-3.  */
  596.     p_index = ((reg & 0x3f) * 3) + 8;    /* reg 0-63 in p_row */
  597.     if (lacer) /* then we need both fields! */
  598.       {
  599.         /* for the even field: */
  600.         write_byte(p_index,   p_row*2, rr);         /* put RED value     */
  601.         write_byte(p_index+1, p_row*2, gg);         /* put GREEN value   */
  602.         write_byte(p_index+2, p_row*2, bb);         /* put BLUE value    */
  603.         
  604.         /* here is the stuff for the ODD field: */
  605.         write_byte(p_index,   (p_row*2)+1, rr);     /* put RED value     */
  606.         write_byte(p_index+1, (p_row*2)+1, gg);     /* put GREEN value   */
  607.         write_byte(p_index+2, (p_row*2)+1, bb);     /* put BLUE value    */
  608.       }
  609.     else /* just write to one field */
  610.       {
  611.         write_byte(p_index,   p_row, rr);     /* put RED value     */
  612.         write_byte(p_index+1, p_row, gg);     /* put GREEN value   */
  613.         write_byte(p_index+2, p_row, bb);     /* put BLUE value    */
  614.       }
  615.   }
  616.  
  617. void display_init (input_width, input_height)
  618.    int input_width, input_height;
  619.    {
  620.    int screen_width, screen_height;
  621.  
  622.  
  623.    Amiga_open();
  624.    open_requestor();
  625.  
  626.    Delay (10);
  627.  
  628.    if (DisplayFormat == 'E') {
  629.       screen_width = GfxBase->NormalDisplayColumns>>1;
  630.       Ham_E_Screen.Width = screen_width<<1;
  631.       screen_height = GfxBase->NormalDisplayRows<<1;
  632.       Ham_E_Screen.Height = screen_height+2;
  633.  
  634.       if ((s = (struct Screen *) OpenScreen (&Ham_E_Screen)) == NULL)
  635.       {
  636.          display_close ();
  637.          exit (FALSE);
  638.       }
  639.  
  640.       ShowTitle (s, FALSE);
  641.       lacer = 1;
  642.       fp0 = s->BitMap.Planes[0];
  643.       fp1 = s->BitMap.Planes[1];
  644.       fp2 = s->BitMap.Planes[2];
  645.       fp3 = s->BitMap.Planes[3];
  646.       make_hame_palette(&s->ViewPort);
  647.       SetAPen (&(s->RastPort), 0L);
  648.       RectFill (&(s -> RastPort), 0L, 0L, Ham_E_Screen.Width-1, 1);
  649.       SetAPen (&(s->RastPort), 1L);
  650.       RectFill (&(s -> RastPort), 0L, 2L, Ham_E_Screen.Width-1,
  651.                                           Ham_E_Screen.Height-1);
  652.       write_cookie(ham_cookie, 0);
  653.       SetRGB8 (0x11, 0x80, 0x80, 0x80, 0);
  654.       }
  655.    else if (DisplayFormat == 'F') {
  656.     if (OpenFirecracker() == TRUE)
  657.         {
  658.            display_close ();
  659.            exit (999);
  660.         }
  661.       }
  662.    else if (DisplayFormat == '8') {
  663.     if (OpenHam8() == TRUE)
  664.         {
  665.            display_close ();
  666.            exit (FALSE);
  667.         }
  668.       }
  669.    else {
  670.     int viewmodes;
  671.  
  672.  
  673.     viewmodes = 0;
  674.  
  675.     if (input_width < 1 || input_height < 1)
  676.     {
  677.         display_close ();
  678.         printf ("? Size must be no smaller than 1 x 1\n");
  679.         exit (FALSE);
  680.     }
  681.  
  682.     if (input_width > 736 || input_height > 482)
  683.     {
  684.         display_close ();
  685.         printf ("? Size must be no larger than 736 x 482\n");
  686.         exit (FALSE);
  687.     }
  688.  
  689.     screen_width = 320;
  690.     if (input_width > 368 && input_width <= 736)
  691.     {
  692.         screen_width = 640;
  693.         viewmodes |= HIRES;
  694.     }
  695.     else if (input_width > 736)    /* Add higher resolutions */
  696.     {
  697.         screen_width = 640;
  698.         viewmodes |= HIRES;
  699.     }
  700.  
  701.     width = (input_width>screen_width) ? input_width : screen_width;
  702.  
  703.     leftedge = 0;
  704.     rightedge = width;
  705.     if (input_width < screen_width)
  706.     {
  707.          leftedge  = (screen_width - input_width) / 2;
  708.         rightedge = leftedge + input_width;
  709.     }
  710.  
  711.     screen_height = 200;
  712.     if (input_height > 241 && input_height <= 482)
  713.     {
  714.         screen_height = 400;
  715.         viewmodes |= INTERLACE;
  716.     }
  717.     else if (input_height > 482)    /* Add higher resolutions */
  718.     {
  719.         screen_height = 400;
  720.         viewmodes |= INTERLACE;
  721.     }
  722.  
  723.     height = (input_height>screen_height) ? input_height : screen_height;
  724.  
  725.     topedge = 0;
  726.     bottomedge = height;
  727.     if (input_height < screen_height)
  728.     {
  729.         topedge    = (screen_height - input_height) / 2;
  730.         bottomedge = topedge + input_height;
  731.     }
  732.  
  733.     Ham_Screen.Width = Rect1.MaxX = width;
  734.     Ham_Screen.Height = Rect1.MaxY = height;
  735.     Ham_Screen.ViewModes |= viewmodes;
  736.  
  737.         if ((s = (struct Screen *) OpenScreen (&Ham_Screen)) == NULL)
  738.         {
  739.            display_close ();
  740.            printf ("? Can't open screen.\n");
  741.            exit (FALSE);
  742.         }
  743.  
  744.     /* What did we actually get? */
  745.     width = s->Width;
  746.     height = s->Height;
  747.     depth = s->BitMap.Depth;
  748.     bytesperrow = s->BitMap.BytesPerRow;
  749.  
  750.         ShowTitle (s, FALSE);
  751.  
  752.         LoadRGB4 (&(s->ViewPort), ColorTbl, 16L);
  753.         SetAPen (&(s->RastPort), 7L);
  754.         RectFill (&(s -> RastPort), 0L, 0L, width-1, height-1);
  755.       }
  756.    }
  757.  
  758. void display_close ()
  759.    {
  760.    if (Requestor_Running) {
  761.       Signal (Requestor_Task, 1 << Requestor_Port -> mp_SigBit);
  762.       Delay (2L);
  763.       }
  764.  
  765.    if (Requestor_Window)
  766.       FreeSysRequest (Requestor_Window);
  767.  
  768.    Requestor_Window = NULL;
  769.  
  770.    if (DisplayFormat != 'F')
  771.       if (s != NULL)
  772.       {
  773.          CloseScreen (s);
  774.          s = NULL;
  775.       }
  776.    }
  777.  
  778. #define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  779. #define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  780.  
  781. void write_ham8_pixel (x, y, Red, Green, Blue)
  782.    int x, y;
  783.    char Red, Green, Blue;
  784.    {
  785.  
  786.    }
  787.  
  788. void write_hame_pixel (x, y, Red, Green, Blue)
  789.    int x, y;
  790.    char Red, Green, Blue;
  791.    {
  792.    register unsigned char colour;
  793.    short delta_red, delta_green, delta_blue;
  794.  
  795.    if (y < last_y)
  796.       return;
  797.  
  798.    if ((x >= width) || (y >= height))
  799.       return;
  800.  
  801.    if (last_y != y) {
  802.       last_y = y;
  803.       last_red = last_green = last_blue = 0;
  804.       }
  805.    else
  806.       if (x <= last_x)
  807.          return;
  808.  
  809.    last_x = x;
  810.  
  811.    Red = (Red >> 2) & 0x3F;
  812.    Green = (Green >> 2) & 0x3F;
  813.    Blue = (Blue >> 2) & 0x3F;
  814.  
  815.    delta_red = absdif (Red, last_red);
  816.    delta_green = absdif (Green, last_green);
  817.    delta_blue = absdif (Blue, last_blue);
  818.  
  819.    switch (max3(delta_red, delta_green, delta_blue)) {
  820.       case 1:
  821.          last_red = Red;
  822.          colour = 0x80 + Red;
  823.          break;
  824.       case 2:
  825.          last_green = Green;
  826.          colour = 0xc0 + Green;
  827.          break;
  828.       case 3:
  829.          last_blue = Blue;
  830.          colour = 0x40 + Blue;
  831.          break;
  832.       }
  833.  
  834.    write_byte (x, y+2, colour);
  835.    }
  836.  
  837. void display_plot (x, y, Red, Green, Blue)
  838.    int x, y;
  839.    char Red, Green, Blue;
  840.    {
  841.    register short colour, index, mask, i, colour_mask;
  842.    register char *addr;
  843.    short delta_red, delta_green, delta_blue;
  844.  
  845.    if (DisplayFormat == 'E')
  846.        return (write_hame_pixel(x, y, Red, Green, Blue));
  847.    else if (DisplayFormat == 'F')
  848.     return (write_firecracker_pixel((UWORD) x, (UWORD) y,
  849.         (UBYTE) Red, (UBYTE) Green, (UBYTE) Blue));
  850.    else if (DisplayFormat == '8')
  851.     return (write_ham8_pixel(x, y, Red, Green, Blue));
  852.  
  853.    y += topedge;
  854.    x += leftedge;
  855.  
  856.    if (y < last_y)
  857.       return;
  858.  
  859.    if (last_y != y) {
  860.       last_y = y;
  861.       last_red = last_green = last_blue = 0;
  862.       }
  863.    else
  864.       if (x <= last_x)
  865.          return;
  866.  
  867.    last_x = x;
  868.  
  869.    Red = (Red >> 4) & 0x0F;
  870.    Green = (Green >> 4) & 0x0F;
  871.    Blue = (Blue >> 4) & 0x0F;
  872.  
  873.    delta_red = absdif (Red, last_red);
  874.    delta_green = absdif (Green, last_green);
  875.    delta_blue = absdif (Blue, last_blue);
  876.  
  877.    switch (max3(delta_red, delta_green, delta_blue)) {
  878.       case 1:
  879.          last_red = Red;
  880.          colour = 0x20 + Red;
  881.          break;
  882.       case 2:
  883.          last_green = Green;
  884.          colour = 0x30 + Green;
  885.          break;
  886.       case 3:
  887.          last_blue = Blue;
  888.          colour = 0x10 + Blue;
  889.          break;
  890.       }
  891.  
  892.    index = (bytesperrow * y) + (x >> 3);
  893.    mask = 0x80 >> (x & 7);
  894.  
  895.    colour_mask = 1;
  896.  
  897.    for (i = 0 ; i < 6 ; i++) {
  898.       addr = &s->BitMap.Planes[i][index];
  899.       *addr &= ~mask;
  900.       *addr |= (colour&colour_mask) ? mask : 0x00;
  901.       colour_mask <<= 1;
  902.       }
  903.    }
  904.  
  905. int amiga_close_all ()
  906.    {
  907.    close_all();
  908.    return (1);
  909.    }
  910.