/*  --- Copyright University of Sussex 1986.  All rights reserved. ---------
 >  File:           C.all/lib/turtle/active.p
 >  Purpose:        turtle program to drive a VDU screen (vt-52 or v200)
 >  Author:         Steven Hardy (originally), July 1980 (see revisions)
 >  Documentation:  HELP * ACTIVE
 >  Related Files:	LIB * V200GRAPH
 */

;;; NB "uses active" can't be used to load this because "active" is now
;;; a syntax word - Sept 1986

;;; A space between the bottom of
;;; the picture and the bottom of the screen is reserved for commands,
;;; and scrolling is handled by the program instead of the terminal.
;;; For this purpose make the picture not more than about 15 high.
;;; Scrolling is defeated by
;;; local 'wrap-around', if lines of more than 79 characters are typed.
;;; Setpop will redraw picture (e.g. ^X)

;;; The function cursor(x,y) shows the cursor at (x,y)
;;; function move(n) moves the cursor continuously n steps

section;

vars Vpoint;
unless isprocedure(Vpoint) then
	popval([lib v200graph;])     ;;; get functions to drive Visual 200
endunless;

uses turtle;

;;; Picture bounds: set in display and newpicture
vars Xsize Ysize;

define procedure Outpt(x,y);
	lvars x,y;
	;;; Like Vpoint in /usr/lib/v200.p, but uses turtle coords, not screen co-ords.
	Vpoint(x,Ysize-y)
enddefine;

define procedure Outchar(p);
	;;;make sure only a character is output, corresponding to p
	lvars p;
	rawcharout(
	if  isword(p) then  subscrw(1,p)
	elseif  isinteger(p) and 0 fi_<= p and p fi_<= 9 then `0` fi_+ p
	else `?`
	endif)
enddefine;


vars activesetpop;
define sysdisplay();
	lvars x, y, p;
	define prmishap();
		rawoutflush();
		sysprmishap(); exitfrom(activesetpop);
	enddefine;
	sysflush(popdevout);     ;;; flush charout buffer
	true -> Displayed;
	pdprops(picture)(3) + 1 -> Xsize;
	pdprops(picture)(5) ->> Ysize->Scrolrow;
	;;; clear the picture area
	fast_for y from 0 to Ysize do Vcl(y) endfast_for;
	for Ysize->y step y fi_- 1->y till y==0 do
		for 1->x step x fi_+ 1->x till x==Xsize do
			picture(x, y) -> p;
			unless  p == space then
				Outpt(x,y);
				Outchar(p);
			endunless
		endfor
	endfor;
	Scroll();
	rawoutflush();
enddefine;
vars display; sysdisplay -> display;


define Active(p, x, y, f);
	;;; f is the updater of picture, p the new contents for picture(x,y)
	lvars p,x,y,f;
	pdprops(picture)(3) -> Xsize;
	pdprops(picture)(5) ->> Ysize->Scrolrow;
	f(p, x, y);
	Outpt(x,y);
	Outchar(p);
	true -> Displayed;
enddefine;

vars NEWPICTURE;
unless NEWPICTURE.isprocedure then  newpicture -> NEWPICTURE endunless;

;;; some functions for 'movies' made with the cursor

vars cursorcount;
1 -> cursorcount;
	;;; make this bigger to slow down move

define procedure cursor(x,y);
	;;; Show the cursor at picture location x,y
	lvars x,y;
	repeat cursorcount times Outpt(x,y) endrepeat;
	true -> Displayed;
enddefine;

define move(x);
	;;; for dynamic display with cursor
	lvars x;
	cursor(xposition,yposition);
	repeat x times
		jump(1); cursor(xposition,yposition)
	endrepeat;
enddefine;

define newpicture(x, y);
	lvars x,y;
	NEWPICTURE(x, y);
	Active(%updater(picture)%) -> updater(picture);
	x ->Xsize;
	y ->>Ysize->Scrolrow;
	sysdisplay();
enddefine;

procedure; vars sysdisplay; identfn -> sysdisplay;
	newpicture(50,15);
end.apply;


define activesetpop;
	vars interrupt;
	setpop -> interrupt;
	Vout -> cucharout;
	define prmishap;
		vars cucharout;
		charout -> cucharout;
		Scroll();
		rawoutflush();
		sysprmishap();
		pr('\n\nTYPE CTRL-C to CONTINUE'); charin();
	enddefine;
	sysdisplay();
	;;; prevent charout messing up format.
	100000000 ->> poplinewidth  -> poplinemax;
	pop11_compile(Vin);
enddefine;

activesetpop -> popsetpop;

define 1 startactive;
	activesetpop -> popsetpop;
	Scroll();
	popsetpop();
enddefine;

define 1 endactive;
	identfn -> popsetpop;
	if cucharin = Vin then exitfrom(pop11_compile) endif;
enddefine;

vars macro activemode;
[;vars cucharin cucharout proglist;
  Vin -> cucharin;
  Vout -> cucharout;
  pdtolist(incharitem(cucharin)) -> proglist;] -> nonmac activemode;

pr('DO CTRL-C or \'ACTIVE;\' TO SET UP ACTIVE TURTLE');

endsection;

nil -> proglist;
/*  --- Revision History ---------------------------------------------------
--- Aaron Sloman, Nov 21 1986
	Changed "active" to "startactive" and updated help file.
--- Aaron Sloman, Aug 19 1986
	Some tidying up. replaced spaces with tabs.
--- Roger Evans, June 1983 - addition of macro ACTIVEMODE plus renaming of
	display procedure as sysdisplay - the default value of display (but can
	set display to identfn for better compatibility with turtle). Note -
	activesetpop calls sysdisplay not display!
--- Aaron Sloman, 1982 - modified to use v200graph properly.
 */
