program Tutorial7;
var
	xscroll, x,y, val, i: byte;
	textshift, wobbleShift: byte;
	text: string =("                  I WANT SMOOTH HARDWARE SCROLLING IRL      ",
	           "     THIS SCOLLING DOESN'T END PROPERLY     ");

	time: byte;
	colorIndex: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);
	image_color: IncBin("images/octopus_color.bin", $5000);
	
	image_data: IncBin("images/octopus_data.bin", $6000);


procedure PrintText();
begin
	// move to screen position
	moveto(2,23, $04);
	fill(screenmemory,0,55);
	
	// print text
	PrintString(text, textshift, 36);
	// move cursor to color
	moveto(2,23, $D8);
	colorIndex:=colorIndex+1;

	for x:=0 to 36 do begin
		val:=fade[((x+colorIndex)/2)&15];
		screenmemory[x]:=val;

	end;
end;

interrupt UpdateRaster();
begin
	startirq(0);
	// increase global time
	inc(time);
	// increase text shift only every 8th step due to scrolling 

	x:=time&7;
	if time&7=0 then begin
		textshift:=textshift+ 1;
	end;
	// increase scrolling 
	asm(";collect");
	xscroll:=(xscroll+1)&7;
	y:=0;
	// Optional wobble effect on the main image	
	wobbleShift:=sine[time*2];
	while y<200 do begin
		// Keep until rasterline hits text line
		y:=peek(RASTERLINE_POS, 0);
		val:=sine[y*8 + wobbleShift]/32;
		scrollx(val);
		waitnoraster(1);
	end;

	// Wait for text line raster
	waitForRaster(220);
	// switch bank to 0
	setbank(VIC_BANK0);
	// switch graphics mode
	
	setregularcolormode();
	settextmode();
	// set the vic's data to point to the custom character set
//	poke(VIC_DATA_LOC, 0, $1A);
	SetCharsetLocation($2800);

	// scroll this area
	Scrollx((8-xscroll)&7);
	// wait for rasterline to hit near bottom
	waitForRaster(250);
	// switch back to bank 1 and set multi color mode again
	
	setbank(VIC_BANK1);

	Scrollx(0); 
	setmulticolormode();
	setbitmapmode();

	// Make sure we point the vic to the bitmap data
	poke(VIC_DATA_LOC, 0, $18);
	// finally, update the text
	PrintText();
	// call system kernel
	closeirq();	
end;


procedure Setup();
begin
	ClearScreen($20, SCREEN_CHAR_LOC);
	Poke(SCREEN_BG_COL, 0, BLACK);
	Poke(SCREEN_FG_COL, 0, BLACK);

	setbank(VIC_BANK1);
	copyimagecolordata(image_color,1);


end;

begin


	Setup();
	preventirq();
	setmemoryconfig(1,0,0);
	DisableCIAInterrupts();
	RasterIRQ(UpdateRaster(), 0,0);
	enablerasterirq();
	enableirq();
	

	Loop();

end.
