program Tutorial3_plasma;

@projectsettings "petmodel" "8032"

var
	// some plasma variables
	c,val,c2x, c2y,ax, ay : byte;
	x,y : byte;
	colorP: pointer;	

// charset will be placed at $2000 in bank 1	
@define charsetLocation $2000
 // look in the character set
@define baseCharacter $68



	// mini sine table
    siny : array[screen_height] of byte; 
	sinx : array[screen_width] of byte; 


	// Lookup table for division by 16
	lookupDiv : array[256] of byte = buildtable("i/32");

   vals : array[8] of byte = ($20, $2E,  $A6, $A0, $A0, $A6, $2E, $20);




// Plasma procedure

procedure Plasma();
begin
	c2x:=ax;
	c2y:=ay;
	
	// Set up y-sine table
	for x:=0 to screen_height do begin 
		siny[x]:=  sine[c2x] + sine[c2y];
		c2x:=c2x+4;
		c2y:=c2y+9;
	end;

	ax:=ax+3;
	ay:=ay-5;

	// Set up x-sine table
	for x:=0 to screen_width do begin 
		sinx[x] := sine[c2x] + sine[c2y];
		c2x:=c2x+3;
		c2y:=c2y+7;

	end;
	// Move cursor to (1,y) on $0400 on bank 1
	moveto(0,screen_height/4, hi(screen_char_loc));
	// moveto could also be replaced with : screenmemory:=$0400 + @y_start*40;
	
	
	for y:=0 to screen_height/2 do begin
		val:=siny[y];
		for x:=0 to screen_width do begin
		
			// here, we take (sin[x]+val) and divide by 16. However, since this is a slow procedure,
			// we have created a lookup table instead!
			c:=lookupDiv[ (sinx[x] +val) ];
			// Set the screen memory
			screenmemory[x]:=vals[c];

		end;
		// Increase screen memory pointer by 40 (next row)
		screenmemory:=screenmemory+screen_width;
		// Increase color pointer by 40 (next row)
		colorP:=colorP+screen_width;
	end;

end;



begin
	// Set charmap location at $2000
	ax:=1;
	ay:=5;

	// Clear screen and color memory
	ClearScreen($20, screen_char_loc);
	// Main loop
	while (true) do begin
		Plasma();
	end;

end.
