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

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

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

#define XBUF 16


int vx1=64;
int vy1=64;
int vx2=447;
int vy2=415;

int mnx,mny,max,may;

void circleArea(int *plg,int cx,int cy,int r)
{
	int ro,rf,mx,my,x,y;
	int *p1,*p2,*p3,*p4;

	mnx=cx-r;
	max=cx+r;
	mny=cy-r;
	may=cy+r;

	ro=r*r;
	rf=ro;
	mx=r;
	my=0;

	p1=plg+cy*XBUF;
	p2=plg+cy*XBUF;
	p3=plg+(cy-r)*XBUF;
	p4=plg+(cy+r)*XBUF;


	if(vy1<=cy && cy<vy2)
	{
		p1[0]=2;
		x=cx-r;
		if(x<vx1)x=vx1;
		p1[1]=x;
		x=cx+r;
		if(x>vx2)x=vx2;
		p1[2]=x;
	}

	while(my<mx)
	{
		y=cy-mx;
		if(vy1<=y && y<=vy2)
		{
			p3[0]=2;
			x=cx-my;
			if(x<vx1)x=vx1;
			p3[1]=x;
			x=cx+my;
			if(x>vx2)x=vx2;
			p3[2]=x;
		}

		y=cy+mx;
		if(vy1<=y && y<=vy2)
		{
			p4[0]=2;
			x=cx-my;
			if(x<vx1)x=vx1;
			p4[1]=x;
			x=cx+my;
			if(x>vx2)x=vx2;
			p4[2]=x;
		}

		rf+=(1+my+my);
		p1-=XBUF;
		p2+=XBUF;
		my++;

		if(rf>=ro)
		{
			rf+=(1-mx-mx);
			p3+=XBUF;
			p4-=XBUF;
			mx--;
		}

		y=cy-my;
		if(vy1<=y && y<=vy2)
		{
			p1[0]=2;
			x=cx-mx;
			if(x<vx1)x=vx1;
			p1[1]=x;
			x=cx+mx;
			if(x>vx2)x=vx2;
			p1[2]=x;
		}

		y=cy+my;
		if(vy1<=y && y<=vy2)
		{
			p2[0]=2;
			x=cx-mx;
			if(x<vx1)x=vx1;
			p2[1]=x;
			x=cx+mx;
			if(x>vx2)x=vx2;
			p2[2]=x;
		}
	}
}



void paint(int *plg)
{
	int y,y1,y2;
	y1=mny;
	y2=may;

	if(y2<vy1 || y1>vy2)return;

	if(y1<vy1)y1=vy1;
	if(y2>vy2)y2=vy2;

	plg+=y1*XBUF;

	for(y=y1; y<=y2; y++)
	{
		int i,x1,x2;

		for(i=0; i<*plg; i+=2)
		{
			x1=plg[1+i];
			x2=plg[2+i];
			if(x2<=vx1 || vx2<=x1)continue;
			YGB_line(EGB_work,x1,y,x2,y);
		}

		plg+=XBUF;
	}
}






void main()
{
	int pad,x,y,r,plg[480*XBUF];

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

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

		x=rand()%512;
		y=rand()%512;
		r=rand()%100;
		circleArea(plg,x,y,r);
		YGB_color(EGB_work,rand()%32768);
		paint(plg);
	}

	SND_end();
}
