program reverseeffect;

uses crush13h,crt;

var x,y,a:integer;
    c:char;

procedure reverse(x1,y1,x2,y2,c:integer;from,too:word);
var xx,yy: integer;
begin

     for xx:=0 to x2-x1 do
      for yy:=0 to y2-y1 do
       if mem[too:(xx+x1)+(yy+y1)*320]=c then mem[too:(xx+x1)+(yy+y1)*320]:=mem[from:((x2-x1)-xx+x1)+((y2-y1)-yy+y1)*320];

end;

procedure fillcircle(x1,y1,x2,y2,c:integer;where:word);
var xx,yy,xd,yd,x3:integer;

begin
     for xx:=x1 to x2 do
         for yy:=y1 to y2 do
             if mem[where:xx+yy*320]=c then
                for xd:=xx+1 to x2 do
                    if mem[where:xd+yy*320]=c then
                       for x3:=xx to xd do
                           mem[where:x3+yy*320]:=c;

end;

begin
     video_mode ( $13);
     initvirt;
     inittables;

     loadpcx ('ps-00000.pcx',vp[2]);
     move(mem[vp[2]:0],mem[vp[1]:0],64000);
     cls (0,vp[1]);

     a:=0;
     repeat
      a:=a+1;
      if a>100 then a:=0;

      circle (20+a,20+a,20,1,vp[1]);
      fillcircle (0+a,0+a,40+a,40+a,1,vp[1]);
      reverse (0+a,0+a,40+a,40+a,1,vp[2],vp[1]);
      move (mem[vp[1]:0],mem[vga:0],64000);

      move(mem[vp[2]:0],mem[vp[1]:0],64000);

      if keypressed=true then c:=readkey;
     until c=#27;

     cleartables;
     closevirt;
     video_mode ( 03);
end.