program Tutorial24_screenClearer;
var  
   	i,j,k,l,m,c2x: byte; 


	zp,p1,l1,tp: pointer;

	temp:array[255] of byte;




	colorsc1 : array [16] of byte = (  0, 11,6,12,12,4,14,15,7,1,7,6,   6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6  );
	colorsc2 : array [16] of byte = (  0, 11,6,12,12,4,14,14,14,   14,14,14,14,14,14,14,14,14  );
	orgperlin:incbin("data/perlin16.dat");	
	



procedure CalculateNewCirclesInit();
begin
	zp:=$5000;
	
	for j:=0 to 25 do begin
		for i:=0 to 40 do begin
			m:=j&15;
			l:=i&15+m*16;	
			k:=orgperlin[l]/8;
			m:=(j/2);
			l:=(i/2)+m*16;	
			k:=k+orgperlin[l+133]/8;
//			k:=k+sine[i+j]/8;
			zp[i]:=k/2+8;

		end;
		inczp(zp,40);
	end;
end;


procedure PerformPerlin();
begin
	zp:=$0400;
	p1:=$D800;
	l1:=$5000;
	for j:=0 to 25 do begin
		for i:=0 to 40 do begin
			asm("
				ldy i
				lda (l1),y
				beq pp_cont
				sec
				sbc #1
				sta (l1),y
				tax
				lda colorsc1,x
				ldy i
				sta (p1),y
pp_cont:
");			
		end;
		inczp(p1,40);
		inczp(l1,40);
//		inczp(zp,40);
	end;

end;




begin
	fill(^$5800,255,8);
	setcharsetlocation($5800);
	Clearscreen(BLUE,^$D800);
	Clearscreen(0,^$4400);
	setbank(VIC_BANK1);


	CalculateNewCirclesInit();
	c2x:=16;
	while(1=1) do begin
		waitforraster(0);
		if (c2x<127) then begin
			SCREEN_BG_COL:=colorsc2[c2x];
			dec(c2x);

		end;
		PerformPerlin();
	end;

end.
