/************************************************************************
 * View.c								*
 * 									*
 * View.c is a collection of routines designed to create, maintain and  *
 * manipulate a specifyed graphics view					*
 ************************************************************************/
 
#define VIEW
#include "def.h"

/* The varables to maintain the View */

struct View 	View;	   /* The main View node for the screen display */
struct View 	*OldView;  /* Pointer to the old view to restore at exit*/
struct ViewPort ViewP;
struct ColorMap *cm;
struct RasInfo  RasInfo;
struct BitMap   *Screenbm[2];
struct RastPort Srp;
struct RastPort Srp1,Srp2;
struct cprlist  *LOFCprList[2];

int		Display_Frame,Draw_Frame;

struct Layer_Info *LayerInfo;


SetPickColors(VPP)
struct ViewPort *VPP;
{
SetRGB4(VPP,  0,  0,  0,  0);
SetRGB4(VPP,  1,  7,  7,  7);
SetRGB4(VPP,  2, 14, 14, 14);
SetRGB4(VPP,  3, 15, 15,  0);
SetRGB4(VPP,  4,  0,  0, 14);
SetRGB4(VPP,  5, 13,  0,  0);
SetRGB4(VPP,  6, 15, 15,  0);
SetRGB4(VPP,  7,  0,  0, 14);
SetRGB4(VPP,  8, 13,  0,  0);
SetRGB4(VPP,  9, 15, 15,  0);
SetRGB4(VPP, 10,  0,  0, 14);
SetRGB4(VPP, 11, 13,  0,  0);
SetRGB4(VPP, 12,  9,  7,  7);
SetRGB4(VPP, 13, 11,  9,  8);
SetRGB4(VPP, 14, 10,  8,  7);
SetRGB4(VPP, 15, 11,  9,  8);
SetRGB4(VPP, 16,  8,  8,  8);
SetRGB4(VPP, 17,  5,  5,  5);
SetRGB4(VPP, 18,  0,  0,  0);
SetRGB4(VPP, 19,  0,  0,  0);
SetRGB4(VPP, 20,  0,  0,  0);
SetRGB4(VPP, 21,  0,  0,  0);
SetRGB4(VPP, 22,  0,  0,  0);
SetRGB4(VPP, 23,  0,  0,  0);
SetRGB4(VPP, 24,  0,  0,  0);
SetRGB4(VPP, 25,  0,  0,  0);
SetRGB4(VPP, 26,  0,  0,  0);
SetRGB4(VPP, 27,  0,  0,  0);
SetRGB4(VPP, 28,  0,  0,  0);
SetRGB4(VPP, 29, 15, 15,  0);
SetRGB4(VPP, 30,  0,  0, 14);
SetRGB4(VPP, 31, 13,  0,  0);
}


BOOL OpenView()
{
   int i;
   
   /* Assumes the libraries are opened */
   InitView(&View);
   InitVPort(&ViewP);
   View.ViewPort = &ViewP;
   
   for (i=0;i<2;i++) {
      if (!(Screenbm[i] = 
            MakeBitMap((long)SCREEN_DEPTH,SCREEN_WIDTH,SCREEN_HEIGHT))) {
         RemoveView();
         return(FALSE);
      }
   }
   
   /*Init RasInfo */
   RasInfo.BitMap = Screenbm[1];
   RasInfo.RxOffset = 0/*SCREEN_OFFSETX*/;
   RasInfo.RyOffset = 0/*SCREEN_OFFSETY*/;
   RasInfo.Next = NULL;
   
   ViewP.DWidth  = SCREEN_WIDTH;
   ViewP.DHeight = SCREEN_HEIGHT;
   ViewP.DxOffset = SCREEN_OFFSETX;
   ViewP.DyOffset = SCREEN_OFFSETY;
   ViewP.RasInfo = &RasInfo;
   
   /* init color table */
   if (!(cm = GetColorMap((long) NO_OF_COLORS))) {
      RemoveView();
      return(FALSE);
   }
   ViewP.ColorMap = cm;
   
   MakeVPort(&View,&ViewP);
   MrgCop(&View);
   SetColors(&ViewP);
   /* Set up the Double-Buffer View copper list */
      LOFCprList[1] = View.LOFCprList;
      View.LOFCprList = View.SHFCprList = 0;
      RasInfo.BitMap = Screenbm[0];
      MakeVPort(&View,&ViewP);
      MrgCop(&View);
      SetColors(&ViewP);
      LOFCprList[0] = View.LOFCprList;
      Display_Frame = 0;
      Draw_Frame = 1;
      
   /* Set up the Screen raster ports */
   InitRastPort(&Srp);
   InitRastPort(&Srp1);
   InitRastPort(&Srp2);
   
   Srp.BitMap = Screenbm[1];
   Srp1.BitMap = Screenbm[0];
   Srp2.BitMap = Screenbm[1];
}

ToggleFrame()
/* Changes from one bitmap to another bitmap */
/* All bitmap pointers are changed here also */
/*    Translate to Machine Code */
{
   Display_Frame  ^= 1;
   Draw_Frame     ^= 1;

   RasInfo.BitMap  = Screenbm[Display_Frame];
   View.LOFCprList = LOFCprList[Display_Frame];

   Srp.BitMap      = Screenbm[Draw_Frame];

   WaitBOVP(&View);
   LoadView(&View);
/*   Pw[0].Rp->BitMap = Screenbm[Draw_Frame];
   Pw[1].Rp->BitMap = Screenbm[Draw_Frame];*/
}


RemoveView()
{
   int i;
   
   FreeVPortCopLists(&ViewP);
   if (cm) FreeColorMap(cm);
   for (i=0;i<2;i++) {
      RemoveBitMap(Screenbm[i],SCREEN_DEPTH,SCREEN_WIDTH,SCREEN_HEIGHT);
      if (LOFCprList[i]) FreeCprList(LOFCprList[i]);
   }
}


BOOL ViewSetup()
{
   OldView = GfxBase->ActiView;
      
   if (!(OpenView())) {
      ViewRemove();
      return(FALSE);
   }
   return(TRUE);
}
     
ViewRemove()
{
   LoadView(OldView);
   RemoveView();
}

SetColors(vp)
struct ViewPort *vp;
{
SetRGB4(vp,  0,  0,  0,  0);
SetRGB4(vp,  1, 15, 15, 15);
SetRGB4(vp,  2, 13, 11,  9);
SetRGB4(vp,  3,  8, 11, 13);
SetRGB4(vp,  4, 12,  9,  7);
SetRGB4(vp,  5,  7,  9, 11);
SetRGB4(vp,  6,  9,  8,  7);
SetRGB4(vp,  7, 11,  7,  4);
SetRGB4(vp,  8,  7,  7,  7);
SetRGB4(vp,  9,  6,  7,  8);
SetRGB4(vp, 10,  6,  7,  7);
SetRGB4(vp, 11,  6,  6,  7);
SetRGB4(vp, 12,  8,  6,  5);
SetRGB4(vp, 13,  6,  6,  6);
SetRGB4(vp, 14,  5,  6,  6);
SetRGB4(vp, 15,  5,  5,  6);
SetRGB4(vp, 16,  6,  4,  6);
SetRGB4(vp, 17,  6,  5,  5);
SetRGB4(vp, 18,  4,  5,  6);
SetRGB4(vp, 19,  6,  5,  4);
SetRGB4(vp, 20,  5,  6,  4);
SetRGB4(vp, 21,  5,  5,  5);
SetRGB4(vp, 22,  5,  5,  4);
SetRGB4(vp, 23,  4,  5,  5);
SetRGB4(vp, 24,  6,  4,  4);
SetRGB4(vp, 25,  4,  4,  5);
SetRGB4(vp, 26,  3,  4,  5);
SetRGB4(vp, 27,  4,  4,  3);
SetRGB4(vp, 28,  3,  3,  4);
SetRGB4(vp, 29,  2,  3,  4);
SetRGB4(vp, 30,  2,  2,  3);
SetRGB4(vp, 31,  0,  0,  0);
}

