/*
 *	Perlin Production Co. Ltd. September 1993 (c).
 *			GRAB0.C
 */

#include "stdio.h"
#include "wios.h"
#include "conio.h"
#include "graphics.h"
#include "grab.h"

int		src,dst;
char		*srcname,*dstname;
unsigned	scrpos;
char		scrcol[VSIZE];
char		points[MAXBAR];
char		*ptscnt;
int		p_enabled[4];
int		evenHght;
char*		modeText[3];
char*		pln_nam;

main(argc,argv)
int	argc;
char	**argv;
{
	WINDOW		whelp;
	int		xs,ys,xe,ye,sav_format,exitcode;
	char		key,mode;
	unsigned	plane;

	init_wns();
	def_wn(0,0,MAXX,4,S_BORD,HELP);
	sw_shadow(NO_SHADOW,HELP);
	cls();
	if ( argc!=3 )
	{
		set_wn(HELP);
		mv_csr(4,0,HELP);
		w_puts(
"GRAB v.3.0 Perlin Production Co.,Ltd. 1993, 02.03.12, Stamford,CT\n",
		HELP);
		mv_csr(4,1,HELP);
		w_puts(
"Bitmap Grabber from VECTOR-06c Video-RAM binary 32K snapshot file\n",
		HELP);
		mv_csr(18,2,HELP);
		w_puts("Usage: GRAB source_file destination_file",HELP);
		goto_xy(0,MAXY);
		exit(0);
	}
	srcname=argv[1];
	dstname=argv[2];
	dst=ERROR;

	if (strcmp(srcname,dstname)==SUCCESS)
	 trouble("Source and destination files",
		strcat(srcname," must have different names."));

	unlink(dstname);
	if ((dst=creat(dstname,FCB_WRITE)) == ERROR)
	 trouble("Failed to create destination file ",dstname);

	xs=256;
	xe=xs+15;
	ys=120;
	ye=127;

	mode=OM_MOVE;
	modeText[OM_MOVE] ="M o v e";
	modeText[OM_ZOOM] ="Z o o m";
	modeText[OM_SHIFT]="<Shift>";
	pln_nam="8ACE";

	open_src(plane=0);
	sh_status(plane,mode,xs,ys,xe,ye);

	while( ((key=ki()) != ENTER ) && (key != ESC) )
	{
            if  (key)
	    {
		putimg(xs,ys,xe,ye);
		switch (key)
		{
		case HOME:
			mode=OM_SHIFT;
			goto_xy(50,0);
			puts(modeText[mode]);
			break;
		case STR:
			if (mode != OM_MOVE) mode=OM_MOVE;
			else mode = OM_ZOOM;
			goto_xy(50,0);
			puts(modeText[mode]);
			break;
		case LEFT:
			if (mode == OM_SHIFT) shiftscr(key,plane);
			else
			{
				if (mode == OM_MOVE) if ((xs-=16) < 0) xs=0;
				if ((xe-=16) < xs) xe+=16;
			}
			break;
		case RIGHT :
			if (mode == OM_SHIFT) shiftscr(key,plane);
			else
			{
				if ((xe+=16) > GMAXX) xe-=16;
				if (mode == OM_MOVE) if ((xs+=16) > xe) xs-=16;
			}
			break;
		case DOWN:
			if (mode != OM_SHIFT)
			{
				if (mode == OM_MOVE)
				{
					if (--ys < 0) ys=0;
					else ye--;
				}
				else if (--ye <= ys) ye++;
			}
			break;
		case UP:
			if (mode != OM_SHIFT)
			{
				if (++ye > GMAXY) ye--;
				else if (mode==OM_MOVE) ys++;
			}
			break;
		 case PGUP:
			plane = (plane - 1) & 0x3;
			rd_plane(plane);
			rd_plane(plane+1);
			break;
		 case PGDN:
			plane = (plane + 1) & 0x3;
			rd_plane(plane);
			rd_plane(plane+1);
			break;
		}		
		sh_status(plane,mode,xs,ys,xe,ye);
	    }
	}
	_SETCOL=6;
	if (key==ESC) bye_bye();
	evenHght=(ye-ys) & 0x1;
	sav_format=select_panels();
	exitcode=sav_sprite(xs,ys,xe,ye,sav_format);
	cls();
	set_wn(HELP);
	mv_csr(0,1,HELP);
	w_puts("Grabbed data successfully written to destination file "
		,HELP);
	w_puts(dstname,HELP);
	goto_xy(0,MAXY);
	sh_csr();
}


sh_status(plane,mode,xs,ys,xe,ye)
unsigned plane;
char	 mode;
int	 xs,ys,xe,ye;
{
	goto_xy(4,0);
	printf("%d , %d  ... %d , %d      ",xs >> 1,ys,(xe-1) >> 1,ye);
	goto_xy(50,0);
	puts(modeText[mode]);
	puts(", Planes:");
	putchar(pln_nam[plane]);
	putchar(pln_nam[(plane+1) & 0x3]);
	putbar(xs,ys,xe,ye);
}
