/* :ts=8 bk=0
 **************************************************************************
 ** Tilt.c *** Un divertimento da Workbench *** Scritto da Leo L. Schwab **
 ** Tradotto ed adattato ad Aztec C V3.6a da Luigi R. Callegari - 1/1989 **
 **************************************************************************
*/

#include <exec/types.h>
#include <intuition/intuition.h>

#define	DEPTH		2

extern void	*OpenLibrary(), *OpenWindow(), *OpenScreen();

struct NewScreen scrdef = {
	0, 0, 0, 0, DEPTH,	/*  Dimensioni fissate dopo  */
	0, 1,
	NULL,			/*  Modi fissati dopo */
	CUSTOMSCREEN,
	NULL,
	(UBYTE *) "Dink!",	/*  Titolo fissato dopo  */
	NULL, NULL
};

struct NewWindow windef = {
	0, 30, 100, 10,
	-1, -1,
	NULL,
	WINDOWDRAG | WINDOWDEPTH | SMART_REFRESH | ACTIVATE,
	NULL, NULL,
	(UBYTE *) "Tilt",
	NULL, NULL,
	0, 0, 0, 0,
	WBENCHSCREEN
};

char *msg = "\001\034\022T I L T !\0";

struct Screen	*scr;
struct Window	*win;
void		*IntuitionBase, *GfxBase;

/* Qui si aprono tutte le librerie, se possibile, altrimenti si termina */

openstuff ()
{
	if (!(IntuitionBase = OpenLibrary ("intuition.library", 0L))) {
		puts ("Intuition missing.");
		exit (100);
	}

	if (!(GfxBase = OpenLibrary ("graphics.library", 0L))) {
		puts ("Non trovo la grafica!");
		closestuff ();
		exit (100);
	}

	if (!(win = OpenWindow (&windef))) {
		puts ("Non s'apre la finestra!");
		closestuff ();
		exit (100);
	}
}

/* Funzione di uscita in caso di errori o di librerie inapribili */

die(str)
char *str;
{
	puts (str);
	closestuff ();
	exit (100);
}

/* Funzione di chiusura totale */

closestuff()
{
	if (scr)		CloseScreen (scr);
	if (win)		CloseWindow (win);
	if (GfxBase)		CloseLibrary (GfxBase);
	if (IntuitionBase)	CloseLibrary (IntuitionBase);
}

main()
{
	struct Screen	*wb;
	struct RastPort *rp;
	struct BitMap	*wbm, *mbm;
	long x, y, n;
	register int i;

	openstuff();

	wb = win -> WScreen;		/*  Workbench Screen  */
	scrdef.LeftEdge	= wb -> LeftEdge;
	scrdef.TopEdge	= wb -> TopEdge;
	scrdef.Width	= wb -> Width;
	scrdef.Height	= wb -> Height;
	scrdef.ViewModes = wb -> ViewPort.Modes;
	if (!(scr = OpenScreen (&scrdef)))
		die ("Non s'apre lo schermo!");
	ScreenToBack (scr);

	rp = &scr -> RastPort;
	mbm = rp -> BitMap;
	wbm = win -> WScreen -> RastPort.BitMap;
	BltBitMap (wbm, 0L, 0L, mbm, 0L, 0L,
		   (long) scrdef.Width, (long) scrdef.Height,
		   0xc0L, 0xffL, NULL);

	y = scrdef.Height-1;
	for (i=0; i<40; i++) {
		x = i << 4;
		ScrollRaster (rp, 0L, i-20L, x, 0L, x+15L, y);
	}

	x = scrdef.Width-1;
	n = scrdef.Height/50;
	for (i=0; i<50; i++) {
		y = i * n;
		ScrollRaster (rp, 25L-i, 0L, 0L, y, x, y+n-1);
	}
	ScreenToFront (scr);

	Delay (50L);
	DisplayAlert (RECOVERY_ALERT, msg, 30L);
	closestuff ();
}
