program EOEC_Config;

uses
    Crt, Ernst, Ernst2;

{$I c:\tp\pas\screen1.pas}

type
    ScreenPtr = ^ScreenType;
    ScreenType = record
                 pos: array [1..80, 1..25] of record
                      Ch : Char;
                      At : Byte;
                      end;
                 CursX, CursY : integer;
                 end;
    RGBType = Record
              R, G, B : Byte;
              End;
    PalType = Array[0..255] of RGBType;

var
    Screen    : ScreenPtr;
    THEScreen : ScreenType;
    Editor    : string;
    Bob       : PalType;

(* ---------------------------------------------------------------------- *)
function VidSeg : Word;

begin

    if mem[$0000:$0449] = 7 then
        VidSeg := $B000
    else
        VidSeg := $B800;

end;

(* ---------------------------------------------------------------------- *)
procedure WaitRetrace; assembler;
asm
    mov   dx,3DAh
@@1:
    in    al,dx
    test  al,8
    jnz   @@1
@@2:
    in    al,dx
    test  al,8
    jz    @@2
end;

(* ---------------------------------------------------------------------- *)
procedure SingleFadeAR(Var Palette  : PalType); Assembler;
Asm
    push  ds
    les   di, Palette
    lds   si, Palette
    mov   cx, 768

@@ColLoop:

    lodsb
    or    al, al
    jz    @@NoDec
    dec   al
    stosb
    dec   cx
    jnz   @@ColLoop
    jmp   @@Ender
@@NoDec:
    inc   di
    dec   cx
    jnz   @@ColLoop
@@Ender:

    pop   ds
End;

(* ---------------------------------------------------------------------- *)
Procedure GetAllPal(Var Palette : PalType); Assembler;
Asm
    les    di, Palette
    mov    dx, 3c7h
    mov    al, 0
    out    dx, al
    add    dx, 2
    mov    cx, 768
    rep    insb
End;

(* ---------------------------------------------------------------------- *)
Procedure SetAllPal(Var Palette : PalType); Assembler;
asm
    push   ds
    lds    si, Palette
    mov    dx, 3c8h
    mov    al, 0
    out    dx, al
    inc    dx
    mov    cx, 768
    rep    outsb
    pop    ds
End;

(* ---------------------------------------------------------------------- *)
Procedure FadeIn(Palette : PalType; Del : Word);
Var
   TPal  : PalType;
   Loop  : Byte;
   Loop2 : Byte;

Begin
    FillChar(TPal, 768, 0);
    For Loop2 := 0 to 63 do
    Begin
        For Loop := 0 to 255 do
        Begin
            If TPal[Loop].R < Palette[Loop].R Then Inc(TPal[Loop].R);
            If TPal[Loop].G < Palette[Loop].G Then Inc(TPal[Loop].G);
            If TPal[Loop].B < Palette[Loop].B Then Inc(TPal[Loop].B);
        End;
        WaitRetrace;
        SetAllPal(TPal);
        Delay(Del);
    End;
End;

(* ---------------------------------------------------------------------- *)
Procedure FadeOut(Del : Word);
Var
    TPal : PalType;
    Loop : Byte;

Begin
    GetAllPal(TPal);
    For Loop := 0 to 63 do
    Begin
        WaitRetrace;
        SetAllPal(TPal);
        SingleFadeAR(TPal);
        Delay(Del);
    End;
End;


(* ====================================================================== *)
begin

    Screen := ptr(VidSeg, $0000);
    THEScreen := Screen;
    GetAllPal(Bob);
    FadeOut(1);
    clrscr;
    move(ImageData, Screen, 4000);
    FadeIn(Bob,0);
    InputStringShift(Editor, 38, 40, 36, 4, 'T', ord('_'));
    FadeOut (0);
    Screen := THEScreen;
    FadeIn(Bob,0);

end.
