#include "viewport.h"
#include "libraries/mathffp.h"

#define DEPTH 3
#define WIDTH 325
#define HEIGHT 205

struct View v1,v2,*oldview;
struct ViewPort vp1,vp2;
struct ColorMap *cm1,*cm2;
struct RasInfo ri1,ri2;
struct BitMap b1,b2;
struct RastPort rastport,*rp;
extern struct ColorMap *GetColorMap();
USHORT colortable[]={0x000,0x333,0x555,0x777,0x999,0xbbb,0xddd,0xfff};
USHORT *colorpalette,visible=1;
struct GfxBase *GfxBase,*OpenLibrary();
unsigned char *displaymem;
PLANEPTR *AllocRaster();

#define MAXPTS 64
struct AreaInfo ai;
USHORT areamem[(MAXPTS/2)*5];

struct TmpRas tr;
PLANEPTR *Rast;
int i;

int MathBase,MathTransBase;

int x1[400],y1[400],z1[400];
int x2[400],y2[400],z2[400];
int x3[400],y3[400],z3[400];
int x4[400],y4[400],z4[400];

main()
{
  int a=160,b=0,c=200;
  int x,y,z;
  int q,t;
  float w;
  Init();

  SetAPen(rp,7);
  w=0;
  for(q=0;q<300;q++) {
    w+=PI/20;

    x1[q]=160+100*SPCos(w);
    y1[q]=50;
    z1[q]=160+100*SPSin(w);

    x2[q]=x1[q];
    y2[q]=150;
    z2[q]=z1[q];

    x3[q]=160+100*SPCos(w+PI*0.5);
    y3[q]=y2[q];
    z3[q]=160+100*SPSin(w+PI*0.5);

    x4[q]=x3[q];
    y4[q]=50;
    z4[q]=z3[q];

  };
#define STEP 240
  for(q=0;q<300;q++) {
    z1[q]+=7*STEP-50;
    z2[q]+=7*STEP-50;
    z3[q]+=7*STEP-50;
    z4[q]+=7*STEP-50;
    for(t=0;t<7;t++) {
      z1[q]-=STEP;
      z2[q]-=STEP;
      z3[q]-=STEP;
      z4[q]-=STEP;
      SetAPen(rp,t+1);
      AreaMove(rp,(a*z1[q]+c*x1[q])/(c+z1[q]),(b*z1[q]+c*y1[q])/(c+z1[q]));
      AreaDraw(rp,(a*z2[q]+c*x2[q])/(c+z2[q]),(b*z2[q]+c*y2[q])/(c+z2[q]));
      AreaDraw(rp,(a*z3[q]+c*x3[q])/(c+z3[q]),(b*z3[q]+c*y3[q])/(c+z3[q]));
      AreaDraw(rp,(a*z4[q]+c*x4[q])/(c+z4[q]),(b*z4[q]+c*y4[q])/(c+z4[q]));
      AreaEnd(rp);
    };
    Switch();
  };

  FreeMemory();
}

Init()
{
  int i;

  GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0);
  if (GfxBase == NULL) exit(100);

  MathBase=OpenLibrary("mathffp.library",0);
  if (MathBase == NULL) exit(100);

  MathTransBase=OpenLibrary("mathtrans.library",0);
  if (MathTransBase == NULL) exit(100);

  oldview=GfxBase->ActiView;

  InitView(&v1);
  InitView(&v2);

  InitVPort(&vp1);
  InitVPort(&vp2);

  v1.ViewPort=&vp1;
  v2.ViewPort=&vp2;

  InitBitMap(&b1,DEPTH,WIDTH,HEIGHT);
  for(i=0;i<DEPTH;i++) {
    b1.Planes[i]=(PLANEPTR)AllocRaster(WIDTH,HEIGHT);
    if (b1.Planes[i]==NULL) exit(200);
  }

  InitBitMap(&b2,DEPTH,WIDTH,HEIGHT);
  for(i=0;i<DEPTH;i++) {
    b2.Planes[i]=(PLANEPTR)AllocRaster(WIDTH,HEIGHT);
    if (b2.Planes[i]==NULL) exit(300);
  }

  ri1.BitMap=&b1;      /* RasInfo zeigt auf BitMap */
  ri1.RxOffset=0;      /* Verschiebung des 0-Punkts im Grafikspeicher */
  ri1.RyOffset=0;
  ri1.Next=NULL;       /* Naechster RasInfo: Keiner */

  ri2.BitMap=&b2;
  ri2.RxOffset=0;
  ri2.RyOffset=0;
  ri2.Next=NULL;

  cm1=GetColorMap(8);
  if(cm1==NULL) exit(300);
  colorpalette=(unsigned short *)cm1->ColorTable;
  for(i=0;i<8;i++) {
    *colorpalette++=colortable[i];
  }

  cm2=GetColorMap(8);
  if(cm2==NULL) exit(400);
  colorpalette=(unsigned short *)cm2->ColorTable;
  for(i=0;i<8;i++) {
    *colorpalette++=colortable[i];
  }


  vp1.DWidth=WIDTH;
  vp1.DHeight=HEIGHT;
  vp1.RasInfo=&ri1;
  vp1.ColorMap=cm1;

  vp2.DWidth=WIDTH;
  vp2.DHeight=HEIGHT;
  vp2.RasInfo=&ri2;
  vp2.ColorMap=cm2;

  InitRastPort(&rastport);

  MakeVPort(&v1,&vp1);
  MrgCop(&v1);

  MakeVPort(&v2,&vp2);
  MrgCop(&v2);

  for(i=0;i<DEPTH;i++) {
    displaymem=(unsigned char *)b1.Planes[i];
    BltClear(displaymem,RASSIZE(WIDTH,HEIGHT),0);
  }

  for(i=0;i<DEPTH;i++) {
    displaymem=(unsigned char *)b2.Planes[i];
    BltClear(displaymem,RASSIZE(WIDTH,HEIGHT),0);
  }

  LoadView(&v1);

  Rast=AllocRaster(WIDTH,HEIGHT);
  if(Rast==NULL) {
    printf("Shit!\n");
    exit(100);
  }
  tr.RasPtr=Rast;
  tr.Size=RASSIZE(WIDTH,HEIGHT);

  InitArea(&ai,areamem,MAXPTS);

  rastport.TmpRas=&tr;
  rastport.AreaInfo=&ai;
  rastport.BitMap=&b2;
  rp=&rastport;

  SetAPen(rp,1);
  SetBPen(rp,0);
  SetDrMd(rp,JAM1);
}

FreeMemory()
{
  int i;
  LoadView(oldview);

  for(i=0;i<DEPTH;i++) {
    FreeRaster(b1.Planes[i],WIDTH,HEIGHT);
    FreeRaster(b2.Planes[i],WIDTH,HEIGHT);
  }

  FreeRaster(Rast,WIDTH,HEIGHT);

  FreeColorMap(cm1);
  FreeColorMap(cm2);
  FreeVPortCopLists(&vp1);
  FreeVPortCopLists(&vp2);
  FreeCprList(v1.LOFCprList);
  FreeCprList(v2.LOFCprList);

  CloseLibrary(MathTransBase);
  CloseLibrary(MathBase);
  CloseLibrary(GfxBase);

  exit(0);
}

Switch()
{
  int a;
  if(visible==1) {
    visible=2;
    WaitBOVP(&vp1);
    LoadView(&v2);
    rastport.BitMap=&b1;
    for(a=0;a<DEPTH;a++) BltClear((unsigned char *)b1.Planes[a],RASSIZE(WIDTH,HEIGHT),0);
  }
  else {
    visible=1;
    WaitBOVP(&vp2);
    LoadView(&v1);
    rastport.BitMap=&b2;
    for(a=0;a<DEPTH;a++) BltClear((unsigned char *)b2.Planes[a],RASSIZE(WIDTH,HEIGHT),0);
  };
}
     
  

