/*****************************
 * Programm: Stars		  	 *
 * Autoren: Mathias Ortmann	 *
 * und Holger Roeser		 *
 * (c) MAXON Computer Gmbh   *
 * KICKSTART 1990	  		 *
 * Aufruf: [run] stars		 *
 * Aztec C 3.6a			  	 *
 * Compilieren mit:		  	 *
 * cc stars			  	     *
 * ln stars -lc 	         *
 * Aztec C 5.0a			  	 *
 * Compilieren mit:		  	 *
 * cc stars -so -ps		     *
 * ln stars -lc16            *
 *****************************/

#include <graphics/gfxbase.h>
#include <intuition/intuition.h>

void *OpenLibrary();

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *scr, *OpenScreen();
struct RastPort *rp;
struct ViewPort *vp;

long peekl();

struct NewScreen ns = { 0, 0, 320, 256, 5, 0, 0, NULL, CUSTOMSCREEN };

#define MAXSTARS 50

short lastc[MAXSTARS][2];
short reac[MAXSTARS][3];
char count[MAXSTARS];

short rnd()  /* liefert Zufallszahl von 0...511 */
{
	register long random;
	random = random<<2 ^ peekw(0xdff006) + 17910192L;
	return random & 511;
}

main()
{
	register short xc,yc,s;
	short col,i;

	IntuitionBase = OpenLibrary("intuition.library",0L);
	GfxBase = OpenLibrary("graphics.library",0L);

	if (!(scr = OpenScreen(&ns))) goto cleanup;

	rp = &scr->RastPort;
	vp = &scr->ViewPort;

	for (i=0; i<16; i++) SetRGB4(vp,(long)i,(long)i,(long)i,(long)i);

	for (i=0; i<MAXSTARS; i++)
	{
		reac[i][0] = rnd()+rnd()-512;
		reac[i][1] = rnd()+rnd()-512;
		reac[i][2] = rnd()>>1;
	}

	while (peek(0xbfec01) != 57)	/* Test, ob ESC gedrückt */
	{
		for (s = 0; s < MAXSTARS; s++)
		{
			if (reac[s][2] <= 0)
			{
				reac[s][0] = rnd()+rnd()-512;
				reac[s][1] = rnd()+rnd()-512;
				reac[s][2] = 250;
			}
			else
			{
				xc = 50*reac[s][0]/reac[s][2]+160;
				yc = 50*reac[s][1]/reac[s][2]+128;
				col = 17-reac[s][2]/15;
				if (col > 15) col = 15;

				SetAPen(rp,0L);
				WritePixel(rp,(long)lastc[s][0],(long)lastc[s][1]);
				if (xc>=0 && yc>=0 && xc<=319 && yc<=255)
				{
					SetAPen(rp,(long)col);
					WritePixel(rp,(long)xc,(long)yc);
					lastc[s][0] = xc;
					lastc[s][1] = yc;
				}
				reac[s][2] -= 5;
			}
		}
		WaitTOF(vp);
	}

cleanup:
	if (scr) CloseScreen(scr);
	CloseLibrary(IntuitionBase);
	CloseLibrary(GfxBase);
}

