/*******************************\
*				*
*   Listing zu "NEW JUGGLER":   *
*				*
*********************************
*				*
* (c)1988 by Peter Weiland	*
*	     Ernst-August-Str.7 *
*	     D-3109 Wietze	*
*				*
*********************************
*				*
* Compiler-Aufruf für Aztec C:	*
*				*
* cc New_Juggler.c		*
* ln +Cd +Fbp New_Juggler.o -lc *
*				*
\*******************************/

#include <stdio.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>

#define ever    (;;)
#define N       NULL

struct IntuitionBase *IntuitionBase=N;
struct GfxBase       *GfxBase=N;
void *OpenLibrary();

struct Screen *scr1A=N, *scr1B=N, *scr2=N, *OpenScreen();
struct RastPort *rp1A, *rp1B, *rp2;
struct BitMap   *ptr1, *ptr2;
FILE *datei=N, *Open();

UBYTE *Button   = (UBYTE *)0xBFE001;
UBYTE *Tast     = (UBYTE *)0xBFEC01;

ULONG xx=102, yy=42;
UBYTE text[] = { "Bitte drücken Sie die linke Maustaste" };

/*-----> Daten für Screens <-----*/

UWORD Farbwerte[32] =
{ 0x000, 0x160, 0x0E0, 0x0F0,  0x555, 0x475, 0x4D0, 0x3F0,
  0x5F0, 0x9E0, 0xDE0, 0xAAA,  0xFF0, 0x99F, 0xAAF, 0xBBF };

struct TextAttr Font =
{ (UBYTE *)"topaz.font", 8, 0, FPF_ROMFONT };

struct NewScreen scr1def =
{ 0, 0, 320, 210, 6, 0, 1, HAM, CUSTOMSCREEN, &Font, N, N, N };
struct NewScreen scr2def =
{ 0, 0, 320, 564, 6, 0, 1, HAM | LACE, CUSTOMSCREEN, N, N, N, N };

/*-----> Texte für Requester <-----*/

struct IntuiText neg = { 0, 0, JAM1, 6, 3, N, (UBYTE *) " Ok ", N };
struct IntuiText rtxt0B =
 { 0, 0, JAM1, 5, 14, N, (UBYTE *) "Speicher nicht mehr aus!", N };
struct IntuiText rtxt0 =
 { 0, 0, JAM1, 5,  5, N, (UBYTE *) "Leider reicht der freie", &rtxt0B };
struct IntuiText rtxt1B =
 { 0, 0, JAM1, 5, 14, N, (UBYTE *) "nicht finden!", N };
struct IntuiText rtxt1 = { 0, 0, JAM1, 5, 5, N,
 (UBYTE *) "Ich kann die Datei >> Juggler.Dat <<", &rtxt1B };

/*-----> Hauptprogramm <-----*/

main()
{
 register long x, y, z, d;
 UBYTE t;
 Open_all();
 FreeSprite(0L); ScreenToFront(scr1A);

 if (!(datei = Open("Juggler.Dat", MODE_OLDFILE))) NoDat();
 for (x=0; x<6; x++) Read(datei, ptr1->Planes[x], 8000L);
 for (x=0; x<6; x++) Read(datei, ptr2->Planes[x], 22560L);
 Close(datei); datei=N;

 Cls();
 ClipBlit(rp2,  10L, 536L, rp1A, xx+6, yy+ 2,  89L, 26L, 192L);
 ClipBlit(rp2, 108L, 536L, rp1A, xx+4, yy+33, 100L, 26L, 192L);
 ClipBlit(rp2, 213L, 536L, rp1A, xx+4, yy+57, 100L,  8L, 192L);
 for (y=0; y<200; y++) ClipBlit(rp1A, 0L, y, rp1B, 0L, y, 320L, 1L, 192L);

 x=TextLength(rp1A, &text[0], (long)strlen(text))>>1;
 y=12;
 for ever
 {
  SetAPen(rp1A, y); y^=8; Move(rp1A, 159L-x, 207L); 
  Text(rp1A, &text[0], (long)strlen(text));
  if (!(*Button & 64)) break; Delay(5L);
  if (!(*Button & 64)) break; Delay(5L);
  if (!(*Button & 64)) break; Delay(5L);
  if (!(*Button & 64)) break; Delay(5L);
 }
 SetAPen(rp1A, 0L); RectFill(rp1A, 0L, 200L, 319L, 209L); 
 Cls();
 ClipBlit(rp2, 0L, 0L, rp1A, xx, yy, 107L, 66L, 192L); 
 ClipBlit(rp2, 0L, 0L, rp1B, xx, yy, 107L, 66L, 192L); 

 x=y=z=0; d=1; FreeSprite(0L);
 for ever
 {
  x--; if (x<0) { x=2; y++; if (y>7) y=0; } z=(z+1)&255;
  if (z & 1)
  {
   ClipBlit(rp2, x*107L+20L, y*67L, rp1A, xx+20L, yy, 66L, 66L, 192L); 
   ScreenToFront(scr1A);
  }
  else
  {
   ClipBlit(rp2, x*107L+20L, y*67L, rp1B, xx+20L, yy, 66L, 66L, 192L); 
   ScreenToFront(scr1B);
  }
  if (d) Delay(d);
  t=*Tast;
  if (t & 1)
  {
   if (t == 117) break;
   if ((t>86) && (t<96)) { d=(95-t)>>1; if (d) d=1<<(d-1); }
   if (t == 57)
   {
    for ever if (*Tast != 57) break;
    for ever if (*Tast == 57) break;
    for ever if (*Tast != 57) break;
   }
  }
 }
 ScreenToFront(scr1A); Cls();
 SetAPen(rp1A, 0L);
 for (y=0; y<100; y++)
 {
  Move(rp1A, 0L, y); Draw(rp1A, 319L, y);
  Move(rp1A, 0L, 199-y); Draw(rp1A, 319L, 199-y);
  Delay(1L);
 }
 Close_all();
}

/*-----> Fehlermeldung: zu wenig freier Speicher <-----*/

NoRam()
{
 AutoRequest(N, &rtxt0, N, &neg, N, N, 320L, 60L);
 Close_all(); exit(1L);
}

/*-----> Fehlermeldung: Datei nicht gefunden <-----*/

NoDat()
{
 AutoRequest(N, &rtxt1, N, &neg, N, N, 320L, 60L);
 Close_all(); exit(1L);
}

/*-----> Libraries und Screens öffnen <-----*/

Open_all ()
{
 IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 1L);
 GfxBase =       (struct GfxBase *)       OpenLibrary("graphics.library", 1L);

 if (!(scr2 = OpenScreen(&scr2def))) NoRam();
 if (!(scr1B = OpenScreen(&scr1def))) NoRam();
 if (!(scr1A = OpenScreen(&scr1def))) NoRam();
 LoadRGB4(&scr1A->ViewPort, &Farbwerte[0], 16L);
 LoadRGB4(&scr1B->ViewPort, &Farbwerte[0], 16L);
 LoadRGB4(&scr2->ViewPort,  &Farbwerte[0], 16L);
 RemakeDisplay();
 rp2=&scr2->RastPort;
 rp1A=&scr1A->RastPort; rp1B=&scr1B->RastPort;
 ptr1=rp1A->BitMap; ptr2=rp2->BitMap;
}

/*-----> Libraries und Screens schließen <-----*/

Close_all()
{
 if (datei) Close(datei);
 if (scr2) CloseScreen(scr2);
 if (scr1B) CloseScreen(scr1B);
 if (scr1A) CloseScreen(scr1A);
 if (GfxBase) CloseLibrary(GfxBase);
 if (IntuitionBase) CloseLibrary(IntuitionBase);
}

/*-----> Kleinen Bildschirm löschen <-----*/

Cls()
{
 ULONG y;
 ScreenToFront(scr1A); FreeSprite(0L);
 SetAPen(rp1A, 0L);
 for (y=0; y<34; y++)
 {
  Move(rp1A, xx, yy+y); Draw(rp1A, xx+106, yy+y);
  Move(rp1A, xx, yy+66-y); Draw(rp1A, xx+106, yy+66-y);
  Delay(2L);
 }
}
