#include "viewport.h"
#include "animation.h"

#define DEPTH 4
#define WIDTH 352
#define HEIGHT 200

#define SIZE 2000


/* Hier sind die noetigen Strukturen fuer eine Anzeige: */
struct GfxBase *GfxBase;
struct View v,*oldview;
struct ViewPort vp;
struct ColorMap *cm;
struct RasInfo ri;
struct BitMap b;
struct RastPort rp;
extern struct ColorMap *GetColorMap();

char *txt;
short spgot[7],CopList;
PLANEPTR AllocRaster();

USHORT *colorpalette;
USHORT colortable[] =
{
  0x000,0xff0,0xfe0,0xfd0,0xfc0,0xfb0,0xfa0,0xf90,
  0xf80,0xf70,0xf60,0xf50,0xf40,0xf30,0xf20,0xf10,
  0    ,0    ,0    ,0    ,0    ,0    ,0    ,0    ,
  0    ,0    ,0    ,0    ,0    ,0    ,0    ,0
};

struct SimpleSprite sprite[7];

UWORD sprite_dat[]=
{
0,0,                      /* ....----....---- */
0xf718,0x0000,            /* xxxx xxx   xx   */
0xc6b4,0xf718,            /* xx   xx x xx x  */
0xe734,0xc6b4,            /* xxx  xxx  xx x  */
0xc6b4,0xe734,            /* xx   xx x xx x  */
0xc698,0xc6b4,            /* xx   xx x  xx   */
0x0000,0xc698,            /*                 */
0x0000,0x0000,
0xe31c,0x0000,            /* xxx   xx   xxx  */
0xd6b0,0xe31c,            /* xx x xx x xx    */
0xe7b0,0xd6b0,            /* xxx  xxxx xx    */
0xd6b0,0xe7b0,            /* xx x xx x xx    */
0xe69c,0xd6b0,            /* xxx  xx x  xxx  */
0x0000,0xe69c,            /* ....----....---- */
0,0
};

UWORD *sprite_data;
main()
{
  char *zeile[25];
  int laenge,pointer;

  USHORT *displaymem;
  int x[6],y[6],yv[6],xv[6],yy;
  int i,j;
  short color;

  /* ------------------------------------------------------------------- *
   *       Schnell-Abbruch, wenn am Anfang Taste gedrueckt war           *
   * ------------------------------------------------------------------- */
  if(peekb(0xbfe001)==0xbc) {
    printf("You really don't want to see my great intro? What a pity! But ok, I obey.\n");
    exit(0);
  }

  /* ------------------------------------------------------------------- *
   *                        Text bereitstellen                           *
   * --------------------------------------------------------------------*/
  zeile[0]="   Pieter was here groetjes aan tante TERRY and nonkel BOB  ";
  zeile[1]="of the year!     Frozen Banana presents:  FROBAC  -  FRO zen   ";
  zeile[2]="B anana's   A ztec   C.         Pieter is the absolutely new ";
  zeile[3]="WIE NOG VERDER LEEST IS STAPELZOT *********************** ";
  zeile[4]="program using Lattice C?    Gave up?    Start using C again!   ";
  zeile[5]="Now the compiling time can be       L E S S   T H A N   3 0   ";
  zeile[6]="S E C O N D S   ! ! !          However, I wish you good luck ";
  zeile[7]="and much pleasure.     Now I have to greet the people I know:  ";
  zeile[8]="Greetings to :    Topeka (first owner of this disk), TCS (what ";
  zeile[9]="about the ski-weekend??), Tytan (How is Zurich?), Wotan, Maesi ";
  zeile[10]="(the AmiGazette is not bad!), Big Apple, the Punk, Flato ";
  zeile[11]="(thanks for the 1 meg expansion, it's great!) and every";
  zeile[12]="body else.        Special greetings to Niki and his dog ";
  zeile[13]="Zorro.    And to all the other ones:  Enjoy this disk!    How ";
  zeile[14]="about to send me your sources?    I'm waiting.     Bye!      ";
  zeile[15]="                                   ";

  {
    char *AllocMem();
    txt=AllocMem(SIZE,0);
    if (txt==NULL) exit(0);
  }

  j=0;
  for(i=0;i<=15;i++) {
    while(*(txt+(j++))=*(zeile[i]++));
    j--;
  };

  /* ------------------------------------------------------------------- *
   *                    Graphics.library eroeffnen                       *
   * ------------------------------------------------------------------- */

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

  oldview=GfxBase->ActiView;

  /* ------------------------------------------------------------------- *
   *                 Viewport-Strukturen initialisieren                  *
   * ------------------------------------------------------------------- */
  InitView(&v);
  InitVPort(&vp);
  v.ViewPort=&vp;

  InitBitMap(&b,DEPTH,WIDTH,HEIGHT);
  for(i=0;i<DEPTH;i++) {
    b.Planes[i]=AllocRaster(WIDTH,HEIGHT);
    if (b.Planes[i]==NULL) exit(100);
    BltClear(b.Planes[i],RASSIZE(WIDTH,HEIGHT),0);
  };

  ri.BitMap=&b;
  ri.RxOffset=0;
  ri.RyOffset=0;
  ri.Next=NULL;

  cm=GetColorMap(32);
  if(cm==NULL) Stop("No mem for ColorMap");
  colorpalette=(unsigned short *)cm->ColorTable;
  for (i=0;i<32;i++) {
    *colorpalette++=colortable[i];
  }

  vp.DWidth=WIDTH;
  vp.DHeight=HEIGHT;
  vp.RasInfo=&ri;
  vp.DxOffset=-16;
  vp.ColorMap=cm;
  vp.Modes=SPRITES;

  InitRastPort(&rp);
  rp.BitMap=&b;

  MakeVPort(&v,&vp);
  MrgCop(&v);
  CopList=1;

  /* ------------------------------------------------------------------ *
   *                             Bildaufbau                             *
   * ------------------------------------------------------------------ */

  SetDrMd(&rp,JAM2);

  SetAPen(&rp,1);
  RectFill(&rp,0,0,WIDTH-1,199);

  for(i=10;i<10+99;i+=33) {

    for(yy=1;yy<16;yy++) {
      SetAPen(&rp,yy);
      Move(&rp,0,i+yy);
      Draw(&rp,WIDTH-1,i+yy);
    };

    for(yy=1;yy<16;yy++) {
      SetAPen(&rp,16-yy);
      Move(&rp,0,i+15+yy);
      Draw(&rp,WIDTH,i+15+yy);
    };
  };

  for(yy=1;yy<15;yy++) {        /* Gelb bis Rot */
    SetAPen(&rp,yy);
    Move(&rp,0,155+yy);
    Draw(&rp,WIDTH-1,155+yy);
  };

                             /* Hier kommt die Laufschrift */


  for(yy=1;yy<16;yy++) {        /* Rot bis Gelb */
    SetAPen(&rp,yy);
    Move(&rp,0,185-yy);
    Draw(&rp,WIDTH-1,185-yy);
  }

  pointer=-1;
  laenge=strlen(txt);

  WaitBlit();

  LoadView(&v);

  Delay(15
);

  /* -------------------------------------------------------------------- *
   *                       Loch auf fuer Laufschrift                      *
   * -------------------------------------------------------------------- */

  for(i=186;i<=200;i++) {
    WaitBOVP(&vp);
    for(yy=1;yy<16;yy++) {
      SetAPen(&rp,yy);
      Move(&rp,0,i-yy);
      Draw(&rp,WIDTH-1,i-yy);
    }
    SetAPen(&rp,0);
    Move(&rp,0,i-16);
    Draw(&rp,WIDTH-1,i-16);
  }

  /* -------------------------------------------------------------------- *
   *                            Sprite darstellen                         *
   * -------------------------------------------------------------------- */
  sprite_data=AllocMem(500,2);  /*   2 = MEMF_CHIP    */

  for(i=0;i<5;i++) {
    x[i]=200+20*i;
    y[i]=2030;
    yv[i]=-40;
    xv[i]=-4;
    for(j=0;j<=i*4;j++) {
      y[i]=y[i]-yv[i];
      yv[i]=yv[i]-2;
    };
    for(j=0;j<40;j++) *(sprite_data+40*i+j)=sprite_dat[j];

    spgot[i]=GetSprite(&sprite[i],-1);
    if(spgot[i]<0) exit(100);
 

    sprite[i].x=400;
    sprite[i].y=0;
    sprite[i].height=13;

    color=((spgot[i] & 6)*2)+16;

    SetRGB4(&vp,color+1,15,0,0);
    SetRGB4(&vp,color+2,0,0,0);
    SetRGB4(&vp,color+3,15,0,0);

    ChangeSprite(0,&sprite[i],sprite_data+40*i);

  }

  /* -------------------------------------------------------------------- *
   *                               Scroll                                 *
   * -------------------------------------------------------------------- */

  SetAPen(&rp,1);
  SetBPen(&rp,0);

  while((peekb(0xbfe001)!=0xbc)&&(++pointer<laenge)) {
    Move(&rp,WIDTH-8,180);
    Text(&rp,txt+pointer,1);
    for(j=0;j<8;j+=2) {
      WaitBOVP(&vp);
      ScrollRaster(&rp,2,0,0,173,WIDTH-1,181);
      for(i=0;i<5;i++) {
        if((x[i]+=xv[i])<-30) x[i]=350;
        if((y[i]+=(yv[i]+=2))>=1550) yv[i]=-40;
        MoveSprite(0,&sprite[i],x[i],y[i]/10);
      }
    }
  }

  /* -------------------------------------------------------------------- *
   *                                 Loch zu                              *
   * -------------------------------------------------------------------- */

  for(i=0;i<5;i++) {
    xv[i]= (FastRand(i*129  ) % 8 )-4;
    yv[i]=-(FastRand(i*79+13) % 42);
  }

  for(i=200;i>=184;--i) {
    WaitBOVP(&vp);
    for(yy=1;yy<16;yy++) {
      SetAPen(&rp,yy);
      Move(&rp,0,i-yy);
      Draw(&rp,WIDTH-1,i-yy);
    };
    for(j=0;j<5;j++)
      MoveSprite(0,&sprite[j],x[j]+=xv[j],(y[j]+=(yv[j]+=2))/10);
  };

  /* -------------------------------------------------------------------- *
   *                             Sprite fallen lassen                     *
   * -------------------------------------------------------------------- */

  i=1;
  while(i) {
    i=0;
    for(j=0;j<5;j++) {
      x[j]+=xv[j];
      y[j]+=(yv[j]+=2);
      if(y[j]>3000)
        y[j]=3000;
      else
        i=1;
    }
    WaitTOF();
    for(j=0;j<50;j++);
    for(j=0;j<5;j++) MoveSprite(0,&sprite[j],x[j],y[j]/10);
  }

  /* -------------------------------------------------------------------- *
   *                            Bildschirm ausblenden                     *
   * -------------------------------------------------------------------- */

  SetAPen(&rp,0);
  for(i=0;i<=16;i++) {
    for(j=16;j<WIDTH+15;j+=32) {
      if(j-i<WIDTH) {
        Move(&rp,j-i,0);
        Draw(&rp,j-i,199);
      };
      if(j+i<WIDTH) {
        Move(&rp,j+i,0);
        Draw(&rp,j+i,199);
      };
      if(j-i<200) {
        Move(&rp,0,j-i);
        Draw(&rp,WIDTH-1,j-i);
      };
      if(j+i<200) {
        Move(&rp,0,j+i);
        Draw(&rp,WIDTH-1,j+i);
      };
    };
  };

  Delay(15);

  Stop();

}

Stop()
{
  int i;

  LoadView(oldview);

  FreeMem(sprite_data,500);
  FreeMem(txt,SIZE);
  for(i=0;i<DEPTH;i++)  {
    if(b.Planes[i]) FreeRaster(b.Planes[i],WIDTH,HEIGHT);
  }
  if(cm) FreeColorMap(cm);
  for(i=0;i<5;i++) if(spgot[i]!=-1) FreeSprite(spgot[i]);

  FreeVPortCopLists(&vp);
  FreeCprList(v.LOFCprList);
  CloseLibrary(GfxBase);
  exit(0);
}

