/***************************************************************
 *            basic ray tracing package tracer2.0              *
 *            version 0.0 done 6/1/86-6/10/86                  *
 *            version 1.0 released 6/29/86                     *
 *            version 2.0 released 7/5/86                      *
 *            by friedrich knauss (one tired programmer)       *
 *							       *
 *	      amiga version adapted by mark reichert           *
 ***************************************************************/

#include <stdio.h>
#include <math.h>
#include "MyMath.h"
#include "rtd.h"
#include "macros.h"

#define	CHECK_IT	if ( Chk_Abort() ) {\
				printf("Aborted\n");\
				gfx_close();\
				exit(104);\
			}

FILE	*fptr;
char	suzie[300][300];
FFP	sam;

int	xsue,
	ysue;

struct	ball	*bl[150];

int	level=0,
	nob;

struct	sphere	ls;

main(argc, argv)
int	argc;
char	**argv;
{
	FILE	*df, *texfile;
	static	double	xco,
			yco;
	struct	ray	rr;
	struct	vector	vp;
	int	i,
		in = 0,
		tex = 0;
	int	c;
	char	temp[10];

	if( !open_math() ) {
		printf("couldn't open math library\n");
		exit(103);
	}

	sam = SPFlt(1);

	/* command interp */
	for (i = 1; i < argc; i++) {
		if (argv[i][0] != '-')
		    booboo ("Options start with a '-'");

		c = argv[i][1];

		switch (c) {
		case ('i') : /* input file */
			if (in)
		    		booboo ("Sorry, only one input file");
			in = 1;

			if ((i + 1) >= argc || argv[i + 1][0] == '-')
				/* no arg */
				df = stdin;
			else if ((df = fopen (argv[++i], "r")) == NULL)
				booboo ("input file not found");

			break;

		case ('o'): /* output file */
			booboo ("-o option no longer supported.");
			break;

		case ('s'): /* susie file */
			if (tex)
				booboo ("Sorry, only one image file");

			if ((i + 1) >= argc || argv[i + 1][0] == '-')
				/* no arg */
				booboo ("-s requires an argument");

			tex = 1;

			if ((texfile = fopen (argv[++i], "r")) == NULL)
				booboo ("image file not found");

			break;

		case ('S'): /* amount of susie */
			if (argv[i][2] < '0' || argv[i][2] > 9)
				booboo ("-S needs a numerical argument");

			sam = ieee_to_ffp( atof (&(argv[i][2])) );

			break;

		default: 
			booboo ("Unrecognized option. Better try again");
		}
	}


	if (!in)
		if ((df = fopen ("bdata.i", "r")) == NULL)
			booboo ("bdata.i not found");

	if (!tex)
		if ((texfile = fopen ("pat.def", "r")) == NULL)
			booboo ("pat.def not found");


	/* if you can't figure *this* out, you should go home */
	nob = g_bal (df);

	g_bod (texfile);

	MV (SPFlt(95), SPFlt(140), SPFlt(-200), vp);	/* view point	*/
	MV (SPFlt(95), SPFlt(900), SPFlt(0), ls.cent);	/* light source	*/
	ls.rad = SPFlt(70);

	gfx_open();

	for (yco = (YMAX * SCALE); yco > YMIN * SCALE; yco-= 2.0 ) {
		for (xco = XMIN * SCALE; xco < XMAX * SCALE; xco+= 2.0 ) {
			MV (ieee_to_ffp(xco / SCALE),ieee_to_ffp(yco / SCALE), SPFlt(0), rr.org );
			SV (rr.dir, rr.org, vp);
			do_pixel( (int)xco, (int)yco, shade(&rr) );
		}
		CHECK_IT;
	}

	for (yco = (YMAX * SCALE) + 1.0; yco > YMIN * SCALE; yco-= 2.0 ) {
		for (xco = (XMIN * SCALE)+1.0; xco < XMAX * SCALE; xco+= 2.0 ) {
			MV (ieee_to_ffp(xco / SCALE),ieee_to_ffp(yco / SCALE), SPFlt(0), rr.org );
			SV (rr.dir, rr.org, vp);
			do_pixel( (int)xco, (int)yco, shade(&rr) );
		}
		CHECK_IT;
	}

	for (yco = (YMAX * SCALE); yco > YMIN * SCALE; yco-= 2.0 ) {
		for (xco = (XMIN * SCALE)+1.0; xco < XMAX * SCALE;xco+=2.0) {
			MV (ieee_to_ffp(xco / SCALE),ieee_to_ffp(yco / SCALE), SPFlt(0), rr.org );
			SV (rr.dir, rr.org, vp);
			do_pixel( (int)xco, (int)yco, shade(&rr) );
		}
		CHECK_IT;
	}

	for (yco = (YMAX * SCALE) + 1.0; yco > YMIN * SCALE; yco-= 2.0 ) {
		for (xco = XMIN * SCALE; xco < XMAX * SCALE;xco+=2.0) {
			MV (ieee_to_ffp(xco / SCALE),ieee_to_ffp(yco / SCALE), SPFlt(0), rr.org );
			SV (rr.dir, rr.org, vp);
			do_pixel( (int)xco, (int)yco, shade(&rr) );
		}
		CHECK_IT;
	}

	gets(temp);
	gfx_close();
}

booboo (str)
char 	*str;
{
	printf ("%s\n", str);
	exit (-1);
}
