#include "exec/types.h"
#include "exec/exec.h"
#include "graphics/gfx.h"
#include "graphics/gfxmacros.h"
#include "graphics/copper.h"
#include "graphics/view.h"
#include "graphics/regions.h"
#include "graphics/clip.h"
#include "graphics/gfxbase.h"
#include "graphics/rastport.h"
#include "hardware/custom.h"
#include "hardware/dmabits.h"

#define DEPTH 4
#define COLORS 16
#define WIDTH 320
#define HEIGHT 200
#define NOT_ENOUGH_MEMORY -1000

extern struct Custom custom;
struct View v;
struct ViewPort vp;
struct ColorMap *cm;
struct RasInfo ri;
struct BitMap b;
struct BitMap b2;
struct cprlist *lof1;
struct cprlist *lof2;
struct cprlist *shf1;
struct cprlist *shf2;
struct CopList *Disp1, *Disp2;

PLANEPTR rastpoint, rastpoint2;

struct RastPort rp;
struct RastPort rp2;

WORD areabuffer[250];
struct TmpRas tmpras;
struct AreaInfo myAreaInfo;

WORD areabuffer2[250];
struct TmpRas tmpras2;
struct AreaInfo myAreaInfo2;


extern struct ColorMap *GetColorMap() ;
struct GxfBase *GfxBase;
struct View *oldview;

USHORT colortable[COLORS] = { 0x000, 0x222, 0x444, 0x555, 0x666, 0x777,
       0x888, 0x999, 0xaaa, 0xbbb, 0xccc, 0xddd, 0xeee, 0xfff, 0x005, 0x00a };

UBYTE *displaymem;
UWORD *colorpalette;

/* ------------------------------------------------------------------------- */

main()
{

 LONG i;
 SHORT x;

 /* ---------------- end of declarations ----------------------------------- */

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

 oldview = GfxBase->ActiView;

 OFF_SPRITE;
 InitView(&v);              /* init view */
 InitVPort(&vp);            /* init ViewPort */
 v.ViewPort = &vp;          /* link viewport into view */
 
 InitBitMap(&b , DEPTH, WIDTH, HEIGHT); /* init bitmap voor RasInfo & RastPort */
 InitBitMap(&b2, DEPTH, WIDTH, HEIGHT); /* double buffering */

 ri.BitMap = &b;            /* init RasInfo */
 ri.RxOffSet = 0;
 ri.RyOffset = 0;
 ri.Next = NULL;

 vp.DWidth = WIDTH;         /* specifieren van viewport karakteristieken */
 vp.DHeight = HEIGHT;
 vp.RasInfo = &ri;

 cm = GetColorMap(COLORS);  /* init colortable */
 vp.ColorMap = cm;          /* Set viewport pointer naar mijn kleuren */
 LoadRGB4(&vp, colortable, COLORS); /* Set de viewport kleuren */

 for(i=0; i<DEPTH; i++) /* Allocate RAM for bitmaps */
 {
  b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  b2.Planes[i] = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
  if(b2.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
 }

 ri.BitMap = &b; 
 MakeVPort(&v,&vp); /* maak copper instruct list */
 MrgCop(&v);  /* merge prelim lists samen tot een echte copper list */
              /* in the view structure */
 lof1 = v.LOFCprList;      /* long frame */
 shf1 = v.SHFCprList;      /* short frame */
 Disp1 = v.ViewPort->DspIns; /* zorg dat colormap */
                             /* op correcte manier verandert */
 v.LOFCprList = 0;
 v.SHFCprList = 0;
 v.ViewPort->DspIns = 0;

 ri.BitMap = &b2; 
 MakeVPort(&v,&vp); /* maak copper instruct list */
 MrgCop(&v);  /* merge prelim lists samen tot een echte copper list */
              /* in the view structure */
 lof2 = v.LOFCprList;      /* store pointers to copper lists voor bitmap 2 */
 shf2 = v.SHFCprList;      
 Disp2 = v.ViewPort->DspIns; /* zorg dat colormap */
                             /* op correcte manier verandert */

 LoadView(&v); /* nu gebruikt de copper de list voor display */ 

 InitRastPort(&rp);
 rp.BitMap = &b;
 SetDrMd(&rp, JAM1);

 InitArea(&myAreaInfo,areabuffer, 100);
 rp.AreaInfo = &myAreaInfo;
 
 rastpoint = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
 InitTmpRas(&tmpras,rastpoint, RASSIZE(WIDTH,HEIGHT));
 rp.TmpRas = &tmpras;


 InitRastPort(&rp2);
 rp2.BitMap = &b2;
 SetDrMd(&rp2, JAM1);

 InitArea(&myAreaInfo2,areabuffer2, 100);
 rp2.AreaInfo = &myAreaInfo2;
 
 rastpoint2 = (PLANEPTR)AllocRaster(WIDTH, HEIGHT);
 InitTmpRas(&tmpras2,rastpoint2, RASSIZE(WIDTH,HEIGHT));
 rp2.TmpRas = &tmpras2;

 SetOPen(&rp,0);
 SetOPen(&rp2,0);

 SetRast(&rp,0);
 SetRast(&rp2,0);

 for(x=0; x<25; x++)
 {

  v.LOFCprList = lof1;
  v.SHFCprList = shf1;
  v.ViewPort->DspIns = Disp1;
  WaitBOVP(&vp);
  LoadView(&v);
  SetAPen(&rp,3);
  for(i=0; i<HEIGHT; i+=4)
  {
   Move(&rp,0,i);Draw(&rp,WIDTH-1,i);
  }

  v.LOFCprList = lof2;
  v.SHFCprList = shf2;
  v.ViewPort->DspIns = Disp2;
  WaitBOVP(&vp);
  LoadView(&v);
  SetAPen(&rp,9);
  for(i=2; i<HEIGHT; i+=4)
  {
   Move(&rp2,0,i);Draw(&rp2,WIDTH-1,i);
  }

 }

 ON_SPRITE;
 LoadView(oldview);
 FreeMemory();
 CloseLibrary(GfxBase);
}

FreeMemory()
{
 int i;

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

 FreeColorMap(cm);
 v.LOFCprList = lof1;
 v.SHFCprList = shf1;
 FreeVPortCopLists(&vp);
 FreeCprList(v.LOFCprList);
 FreeCprList(v.SHFCprList);
 v.LOFCprList = lof2;
 v.SHFCprList = shf2;
 FreeVPortCopLists(&vp);
 FreeCprList(v.LOFCprList);
 FreeCprList(v.SHFCprList);
 return(0);

}






