


procedure RenderTwisterScreen();
begin
//	moveto(0,4,$10);
	screenmemory:=$1000;	
	k:=64;
	for y:=0 to 23 do begin
		for x:=0 to 8 do begin
			screenmemory[x+12]:=k;
			inc(k);
		end;
		screenmemory := screenmemory +22;

	end;

end;


procedure RenderTwister();
begin
	//zp:=@twister;
	PrintText();

	vscroll:=vscrollTarget;
	if (transition=1) then ClearWithStripes();


//	if (dy<>22 and dx<>8) then
	if (startEffect=1) then begin
		if (time&3=3) then
		if (dy<>23) then inc(dy);	
/*		inc(dx);
		if (dx=8) then begin
			dx:=0;
			inc(dy);
			
		end;
*/
	end;


	zp2:=$1400+64*8;
	t1:=time*3+0 + sine[time];
	t2:=-time*2+99;
	k:=time*8;

	xp:=0;


//	if (startEffect=1) then
	if (dy<>0) then
	for y:=0 to dy do begin
		for x:=0 to 8 do begin
//			l:=0;
			tt:=peek(^@sine2,t1+xp);
			m:=peek(^@sine2,t2-xp);
//			m:=sine2[tt+m];
//			tt:=(tt-m-k)&%11111110;
			tt:=(m+tt-k)&%11111110;

			asm("
				ldx tt
				lda @table,x
				sta zp
				inx
				lda @table,x
				sta zp+1

			");


/*			for k:=0 to 8 do begin
				zp2[l]:=zp[l];
				l:=l+8;
			end;*/



			asm("
			ldx #8
			clc

blah:
			ldy eights,x
			lda (zp),y
			sta (zp2),y
			dex
			bne  blah
			");
			zp2:=zp2+1;
			inc(xp);
//			inc(xp);
//			xp:=xp+3;
		end;
		zp2:=zp2+8*7;
	end;

end;

procedure InitTable();
begin
	zp:=@unpackAddress;
	p1:=@table;
	k:=0;
	for i:=0 to 128 do begin
		j:=i*2;
		asm("
			ldy j
			lda zp
			sta (p1),y
			iny 
			lda zp+1
			sta (p1),y
		");	
		zp:=zp+1;
		inc(k);
		if (k=8) then begin
			k:=0;
			zp:=zp+7*8;
		end;
		if (i=63) then 	zp:=@unpackAddress;

	end;

end;

procedure InitSine();
begin
	for i:=0 to 0 do poke(^@sine2,i,sine[i]/4);

end;



procedure InitTwister();
begin
	SCREEN_BG_COLOR:=PURPLE + 16*PURPLE;
	TransitionH(BLACK, 1, 1);
	
//	vscroll:=250;
//	blockmemcpy(^$8000,^$1400,8);
	charsetShift:=34;
	dx:=0;
	InitCharset();
	SCREEN_TEXT_WIDTH:=22;
	SCREEN_TEXT_HEIGHT:=46;
//	memcpy(^$8000,0,^$1400,0);
	i:=AUX_COLOR_AND_VOLUME;
	i:=i&15;
	AUX_COLOR_AND_VOLUME:=6*16 | i;
//	setcharsetlocation($1400);
	SCREEN_BG_COLOR:=PURPLE+8;

//	decrunch(charsetc);
	fill(^$1400+^8*^$20,0,8);
	dy:=0;
	dx:=0;
	decrunch(#twisterc);
//	fill(^$1500,0,0);
	fill(^$1600,0,0);
	fill(^$1700,0,0);
	fill(^$1800,0,0);
	fill(^$1900,0,0);
	fill(^$1A00,0,0);
	fill(^$1B00,0,0);

	InitTable();
	InitSine();

	textP:=#textTwister;
	fill(^$9400,YELLOW+8,0);
	fill(^$9500,YELLOW+8,0);
	fill(^$1000,$20,0);
	fill(^$1100,$20,0);
	RenderTwisterScreen();

end;


