/*
	
	cl /AL xxxxx.c vimage3 mouseg graphlib

*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "mouseg.h"
#include "vimage.h"
#include "font.h"

#define BLACK      0
#define D_GREY     1
#define M_GREY     2
#define L_GREY     3
#define WHITE      4
#define RED        5
#define ORANGE     6
#define YELLOW     7
#define GREEN      8
#define CYAN       9
#define BLUE      10
#define PURPLE    11
#define BUFF2     12
#define BROWN     13
#define D_BLUE    14
#define BUFF      15

int Mouse=0;
int ScreenXs=640,ScreenYs=480;
struct Color Vcolor[16]=
{
	{   0,  0,  0},
	{  46, 46, 46},
	{ 128,128,128},
	{ 191,191,191},
	{ 255,255,255},
	{ 255,  0,  0},
	{ 255,128,  0},
	{ 191,191,  0},
	{   0,255,  0},
	{   0,191,191},
	{   0,  0,255},
	{ 128,  0,255},
	{  95, 81, 67},
	{  96, 28, 14},
	{   0, 20, 60},

	{ 105, 90, 75}
};
double K=2500.0;	/* K = pixels from screen */
double S=72;		/* S = 1/2 Separation of eyes in pixels */

int get_point_3d(int cursize,double *x,double *y,double *z,
	int *sxr,int *sxb,int *sy);

main()
{
	int i,j,k;
	double x,y,z;
	int sy,sxr,sxb;	/* screen y & screen x (red and blue) */

	if(mouse_initialize(4))
	{
		printf("Mouse initialized.\n\n");
		Mouse=4;	/* sensitivity set to maximum (1 is minimum) */
	}
	video_on();
	x=0;
	y=0;
	z=0;
	set_lut();
	get_point_3d(10,&x,&y,&z,&sxr,&sxb,&sy);
	video_off();
}


/**********************************************************************
**
**
**
**********************************************************************/

int get_point_3d(int cursize,double *x,double *y,double *z,
	int *sxr,int *sxb,int *sy)

{
	int right,left,ix,iy,iz,ixo,iyo,izo,row,col;
	int rowo,rowzo,colo,ro,co;
	int val=-1,left_down,right_down;
	int xcent=ScreenXs/2,ycent=ScreenYs/2;	
	int cursizeo=cursize;
	
	mouse_horizontal_range(0,ScreenXs-1);
	mouse_vertical_range(0,ScreenYs-1);
	if((*z/-K+1.0)>0.0)
	{
		*sxr=(*x+S)/(*z/-K+1.0)+xcent-S;
		*sxb=(*x-S)/(*z/-K+1.0)+xcent+S;
		*sy = *y   /(*z/-K+1.0)+ycent;
		cursize=10/(*z/-K+1.0);
	}
	else
		return(-1);
	row=*sy;
	col=(*sxr+*sxb)/2.0;
	rowo=row;
	colo=col;
	cursizeo=cursize;
	mouse_move_cursor(row,col);
/*	mouse_information(&right,&left,&row,&col);*/
	tcursor(0,*sxr,row,cursize,MouseBuffer);
	cursor(0,*sxr,row,RED,cursize);
	tcursor(0,*sxb,row,cursize,MouseBuffer+50);
	cursor(0,*sxb,row,BLUE,cursize);
	for(;;)
	{
		mouse_information(&right,&left,&row,&col);
		if(row!=rowo||col!=colo)
		{
			uncursor(0,*sxb,rowo,cursize,MouseBuffer+50);
			uncursor(0,*sxr,rowo,cursize,MouseBuffer);
			if(left>0)
				*z+=row-rowo;
			else
				*y+=row-rowo;
			*x+=col-colo;
			if((*z/-K+1.0)>0.0)
			{
				*sxr=(*x+S)/(*z/-K+1.0)+xcent-S;
				*sxb=(*x-S)/(*z/-K+1.0)+xcent+S;
				row=*sy = *y   /(*z/-K+1.0)+ycent;
				cursize=10/(*z/-K+1.0);
				mouse_move_cursor(row,col);
			}
			else
				return(-1);
			tcursor(0,*sxr,row,cursize,MouseBuffer);
			cursor(0,*sxr,row,RED,cursize);
			tcursor(0,*sxb,row,cursize,MouseBuffer+50);
			cursor(0,*sxb,row,BLUE,cursize);
			colo=col;
			rowo=row;
			cursizeo=cursize;
		}
		if(right>0)
		{
			uncursor(0,*sxb,rowo,cursize,MouseBuffer+50);
			uncursor(0,*sxr,rowo,cursize,MouseBuffer);
			do
			{
				mouse_information(&right_down,&left_down,&row,&col);
				if(right_down>0)
					right=right_down;
			}while(right_down!=0);
			return(val);
		}
	}






}


/**************************************************************************
**
**
**
************************************************************************* */

int video_on()

{
	int i,row;

	i=GetVideoBoardID();
	if(i==0)
		VideoType='E';
	else if(i==1&&(VideoType=='X'||VideoType=='Y'))
		VideoType='V';
	if(VideoType=='X')
	{
		row=SetVideoMode(480,&B_Id);
		if(B_Id.color==256&&B_Id.row==480)
		{
			ScreenXs=640;
			ScreenYs=480;
		}
		else if(B_Id.color==256&&B_Id.row==400)
			VideoType='Y';
		else
			VideoType='V';
	}
	if(VideoType=='Y')
	{
		row=SetVideoMode(400,&B_Id);
		if(B_Id.row<400)
			VideoType='V';
		else
		{
			ScreenXs=640;
			ScreenYs=row;
		}
	}
	if(VideoType=='V')
	{
		row=SetVideoMode(0x12,&B_Id);
		if(B_Id.row!=480||row!=480||B_Id.color!=16||B_Id.col!=640)
			VideoType='E';
		else
		{
			ScreenXs=640;
			ScreenYs=480;
		}
	}
	if(VideoType=='S')
	{
		row=SetVideoMode(0x13,&B_Id);
		if(B_Id.row!=200)
		{
			SetVideoMode(0,&B_Id);
			printf("Could not boot color board.\n");
			exit(0);
		}
		ScreenXs=320;
		ScreenYs=200;
	}
	if(VideoType=='E')
	{
		row=SetVideoMode(0x10,&B_Id);
		if(B_Id.row!=350||row<350||B_Id.col!=640)
		{
			SetVideoMode(0,&B_Id);
			printf("Could not boot color board.\n");
			exit(0);
		}
		ScreenXs=640;
		ScreenYs=350;
	}
	if(B_Id.row<0)
	{
		SetVideoMode(0,&B_Id);
		printf("Could not boot board -- row = %d\n\n",row);
		exit(0);
	}
	set_lut();
}


/**********************************************************************
**
**
**
**********************************************************************/

int video_off()
{
	SetVideoMode(0);
}


/********************************************************************
**
**
**
********************************************************************* */

int set_lut()

{
	int i;
	struct Color lut[256];

	for(i=16;i<256;i++)
	{
		lut[i].r=i;
		lut[i].g=i;
		lut[i].b=i;
	}
	for(i=0;i<16;i++)
	{
		lut[i].r=Vcolor[i].r;
		lut[i].g=Vcolor[i].g;
		lut[i].b=Vcolor[i].b;
	}
	WritePalette(lut); 
}
                 