program SPRITE_GRABBLER_NEW_VERSION;
uses dos,crt,printer;
{$I _NORMVGA.PAS}
var name:string[80];
    datei1:file of byte;
    x,y,x1,y1:integer;
    z,z1,z2:word;
    ok:boolean;
    ch:char;
    savemode:byte;

procedure rectangle(rx1,ry1,rx2,ry2:word);
var ri:word;
begin
  for ri:=rx1 to rx2 do put_pixel(ri,ry1,get_pixel(ri,ry1) xor 20);
  for ri:=rx1 to rx2 do put_pixel(ri,ry2,get_pixel(ri,ry2) xor 20);
  for ri:=ry1 to ry2 do put_pixel(rx1,ri,get_pixel(rx1,ri) xor 20);
  for ri:=ry1 to ry2 do put_pixel(rx2,ri,get_pixel(rx2,ri) xor 20);
end;


begin
   write('Dateiname:');
   readln(name);
   write('Save in Mode-X format [Y]');
   ch:=readkey;
   if (ch=chr(13)) or (upcase(ch)='Y') then savemode:=1 else savemode:=0;
   video_mode($13);
   load_vga(name);
   x:=160;
   y:=136;
   x1:=160+56;
   y1:=136+47; {}

{     x:=80;
     y:=136;
     x1:=80+56;
     y1:=136+47;{}

  ok:=false;
  rectangle(x,y,x1,y1);
  repeat
    repeat
    until KeyPressed;
    rectangle(x,y,x1,y1);
    ch:=readkey;
    if ch='8' then y1:=y1-4;
    if ch='2' then y1:=y1+4;
    if ch='4' then x1:=x1-8;
    if ch='6' then x1:=x1+8;
    if ch='5' then ok:=true;
    if ch='s' then y:=y-4;
    if ch='x' then y:=y+4;
    if ch='y' then x:=x-8;
    if ch='c' then x:=x+8;

    if x1<0 then x1:=0;
    if y1<0 then y1:=0;
    if x1>319 then x1:=319;
    if y1>199 then y1:=199;

    rectangle(x,y,x1,y1);
  until ok;
    rectangle(x,y,x1,y1);

  write(lst,name,' ');
  if savemode=1 then writeln(lst,'MODE-X') else writeln(lst,'NORMAL');
  writeln(lst,'VON: ',x,' ',y,' BIS: ',x1,' ',y1);
  writeln(lst,'XAusdehnung: ',x1-x);
  writeln(lst,'YAusdehnung: ',y1-y+1);

  if savemode=1 then begin
    assign(datei1,name+'.gfx');
    rewrite(datei1);
    for z2:=0 to 3 do begin
      for z:=y to y1 do begin
        for z1:=x+1 to x1 do begin
          write(datei1,mem[$a000:z*320+z1+z2]);
          inc(z1,3);
        end;
      end;
    end;
    close(datei1);
  end;

  if savemode=0 then begin
    assign(datei1,name+'.gfx');
    rewrite(datei1);
    for z:=y to y1 do begin
      for z1:=x+1 to x1 do begin
        write(datei1,mem[$a000:z*320+z1]);
      end;
    end;
    close(datei1);
  end;
  video_mode(3);
  write(name,' ');
  if savemode=1 then writeln('MODE-X') else writeln('NORMAL');
  writeln('VON: ',x,' ',y,' BIS: ',x1,' ',y1);
  writeln('XAusdehnung: ',x1-x);
  writeln('YAusdehnung: ',y1-y+1);
  repeat until keypressed;
  ch:=readkey;
end.
