uses crt, screen, resource, memory, palette;

var
Aantal, T1: longint;
Timer                : Longint ABSOLUTE $0040:$006C;


procedure draw_chunk(x_y:word;bg_x_y:integer; chunk, size:byte; source,where:word);assembler;
asm

mov es, where

mov di, x_y
mov si, bg_x_y

mov dh, size
mov dl, dh

xor bx, bx
mov bl, dl
shl bx, 2
neg bx
add bx, 320*4

push ds
mov ds, source

mov cl, chunk
cmp cl, 0;  je @n0
cmp cl, 1;  je @n1
cmp cl, 2;  je @n2
cmp cl, 3;  je @n3
cmp cl, 4;  je @n4
cmp cl, 5;  je @n5
cmp cl, 6;  je @n6
cmp cl, 7;  je @n7
cmp cl, 8;  je @n8
cmp cl, 9;  je @n9
cmp cl, 10; je @n10
cmp cl, 11; je @n11
cmp cl, 12; je @n12
cmp cl, 13; je @n13
cmp cl, 14; je @n14
cmp cl, 15; je @n15

@n0:
mov ch, dh
@y0:
mov cl, dl
@x0:
db $66; mov ax, ds:[si];     db $66; mov es:[di], ax
db $66; mov ax, ds:[si+320]; db $66; mov es:[di+320], ax
db $66; mov ax, ds:[si+640]; db $66; mov es:[di+640], ax
db $66; mov ax, ds:[si+960]; db $66; mov es:[di+960], ax
add di, 4
add si, 4
dec cl
jnz @x0
add di, bx
add si, bx
dec ch
jnz @y0
jmp @next
@n1:
mov ch, dh
@y1:
mov cl, dl
@x1:
mov ax, ds:[si+1]; mov es:[di+1], ax
mov al, ds:[si+3]; mov es:[di+3], al
db $66; mov ax, ds:[si+320]; db $66; mov es:[di+320], ax
db $66; mov ax, ds:[si+640]; db $66; mov es:[di+640], ax
db $66; mov ax, ds:[si+960]; db $66; mov es:[di+960], ax
add di, 4
add si, 4
dec cl
jnz @x1
add di, bx
add si, bx
dec ch
jnz @y1
jmp @next
@n2:
mov ch, dh
@y2:
mov cl, dl
@x2:
mov ax, ds:[si+1]; mov es:[di+1], ax
mov al, ds:[si+3]; mov es:[di+3], al
db $66; mov ax, ds:[si+320]; db $66; mov es:[di+320], ax
mov ax, ds:[si+640]; mov es:[di+640], ax
mov al, ds:[si+643]; mov es:[di+643], al
db $66; mov ax, ds:[si+960]; db $66; mov es:[di+960], ax
add di, 4
add si, 4
dec cl
jnz @x2
add di, bx
add si, bx
dec ch
jnz @y2
jmp @next
@n3:
mov ch, dh
@y3:
mov cl, dl
@x3:
mov al, ds:[si+1]; mov es:[di+1], al
mov al, ds:[si+3]; mov es:[di+3], al
db $66; mov ax, ds:[si+320]; db $66; mov es:[di+320], ax
mov ax, ds:[si+640]; mov es:[di+640], ax
mov al, ds:[si+643]; mov es:[di+643], al
db $66; mov ax, ds:[si+960]; db $66; mov es:[di+960], ax
add di, 4
add si, 4
dec cl
jnz @x3
add di, bx
add si, bx
dec ch
jnz @y3
jmp @next
@n4:
mov ch, dh
@y4:
mov cl, dl
@x4:
mov al, ds:[si+1]; mov es:[di+1], al
mov al, ds:[si+3]; mov es:[di+3], al
db $66; mov ax, ds:[si+320]; db $66; mov es:[di+320], ax
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+643]; mov es:[di+643], al
db $66; mov ax, ds:[si+960]; db $66; mov es:[di+960], ax
add di, 4
add si, 4
dec cl
jnz @x4
add di, bx
add si, bx
dec ch
jnz @y4
jmp @next
@n5:
mov ch, dh
@y5:
mov cl, dl
@x5:
mov al, ds:[si+1]; mov es:[di+1], al
mov al, ds:[si+3]; mov es:[di+3], al
mov al, ds:[si+320]; mov es:[di+320], al
mov ax, ds:[si+322]; mov es:[di+322], ax
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+643]; mov es:[di+643], al
db $66; mov ax, ds:[si+960]; db $66; mov es:[di+960], ax
add di, 4
add si, 4
dec cl
jnz @x5
add di, bx
add si, bx
dec ch
jnz @y5
jmp @next
@n6:
mov ch, dh
@y6:
mov cl, dl
@x6:
mov al, ds:[si+1]; mov es:[di+1], al
mov al, ds:[si+3]; mov es:[di+3], al
mov al, ds:[si+320]; mov es:[di+320], al
mov ax, ds:[si+322]; mov es:[di+322], ax
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+643]; mov es:[di+643], al
mov ax, ds:[si+960]; mov es:[di+960], ax
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x6
add di, bx
add si, bx
dec ch
jnz @y6
jmp @next
@n7:
mov ch, dh
@y7:
mov cl, dl
@x7:
mov al, ds:[si+1]; mov es:[di+1], al
mov al, ds:[si+3]; mov es:[di+3], al
mov al, ds:[si+320]; mov es:[di+320], al
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+643]; mov es:[di+643], al
mov ax, ds:[si+960]; mov es:[di+960], ax
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x7
add di, bx
add si, bx
dec ch
jnz @y7
jmp @next
@n8:
mov ch, dh
@y8:
mov cl, dl
@x8:
mov al, ds:[si+1]; mov es:[di+1], al
mov al, ds:[si+3]; mov es:[di+3], al
mov al, ds:[si+320]; mov es:[di+320], al
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+643]; mov es:[di+643], al
mov al, ds:[si+960]; mov es:[di+960], al
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x8
add di, bx
add si, bx
dec ch
jnz @y8
jmp @next
@n9:
mov ch, dh
@y9:
mov cl, dl
@x9:
mov al, ds:[si+3]; mov es:[di+3], al
mov al, ds:[si+320]; mov es:[di+320], al
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+643]; mov es:[di+643], al
mov al, ds:[si+960]; mov es:[di+960], al
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x9
add di, bx
add si, bx
dec ch
jnz @y9
jmp @next
@n10:
mov ch, dh
@y10:
mov cl, dl
@x10:
mov al, ds:[si+3]; mov es:[di+3], al
mov al, ds:[si+320]; mov es:[di+320], al
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+960]; mov es:[di+960], al
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x10
add di, bx
add si, bx
dec ch
jnz @y10
jmp @next
@n11:
mov ch, dh
@y11:
mov cl, dl
@x11:
mov al, ds:[si+320]; mov es:[di+320], al
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+641]; mov es:[di+641], al
mov al, ds:[si+960]; mov es:[di+960], al
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x11
add di, bx
add si, bx
dec ch
jnz @y11
jmp @next
@n12:
mov ch, dh
@y12:
mov cl, dl
@x12:
mov al, ds:[si+320]; mov es:[di+320], al
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+960]; mov es:[di+960], al
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x12
add di, bx
add si, bx
dec ch
jnz @y12
jmp @next
@n13:
mov ch, dh
@y13:
mov cl, dl
@x13:
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+960]; mov es:[di+960], al
mov al, ds:[si+962]; mov es:[di+962], al
add di, 4
add si, 4
dec cl
jnz @x13
add di, bx
add si, bx
dec ch
jnz @y13
jmp @next
@n14:
mov ch, dh
@y14:
mov cl, dl
@x14:
mov al, ds:[si+322]; mov es:[di+322], al
mov al, ds:[si+960]; mov es:[di+960], al
add di, 4
add si, 4
dec cl
jnz @x14
add di, bx
add si, bx
dec ch
jnz @y14
jmp @next
@n15:
mov ch, dh
@y15:
mov cl, dl
@x15:
mov al, ds:[si+960]; mov es:[di+960], al
add di, 4
add si, 4
dec cl
jnz @x15
add di, bx
add si, bx
dec ch
jnz @y15
@next:

pop ds
end;

var
Logo_clear,Logo_fill: array[0..1] of word;
procedure Draw_Logo(var Logo_fill, Logo_clear : word;
   mode, cell_size, pos,pos_bg, size, step,   logo, source, where:word);
var
vdi,m,n,offset,pos2:word;
s, l:integer;
begin
   s:=5; {fade power}

   if size>0 then
   for n:=0 to size-1 do
   begin
       l:=memw[logo:n*4] shr 4;
       offset:=memw[logo: n*4+2] + pos;
       pos2:=offset+pos_bg;
       if (l<16) then draw_chunk(offset, pos2, l, cell_size, source, where);
   end;

if step>0 then begin {-----------------}

   if mode=0 then
       if Logo_fill<size then
         begin
           Logo_fill:=Logo_fill+step;
           if Logo_fill>size then Logo_fill:=size;
         end;

   if mode=1 then
       if Logo_clear<size then
         begin
           Logo_clear:=Logo_clear+step;
           if Logo_clear>size then Logo_clear:=size;
         end;


   if mode=0 then begin
      vdi:=0;
      m:=Logo_fill-step;
      if m>0 then
      for n:=1 to m do begin
         l:=memw[logo:vdi*4];
         if l>0 then begin
             l:=l-s*step;
             if l<0 then l:=0;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
      for n:=step downto 1 do begin
         l:=memw[logo:vdi*4];
         if l>0 then begin
             l:=l-s*n;
             if l<0 then l:=0;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
   end;

   if mode=1 then begin
      vdi:=0;
      m:=Logo_clear-step;
      if m>0 then
      for n:=1 to m do begin
         l:=memw[logo:vdi*4];
         if l<256 then begin
             l:=l+s*step;
             if l>256 then l:=256;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
      for n:=step downto 1 do begin
         l:=memw[logo:vdi*4];
         if l<256 then begin
             l:=l+s*n;
             if l>256 then l:=256;
             memw[logo:vdi*4]:=l;
             end;
         inc(vdi);
      end;
   end;
end;{-----------------------}
end;

var
logo_mode:byte;
q:word;
bg_pos:integer;
start_time, old_time:longint;
elapsed_time:word;

begin
SetUpVirtual(scr,vaddr);
SetUpVirtual(scr2, vaddr2);
SetUpVirtual(scr3, vaddr3);
SetUpVirtualPalette(pale, palitra);


asm
mov ax, 13h
int 10h
end;
set60hz;

asm
mov al, 34h
out 43h, al
mov al, 19886 and 0ffh {1193180/freq}
out 40h, al
mov al, 19886 shr 8
out 40h, al
end;


{LoadBin('7dump.bin', 236, vaddr2);
LoadBin('7dump2.bin', 1040, vaddr2+16);}
LoadBin('mask.bin', 1916, vaddr2);

LoadBin('manwe.bin', 64000, vaddr3);
LoadBin('bitl.pal', 1536, palitra);
setpalette_from_memW(palette1,palitra);


Logo_mode:=0;
Logo_clear[0]:=0; Logo_Fill[0]:=0;
Logo_clear[1]:=0; Logo_Fill[1]:=0;
q:=0;

Aantal:=0; T1:=Timer;
old_time:=timer;
start_time:=timer;

repeat
q:=timer-start_time;
elapsed_time:=(timer-old_time);
old_time:=timer;


clear(39, 64000,vaddr); {Clear framebuffer}

{if q>260 then logo_mode:=1;{}

bg_pos:=round(cos(q/15)*10)+round(sin(q/10)*5)*320;
bg_pos:=0;

if q>60 then begin
Draw_Logo(Logo_fill[0], Logo_clear[0],
          logo_mode, 2,  0, bg_pos,  1916 shr 2, elapsed_time*5,       vaddr2, vaddr3, vaddr);{}

end;

{WaitVR;{}
{WaitVR;{}
{WaitVR;{}
WaitVR;{}
FlipScreen(vaddr,VGA);
Inc(Aantal);
until port[$60]=1;

asm
mov ax, 3h
int 10h
end;

asm
mov al, 34h
out 43h, al
xor al, al
out 40h, al
out 40h, al
end;

T1:=timer-T1;
WriteLn(Aantal/(T1/60):2:1,' Frames per second');

readkey;
repeat until keypressed;


end.