program Raster_and_music;
var
/*
NB: This no longer appears to work
*/

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


@ignoremethod "init16x8div"

@ignoremethod "init16x8mul"

@ignoremethod "init8x8div"

@ignoremethod "initrandom"


const screen:address = 2000;
const chrset:address = $1c00;

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

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

@startblock $2400 music
	@include "resources/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.

