;/*
sc RESOPT IGNORE=73 DATA=NEAR NMINC UCHAR CONSTLIB STREQ STRMERGE NOSTKCHK NOSTDIO OPTIMIZE OPTSIZE StarField.c
slink from LIB:c.o StarField.o to //Clients/StarField LIB LIB:sc.lib LIB:amiga.lib /lib/client.lib SC SD NOICONS STRIPDEBUG
delete starfield.o
quit

 StarField 1.2  (Client for BServer)

 Copyright © 1994-1995 by Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.
*/

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

#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <clib/alib_protos.h>

#include "/include/client.h"

char *ver = "$VER: StarField 1.2 "__AMIGADATE__;

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

struct DisplayIDInformation *dinfo;

struct Screen *scr;

UWORD Stars[512][2];


void DrawStars( void )
{
UWORD n, tmpx, trnd, swidth, sheight;
struct Rectangle *rect;
register PLANEPTR plane0, plane1;
register UWORD last_star;
UBYTE brightness, color;

rect = GETTXTOSCANRECT(dinfo);

swidth = RECTANGLEWIDTH(rect);
sheight = RECTANGLEHEIGHT(rect);
brightness = GETBRIGHTNESS(dinfo);

if ( DISPLAYID( dinfo ) & LACE )
	last_star = ( sheight > 512 ? 512 : sheight );
else
	last_star = ( sheight > 256 ? 256 : sheight );

swidth = (swidth>>8)<<8;

if ( scr = OpenScreenTags( NULL,
	SA_DisplayID, DISPLAYID( dinfo ),
	SA_Width, swidth,
	SA_Height, sheight,
	SA_Left, (RECTANGLEWIDTH(rect) - swidth)/2,
	SA_Top, 0,
	SA_Depth, 2,
	SA_Overscan, OSCAN_TEXT,
	SA_Type, CUSTOMSCREEN,
	SA_Quiet, TRUE,
	TAG_END ) )
	{
	register struct ViewPort *vp = &(scr->ViewPort);

	SpritesOff();

	SetRGB4( vp, 0, 0, 0, 0 );
	color = 5*brightness/100;
	SetRGB4( vp, 1, color, color, color );
	color = 10*brightness/100;
	SetRGB4( vp, 2, color, color, color );
	color = 15*brightness/100;
	SetRGB4( vp, 3, color, color, color );

	for ( n = 0; n < last_star; n++ )
		{
		Stars[n][0] = RangeRand(swidth-1);
		Stars[n][1] = RangeRand( 7 );
		}

	while( STILL_BLANKING )
		{
		/* Draw stars */
		plane0 = scr->RastPort.BitMap->Planes[0];
		plane1 = scr->RastPort.BitMap->Planes[1];

		WaitTOF();
		for ( n = 0; n < last_star; n++ )
			{
			* (plane0 + (Stars[n][0]>>3) ) = 0;
			* (plane1 + (Stars[n][0]>>3) ) = 0;

			tmpx = Stars[n][0] + Stars[n][1];
			if ( tmpx >= swidth )
				{
				while ( !(trnd = RangeRand( 7 ) ) );
				Stars[n][0] = 0;
				Stars[n][1] = trnd;
				}
			else	Stars[n][0] = tmpx;

			if ( Stars[n][1]>>1 & 1 )
				* (plane0 + (Stars[n][0]>>3) ) = 128>>(Stars[n][0] & 7);
			if ( Stars[n][1]>>1 & 2 )
				* (plane1 + (Stars[n][0]>>3) ) = 128>>(Stars[n][0] & 7);

			plane0 += swidth >>3;
			plane1 += swidth >>3;
			}
		}
	CloseScreen( scr );
	SpritesOn();
	}
else
	SendClientMsg( ACTION_FAILED );
}

void __main( char *line )
{
if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
	{
	if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 37L ) )
		{
		if ( dinfo = OpenCommunication() )
			{
			DrawStars();
			CloseCommunication( dinfo );
			}
		CloseLibrary( (struct Library *)GfxBase );
		}
	CloseLibrary( (struct Library *)IntuitionBase );
	}
}
