#include "graphics/gfxmacros.h"
#include "exec/types.h"
#include "exec/memory.h"
#include "exec/interrupts.h"
#include "hardware/custom.h"
#include "hardware/intbits.h"
#include "intuition/intuition.h"


struct Interrupt *VertBIntr;

struct IntuitionBase *IntuitionBase;
struct Window *NoBorder;
struct GfxBase *GfxBase;
struct Screen *Scrn;
struct IntuiMessage *MyIntuiMessage;
struct RastPort *r;
struct ViewPort *vp;

#define MAXW  320 /* max width van window ! */
#define MAXH  200 /* max height van window ! */

struct NewScreen NewScrn = {
        0, 0,           /* LeftEdge, TopEdge */
        320, 200,       /* Width, Height */
        3,              /* Depth */
        0, 1,           /* DetailPen, BlockPen */
        NULL,           /* ViewModes ... value of 0 = low resolution */
        CUSTOMSCREEN,   /* Type of screen */
        NULL,           /* Font to be used as default for this screen */
        NULL,           /* DefaultTitle for its title bar */
        NULL,           /* screens user-gadgets, always NULL, ignored */
        NULL };         /* address of custom bitmap for screen */

struct NewWindow NewNoBorder = {
        0,              /* LeftEdge */
        0,              /* TopEdge  */
        MAXW, MAXH,     /* Width, Height of this window */
        -1,             /* DetailPen */
        -1,             /* BlockPen */
        MOUSEBUTTONS|MENUVERIFY|RAWKEY,   /* IDCMP Flags */
        ACTIVATE|SMART_REFRESH|BORDERLESS|GIMMEZEROZERO|BACKDROP,
                        /* Window Flags:  (see below for more info) */
        NULL,           /* FirstGadget */
        NULL,           /* CheckMark   */
        NULL,           /* Window title */
        NULL,           /* Pointer to Screen if not workbench */
        NULL,           /* Pointer to BitMap if a SUPERBITMAP window */
        0, 0,           /* minimum width, minimum height */
        0,0,            /* maximum width, maximum height */
        CUSTOMSCREEN    /* type waarin window opend */
        };


#define INTUITION_REV 0
#define GRAPHICS_REV 0

int teller=0,VertBServer();


main()
{
 SHORT xt,x,i,tw,th,tb,yprintpos,ymin_scrollraster;


 VOID OpenALL();

 OpenALL();

 if((Scrn = (struct Screen *) OpenScreen(&NewScrn)) == 0) exit(10);
 
 NewNoBorder.Screen = Scrn;

 if((NoBorder = (struct Window *) OpenWindow(&NewNoBorder)) == 0) exit(20);

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

 ShowTitle(Scrn,0);
 ClearMenuStrip(NoBorder);

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

 r=NoBorder->RPort;
 vp=ViewPortAddress(NoBorder);


 /* vertical blanking interrupt */

 Forbid();

 VertBIntr= (struct Interrupt *)AllocMem((LONG)sizeof(struct Interrupt),MEMF_PUBLIC);
 if(VertBIntr==0)
 {
  puts("Not enough memory for interrupt server!\n");
  exit(100);
 }

 VertBIntr->is_Node.ln_Type=NT_INTERRUPT;
 VertBIntr->is_Node.ln_Pri=0;
 VertBIntr->is_Node.ln_Name="VertB-scroll";
 VertBIntr->is_Code=(VOID(*)())VertBServer;

 AddIntServer(INTB_VERTB,VertBIntr);


  SetRGB4(vp,0,0,0,0);
  for(x=1;x<8;x++)
   {
   SetRGB4(vp,   x,12,x-1,x*2);
   SetRGB4(vp,16+x,x+1,x*2,15);
   }

 /* scroll */

  tw = r->TxWidth;           /* tw = Text Width */
  th = r->TxHeight;          /* th = Text Height */
  tb = r->TxBaseline;        /* tb = Text BaseLine */

  yprintpos = 199-1-(th-tb);
  ymin_scrollraster = 199-(th+1);

  SetAPen(r,3);

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

  while((MyIntuiMessage = (struct IntuiMessage *)
         GetMsg(NoBorder->UserPort))==NULL)
  {
     if(i>300)
       i=0;
     else
       i++;

     PrintAt(319-tw,yprintpos,"!");
     for(xt=0;xt<tw;xt+=2)
     {
      ScrollRaster(r,2,0,tw-xt,ymin_scrollraster,319-xt,199);
      WaitTOF();
     }
  }
  ReplyMsg(MyIntuiMessage);

  while(MyIntuiMessage=(struct IntuiMessage *)
   GetMsg(NoBorder->UserPort)) ReplyMsg(MyIntuiMessage);

 RemIntServer(INTB_VERTB,VertBIntr);
 FreeMem(VertBIntr,(LONG)sizeof(struct Interrupt));

 Permit();

 CloseWindow(NoBorder);
 CloseScreen(Scrn);
 CloseLibrary(GfxBase);
} /********************************** 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);

}


PrintAt(x,y,s)
int x,y;
char *s;
{
 Move(r,x,y);
 Text(r,s,strlen(s));
}

VertBServer()
{
 
 int_start();

 if(teller>160)
 {
  teller=0;
  SetAPen(r,0);
  Move(r,0,99);Draw(r,319,99);
 }
 else
 {
  teller++;
  SetAPen(r,teller%7+1);
  Move(r,159-teller,99);Draw(r,159+teller,99);
 }
 int_end();
}

