program Twister;
@use "crtc"
@use "input"
@use "compression"

var  
  	k,j,time,tt,t2,ll,frame,y: byte; 
  	
//   	data : incbin("data/twister_data.bin");
   	data : incbin("data/twister_data.bin");
   	table : incbin("data/twister_table.bin");
   	
	dst,zp,tp,p1 : pointer;
	tab : pointer of integer;   
	text : string = ("PROXIMA WHOO THIS IS A SCROLLTEXT YES IT IS ");
    sine32 : array[256] of byte = BuildTable("(Math.sin(i/256.0*Math.PI*2.0)*80+79)/7");
@export "resources/images/image1.flf" "resources/images/image1.bin" 256 
	pal : incbin("resources/images/image1_palette.bin");
	charLookup : array[32] of integer = BuildTable("(2*16*16*i)");
	frameLookup : array[16] of integer = BuildTable("(2*16*i)");

@requirefile "data/twister_data.bin" "Please run the corresponding fjong file (twister.fjo) before compiling this program"

procedure RenderTwister(tx:byte);
begin
	t2:=t2+1;
	tt:=0-tx; // Text goes up
	t2:=tx; // Twist goes down
	tp:=#text;
//	dst:=dst+xp;
	j:=0;
//		zp:=#data;
	y:=0;
	for k:=0 to 100 offpage do
	begin
		j:=sine32[(k+t2)&255]; // Twist turning
		frame:=(((tx-j))&15); // Current rotation frame
		ll:=(k-tt); // Current shift & text position in y
		j:=ll>>4; // Twister yeah!
	 	j:=(tp[j]-1)&31; // Look up current character, store in j
		tab :=#table + frameLookup[frame];
		tab :=tab +charLookup[j];
		
	
		zp :=tab[ll&15];
		zp:=#data+zp;


		// Copy 2 rows of data
		Crtc::PointToY(y);
		Crtc::sp:=Crtc::sp+36;
		memcpy(zp,Crtc::sp,8);
		y:=y+1;
		Crtc::PointToY(y);
		Crtc::sp:=Crtc::sp+36;
		memcpy(zp,Crtc::sp,8);
		y:=y+1;
		

	end;
	
end;


begin
	Crtc::InitDefault(0);
	
	time := 0;	
	Crtc::SetPalette(#pal);
	while (true) do
	begin
		RenderTwister(time);
		time:=time+1;
	end;
		
	Loop();



end.
