/*Amiga Computing*/
/*March 1991*/
/*graphics primitives*/
/*c source code*/

#include <exec/types.h>
#include  <graphics/gfx.h>
#include  <graphics/view.h>
#include  <graphics/rastport.h>
#include  <graphics/gfxbase.h>

long dosbase=0;                        /*library pointer*/
long libraries=0;
long i,k;                              /*loop variables*/
short j;
struct GfxBase *GfxBase;               /*graphics base*/
struct View myview;                    /*view*/
struct View *oldview;                  /*viewports*/
struct ViewPort vpblue;
struct ViewPort vpredandgrey;
struct ViewPort vpgreen;
struct ColorMap *colourmap;           /*colormap pointers*/
struct ColorMap *colourmap2;
struct ColorMap *colourmap3;
struct BitMap   BMblue;               /*bitmaps*/
struct BitMap   BMred;
struct BitMap   BMgrey;
struct BitMap   BMgreen;
struct ColorMap *GetColorMap();       /*returned colormap pointer*/
struct RastPort bluerastport;         /*rastport structures*/
struct RastPort redrastport;
struct RastPort greyrastport;
struct RastPort greenrastport;
UWORD *colourtable;                   /*colortable pointers*/
UWORD *colourtable2;
UWORD *colourtable3;
static USHORT colours[] =             /*colourpalettes*/
{
0x000,0x00f,0x00e,0x00c,0x00a,0x008,0x006,0x004,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};
 
static USHORT colours2[] =
{
0x000,0xf00,0xe00,0xc00,0xa00,0x800,0x600,0x400,
0xeee,0xccc,0xaaa,0x999,0x777,0x666,0x444,0xddd,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};
 
static USHORT colours3[] =
{
0x000,0x0f0,0x0e0,0x0c0,0x0a0,0x080,0x060,0x040,
0x0f1,0x0f2,0x0f3,0x0f4,0x0f5,0x0f6,0x0f7,0x0f8,
0x0c1,0x0c2,0x0c3,0x0c4,0x0c5,0x0c6,0x0c7,0x0c8,
0x081,0x082,0x083,0x084,0x085,0x086,0x087,0x088
};
struct RasInfo riblue =                /*RasInfo structures*/
{
0,&BMblue,0,0
};
struct RasInfo rigrey =
{
0,&BMgrey,0,0
};
struct RasInfo rired =
{
&rigrey,&BMred,0,0
};
struct RasInfo rigreen =
{
0,&BMgreen,0,0
};


void main(),init(),remakedisplay(),closelibraries();
void greyarea(),stripes(),cyclecolours(),minicycle(),cleanup();

void main()
{
libraries= openlibraries();            /*open the libraries*/
if (libraries == 0) exit(1);
init();                                /*initialise the viewports*/
oldview  =  GfxBase->ActiView;         /*store the old view address*/
remakedisplay();                       /*load the new view*/
greyarea();                            /*make the grey playfield into
                                       /*a window frame*/
stripes();                             /*display coloured stripes*/
Delay(300);                            /*wait a few seconds*/
cyclecolours();                        /*cycle the reds, blues and greens*/
LoadView(oldview);                     /*replace the old view*/
cleanup();                             /*free memory used by the program*/
closelibraries();                      /*close the libraries*/
}

openlibraries()
{
dosbase= OpenLibrary("dos.library",0);
if (dosbase == 0) return(0);
GfxBase= (struct GfxBase *)OpenLibrary("graphics.library",0);
if (GfxBase == 0) return(0);
return(1);
}

void init()
{
InitView(&myview);                    /*initialise the view*/
myview.ViewPort = &vpblue;            /*connect it to the first viewport*/
InitVPort(&vpblue);                   /*initialise the first viewport*/
vpblue.DWidth = 320;                  /*viewport width*/
vpblue.DHeight = 48;                  /*viewport height*/
vpblue.RasInfo = &riblue;             /*pointer to RasInfo*/
vpblue.Next = &vpredandgrey;          /*pointer to next viewport*/
colourmap = GetColorMap(32);          /*get a pointer to a colormap*/
                                      /*with 32 registers*/
colourtable = (UWORD *)colourmap->ColorTable;
                                      /*find the pointer to the colormap's
                                      /*colortable*/
for(i=0;i<32;i++)                     /*copy your own colours into this*/
{                                     /*colourtable*/
*colourtable++ = colours[i];
}
vpblue.ColorMap = colourmap;          /*connect your colormap to the*/
                                      /*viewport*/
InitBitMap(&BMblue,3,320,48);         /*initialise a bitmap 320x48x3*/
for(i=0;i<3;i++)                      /*allocate memory for 3 bitplanes*/
{
BMblue.Planes[i] = (PLANEPTR)AllocRaster(320,48);
if (BMblue.Planes[i] == 0) exit(2);
}
InitVPort(&vpredandgrey);             /*initialise the second viewport*/
vpredandgrey.DWidth = 320;
vpredandgrey.DHeight = 100;
vpredandgrey.RasInfo = &rired;        /*connect it to the first RasInfo*/
                                      /*which has a pointer to the second*/
vpredandgrey.Next  = &vpgreen;
vpredandgrey.DyOffset = 50;           /*starts after first viewport ends*/
vpredandgrey.Modes = DUALPF|PFBA;     /*dual playfields,second field in*/
                                      /*front of first*/
colourmap2 = GetColorMap(32);
colourtable2 = (UWORD *)colourmap2->ColorTable;
for(i=0;i<32;i++)                    /*the first 8 red colour registers are*/
{                                    /*for the first playfield, the next 8*/
*colourtable2++ = colours2[i];       /*grey colour registers are for the*/
}                                    /*second playfield*/
vpredandgrey.ColorMap = colourmap2;
InitBitMap(&BMred,3,320,100);        /*a separate bitmap for each RasInfo*/
for(i=0;i<3;i++)
{
BMred.Planes[i] = (PLANEPTR)AllocRaster(320,100);
if (BMred.Planes[i] == 0) exit(2);
}
InitBitMap(&BMgrey,3,320,100);
for(i=0;i<3;i++)
{
BMgrey.Planes[i] = (PLANEPTR)AllocRaster(320,100);
if (BMgrey.Planes[i] == 0) exit(2);
}
InitVPort(&vpgreen);                /*initialise the third viewport*/
vpgreen.DWidth = 320;
vpgreen.DHeight = 100;
vpgreen.DyOffset = 152;             /*allow for other viewports*/
vpgreen.RasInfo = &rigreen;
colourmap3 = GetColorMap(32);
colourtable3 = (UWORD *)colourmap3->ColorTable;
for(i=0;i<32;i++)
{
*colourtable3++ = colours3[i];
}
vpgreen.ColorMap = colourmap3;
InitBitMap(&BMgreen,5,320,100);      /*five bitplanes this time*/
for(i=0;i<5;i++)
{
BMgreen.Planes[i] = (PLANEPTR)AllocRaster(320,100);
if (BMgreen.Planes[i] == 0) exit(2);
}
}

void remakedisplay()
{
MakeVPort(&myview,&vpblue);        /*make a copper list for each viewport*/
MakeVPort(&myview,&vpredandgrey);
MakeVPort(&myview,&vpgreen);
MrgCop(&myview);                   /*merge the copper lists*/
WaitTOF();                         /*wait for top of frame*/
LoadView(&myview);                 /*and load the new view*/
}

void greyarea()
{
InitRastPort(&greyrastport);       /*initialise a rasport for drawing*/
                                   /*into the grey bitmap*/
greyrastport.BitMap = &BMgrey;     /*pointer to grey bitmap*/
SetAPen(&greyrastport,4);          /*light grey outer rectangle*/
RectFill(&greyrastport,0,0,319,100);
SetAPen(&greyrastport,0);          /*transparant inner rectangle*/
RectFill(&greyrastport,100,20,196,80);
}

void stripes()
{
InitRastPort(&bluerastport);
bluerastport.BitMap = &BMblue;
for(i=0;i<8;i++)
{
SetAPen(&bluerastport,i);
RectFill(&bluerastport,40*i,0,40*i+39,47);
}
InitRastPort(&redrastport);
redrastport.BitMap = &BMred;
for(i=0;i<8;i++)
{
SetAPen(&redrastport,i);
RectFill(&redrastport,100+12*i,0,112+12*i,100);
}
InitRastPort(&greenrastport);
greenrastport.BitMap = &BMgreen;
for(i=0;i<32;i++)
{
SetAPen(&greenrastport,i);
RectFill(&greenrastport,10*i,0,10*i+9,99);
}
}

void cyclecolours()
{
for(k=0;k<100;k++)                  /*64 times, with short delay*/
{
Delay(2);
minicycle();
}
}

void minicycle()                  /*cycle the first 8 colours of*/
{                                 /*each colourpalette*/
j = colours[0];
for(i=0;i<7;i++)
{colours[i]=colours[i+1];
}
colours[7]=j;
LoadRGB4(&vpblue,&colours[0],8); /*load the new colours into the viewport*/
j = colours2[0];
for(i=0;i<7;i++)
{colours2[i]=colours2[i+1];
}
colours2[7]=j;
LoadRGB4(&vpredandgrey,&colours2[0],8);
j = colours3[0];
for(i=0;i<7;i++)
{colours3[i]=colours3[i+1];
}
colours3[7]=j;
LoadRGB4(&vpgreen,&colours3[0],8);
}

void cleanup()
{
FreeVPortCopLists(&vpblue);      /*free the blue copperlists*/
FreeColorMap(colourmap);         /*free the blue colormap*/
for (i=0;i<3;i++)
{                                /*free the blue bitplanes*/
FreeRaster(BMblue.Planes[i],320,48);
}

FreeVPortCopLists(&vpredandgrey);
FreeColorMap(colourmap2);
for (i=0;i<3;i++)
{
FreeRaster(BMred.Planes[i],320,100);
FreeRaster(BMgrey.Planes[i],320,100);
}
FreeVPortCopLists(&vpgreen);
FreeColorMap(colourmap3);
for (i=0;i<5;i++)
{
FreeRaster(BMgreen.Planes[i],320,100);
}
}

void closelibraries()
{
CloseLibrary(dosbase);
CloseLibrary(GfxBase);
}
