program Boxes;

@projectsettings "petmodel" "8032"

var  
	saddr: array[screen_height] of integer; // Screen address table
	
	box: array[8] of byte = ($55, $43, $49, $5d, $4b, $43, $4a, $5d);
	i,x,y,dx,dy,t: byte = 0;

/*

	This tutorial draws boxes on the screen using the "drawcolortextbox" method. This method
	requires pointers to screen address tables, which are set up using the createaddresstable methods.

	createaddresstable lets the user create a lookup table instead of using multiplications. example:
	to find the adress on the screen, a "address:=position + y*40;" multiplication is required. An adress
	table eliminates the need of having this multiplication, and relies solely on a lookup.  	

*/

	
begin
	// Set screen background/border color
	
    clearscreen($20, screen_char_loc);
	// Sets up the address tables for the screen & color memory    
	createaddresstable(#saddr,screen_char_loc,screen_width,screen_height);

	// dx and dy are initialized to 1
	dx:=1;
	dy:=1;
	while (true) do begin
		// Make sure we only draw 1 box per frame
		// Add the delta dx and dy to x and y
		for i:=0 to 255 do wait(2);
		x:=x+dx;
		y:=y+dy;
		// Flip dx and dy when borders are reached
	    case x of
		    	71: dx := -1;
		    	0: dx := 1;
		end;
	    case y of
	    		20: dy := -1;
	    		0: dy := 1;
		end;
		// Draw two boxes in opposing corners
		drawtextbox(#saddr, #box, x, y, 9, 5);
		drawtextbox(#saddr, #box, 31 - x, 20 - y, 9, 5);
	end;
end.