program Example1;
var  
   	j,val,k,col,l,c1,m,x,i,shift: byte = 0; 
   	xx,yy,dy,dx,tangent,v2,v1 : byte;
   	radial,r2:integer;
	tanP, radP : pointer;

	perlin : incbin("data/perlin16.dat");

/*

	Commander X16 example. 
	Disclaimer: Support for the X16 & methods & this example was created the very same
	day that TRSE 0.08 was released. Expect a huge update for TRSE 0.09.

	No help text yet, no time. 
	
*/


// Positions for the generated data
@define tanData $2000
@define radData $3400
@define margin 0

// Set som funky palette
procedure SetPalette();
begin
	Initpalette(0);
	for i:=0 to 256 do 
		SetColor(i/16,i/30,i/20);

end;

procedure SetupTables();
begin
	tanP:=@tanData;
	radP:=@radData;
	for i:=0 to screen_height do begin
		x:=0;
		for j:=0 to screen_width do begin

			tangent:=(atan2(40,j,30,i));
				
			dx:=abs(40-j);
			dy:=abs(30-i);
			
			radial:=dx*dx;
			r2:=dy*dy;
			radial:=radial+r2;
			radial:=radial/20;
			m:=sqrt(radial);
			m:=m+2;
			m:=256/m;
			
			tanP[j]:=(tangent/4)&15;
			radP[j]:=(m&15)*16;
		end;
		tanP:=tanP+80;
		radP:=radP+80;
			
	end;


end;

// Fill screen with a specific value + color
procedure FillScreen(val,color:byte);
begin
	SetVeraBank(0);
	SetVeraIncrement(2);
	for i:=0 to screen_height do
		for j:=0 to screen_width do
			verapokedual(i,j*2,val,color);

end;

// Render the tunnel!
procedure RenderScreen();
begin
	tanP:=@tanData;
	radP:=@radData;
	SetVeraIncrement(2);
	SetVeraBank(0);

	for i:=0 to screen_height do begin
		vera_addr_mid:=i; // y
		vera_addr_lo:=@margin+1; // x
		x:=@margin/2;

		for j:=@margin to screen_width-@margin/2 do 
		begin
			xx:=(tanP[j]);
			yy:=(radP[j]+v2);
			col:=perlin[xx+yy];				
			VERA_DATA1:=col;
		end;
		tanP:=tanP+80;
		radP:=radP+80;
			
	end;

end;


begin
	// [ video mode, layer (1 or 2), enabled=1, disabled =0 ]
	SetVideoMode(1,1,true);

	SetPalette();	
	SetupTables();
	
	FillScreen($A0,0);

	while (true) do 
	begin
		RenderScreen();
		val:=val+1;
		v2:=v2+16;
	end;
	Loop();
end.
