#resize TRUE
#jumptomain SHORT

#define SCREENSIZE 64000
#define MEMBLOCKSIZE SCREENSIZE/16

word flowers;
word screen;

void setrgbpalette(char color,r,g,b);

float const1=0.5;
float const2=1.0;
float const3=1.5;
float const4=2.0;
float const5=90;
float const6=120;
float const7=96;
float const8=192;
float const9=8;
float const10=16;
float const11=0.02;

void main()
long offset1,offset2,offset3;
long x,y;
float time=0;
char c1,c2,c3;
{
	screen=GETMEM(MEMBLOCKSIZE);
	flowers=GETMEM(MEMBLOCKSIZE*4);
	initflower();
	@SETVIDEOMODE(byte 0x13);
	setcolortable();
	while(!KBHIT())
	{
		asm{
			fld time
			fmul const1
			fadd const2
			fsin
			fmul const6
			fistp x
			fwait
		}
		x+=160;
//		x=160+120*sin(time*0.5+1.0);
		asm{
			fld time
			fmul const3
			fadd const4
			fcos
			fmul const5
			fistp y
			fwait
		}
//		y=100+90*cos(time*1.5+2.0);
		EAX=y+100<<7;
		EBX=EAX<<2;
		offset1=EAX+EBX+x;
		asm{
			fld time
			fmul const2
			fadd const3
			fsin
			fmul const6
			fistp x
			fwait
		}
		x+=160;
//		x=160+120*sin(time*1.0+1.5);
		asm{
			fld time
			fmul const4
			fadd const1
			fcos
			fmul const5
			fistp y
			fwait
		}
//		y=100+90*cos(time*2.0+0.5);
		EAX=y+100<<7;
		EBX=EAX<<2;
		offset2=EAX+EBX+x;
		asm{
			fld time
			fmul const3
			fadd const4
			fsin
			fmul const6
			fistp x
			fwait
		}
		x+=160;
//		x=160+120*sin(time*1.5+2.0);
		asm{
			fld time
			fmul const1
			fadd const2
			fcos
			fmul const5
			fistp y
			fwait
		}
//		y=100+90*cos(time*0.5+1.0);
		EAX=y+100<<7;
		EBX=EAX<<2;
		offset3=EAX+EBX+x;
		FS=screen;
		for(ECX=0;ECX<200;ECX++){
			for(EDX=0;EDX<320;EDX++){
				EAX=ECX<<7;
				EBX=EAX<<2;
				EDI=EAX+EBX+EDX;
				ES=flowers;
				ESI=EDI+offset1;
				EAX=ESI>>16;
				AX=AX<<12;
				BX=ES;
				AX+=BX;
				ES=AX;
				c1=ESBYTE[SI];
				ES=flowers;
				ESI=EDI+offset2;
				EAX=ESI>>16;
				AX=AX<<12;
				BX=ES;
				AX+=BX;
				ES=AX;
				c2=ESBYTE[SI];
				ES=flowers;
				ESI=EDI+offset3;
				EAX=ESI>>16;
				AX=AX<<12;
				BX=ES;
				AX+=BX;
				ES=AX;
				c3=ESBYTE[SI];
				AX=CX<<6;
				BX=AX<<2;
				SI=AX+BX+DX;
				FSBYTE[SI]=c1+c2+c3;
			}
		}
		asm{
			fld time
			fadd const11
			fstp time
		}
		copyvirtualscreen();
	}
	@BIOSREADKEY();
	@SETVIDEOMODE(byte 0x3);
}

void initflower()
long x,y;
long v,u;
{
	for(y=-200;y<200;y++){
		for(x=-320;x<320;x++){
			v=x*x;
			u=y*y+v;
			asm{
				fild u
				fsqrt
				fild x
				fild y
				fpatan
				fld st(0)
				fmul const10	//*16
				fcos
				fmul const7 //*96
				fxch st(1)
				fmul const9 //*8
				fsin
				fmul const8 //*192
				faddp st(1)
				fldpi
				fdivp st(1)
				faddp st(1)
				fistp v
				fwait
			}
			ES=flowers;
			ESI=y+200*640+x+320;
			EAX=ESI>>16;
			AX=AX<<12;
			BX=ES;
			AX+=BX;
			ES=AX;
			ESBYTE[SI]=v;
		}
	}
/*			flowers[(x+320)+(y+200)*640]=0.75*cos(16*atan2(y,x))*256/TWO_PI+
																	 1.5*sin(8*atan2(y,x))*256/TWO_PI+
																	 sqrt(x*x+y*y);*/
}

void copyvirtualscreen()
{
	@WAITVSYNC();
	copyfard(0xA000,0,screen,0,16000);
}

void setcolortable()
int i=0;
int c;
{
	do{
		c=i*3/2;
		setrgbpalette(i,63,c,0);
		i++;
	}while (i<43);
	do{
		c=i-43*3/2;
		setrgbpalette(i,63-c,63,0);
		i++;
	}while (i<85);
	do{
		c=i-85*3/2;
		setrgbpalette(i,0,63,c);
		i++;
	}while (i<128);
	do{
		c=i-128*3/2;
		setrgbpalette(i,0,63-c,63);
		i++;
	}while (i<171);
	do{
		setrgbpalette(i,c,0,63);
		i++;
	}while (i<213);
	do{
		c=i-213*3/2;
		setrgbpalette(i,63,0,63-c);
		i++;
	}while (i<256);
}

void setrgbpalette(char color,r,g,b)
{
	DX=0x3C8;
	outp(color,DX);
	DX++;
	outp(r,DX);
	outp(g,DX);
	outp(b,DX);
}

