#include <stdio.h>
#include <snd.h>
#include <egb.h>

#include <\src\ysuty2\ysuty2.h>

char EGB_work[EgbWorkSize];
char SND_work[16384];


void line(int x1,int y1,int x2,int y2)
{
	int rf,x,y,a,b,vx,vy;

	rf=0;
	if     (x2-x1==0)vx= 0;
	else if(x2-x1< 0)vx=-1;
	else if(x2-x1> 0)vx= 1;

	if     (y2-y1==0)vy= 0;
	else if(y2-y1< 0)vy=-1;
	else if(y2-y1> 0)vy= 1;

	a=_abs(x2-x1);
	b=_abs(y2-y1);

	x=x1;
	y=y1;

	YGB_pset(EGB_work,x,y);

	if     (a>b )goto xlarge;
	else if(a<b )goto ylarge;
	else if(a==0)goto vertical;
	else if(b==0)goto horizontal;
	else if(a==b)goto midium;
	return;

	xlarge:
	while(x!=x2 || y!=y2)
	{
		x+=vx;
		rf-=b;
		if(rf<0)
		{
			y+=vy;
			rf+=a;
		}
		YGB_pset(EGB_work,x,y);
	}
	return;

	ylarge:
	while(x!=x2 || y!=y2)
	{
		y+=vy;
		rf+=a;
		if(rf>0)
		{
			x+=vx;
			rf-=b;
		}
		YGB_pset(EGB_work,x,y);
	}
	return;

	vertical:
	while(y!=y2)
	{
		y+=vy;
		YGB_pset(EGB_work,x,y);
	}
	return;

	horizontal:
	while(x!=x2)
	{
		x+=vx;
		YGB_pset(EGB_work,x,y);
	}
	return;

	midium:
	while(x!=x2 || y!=y2)
	{
		x+=vx;
		y+=vy;
		YGB_pset(EGB_work,x,y);
	}
	return;

}



void main()
{
	int pad,x1,y1,x2,y2;

	SND_init(SND_work);
	YGB_init(EGB_work,17,17);

	pad=255;
	while(pad==255)
	{
		SND_joy_in_2(0,&pad);

		x1=rand()%512;
		y1=rand()%480;
		x2=rand()%512;
		y2=rand()%480;

		if     (x1==x2 || y1==y2)YGB_color(EGB_work,31);
		else if(_abs(x1-x2)==_abs(y1-y2))YGB_color(EGB_work,31*32);

		line(x1,y1,x2,y2);
		YGB_box(EGB_work,x1-1,y1-1,x1+1,y1+1);
		YGB_box(EGB_work,x2-1,y2-1,x2+1,y2+1);
	}

	SND_end();
}
