program Tutorial_4_raster_and_music;
var
/*
Note: TRSE does not yet support the Vic-20 natively, so most of the methods
that are c64-specific (rasters, sprites etc) are disabled in VIC 20 mode.

We hope to add more native support (built-in methods) for the VIC later on!
	

*/
// Place to dump generated data.



@ifdef ORGASM
@error "Unfortunately, this program can not be assembled with OrgAsm. Use DASM instead. "
@endif


@ignoremethod "init16x8div"

@ignoremethod "init16x8mul"

@ignoremethod "init8x8div"

@ignoremethod "initrandom"


@define screen 2000

// We actually multiply eightbit
//@ignoremethod "initeightbitmul"

	i,j,time,time4,t,k : byte;
	
	charset : IncBin("charsets/charset_8x8.bin", $1C00);
	zp1,zp2 : pointer;
	text1 : cstring = ("YAAAY VIC 20");
	perlin : incbin("data/perlin16.dat");
	

@startblock $2400 music
	@include "music/mystic_runner.ras"
@endblock

	
procedure ClearScreen();
begin
	zp1:=SCREEN_CHAR_LOC;
	zp2:=SCREEN_COL_LOC;
	for i:=0 to 23 do begin
		fill(zp1,$20,22);
		if (i=8 or i=12) then
			fill(zp2,$03,22)
		else
			fill(zp2,$07,22);
		inczp(zp1,22);
		inczp(zp2,22);
	end;
	
end;



begin
	DisableVIC20Irq();
	SCREEN_BG_COLOR:=BLACK;

	setscreenlocation($1000);
	setcharsetlocation($1C00);
	Music();

	ClearScreen();
	MoveTo(5,10,$10);
	printstring(text1,0,40);
	MoveTo(0,8,$10);
	
	for i:=0 to 0 do begin
		j:=peek(perlin, i);
		j:=j/16;
		poke(perlin,i,j);
	end;
	// Start playing music
	asm("
		jsr pl_Init
	");

	// Raster loop
	while (1=1) do begin
		time:=time+1;
		time4:=time4+4;
		t:=sine[time4]/4+32;
		

		asm("
			jsr pl_Play
		");
		
		waitforraster(t);
		for i:=0 to 32 do begin
			poke(SCREEN_BG_COLOR,0,i&7);
			waitnoraster(1);
			
			
		end;
		poke(SCREEN_BG_COLOR,0,%0001000);
		
		for i:=0 to 22 do begin
			j:=(peek(perlin,i+time));
			screenmemory[i]:=j+ 64;
			j:=(peek(perlin,i-time));
			screenmemory[i+88]:=j+ 64;
		end;
		
		
	end;
	
	Loop();
	
	
end.

