program Tutorial_spiral;
@use "screen"
@use "memory"
var  


   	i,j,k, x,y,dx,dy, time,xx,add: byte; 
	int : integer;
	zp,zp1,zp2 : pointer;
//	cpLo, cpHi : pointer of long;

	perlin : incbin("resources/data/perlin80.dat", $5000);
	charset : incbin("resources/charsets/charset.bin", $6800);

//	music : incsid("resources/music/courier30.sid",2);
	
@define c1pos $4000
@define l1pos $4800


procedure PreCalculate();
begin
	zp1:=@c1pos;
	zp2:=@l1pos;
	xx:=255;
	for y:=0 to 25 do begin
		for x:=0 to 80 do begin
			dx:=abs(40-x);
			dx:=dx/3;
		
			dy:=abs(12-y);
			int := dx*dx + dy*dy;
			int:=int/7;
			i:=sqrt(int);
			i:=i+5;
			zp1[x]:=64/i;

			j:=atan2(40,x,24,y*2);

			zp2[x]:=j/5;

		end;
		zp1+=80;
		zp2+=80;
	end;

end;





procedure Render();
begin

	zp1:=@c1pos;
	zp2:=@l1pos;
	zp:=$0800;

	Memory::zpLo := $F800;  
	Memory::zpHi := $0001; // 32 bit addressing through pointers
	
	for y:=0 to 25 do begin
		for x:=0 to 80 do 
		begin
			i:=zp1[x]+time+zp2[x];
			j:=perlin[i]+add;
			zp[x]:=j+$40;
			Memory::Poke32(x,j);
		end;
		zp:=zp+80;
		Memory::zpLo+=80;
		zp1+=80;
		zp2+=80;
	end;


end;



interrupt Raster();
var ri : byte;
begin
	startirq(0);
	
//	call(sidfile_1_play);
	Render();
	
	inc(time);	
	closeirq();
end;


begin
	// Set 2 mhz mode
	// Enable $4K-$8K
	Screen::SetFastMode();
	Screen::Init();
	Screen::EnableVIC4();
	
//	InitSid(sidfile_1_init);
	Screen::SetCharsetLocation(#charset,0);
	
	PreCalculate();
	disableciainterrupts();
	setmemoryconfig(1,0,0);
	startrasterchain(Raster(),0,0);
	enablerasterirq();
	enableirq();
	Loop();
	
end.
