program Tutorial8;
var  
   index, time, a, val, color, colorShift,i,j,k : byte; 
   fade : array [16] of byte = (11,6,6,12,4,14,15,1,1,15,14,4,12,6,6,11); 
   mainChar: IncBin("charsets/tutorial3.bin","$27FF");

// Include methods for banking

@include "../../RasLib/banking.ras"

// include vscroll methods
@include "../../RasLib/verticalscrolling.ras"


// Print a single line at the top of the screen on the current bank
procedure PrintLine();
begin
	j:=sine[time*1]/8;
	k:=sine[j*time/64]/2;
	for i:=0 to 40 do begin
		val:=val/32+64 - 4;
		// Chose the center of a nice sine functions so that the background looks
		// like a canal		
		val:=sine[(i*4 + j+k)+30]/16 + 64 - 4;


		if val<64 then
			val:=$20;

		if g_currentBank=1 then
			poke(^$0400, i, val)
		else
			poke(^$4400, i, val);
	
	end;

end;

// Print a line of colors at $D800
procedure printColor();
begin
	colorShift:=0;
	color:=fade[(time/16 + colorShift)&15];
	for i:=0 to 40 do begin
		poke(^$D800,i,color);
	end;

end;


// Raster update
interrupt Update();

begin
	startirq(1);
	asm(";crash");
	hideBorderY(1);
	time:=time+1;
	a:=(time*4)&1;

	if a=0 then begin
		VerticalScroll();
		
		// Only print a new color line when scrolling index is 0
		if g_vscroll=0 then 
			printColor();
		
		// Only print a new line when scrolling index is 7
		if g_vscroll=7 then 
			PrintLine();

	end;


	closeirq();
end;

procedure Setup();
begin
	copyfullscreen(^$27FF,^$27FF+^$4000);

	poke(SCREEN_BG_COL, 0, 0);
	poke(SCREEN_FG_COL, 0, 0);

	hideBorderY(0);
	g_dataLoc := $1a;
end;


begin
	time:=0;	
	Setup();
	preventirq();
	StartRasterChain(Update(), 0,1);
	Loop();
end.
