#include "exec/types.h"
#include "intuition/intuition.h"
#include "graphics/gfxmacros.h"
#include "math.h"

struct IntuitionBase *IntuitionBase;
struct Window *NoBorder;
struct GfxBase *GfxBase;
struct Screen *Scrn;
struct IntuiMessage *mesg;
struct RastPort *r;


#define INTUITION_REV 29
#define GRAPHICS_REV 29

main()
{
 ULONG flags,mclass;
 SHORT x,y,w,h,d,c0,c1,einde,xp,yp;
 static SHORT rule[10] = {0,2,2,0,1,3,2,2,0,0};
 SHORT sum[320],tempsum[320];
 USHORT mode;
  

 VOID OpenALL();

 OpenALL();

 /* ======Open een lores custom screen====== */

 y=0;
 w=320;
 h=200;
 d=5; /* max 32 colors ! */
 c0=0x00;
 c1=0x01;
 mode=NULL;

 Scrn=(struct Screen *)
         make_screen(y,w,h,d,c0,c1,mode,NULL);

 /* ======open een borderless window ====== */

 x=y=0;
 w=320;
 h=200;
 flags=ACTIVATE|NOCAREREFRESH|BORDERLESS|BACKDROP;
 NoBorder=(struct Window *)
             make_window(x,y,w,h,NULL,flags,Scrn);

 /* ======geen screentitel====== */

 ShowTitle(Scrn,0);

 /* ======definitie van RPort !====== */

 r=NoBorder->RPort;

   SetRGB4(ViewPortAddress(NoBorder),0,0,0,0);

   SetRGB4(ViewPortAddress(NoBorder),1,15,0,0);
   SetRGB4(ViewPortAddress(NoBorder),2,0,15,0);
   SetRGB4(ViewPortAddress(NoBorder),3,0,0,15);

 /* init van array's */

 for(xp=0;xp<320;xp++)tempsum[xp]=sum[xp]=0;

 /* plaatsen van  'seed(s)" in de array */

  sum[79]=1;
  sum[239]=1;


 for(yp=0;yp<100;yp++)
  {
   /* bereken en teken nieuwe lijn en stop resultaten in TEMPSUM array */
   for(xp=1;xp<319;xp++)
    {
     tempsum[xp]=rule[sum[xp-1]+sum[xp]+sum[xp+1]];

   /* copy tempsum array in sum array */

     sum[xp-1]=tempsum[xp-1];
 
     SetAPen(r,tempsum[xp]);
     WritePixel(r,xp,yp);
     WritePixel(r,xp,199-yp);
    };


  };



 /* ======einde in stijl !====== */

einde=0; /* init einde op 0 ! */
while (einde != 1)
 {
  Wait(1<<NoBorder->UserPort->mp_SigBit);

  while((mesg=(struct IntuiMessage *)
      GetMsg(NoBorder->UserPort))!=NULL)
  {
   ReplyMsg(mesg);
   mclass=mesg->Class;
  }

  if(mclass==MOUSEBUTTONS)
   {
     einde=1;
     cleanwindow(r);
     CloseWindow(NoBorder);
     CloseScreen(Scrn);
   }
 }

} /********************************** einde van main **********************/

VOID OpenALL()
{
 /*==Intuition==*/

 IntuitionBase=(struct IntuitionBase *)
            OpenLibrary("intuition.library",INTUITION_REV);

 if(IntuitionBase==NULL)
    exit(FALSE);

 /*==Graphics Library==*/

 GfxBase=(struct GfxBase *)
          OpenLibrary("graphics.library",GRAPHICS_REV);

 if(GfxBase==NULL)
    exit(FALSE);
}

make_window(x,y,w,h,name,flags,screen)
SHORT x,y,w,h;
UBYTE *name;
ULONG flags;
struct Screen *screen;

{
 struct NewWindow NewWindow;

 NewWindow.LeftEdge = x;
 NewWindow.TopEdge = y;
 NewWindow.Width = w;
 NewWindow.Height = h;
 NewWindow.DetailPen = -1;
 NewWindow.BlockPen = -1;
 NewWindow.Title = name;
 NewWindow.Flags = flags;
 NewWindow.IDCMPFlags = MOUSEBUTTONS;
 NewWindow.Type = CUSTOMSCREEN;
 NewWindow.FirstGadget = NULL;
 NewWindow.CheckMark = NULL;
 NewWindow.Screen = screen;
 NewWindow.BitMap = NULL;
 NewWindow.MinWidth = 0;
 NewWindow.MinHeight = 0;
 NewWindow.MaxWidth = 0;
 NewWindow.MaxHeight = 0;

 return(OpenWindow(&NewWindow));

}


make_screen(y,w,h,d,color0,color1,mode,name)
SHORT y,w,h,d;
UBYTE color0,color1,*name;
USHORT mode;
{
 struct NewScreen NewScreen;

 NewScreen.LeftEdge = 0;
 NewScreen.TopEdge = y;
 NewScreen.Width = w;
 NewScreen.Height = h;
 NewScreen.Depth = d;
 NewScreen.DetailPen = color0;
 NewScreen.BlockPen = color1;
 NewScreen.ViewModes = mode;
 NewScreen.Type = CUSTOMSCREEN;
 NewScreen.Font = NULL;
 NewScreen.DefaultTitle = name;
 NewScreen.Gadgets = NULL;
 NewScreen.CustomBitMap = NULL;

 return(OpenScreen(&NewScreen));
}


cleanwindow(rast)
struct RastPort *rast;
{
 SHORT i,x2,y2;

 SetAPen(rast,0); /* zwart */
 SetBPen(rast,1); /* wit  */


 for(i=0;i<200;i++)
  {
   Move(rast,0,0);Draw(rast,319,i);
   Move(rast,319,0);Draw(rast,0,i);
   Move(rast,0,199);Draw(rast,319,199-i);
   Move(rast,319,199);Draw(rast,0,199-i);
  }
}


