{plasma podle outlaw triad tutorialu, prepsana z asm do pasc}
program plasma;
uses mygraph;
const maxframe=300;
var co1,co2,co3,co4:byte;
    cosine: array [0..255] of byte;
    frame:word;
    stop,delkam,start:longint;

procedure nastavpaletu;
var i,r,g,b:byte;

begin
r:=0; b:=0; g:=0;
for i:=0 to 63 do
 begin
  activepal[i,1]:=r;
  activepal[i,2]:=g;
  activepal[i,3]:=b;
  inc(r);
 end;
for i:=64 to 127 do
 begin
  dec(r);
  activepal[i,1]:=r;
  activepal[i,2]:=g;
  activepal[i,3]:=b;
 end;
for i:=128 to 191 do
 begin
  activepal[i,1]:=r;
  activepal[i,2]:=g;
  activepal[i,3]:=b;
  inc(r);
  inc(g);
 end;
for i:=192 to 255 do
 begin
  dec(r);
  dec(g);
  activepal[i,1]:=r;
  activepal[i,2]:=g;
  activepal[i,3]:=b;
 end;
setpalette;
{ulozi paletu}
{vymaze paletu na fadein}
setpalette;
getpalette;
nullpalette;
setpalette;
end;


procedure gensin;
var
	i : integer;
	v, vadd : real;
begin
	v:=0.0;
	vadd:=(2.0*pi/256.0);
        for i:=0 to 255 do
	begin
		cosine[i]:=round(sin(v)*64);
		v:=v+vadd;
	end;
end;

procedure okraj;
var i:integer;
begin
for i:=0 to 4 do
 begin
  hline(320*i,319+320*i,0,140);
  line(i,0,i,199,140);
  line(i,0,i,199,140);
  line(i+1,0,i+1,199,140);
  line(0,199-i,319,199-i,140);
  line(319-i,0,319-i,199,140);
 end;
end;

procedure fade;
var a:integer;
begin
if frame<64 then
 begin
  for a:= 0 to 255 do
   begin
    If StorePal[a,1] > ActivePal[a,1] then
     activepal[a,1]:=ActivePal[a,1]+1;
    If StorePal[a,2] > ActivePal[a,2] then
     activepal[a,2]:=ActivePal[a,2]+1;
    If StorePal[a,3] > ActivePal[a,3] then
     activepal[a,3]:=ActivePal[a,3]+1;
   end;
  SetPalette;
 end;
if frame>=(maxframe-64) then
 begin
  for a:=0 to 255 do
   begin
    if activepal[a,1]>0 then
     activepal[a,1]:=ActivePal[a,1]-1;
    if activepal[a,2]>0 then
     activepal[a,2]:=ActivePal[a,2]-1;
    if activepal[a,3]>0 then
     activepal[a,3]:=ActivePal[a,3]-1;
   end;
   setpalette;
  end;
end;


procedure delejplasmu;
var x,y:word;
    barva:byte;
    pom1,pom2,pom3,pom4:byte;

begin
pom1:=co1;
pom2:=co2;
for y:=5 to 194 do
 begin
  pom3:=co3;
  pom4:=co4;
  for x:=6 to 314 do
   begin
      barva:=0;
      barva:=cosine[pom4]+cosine[pom3]+cosine[pom2]+cosine[pom1];
      barva:=(barva and 127);
      mem[$a000:x+(y*320)]:=barva;
      pom3:=pom3+1;
      pom4:=pom4+2;
   end;
  pom1:=pom1+3;
  pom2:=pom2+4;
 end;
co1:=co1-4;
co2:=co2+3;
co3:=co3-2;
co4:=co4+1
end;


begin
setmode;
gensin;
nastavpaletu;
okraj; {hm taky reseni}


for frame:=1 to maxframe do
 begin
  waitretrace;
  fade;
  delejplasmu;
  if port[$60]=1 then break;
 end;
setvideo(3);
end.
