#include <exec/types.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

/* Warteschleifen */

#define abs(x) (((x) < 0) ? (-x) : (x))

void                 *OpenLibrary();
struct Screen        *OpenScreen();

extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase       *GfxBase;
static struct Screen        *S;

static struct NewScreen NewS = { 0,0,320,256,3,1,3,(USHORT)0,
                          (USHORT)CUSTOMSCREEN|SCREENQUIET,
                          0L,0L,
                          0L,0L };

static UBYTE *BUTTON = 0xBFE001;

LONG     maxadr1;
PLANEPTR base1,base2;

static struct ViewPort *VPort;
static struct RastPort *DrawPort;

static struct Image ti;

#define MAXBUF	1000
static int xbuf[MAXBUF];
static int ybuf[MAXBUF];
static int numbuf;

DoIntro()
{ 

   S = (struct Screen*) OpenScreen(&NewS);

   VPort = &(S->ViewPort);
   DrawPort = &(S->RastPort);
   
   SetRGB4(VPort,0L,0L,0L,0L);     
   SetRGB4(VPort,1L,15L,15L,15L); 
   SetRGB4(VPort,2L,0L,0L,14L); 
   SetRGB4(VPort,3L,10L,0L,8L); 
   SetRGB4(VPort,4L,6L,3L,3L); 
   SetRGB4(VPort,5L,12L,0L,0L); 
   SetRGB4(VPort,6L,0L,12L,0L); 
   SetRGB4(VPort,7L,12L,10L,10L); 

   base1   = S->RastPort.BitMap->Planes[0];
   base2   = S->RastPort.BitMap->Planes[1];
   
   maxadr1 = (LONG)(S->RastPort.BitMap->Planes[0] + (LONG)(40 * 256));
   
   InitStars();
   WriteStars();

   StartStars();
   
   ZeigeTitel(S);

   while (!(*BUTTON & 0x40));
   
   StopStars();
   CloseScreen(S);
   OFF_DISPLAY();
      
}
      
int Random(n)

int n;

{ int j,r;
  UWORD *s = (UWORD*) 0xDFE006;
  UWORD *m = (UWORD*) 0xbfd800;

  r = (*s ^ *m ^ 0xFFFF);
  j = r/n;
  r = (r-n*j);
  return(abs(r%n));
}

static int FadeText(txt,y,col)

UBYTE *txt;
LONG   y,col;

{
 register int x0,x,x1,y1,l,i,k;
 int	  u,o,t;
 
 l = strlen(txt);
 SetRGB4(VPort,4L,0L,0L,0L);     

 x0 = (int)TextLength(DrawPort,txt,(LONG)l);
 x  = (320-x0)/2;
 SetDrMd(DrawPort,(ULONG)JAM2);
 SetWrMsk(DrawPort,(LONG)0x07);
 SetAPen(DrawPort,4L);
 SetBPen(DrawPort,0L);
 Move(DrawPort,(LONG)x,y);
 Text(DrawPort,txt,(LONG)l);
 SetAPen(DrawPort,col);
 i = 0;
 for (x1 = x; x1 <= x+8*l; x1++)
     {
     for (y1 = (int)y-8; y1 <= (int)y; y1++)
         {
         if ((ReadPixel(DrawPort,(LONG)x1,(LONG)y1)==4L) && (i < MAXBUF))
            {
             xbuf[i] = x1;
             ybuf[i] = y1;
             i++;
            }
      	if (i > 1)
           {
            o = Random(i/2);
            u = Random(i/2)+i/2;
            t = xbuf[o];
            xbuf[o] = xbuf[u];
            xbuf[u] = t;
            t = ybuf[o];
            ybuf[o] = ybuf[u];
            ybuf[u] = t;
           } 
      }
     }    
 numbuf = i;
 for (i = 0; i < numbuf/2; i++)
     {
      x1 = xbuf[i];
      y1 = ybuf[i];
      WritePixel(DrawPort,(LONG)x1,(LONG)y1);
      x1 = xbuf[numbuf/2+i];
      y1 = ybuf[numbuf/2+i];
      WritePixel(DrawPort,(LONG)x1,(LONG)y1);
     }
 SetAPen(DrawPort,col);
 SetBPen(DrawPort,0L);
 Move(DrawPort,(LONG)x,y);
 StopStars();
 DeleteStars();
 ClearStars();
 Text(DrawPort,txt,(LONG)l);
 StartStars();
 SetRGB4(VPort,4L,6L,3L,3L);     /* dbraun */

 return(x);
}

static ZeigeTitel(S)

struct Screen *S;

{
 int         x,y,x0,x1,x2,mx,my;
 BOOL         Game,Ende,First;
 char         pstr[2];
 
 OFF_DISPLAY();

 StopStars();
 DeleteStars();
 
 GetImage((UBYTE*)"cdsintro.img",&ti);
 
 x = (int)ti.Width;
 x = (320-x)/2;
 x0 = x;
 DrawImage(DrawPort,&ti,(LONG)x,40L);

 ClearStars();
 StartStars();

 ON_DISPLAY();

 x2 = FadeText((UBYTE*)"(C) 1991 Miky Wenngatz ",90L,2L);
 
 x2 = FadeText((UBYTE*)"Geschrieben von Michael Kaiser",120L,2L);

 x2 = FadeText((UBYTE*)"Filerequester (C) 1991 HBA",190L,3L);
 x2 = FadeText((UBYTE*)"Preferences (C) 1991 HBA",205L,3L);
 
 x2 = FadeText((UBYTE*)"<< Mausknopf !! >>",225L,5L);

 while (*BUTTON & 0x40);

 DisposeImage(&ti);
}

