

#include <exec/types.h>
#include <graphics/gfx.h>
#include <hardware/dmabits.h>
#include <hardware/custom.h>
#include <hardware/intbits.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <graphics/view.h>
#include <exec/execbase.h>
#include <exec/exec.h>
#include <exec/interrupts.h>

extern struct Custom custom;

#define DEPTH 1
#define WIDTH 640
#define HEIGHT 200 
#define NOT_ENOUGH_MEMORY -1000
#define FOREVER for(;;) 
        /* construct a simple display */ 
 
struct View v[4];
struct ViewPort vp[4];
struct ColorMap *cm[4];  /* pointer to colormap structure, dynamic alloc */
struct RasInfo ri[4];
struct BitMap b[4];
struct RastPort rp[4];
 
short i,j,k,n,Z;
struct ColorMap *GetColorMap();
struct GfxBase *GfxBase;

USHORT colortable[] = { 0x000, 0xfff, 0x0f0, 0x00f,
                        0xf0f, 0x0ff, 0xff0, 0xfff };
                        /* black, red, green, blue */

UWORD *colorpalette;

extern void LensInt();
extern void OpenLensPort();
extern void CloseLensPort();
extern void RemindLensPort();

struct Interrupt IntNode;

struct
  {
   APTR  ExecLibPT;
   APTR  GraphicsLibPT;
   APTR  Task;
   LONG  SignalMask;
   APTR  CLV;
   APTR  CRV;
   APTR  LV[2];
   APTR  RV[2];
   UWORD Frame;
   UBYTE Lens;
   UBYTE Flip;
  } LID;

LONG ExecBase;

main()
{
   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
   if (GfxBase == NULL) exit(1);
   ExecBase = OpenLibrary("exec.library",0);
   if (ExecBase == NULL) exit(1);

   for(Z=0; Z<4; Z++)
    {

                                /* initialize view */
        InitView(&(v[Z]));
                                /* link view into viewport */
        v[Z].ViewPort = &(vp[Z]);
        v[Z].Modes = HIRES;
                                /* init view port */
        InitVPort(&(vp[Z]));
                                /* now specify critical characteristics */
        vp[Z].DWidth = WIDTH;
        vp[Z].DHeight = HEIGHT;
        vp[Z].RasInfo = &(ri[Z]);
        vp[Z].Modes = HIRES;
                                /* init bit map (for rasinfo and rastport) */
        InitBitMap(&(b[Z]),DEPTH,WIDTH,HEIGHT);

        InitRastPort(&(rp[Z]));
        rp[Z].BitMap = &(b[Z]);

                                /* (init RasInfo) */
        ri[Z].BitMap = &(b[Z]);
        ri[Z].RxOffset = 0;        /* align upper left corners of display
                                 * with upper left corner of drawing area */
        ri[Z].RyOffset = 0;
        ri[Z].Next = NULL;
                                /* (init color table) */
        cm[Z] = GetColorMap(1<<DEPTH);
        colorpalette = cm[Z]->ColorTable;
        for(i=0; i< (1<<DEPTH); i++)
                *colorpalette++ = colortable[i];
                                /* copy my colors into this data structure */
        vp[Z].ColorMap = cm[Z];       /* link it with the viewport */

                                 /* allocate space for bitmap */
        for(i=0; i<DEPTH; i++)
           {
           b[Z].Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
           if(b[Z].Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
           }

        MakeVPort( &v[Z], &vp[Z] );   /* construct copper instr (prelim) list */
        MrgCop( &v[Z] );           /* merge prelim lists together into a real 
                                 * copper list in the view structure. */


        SetRast(&rp[Z],0);


   }


   LID.GraphicsLibPT = GfxBase;
   LID.ExecLibPT = ExecBase;
   LID.Task = FindTask(0);
   LID.SignalMask = 1 << AllocSignal(-1);
   LID.CLV = &v[3];
   LID.CRV = &v[2];
   LID.LV[0] = &v[0];
   LID.LV[1] = &v[3];
   LID.RV[0] = &v[1];
   LID.RV[1] = &v[2];
   LID.Frame = 0;          /* index to non-displayed views */
   LID.Lens = 0;
   LID.Flip = 0;



   IntNode.is_Node.ln_Type = NT_INTERRUPT;
   IntNode.is_Node.ln_Pri = 20;
   IntNode.is_Node.ln_Name = "X-Specs Int Handler";
   IntNode.is_Data = &LID;
   IntNode.is_Code = LensInt;



   SetAPen(&rp[0],1);
   SetAPen(&rp[1],1);
   SetAPen(&rp[2],1);
   SetAPen(&rp[3],1);
   SetDrMd(&rp[0],JAM1);
   SetDrMd(&rp[1],JAM1);
   SetDrMd(&rp[2],JAM1);
   SetDrMd(&rp[3],JAM1);

   OFF_SPRITE;

   OpenLensPort();


   AddIntServer(INTB_VERTB, &IntNode);

   Init3D();

    FOREVER
    {
     /*
      *  frame 0
      */
     SetRast(&rp[0],0);
     SetRast(&rp[1],0);
     Update3D(&rp[0],&rp[1]);
     LID.Flip = 1;
     Wait(LID.SignalMask);


     /*
      *  frame 1
      */
     SetRast(&rp[3],0);
     SetRast(&rp[2],0);
     Update3D(&rp[3],&rp[2]);
     LID.Flip = 1;
     Wait(LID.SignalMask);

    }


}       /* end of main() */


